KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > otherops > Log


1 /*
2  * $Id: Log.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.otherops;
25
26 import java.util.*;
27
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30 import org.ofbiz.minilang.*;
31 import org.ofbiz.minilang.method.*;
32
33 /**
34  * Calculates a result based on nested calcops.
35  *
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5462 $
38  * @since 2.0
39  */

40 public class Log extends MethodOperation {
41     
42     public static final String JavaDoc module = Log.class.getName();
43
44     String JavaDoc levelStr;
45     String JavaDoc message;
46     List methodStrings = null;
47     
48     public Log(Element element, SimpleMethod simpleMethod) {
49         super(element, simpleMethod);
50         this.message = element.getAttribute("message");
51         this.levelStr = element.getAttribute("level");
52
53         List methodStringElements = UtilXml.childElementList(element);
54         if (methodStringElements.size() > 0) {
55             methodStrings = new LinkedList();
56             
57             Iterator methodStringIter = methodStringElements.iterator();
58             while (methodStringIter.hasNext()) {
59                 Element methodStringElement = (Element) methodStringIter.next();
60                 if ("string".equals(methodStringElement.getNodeName())) {
61                     methodStrings.add(new StringString(methodStringElement, simpleMethod));
62                 } else if ("field".equals(methodStringElement.getNodeName())) {
63                     methodStrings.add(new FieldString(methodStringElement, simpleMethod));
64                 } else {
65                     //whoops, invalid tag here, print warning
66
Debug.logWarning("Found an unsupported tag under the log tag: " + methodStringElement.getNodeName() + "; ignoring", module);
67                 }
68             }
69         }
70     }
71
72     public boolean exec(MethodContext methodContext) {
73         String JavaDoc levelStr = methodContext.expandString(this.levelStr);
74         String JavaDoc message = methodContext.expandString(this.message);
75         
76         int level;
77         Integer JavaDoc levelInt = Debug.getLevelFromString(levelStr);
78         if (levelInt == null) {
79             Debug.logWarning("Specified level [" + levelStr + "] was not valid, using INFO", module);
80             level = Debug.INFO;
81         } else {
82             level = levelInt.intValue();
83         }
84
85         //bail out quick if the logging level isn't on, ie don't even create string
86
if (!Debug.isOn(level)) {
87             return true;
88         }
89         
90         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
91         
92         if (message != null) buf.append(message);
93         
94         if (methodStrings != null) {
95             Iterator methodStringsIter = methodStrings.iterator();
96             while (methodStringsIter.hasNext()) {
97                 MethodString methodString = (MethodString) methodStringsIter.next();
98                 String JavaDoc strValue = methodString.getString(methodContext);
99                 if (strValue != null) buf.append(strValue);
100             }
101         }
102
103         Debug.log(level, null, buf.toString(), module);
104         
105         return true;
106     }
107
108     public String JavaDoc rawString() {
109         // TODO: add all attributes and other info
110
return "<log level=\"" + this.levelStr + "\" message=\"" + this.message + "\"/>";
111     }
112     public String JavaDoc expandedString(MethodContext methodContext) {
113         // TODO: something more than a stub/dummy
114
return this.rawString();
115     }
116 }
117
Popular Tags