Improved: Fixing defects reported by FindBugs, package

org.apache.ofbiz.base.util.string.
(OFBIZ-9700)

Thanks Julian Leichert for reporting and providing the patch.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1817935 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-12-12 16:41:06 +00:00
parent 3cca77fa21
commit 216226db9b
4 changed files with 53 additions and 38 deletions
@@ -31,6 +31,7 @@ import javax.el.PropertyNotFoundException;
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 FlexibleStringExpander implements Serializable, IsEmpty {
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) {
@@ -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()));
}
}
@@ -22,6 +22,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
@@ -34,6 +35,8 @@ import java.util.Map;
import java.util.TimeZone;
import javax.el.FunctionMapper;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
@@ -41,11 +44,13 @@ import org.apache.ofbiz.base.location.FlexibleLocation;
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.UtilIO;
import org.apache.ofbiz.base.util.UtilProperties;
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;
/** Implements Unified Expression Language functions.
* <p>Built-in functions are divided into a number of
@@ -261,7 +266,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);
}
Debug.logVerbose("UelFunctions.Functions loaded " + this.functionMap.size() + " functions", module);
@@ -403,14 +408,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;
}
@@ -447,11 +452,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 +474,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 +485,13 @@ 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);
}
} else {
Debug.logError("Unable to locate XML document " + str, module);
}
} catch (Exception e) {
} catch (IOException | SAXException | ParserConfigurationException e) {
Debug.logError(e, "Error while reading XML document " + str, module);
}
return document;
@@ -494,14 +501,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 +533,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;
@@ -48,7 +48,7 @@ import org.apache.ofbiz.base.util.collections.LocalizedMap;
/** 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,6 +242,9 @@ public final class UelUtil {
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
@@ -268,7 +271,7 @@ public final class UelUtil {
public boolean isLiteralText() {
return false;
}
}
@SuppressWarnings("serial")
@@ -281,6 +284,9 @@ public final class UelUtil {
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
@@ -338,7 +344,7 @@ public final class UelUtil {
@Override
public void setValue(ELContext context, Object base, Object property, Object val) {
super.setValue(context, base, property, val);
if (!context.isPropertyResolved() && base == null) {
if (!context.isPropertyResolved() && base != null) {
if (Debug.verboseOn()) {
Debug.logVerbose("ExtendedCompositeResolver.setValue: base = " + base + ", property = " + property + ", value = " + val, module);
}