001: /*
002: * $Id: Log.java,v 1.1 2003/08/17 06:06:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.otherops;
025:
026: import java.util.*;
027:
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030: import org.ofbiz.minilang.*;
031: import org.ofbiz.minilang.method.*;
032:
033: /**
034: * Calculates a result based on nested calcops.
035: *
036: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
037: * @version $Revision: 1.1 $
038: * @since 2.0
039: */
040: public class Log extends MethodOperation {
041:
042: public static final String module = Log.class.getName();
043:
044: String levelStr;
045: String message;
046: List methodStrings = null;
047:
048: public Log(Element element, SimpleMethod simpleMethod) {
049: super (element, simpleMethod);
050: this .message = element.getAttribute("message");
051: this .levelStr = element.getAttribute("level");
052:
053: List methodStringElements = UtilXml.childElementList(element,
054: null);
055: if (methodStringElements.size() > 0) {
056: methodStrings = new LinkedList();
057:
058: Iterator methodStringIter = methodStringElements.iterator();
059: while (methodStringIter.hasNext()) {
060: Element methodStringElement = (Element) methodStringIter
061: .next();
062: if ("string".equals(methodStringElement.getNodeName())) {
063: methodStrings.add(new StringString(
064: methodStringElement, simpleMethod));
065: } else if ("field".equals(methodStringElement
066: .getNodeName())) {
067: methodStrings.add(new FieldString(
068: methodStringElement, simpleMethod));
069: } else {
070: //whoops, invalid tag here, print warning
071: Debug.logWarning(
072: "Found an unsupported tag under the log tag: "
073: + methodStringElement.getNodeName()
074: + "; ignoring", module);
075: }
076: }
077: }
078: }
079:
080: public boolean exec(MethodContext methodContext) {
081: String levelStr = methodContext.expandString(this .levelStr);
082: String message = methodContext.expandString(this .message);
083:
084: int level;
085: Integer levelInt = Debug.getLevelFromString(levelStr);
086: if (levelInt == null) {
087: Debug.logWarning("Specified level [" + levelStr
088: + "] was not valid, using INFO", module);
089: level = Debug.INFO;
090: } else {
091: level = levelInt.intValue();
092: }
093:
094: //bail out quick if the logging level isn't on, ie don't even create string
095: if (!Debug.isOn(level)) {
096: return true;
097: }
098:
099: StringBuffer buf = new StringBuffer();
100:
101: if (message != null)
102: buf.append(message);
103:
104: if (methodStrings != null) {
105: Iterator methodStringsIter = methodStrings.iterator();
106: while (methodStringsIter.hasNext()) {
107: MethodString methodString = (MethodString) methodStringsIter
108: .next();
109: String strValue = methodString.getString(methodContext);
110: if (strValue != null)
111: buf.append(strValue);
112: }
113: }
114:
115: Debug.log(level, null, buf.toString(), module);
116:
117: return true;
118: }
119: }
|