Author: mbrohl
Date: Wed Dec 13 13:03:03 2017 New Revision: 1818007 URL: http://svn.apache.org/viewvc?rev=1818007&view=rev Log: Improved: Fixing defects reported by FindBugs, package org.apache.ofbiz.base.util.string. (OFBIZ-9700) Thanks Julian Leichert for reporting and providing the patch. Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/JuelConnector.java ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelUtil.java Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java?rev=1818007&r1=1818006&r2=1818007&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/FlexibleStringExpander.java Wed Dec 13 13:03:03 2017 @@ -31,6 +31,7 @@ import javax.el.PropertyNotFoundExceptio import org.apache.ofbiz.base.lang.IsEmpty; import org.apache.ofbiz.base.lang.SourceMonitored; import org.apache.ofbiz.base.util.Debug; +import org.apache.ofbiz.base.util.GeneralException; import org.apache.ofbiz.base.util.ObjectType; import org.apache.ofbiz.base.util.ScriptUtil; import org.apache.ofbiz.base.util.UtilDateTime; @@ -441,7 +442,8 @@ public abstract class FlexibleStringExpa buffer.append(ObjectType.simpleTypeConvert(obj, "String", null, timeZone, locale, true)); } } - } catch (Exception e) { + } catch (GeneralException | RuntimeException e) { + Debug.log(e, module); buffer.append(obj); } if (buffer.length() > this.hint) { Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/JuelConnector.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/JuelConnector.java?rev=1818007&r1=1818006&r2=1818007&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/JuelConnector.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/JuelConnector.java Wed Dec 13 13:03:03 2017 @@ -78,7 +78,9 @@ public class JuelConnector { Object base = null; try { base = prefix.eval(bindings, context); - } catch (Exception e) {} + } catch (Exception e) { + Debug.log(e, module); + } Object property = getProperty(bindings, context); if (property == null && strict) { throw new PropertyNotFoundException(LocalMessages.get("error.property.property.notfound", "null", base)); @@ -112,7 +114,9 @@ public class JuelConnector { Object base = null; try { base = prefix.eval(bindings, context); - } catch (Exception e) {} + } catch (Exception e) { + Debug.log(e, module); + } Object property = getProperty(bindings, context); if (property == null && strict) { throw new PropertyNotFoundException(LocalMessages.get("error.property.property.notfound", "null", base)); @@ -179,9 +183,7 @@ public class JuelConnector { public Tree build(String expression) throws ELException { try { return new ExtendedParser(this, expression).tree(); - } catch (ScanException e) { - throw new ELException(LocalMessages.get("error.build", expression, e.getMessage())); - } catch (ParseException e) { + } catch (ScanException | ParseException e) { throw new ELException(LocalMessages.get("error.build", expression, e.getMessage())); } } Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java?rev=1818007&r1=1818006&r2=1818007&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java Wed Dec 13 13:03:03 2017 @@ -21,9 +21,12 @@ package org.apache.ofbiz.base.util.strin import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; +import java.net.MalformedURLException; import java.net.URL; import java.sql.Timestamp; import java.text.DateFormat; @@ -34,6 +37,9 @@ import java.util.Map; import java.util.TimeZone; import javax.el.FunctionMapper; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; @@ -42,10 +48,13 @@ import org.apache.ofbiz.base.util.Debug; import org.apache.ofbiz.base.util.FileUtil; import org.apache.ofbiz.base.util.UtilDateTime; import org.apache.ofbiz.base.util.UtilProperties; +import org.apache.ofbiz.base.util.UtilIO; import org.apache.ofbiz.base.util.UtilXml; import org.cyberneko.html.parsers.DOMParser; import org.w3c.dom.Document; import org.w3c.dom.Node; +import org.xml.sax.SAXException; +import org.xml.sax.SAXNotSupportedException; /** Implements Unified Expression Language functions. * <p>Built-in functions are divided into a number of @@ -166,7 +175,7 @@ public class UelFunctions { } protected static class Functions extends FunctionMapper { - protected final Map<String, Method> functionMap = new HashMap<String, Method>(); + protected final Map<String, Method> functionMap = new HashMap<>(); public Functions() { try { this.functionMap.put("date:second", UtilDateTime.class.getMethod("getSecond", Timestamp.class, TimeZone.class, Locale.class)); @@ -261,7 +270,7 @@ public class UelFunctions { this.functionMap.put("dom:toHtmlString", UelFunctions.class.getMethod("toHtmlString", Node.class, String.class, boolean.class, int.class)); this.functionMap.put("dom:toXmlString", UelFunctions.class.getMethod("toXmlString", Node.class, String.class, boolean.class, boolean.class, int.class)); this.functionMap.put("dom:writeXmlDocument", UelFunctions.class.getMethod("writeXmlDocument", String.class, Node.class, String.class, boolean.class, boolean.class, int.class)); - } catch (Exception e) { + } catch (NoSuchMethodException | NullPointerException | SecurityException e) { Debug.logError(e, "Error while initializing UelFunctions.Functions instance", module); } if (Debug.verboseOn()) Debug.logVerbose("UelFunctions.Functions loaded " + this.functionMap.size() + " functions", module); @@ -403,14 +412,14 @@ public class UelFunctions { public static String toLowerCase(String str) { try { - return str.toLowerCase(); + return str.toLowerCase(Locale.getDefault()); } catch (Exception e) {} return null; } public static String toUpperCase(String str) { try { - return str.toUpperCase(); + return str.toUpperCase(Locale.getDefault()); } catch (Exception e) {} return null; } @@ -434,10 +443,14 @@ public class UelFunctions { } public static String label(String ressource, String label, Locale locale) { - if (locale == null) locale = Locale.getDefault(); + if (locale == null) { + locale = Locale.getDefault(); + } try { String resolveLabel = UtilProperties.getMessage(ressource, label, locale); - if (resolveLabel != null) return resolveLabel; + if (resolveLabel != null) { + return resolveLabel; + } } catch (Exception e) {} return label; } @@ -447,11 +460,13 @@ public class UelFunctions { try { URL url = FlexibleLocation.resolveLocation(str); if (url != null) { - InputStream is = url.openStream(); + try (InputStream is = url.openStream();) { result = true; - is.close(); + } } - } catch (Exception e) {} + } catch (IOException e) { + Debug.log(e, module); + } return result; } @@ -467,7 +482,7 @@ public class UelFunctions { } else { Debug.logError("Unable to locate HTML document " + str, module); } - } catch (Exception e) { + } catch (IOException | SAXException e) { Debug.logError(e, "Error while reading HTML document " + str, module); } return document; @@ -478,13 +493,17 @@ public class UelFunctions { try { URL url = FlexibleLocation.resolveLocation(str); if (url != null) { - InputStream is = url.openStream(); - document = UtilXml.readXmlDocument(is, str); - is.close(); + try (InputStream is = url.openStream();) { + document = UtilXml.readXmlDocument(is, str); + } + catch (SAXException | ParserConfigurationException e) { + Debug.logError(e, "Error while reading XML document " + str, module); + } } else { Debug.logError("Unable to locate XML document " + str, module); } - } catch (Exception e) { + } + catch (IOException e) { Debug.logError(e, "Error while reading XML document " + str, module); } return document; @@ -494,14 +513,14 @@ public class UelFunctions { try { File file = FileUtil.getFile(str); if (file != null) { - FileOutputStream os = new FileOutputStream(file); - UtilXml.writeXmlDocument(node, os, encoding, omitXmlDeclaration, indent, indentAmount); - os.close(); - return true; + try (FileOutputStream os = new FileOutputStream(file);) { + UtilXml.writeXmlDocument(node, os, encoding, omitXmlDeclaration, indent, indentAmount); + return true; + } } else { Debug.logError("Unable to create XML document " + str, module); } - } catch (Exception e) { + } catch (IOException | TransformerException | SecurityException e) { Debug.logError(e, "Error while writing XML document " + str, module); } return false; @@ -526,25 +545,23 @@ public class UelFunctions { sb.append("/>\n<xsl:template match=\"@*|node()\">\n"); sb.append("<xsl:copy><xsl:apply-templates select=\"@*|node()\"/></xsl:copy>\n"); sb.append("</xsl:template>\n</xsl:stylesheet>\n"); - ByteArrayInputStream bis = new ByteArrayInputStream(sb.toString().getBytes()); + ByteArrayInputStream bis = new ByteArrayInputStream(sb.toString().getBytes(UtilIO.getUtf8())); TransformerFactory transformerFactory = TransformerFactory.newInstance(); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - UtilXml.transformDomDocument(transformerFactory.newTransformer(new StreamSource(bis)), node, os); - os.close(); - return os.toString(); - } catch (Exception e) { + try (ByteArrayOutputStream os = new ByteArrayOutputStream();) { + UtilXml.transformDomDocument(transformerFactory.newTransformer(new StreamSource(bis)), node, os); + return os.toString(); + } + } catch (IOException | TransformerException e) { Debug.logError(e, "Error while creating HTML String ", module); } return null; } public static String toXmlString(Node node, String encoding, boolean omitXmlDeclaration, boolean indent, int indentAmount) { - try { - ByteArrayOutputStream os = new ByteArrayOutputStream(); + try (ByteArrayOutputStream os = new ByteArrayOutputStream();) { UtilXml.writeXmlDocument(node, os, encoding, omitXmlDeclaration, indent, indentAmount); - os.close(); return os.toString(); - } catch (Exception e) { + } catch (IOException | TransformerException e) { Debug.logError(e, "Error while creating XML String ", module); } return null; Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelUtil.java URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelUtil.java?rev=1818007&r1=1818006&r2=1818007&view=diff ============================================================================== --- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelUtil.java (original) +++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelUtil.java Wed Dec 13 13:03:03 2017 @@ -48,7 +48,7 @@ import org.apache.ofbiz.base.util.collec /** Implements the Unified Expression Language (JSR-245). */ public final class UelUtil { - protected static final String module = UelUtil.class.getName(); + private static final String module = UelUtil.class.getName(); private static final String localizedMapLocaleKey = LocalizedMap.class.getName() + "_locale".replace(".", "_"); private static final ExpressionFactory exprFactory = JuelConnector.newExpressionFactory(); private static final ELResolver defaultResolver = new ExtendedCompositeResolver() { @@ -61,13 +61,13 @@ public final class UelUtil { add(new BeanELResolver(false)); } }; - + private UelUtil () {} - + public static String getLocalizedMapLocaleKey() { return localizedMapLocaleKey; } - + /** Evaluates a Unified Expression Language expression and returns the result. * @param context Evaluation context (variables) * @param expression UEL expression @@ -242,7 +242,7 @@ public final class UelUtil { @Override public boolean equals(Object obj) { - if (this == obj) { + if (obj instanceof ReadOnlyExpression) { return true; } try { @@ -268,7 +268,7 @@ public final class UelUtil { public boolean isLiteralText() { return false; } - + } @SuppressWarnings("serial") @@ -281,7 +281,7 @@ public final class UelUtil { } @Override public boolean equals(Object obj) { - if (this == obj) { + if (obj instanceof BasicValueExpression) { return true; } try { |
Free forum by Nabble | Edit this page |