001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.otherops;
019:
020: import java.util.*;
021:
022: import org.w3c.dom.*;
023: import org.ofbiz.base.util.*;
024: import org.ofbiz.minilang.*;
025: import org.ofbiz.minilang.method.*;
026:
027: /**
028: * Calculates a result based on nested calcops.
029: */
030: public class Log extends MethodOperation {
031:
032: public static final String module = Log.class.getName();
033:
034: String levelStr;
035: String message;
036: List methodStrings = null;
037:
038: public Log(Element element, SimpleMethod simpleMethod) {
039: super (element, simpleMethod);
040: this .message = element.getAttribute("message");
041: this .levelStr = element.getAttribute("level");
042:
043: List methodStringElements = UtilXml.childElementList(element);
044: if (methodStringElements.size() > 0) {
045: methodStrings = new LinkedList();
046:
047: Iterator methodStringIter = methodStringElements.iterator();
048: while (methodStringIter.hasNext()) {
049: Element methodStringElement = (Element) methodStringIter
050: .next();
051: if ("string".equals(methodStringElement.getNodeName())) {
052: methodStrings.add(new StringString(
053: methodStringElement, simpleMethod));
054: } else if ("field".equals(methodStringElement
055: .getNodeName())) {
056: methodStrings.add(new FieldString(
057: methodStringElement, simpleMethod));
058: } else {
059: //whoops, invalid tag here, print warning
060: Debug.logWarning(
061: "Found an unsupported tag under the log tag: "
062: + methodStringElement.getNodeName()
063: + "; ignoring", module);
064: }
065: }
066: }
067: }
068:
069: public boolean exec(MethodContext methodContext) {
070: String levelStr = methodContext.expandString(this .levelStr);
071: String message = methodContext.expandString(this .message);
072:
073: int level;
074: Integer levelInt = Debug.getLevelFromString(levelStr);
075: if (levelInt == null) {
076: Debug.logWarning("Specified level [" + levelStr
077: + "] was not valid, using INFO", module);
078: level = Debug.INFO;
079: } else {
080: level = levelInt.intValue();
081: }
082:
083: //bail out quick if the logging level isn't on, ie don't even create string
084: if (!Debug.isOn(level)) {
085: return true;
086: }
087:
088: StringBuffer buf = new StringBuffer();
089:
090: if (message != null)
091: buf.append(message);
092:
093: if (methodStrings != null) {
094: Iterator methodStringsIter = methodStrings.iterator();
095: while (methodStringsIter.hasNext()) {
096: MethodString methodString = (MethodString) methodStringsIter
097: .next();
098: String strValue = methodString.getString(methodContext);
099: if (strValue != null)
100: buf.append(strValue);
101: }
102: }
103:
104: Debug.log(level, null, buf.toString(), module);
105:
106: return true;
107: }
108:
109: public String rawString() {
110: // TODO: add all attributes and other info
111: return "<log level=\"" + this .levelStr + "\" message=\""
112: + this .message + "\"/>";
113: }
114:
115: public String expandedString(MethodContext methodContext) {
116: // TODO: something more than a stub/dummy
117: return this.rawString();
118: }
119: }
|