|
Author: adrianc
Date: Sun Apr 29 08:07:10 2012 New Revision: 1331874 URL: http://svn.apache.org/viewvc?rev=1331874&view=rev Log: Overhauled Mini-language <log> element. Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Log.java Modified: ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd?rev=1331874&r1=1331873&r2=1331874&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd (original) +++ ofbiz/trunk/framework/minilang/dtd/simple-methods-v2.xsd Sun Apr 29 08:07:10 2012 @@ -4400,61 +4400,44 @@ under the License. <xs:element name="log" substitutionGroup="OtherOperations"> <xs:annotation> <xs:documentation> - The log tag logs a message used the OFBiz Debug class, which uses Log4J to log to the console, a file, or some other location. - The message is a concatenation of the message attribute and then all of the field and string sub-element values in the order they are specified. + Logs a message. </xs:documentation> </xs:annotation> <xs:complexType> - <xs:choice minOccurs="0" maxOccurs="unbounded"> - <xs:element ref="field"> - <xs:annotation> - <xs:documentation> - Inserts the value of the field into the message where specified. - </xs:documentation> - </xs:annotation> - </xs:element> - <xs:element ref="string"> - <xs:annotation> - <xs:documentation> - Inserts the value of the inline string into the message where specified. - </xs:documentation> - </xs:annotation> - </xs:element> - </xs:choice> - <xs:attributeGroup ref="attlist.log"/> + <xs:attribute name="level" use="required"> + <xs:annotation> + <xs:documentation> + The logging/debug level to use. + Must be one of the following: verbose | timing | info | important | warning | error | fatal | always. + Defaults to "info". + <br/><br/> + Required. Attribute type: constant. + </xs:documentation> + </xs:annotation> + <xs:simpleType> + <xs:restriction base="xs:token"> + <xs:enumeration value="info" /> + <xs:enumeration value="verbose" /> + <xs:enumeration value="timing" /> + <xs:enumeration value="important" /> + <xs:enumeration value="warning" /> + <xs:enumeration value="error" /> + <xs:enumeration value="fatal" /> + <xs:enumeration value="always" /> + </xs:restriction> + </xs:simpleType> + </xs:attribute> + <xs:attribute type="xs:string" name="message" use="required"> + <xs:annotation> + <xs:documentation> + The message to log. + <br/><br/> + Required. Attribute type: constant+expr. + </xs:documentation> + </xs:annotation> + </xs:attribute> </xs:complexType> </xs:element> - <xs:attributeGroup name="attlist.log"> - <xs:attribute name="level" use="required"> - <xs:annotation> - <xs:documentation> - The logging/debug level to use. - Must be one of the following: verbose | timing | info | important | warning | error | fatal | always. - These are the standard OFBiz logging levels. - </xs:documentation> - </xs:annotation> - <xs:simpleType> - <xs:restriction base="xs:token"> - <xs:enumeration value="verbose"/> - <xs:enumeration value="timing"/> - <xs:enumeration value="info"/> - <xs:enumeration value="important"/> - <xs:enumeration value="warning"/> - <xs:enumeration value="error"/> - <xs:enumeration value="fatal"/> - <xs:enumeration value="always"/> - </xs:restriction> - </xs:simpleType> - </xs:attribute> - <xs:attribute type="xs:string" name="message"> - <xs:annotation> - <xs:documentation> - A shortcut for simple messages. - If used along with field and/or string sub-elements the inline string in the message will come first. - </xs:documentation> - </xs:annotation> - </xs:attribute> - </xs:attributeGroup> <xs:element name="now" substitutionGroup="OtherOperations"> <xs:annotation> <xs:documentation> Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Log.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Log.java?rev=1331874&r1=1331873&r2=1331874&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Log.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/otherops/Log.java Sun Apr 29 08:07:10 2012 @@ -18,108 +18,92 @@ *******************************************************************************/ package org.ofbiz.minilang.method.otherops; -import java.util.List; - -import javolution.util.FastList; - import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.UtilXml; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.FieldString; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; -import org.ofbiz.minilang.method.MethodString; -import org.ofbiz.minilang.method.StringString; import org.w3c.dom.Element; /** - * Calculates a result based on nested calcops. + * Logs a message. */ -public class Log extends MethodOperation { +public final class Log extends MethodOperation { public static final String module = Log.class.getName(); + private static final String[] LEVEL_ARRAY = {"always", "verbose", "timing", "info", "important", "warning", "error", "fatal", "notify"}; - String levelStr; - String message; - List<MethodString> methodStrings = null; - Object startLine; + private final int level; + private final FlexibleStringExpander messageFse; + private final Object startLine; public Log(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - this.message = element.getAttribute("message"); - this.levelStr = element.getAttribute("level"); - this.startLine = element.getUserData("startLine"); - List<? extends Element> methodStringElements = UtilXml.childElementList(element); - if (methodStringElements.size() > 0) { - methodStrings = FastList.newInstance(); - for (Element methodStringElement : methodStringElements) { - if ("string".equals(methodStringElement.getNodeName())) { - methodStrings.add(new StringString(methodStringElement, simpleMethod)); - } else if ("field".equals(methodStringElement.getNodeName())) { - methodStrings.add(new FieldString(methodStringElement, simpleMethod)); - } else { - // whoops, invalid tag here, print warning - Debug.logWarning("Found an unsupported tag under the log tag: " + methodStringElement.getNodeName() + "; ignoring", module); - } - } + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "level", "message"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "level", "message"); + MiniLangValidate.constantAttributes(simpleMethod, element, "level"); + MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "message"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + this.messageFse = FlexibleStringExpander.getInstance(element.getAttribute("message")); + String levelAttribute = element.getAttribute("level"); + if (levelAttribute.length() == 0) { + levelAttribute = "info"; } + Integer levelInt = Debug.getLevelFromString(levelAttribute); + if (levelInt == null) { + MiniLangValidate.handleError("Invalid level attribute", simpleMethod, element); + this.level = Debug.INFO; + } else { + this.level = levelInt.intValue(); + } + this.startLine = element.getUserData("startLine"); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - String levelStr = methodContext.expandString(this.levelStr); - String message = methodContext.expandString(this.message); - int level; - Integer levelInt = Debug.getLevelFromString(levelStr); - if (levelInt == null) { - Debug.logWarning("Specified level [" + levelStr + "] was not valid, using INFO", module); - level = Debug.INFO; - } else { - level = levelInt.intValue(); - } - // bail out quick if the logging level isn't on, ie don't even create string - if (!Debug.isOn(level)) { - return true; - } - StringBuilder buf = new StringBuilder(); - buf.append("["); - String methodLocation = this.simpleMethod.getFromLocation(); - int pos = methodLocation.lastIndexOf("/"); - if (pos != -1) { - methodLocation = methodLocation.substring(pos + 1); - } - buf.append(methodLocation); - buf.append("#"); - buf.append(this.simpleMethod.getMethodName()); - if (this.startLine != null) { - buf.append(" line "); - buf.append(this.startLine); - } - buf.append("] "); - if (message != null) - buf.append(message); - if (methodStrings != null) { - for (MethodString methodString : methodStrings) { - String strValue = methodString.getString(methodContext); - if (strValue != null) - buf.append(strValue); + if (Debug.isOn(level)) { + String message = this.messageFse.expandString(methodContext.getEnvMap()); + StringBuilder buf = new StringBuilder("["); + String methodLocation = this.simpleMethod.getFromLocation(); + int pos = methodLocation.lastIndexOf("/"); + if (pos != -1) { + methodLocation = methodLocation.substring(pos + 1); + } + buf.append(methodLocation); + buf.append("#"); + buf.append(this.simpleMethod.getMethodName()); + if (this.startLine != null) { + buf.append(" line "); + buf.append(this.startLine); } + buf.append("] "); + buf.append(message); + Debug.log(this.level, null, buf.toString(), module); } - Debug.log(level, null, buf.toString(), module); return true; } @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public String rawString() { - // TODO: add all attributes and other info - return "<log level=\"" + this.levelStr + "\" message=\"" + this.message + "\"/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<log "); + sb.append("level=\"").append(LEVEL_ARRAY[this.level]).append("\" "); + sb.append("message=\"").append(this.messageFse).append("\" "); + sb.append("/>"); + return sb.toString(); } public static final class LogFactory implements Factory<Log> { |
| Free forum by Nabble | Edit this page |
