sonar fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734641 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-03-11 23:14:09 +00:00
parent a16e88c2e5
commit 24e7f1e8fb
7 changed files with 82 additions and 87 deletions

View File

@ -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<String, Class<? extends Expression>> impls =
new HashMap<String, Class<? extends Expression>>();
private static final Map<String, ExpressionEntry> impls =
new HashMap<String, ExpressionEntry>();
private static class ExpressionEntry {
final Pattern regex;
final Constructor<? extends Expression> con;
ExpressionEntry(String regex, Class<? extends Expression> 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<? extends Expression> 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);
}
}

View File

@ -106,14 +106,13 @@ public final class CollaboratingWorkbooksEnvironment {
}
private CollaboratingWorkbooksEnvironment(Map<String, WorkbookEvaluator> evaluatorsByName, WorkbookEvaluator[] evaluators) {
IdentityHashMap<WorkbookEvaluator, String> uniqueEvals = new IdentityHashMap<WorkbookEvaluator, String>(evaluators.length);
for (String wbName : evaluatorsByName.keySet()) {
WorkbookEvaluator wbEval = evaluatorsByName.get(wbName);
if (uniqueEvals.containsKey(wbEval)) {
for (Map.Entry<String, WorkbookEvaluator> 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);

View File

@ -209,10 +209,10 @@ public final class AnalysisToolPak implements UDFFinder {
public static Collection<String> getSupportedFunctionNames(){
AnalysisToolPak inst = (AnalysisToolPak)instance;
Collection<String> lst = new TreeSet<String>();
for(String name : inst._functionsByName.keySet()){
FreeRefFunction func = inst._functionsByName.get(name);
for(Map.Entry<String, FreeRefFunction> 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<String> getNotSupportedFunctionNames(){
AnalysisToolPak inst = (AnalysisToolPak)instance;
Collection<String> lst = new TreeSet<String>();
for(String name : inst._functionsByName.keySet()){
FreeRefFunction func = inst._functionsByName.get(name);
if(func != null && (func instanceof NotImplemented)){
lst.add(name);
for(Map.Entry<String, FreeRefFunction> me : inst._functionsByName.entrySet()){
FreeRefFunction func = me.getValue();
if (func instanceof NotImplemented) {
lst.add(me.getKey());
}
}
return Collections.unmodifiableCollection(lst);

View File

@ -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<String, String> 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();

View File

@ -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() {

View File

@ -575,14 +575,17 @@ public class XSSFRichTextString implements RichTextString {
}
CTRst stf = CTRst.Factory.newInstance();
int runStartIdx = 0;
for (Iterator<Integer> it = formats.keySet().iterator(); it.hasNext();) {
int runEndIdx = it.next();
for (Map.Entry<Integer, CTRPrElt> 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;

View File

@ -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++)