diff --git a/src/java/org/apache/poi/sl/draw/geom/ExpressionParser.java b/src/java/org/apache/poi/sl/draw/geom/ExpressionParser.java index 682869e967..61ab98b8a0 100644 --- a/src/java/org/apache/poi/sl/draw/geom/ExpressionParser.java +++ b/src/java/org/apache/poi/sl/draw/geom/ExpressionParser.java @@ -19,53 +19,70 @@ package org.apache.poi.sl.draw.geom; +import java.lang.reflect.Constructor; import java.util.HashMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * A simple regexp-based parser of shape guide formulas in DrawingML - * - * @author Yegor Kozlov */ public class ExpressionParser { - static final HashMap> impls = - new HashMap>(); + private static final Map impls = + new HashMap(); + + private static class ExpressionEntry { + final Pattern regex; + final Constructor con; + ExpressionEntry(String regex, Class cls) + throws SecurityException, NoSuchMethodException { + this.regex = Pattern.compile(regex); + this.con = cls.getDeclaredConstructor(Matcher.class); + impls.put(op(regex), this); + } + } static { - impls.put("\\*/ +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", MultiplyDivideExpression.class); - impls.put("\\+- +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)( 0)?", AddSubtractExpression.class); - impls.put("\\+/ +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", AddDivideExpression.class); - impls.put("\\?: +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", IfElseExpression.class); - impls.put("val +([\\-\\w]+)", LiteralValueExpression.class); - impls.put("abs +([\\-\\w]+)", AbsExpression.class); - impls.put("sqrt +([\\-\\w]+)", SqrtExpression.class); - impls.put("max +([\\-\\w]+) +([\\-\\w]+)", MaxExpression.class); - impls.put("min +([\\-\\w]+) +([\\-\\w]+)", MinExpression.class); - impls.put("at2 +([\\-\\w]+) +([\\-\\w]+)", ArcTanExpression.class); - impls.put("sin +([\\-\\w]+) +([\\-\\w]+)", SinExpression.class); - impls.put("cos +([\\-\\w]+) +([\\-\\w]+)", CosExpression.class); - impls.put("tan +([\\-\\w]+) +([\\-\\w]+)", TanExpression.class); - impls.put("cat2 +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", CosineArcTanExpression.class); - impls.put("sat2 +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", SinArcTanExpression.class); - impls.put("pin +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", PinExpression.class); - impls.put("mod +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", ModExpression.class); - + try { + new ExpressionEntry("\\*/ +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", MultiplyDivideExpression.class); + new ExpressionEntry("\\+- +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)( 0)?", AddSubtractExpression.class); + new ExpressionEntry("\\+/ +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", AddDivideExpression.class); + new ExpressionEntry("\\?: +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", IfElseExpression.class); + new ExpressionEntry("val +([\\-\\w]+)", LiteralValueExpression.class); + new ExpressionEntry("abs +([\\-\\w]+)", AbsExpression.class); + new ExpressionEntry("sqrt +([\\-\\w]+)", SqrtExpression.class); + new ExpressionEntry("max +([\\-\\w]+) +([\\-\\w]+)", MaxExpression.class); + new ExpressionEntry("min +([\\-\\w]+) +([\\-\\w]+)", MinExpression.class); + new ExpressionEntry("at2 +([\\-\\w]+) +([\\-\\w]+)", ArcTanExpression.class); + new ExpressionEntry("sin +([\\-\\w]+) +([\\-\\w]+)", SinExpression.class); + new ExpressionEntry("cos +([\\-\\w]+) +([\\-\\w]+)", CosExpression.class); + new ExpressionEntry("tan +([\\-\\w]+) +([\\-\\w]+)", TanExpression.class); + new ExpressionEntry("cat2 +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", CosineArcTanExpression.class); + new ExpressionEntry("sat2 +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", SinArcTanExpression.class); + new ExpressionEntry("pin +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", PinExpression.class); + new ExpressionEntry("mod +([\\-\\w]+) +([\\-\\w]+) +([\\-\\w]+)", ModExpression.class); + } catch (Exception e) { + throw new RuntimeException(e); + } } - public static Expression parse(String str){ - for(String regexp : impls.keySet()) { - Pattern ptrn = Pattern.compile(regexp); - Matcher m = ptrn.matcher(str); - if(m.matches()) { - Class c = impls.get(regexp); - try { - return c.getDeclaredConstructor(Matcher.class).newInstance(m); - } catch (Exception e){ - throw new RuntimeException(e); - } - } + private static String op(String str) { + return (str == null || !str.contains(" ")) + ? "" : str.substring(0, str.indexOf(" ")).replace("\\", ""); + } + + public static Expression parse(String str) { + ExpressionEntry ee = impls.get(op(str)); + Matcher m = (ee == null) ? null : ee.regex.matcher(str); + if (m == null || !m.matches()) { + throw new RuntimeException("Unsupported formula: " + str); + } + + try { + return ee.con.newInstance(m); + } catch (Exception e) { + throw new RuntimeException("Unsupported formula: " + str, e); } - throw new RuntimeException("Unsupported formula: " + str); } } diff --git a/src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java b/src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java index 074e7a18da..f2d936f3d9 100644 --- a/src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java +++ b/src/java/org/apache/poi/ss/formula/CollaboratingWorkbooksEnvironment.java @@ -106,14 +106,13 @@ public final class CollaboratingWorkbooksEnvironment { } private CollaboratingWorkbooksEnvironment(Map evaluatorsByName, WorkbookEvaluator[] evaluators) { IdentityHashMap uniqueEvals = new IdentityHashMap(evaluators.length); - for (String wbName : evaluatorsByName.keySet()) { - WorkbookEvaluator wbEval = evaluatorsByName.get(wbName); - if (uniqueEvals.containsKey(wbEval)) { + for (Map.Entry me : evaluatorsByName.entrySet()) { + String uniEval = uniqueEvals.put(me.getValue(), me.getKey()); + if (uniEval != null) { String msg = "Attempted to register same workbook under names '" + - uniqueEvals.get(wbEval) + "' and '" + wbName + "'"; + uniEval + "' and '" + me.getKey() + "'"; throw new IllegalArgumentException(msg); } - uniqueEvals.put(wbEval, wbName); } unhookOldEnvironments(evaluators); hookNewEnvironment(evaluators, this); diff --git a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java index 2aebf41f43..aa04f3b8d6 100644 --- a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java +++ b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java @@ -209,10 +209,10 @@ public final class AnalysisToolPak implements UDFFinder { public static Collection getSupportedFunctionNames(){ AnalysisToolPak inst = (AnalysisToolPak)instance; Collection lst = new TreeSet(); - for(String name : inst._functionsByName.keySet()){ - FreeRefFunction func = inst._functionsByName.get(name); + for(Map.Entry me : inst._functionsByName.entrySet()){ + FreeRefFunction func = me.getValue(); if(func != null && !(func instanceof NotImplemented)){ - lst.add(name); + lst.add(me.getKey()); } } return Collections.unmodifiableCollection(lst); @@ -227,10 +227,10 @@ public final class AnalysisToolPak implements UDFFinder { public static Collection getNotSupportedFunctionNames(){ AnalysisToolPak inst = (AnalysisToolPak)instance; Collection lst = new TreeSet(); - for(String name : inst._functionsByName.keySet()){ - FreeRefFunction func = inst._functionsByName.get(name); - if(func != null && (func instanceof NotImplemented)){ - lst.add(name); + for(Map.Entry me : inst._functionsByName.entrySet()){ + FreeRefFunction func = me.getValue(); + if (func instanceof NotImplemented) { + lst.add(me.getKey()); } } return Collections.unmodifiableCollection(lst); diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentType.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentType.java index 7ed6a98425..10e9136d52 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentType.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentType.java @@ -18,6 +18,7 @@ package org.apache.poi.openxml4j.opc.internal; import java.util.Hashtable; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -186,11 +187,11 @@ public final class ContentType { retVal.append(this.getSubType()); if (withParameters) { - for (String key : parameters.keySet()) { + for (Map.Entry me : parameters.entrySet()) { retVal.append(";"); - retVal.append(key); + retVal.append(me.getKey()); retVal.append("="); - retVal.append(parameters.get(key)); + retVal.append(me.getValue()); } } return retVal.toString(); diff --git a/src/ooxml/java/org/apache/poi/xdgf/usermodel/XDGFShape.java b/src/ooxml/java/org/apache/poi/xdgf/usermodel/XDGFShape.java index ba1a9908fd..42deac4c26 100644 --- a/src/ooxml/java/org/apache/poi/xdgf/usermodel/XDGFShape.java +++ b/src/ooxml/java/org/apache/poi/xdgf/usermodel/XDGFShape.java @@ -582,11 +582,7 @@ public class XDGFShape extends XDGFSheet { } // get default - XDGFStyleSheet style = _document.getDefaultLineStyle(); - if (style != null) - return style.getLineCap(); - - return null; + return _document.getDefaultLineStyle().getLineCap(); } @Override @@ -602,11 +598,7 @@ public class XDGFShape extends XDGFSheet { } // get default - XDGFStyleSheet style = _document.getDefaultLineStyle(); - if (style != null) - return style.getLineColor(); - - return null; + return _document.getDefaultLineStyle().getLineColor(); } @Override @@ -622,11 +614,7 @@ public class XDGFShape extends XDGFSheet { } // get default - XDGFStyleSheet style = _document.getDefaultLineStyle(); - if (style != null) - return style.getLinePattern(); - - return null; + return _document.getDefaultLineStyle().getLinePattern(); } @Override @@ -642,11 +630,7 @@ public class XDGFShape extends XDGFSheet { } // get default - XDGFStyleSheet style = _document.getDefaultLineStyle(); - if (style != null) - return style.getLineWeight(); - - return null; + return _document.getDefaultLineStyle().getLineWeight(); } @Override @@ -662,11 +646,7 @@ public class XDGFShape extends XDGFSheet { } // get default - XDGFStyleSheet style = _document.getDefaultTextStyle(); - if (style != null) - return style.getFontColor(); - - return null; + return _document.getDefaultTextStyle().getFontColor(); } @Override @@ -682,11 +662,7 @@ public class XDGFShape extends XDGFSheet { } // get default - XDGFStyleSheet style = _document.getDefaultTextStyle(); - if (style != null) - return style.getFontSize(); - - return null; + return _document.getDefaultTextStyle().getFontSize(); } public Stroke getStroke() { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index a427082873..30dcbb8bf1 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -575,14 +575,17 @@ public class XSSFRichTextString implements RichTextString { } CTRst stf = CTRst.Factory.newInstance(); int runStartIdx = 0; - for (Iterator it = formats.keySet().iterator(); it.hasNext();) { - int runEndIdx = it.next(); + for (Map.Entry me : formats.entrySet()) { + int runEndIdx = me.getKey(); CTRElt run = stf.addNewR(); String fragment = text.substring(runStartIdx, runEndIdx); run.setT(fragment); preserveSpaces(run.xgetT()); - CTRPrElt fmt = formats.get(runEndIdx); - if(fmt != null) run.setRPr(fmt); + + CTRPrElt fmt = me.getValue(); + if (fmt != null) { + run.setRPr(fmt); + } runStartIdx = runEndIdx; } return stf; diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/ListTables.java b/src/scratchpad/src/org/apache/poi/hwpf/model/ListTables.java index cef396efff..5ec02537f5 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/ListTables.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/ListTables.java @@ -92,11 +92,10 @@ public final class ListTables ByteArrayOutputStream levelBuf = new ByteArrayOutputStream(); byte[] shortHolder = new byte[2]; - LittleEndian.putShort(shortHolder, (short)listSize); + LittleEndian.putShort(shortHolder, 0, (short)listSize); tableStream.write(shortHolder); - for(Integer x : _listMap.keySet()) { - ListData lst = _listMap.get(x); + for(ListData lst : _listMap.values()) { tableStream.write(lst.toByteArray()); ListLevel[] lvls = lst.getLevels(); for (int y = 0; y < lvls.length; y++)