Sonar Fixes

- name clashes with constants
- missing break in switch statements

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1875863 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2020-03-29 22:37:23 +00:00
parent ce544b8550
commit a850690d7c
12 changed files with 124 additions and 280 deletions

View File

@ -62,10 +62,13 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
// additional title formatting from https://stackoverflow.com/questions/50418856
// and legend positioning from https://stackoverflow.com/questions/49615379
// this would probably be an answer for https://stackoverflow.com/questions/36447925 too
public class BarAndLineChart {
public final class BarAndLineChart {
private static final int NUM_OF_ROWS = 7;
private static final Random RNG = new Random();
private BarAndLineChart() {}
public static void main(String[] args) throws Exception {
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet("Sheet1");

View File

@ -368,8 +368,10 @@ public class SlideShowExtractor<
switch (tr.getTextCap()) {
case ALL:
txt = txt.toUpperCase(LocaleUtil.getUserLocale());
break;
case SMALL:
txt = txt.toLowerCase(LocaleUtil.getUserLocale());
break;
}
return txt;

View File

@ -17,11 +17,13 @@
package org.apache.poi.sl.usermodel;
import org.apache.poi.common.Duplicatable;
/**
* This is a replacement for {@link java.awt.Insets} which works on doubles
* instead of ints
*/
public final class Insets2D implements Cloneable {
public final class Insets2D implements Duplicatable {
/**
* The inset from the top.
@ -132,7 +134,7 @@ public final class Insets2D implements Cloneable {
* @return a copy of this <code>Insets2D</code> object.
*/
@Override
public Insets2D clone() {
public Insets2D copy() {
return new Insets2D(top, left, bottom, right);
}

View File

@ -72,7 +72,7 @@ final class NormalisedDecimal {
private static final long MAX_REP_WHOLE_PART = 0x38D7EA4C68000L;
@SuppressWarnings("java:S128")
public static NormalisedDecimal create(BigInteger frac, int binaryExponent) {
// estimate pow2&pow10 first, perform optional mulShift, then normalize
int pow10;
@ -167,7 +167,7 @@ final class NormalisedDecimal {
* Convert to an equivalent {@link ExpandedDouble} representation (binary frac and exponent).
* The resulting transformed object is easily converted to a 64 bit IEEE double:
* <ul>
* <li>bits 2-53 of the {@link #getSignificand()} become the 52 bit 'fraction'.</li>
* <li>bits 2-53 of the {@link #composeFrac()} become the 52 bit 'fraction'.</li>
* <li>{@link #getBinaryExponent()} is biased by 1023 to give the 'exponent'.</li>
* </ul>
* The sign bit must be obtained from somewhere else.
@ -184,21 +184,7 @@ final class NormalisedDecimal {
* @return the significand as a fixed point number (with 24 fraction bits and 47-50 whole bits)
*/
BigInteger composeFrac() {
long wp = _wholePart;
int fp = _fractionalPart;
return new BigInteger(new byte[] {
(byte) (wp >> 56), // N.B. assuming sign bit is zero
(byte) (wp >> 48),
(byte) (wp >> 40),
(byte) (wp >> 32),
(byte) (wp >> 24),
(byte) (wp >> 16),
(byte) (wp >> 8),
(byte) (wp >> 0),
(byte) (fp >> 16),
(byte) (fp >> 8),
(byte) (fp >> 0),
});
return BigInteger.valueOf(_wholePart).shiftLeft(24).or(BigInteger.valueOf(_fractionalPart & 0x00FFFFFF));
}
public String getSignificantDecimalDigits() {

View File

@ -42,8 +42,8 @@ import org.w3c.dom.traversal.NodeIterator;
* e.g. to register id attributes or set prefixes for registered namespaces
*/
public class SignatureMarshalDefaultListener implements SignatureMarshalListener {
private static final String OBJECT_TAG = "Object";
private final Set<String> IGNORE_NS = new HashSet<>(Arrays.asList(null, XML_NS, XML_DIGSIG_NS));
private final String OBJECT_TAG = "Object";
@Override
public void handleElement(SignatureInfo signatureInfo, Document doc, EventTarget target, EventListener parentListener) {

View File

@ -18,6 +18,8 @@
package org.apache.poi.xddf.usermodel.text;
import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
@ -36,6 +38,7 @@ import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
@SuppressWarnings("unused")
@Beta
public class XDDFRunProperties {
private CTTextCharacterProperties props;
@ -55,95 +58,43 @@ public class XDDFRunProperties {
}
public void setBaseline(Integer value) {
if (value == null) {
if (props.isSetBaseline()) {
props.unsetBaseline();
}
} else {
props.setBaseline(value);
}
update(props::isSetBaseline, props::unsetBaseline, props::setBaseline, value);
}
public void setDirty(Boolean dirty) {
if (dirty == null) {
if (props.isSetDirty()) {
props.unsetDirty();
}
} else {
props.setDirty(dirty);
}
update(props::isSetDirty, props::unsetDirty, props::setDirty, dirty);
}
public void setSpellError(Boolean error) {
if (error == null) {
if (props.isSetErr()) {
props.unsetErr();
}
} else {
props.setErr(error);
}
update(props::isSetErr, props::unsetErr, props::setErr, error);
}
public void setNoProof(Boolean noproof) {
if (noproof == null) {
if (props.isSetNoProof()) {
props.unsetNoProof();
}
} else {
props.setNoProof(noproof);
}
update(props::isSetNoProof, props::unsetNoProof, props::setNoProof, noproof);
}
public void setNormalizeHeights(Boolean normalize) {
if (normalize == null) {
if (props.isSetNormalizeH()) {
props.unsetNormalizeH();
}
} else {
props.setNormalizeH(normalize);
}
update(props::isSetNormalizeH, props::unsetNormalizeH, props::setNormalizeH, normalize);
}
public void setKumimoji(Boolean kumimoji) {
if (kumimoji == null) {
if (props.isSetKumimoji()) {
props.unsetKumimoji();
}
} else {
props.setKumimoji(kumimoji);
}
update(props::isSetKumimoji, props::unsetKumimoji, props::setKumimoji, kumimoji);
}
public void setBold(Boolean bold) {
if (bold == null) {
if (props.isSetB()) {
props.unsetB();
}
} else {
props.setB(bold);
}
update(props::isSetB, props::unsetB, props::setB, bold);
}
public void setItalic(Boolean italic) {
if (italic == null) {
if (props.isSetI()) {
props.unsetI();
}
} else {
props.setI(italic);
}
update(props::isSetI, props::unsetI, props::setI, italic);
}
public void setFontSize(Double size) {
if (size == null) {
if (props.isSetSz()) {
props.unsetSz();
}
} else if (size < 1 || 400 < size) {
if (size != null && (size < 1 || 400 < size)) {
throw new IllegalArgumentException("Minimum inclusive = 1. Maximum inclusive = 400.");
} else {
props.setSz((int)(100 * size));
}
update(props::isSetSz, props::unsetSz, props::setSz, size == null ? null : (int)(100 * size));
}
public void setFillProperties(XDDFFillProperties properties) {
@ -184,27 +135,19 @@ public class XDDFRunProperties {
}
public void setCharacterKerning(Double kerning) {
if (kerning == null) {
if (props.isSetKern()) {
props.unsetKern();
}
} else if (kerning < 0 || 4000 < kerning) {
if (kerning != null && (kerning < 0 || 4000 < kerning)) {
throw new IllegalArgumentException("Minimum inclusive = 0. Maximum inclusive = 4000.");
} else {
props.setKern((int) (100 * kerning));
}
update(props::isSetKern, props::unsetKern, props::setKern, kerning == null ? null : (int)(100 * kerning));
}
public void setCharacterSpacing(Double spacing) {
if (spacing == null) {
if (props.isSetSpc()) {
props.unsetSpc();
}
} else if (spacing < -4000 || 4000 < spacing) {
if (spacing != null && (spacing < -4000 || 4000 < spacing)) {
throw new IllegalArgumentException("Minimum inclusive = -4000. Maximum inclusive = 4000.");
} else {
props.setSpc((int) (100 * spacing));
}
update(props::isSetSpc, props::unsetSpc, props::setSpc, spacing == null ? null : (int)(100 * spacing));
}
public void setFonts(XDDFFont[] fonts) {
@ -212,139 +155,59 @@ public class XDDFRunProperties {
CTTextFont xml = font.getXmlObject();
switch (font.getGroup()) {
case COMPLEX_SCRIPT:
if (xml == null) {
if (props.isSetCs()) {
props.unsetCs();
}
} else {
props.setCs(xml);
}
update(props::isSetCs, props::unsetCs, props::setCs, xml);
break;
case EAST_ASIAN:
if (xml == null) {
if (props.isSetEa()) {
props.unsetEa();
}
} else {
props.setEa(xml);
}
update(props::isSetEa, props::unsetEa, props::setEa, xml);
break;
case LATIN:
if (xml == null) {
if (props.isSetLatin()) {
props.unsetLatin();
}
} else {
props.setLatin(xml);
}
update(props::isSetLatin, props::unsetLatin, props::setLatin, xml);
break;
case SYMBOL:
if (xml == null) {
if (props.isSetSym()) {
props.unsetSym();
}
} else {
props.setSym(xml);
}
update(props::isSetSym, props::unsetSym, props::setSym, xml);
break;
}
}
}
public void setUnderline(UnderlineType underline) {
if (underline == null) {
if (props.isSetU()) {
props.unsetU();
}
} else {
props.setU(underline.underlying);
}
update(props::isSetU, props::unsetU, props::setU, underline == null ? null : underline.underlying);
}
public void setStrikeThrough(StrikeType strike) {
if (strike == null) {
if (props.isSetStrike()) {
props.unsetStrike();
}
} else {
props.setStrike(strike.underlying);
}
update(props::isSetStrike, props::unsetStrike, props::setStrike, strike == null ? null : strike.underlying);
}
public void setCapitals(CapsType caps) {
if (caps == null) {
if (props.isSetCap()) {
props.unsetCap();
}
} else {
props.setCap(caps.underlying);
}
update(props::isSetCap, props::unsetCap, props::setCap, caps == null ? null : caps.underlying);
}
public void setHyperlink(XDDFHyperlink link) {
if (link == null) {
if (props.isSetHlinkClick()) {
props.unsetHlinkClick();
}
} else {
props.setHlinkClick(link.getXmlObject());
}
update(props::isSetHlinkClick, props::unsetHlinkClick, props::setHlinkClick, link == null ? null : link.getXmlObject());
}
public void setMouseOver(XDDFHyperlink link) {
if (link == null) {
if (props.isSetHlinkMouseOver()) {
props.unsetHlinkMouseOver();
}
} else {
props.setHlinkMouseOver(link.getXmlObject());
}
update(props::isSetHlinkMouseOver, props::unsetHlinkMouseOver, props::setHlinkMouseOver, link == null ? null : link.getXmlObject());
}
public void setLanguage(Locale lang) {
if (lang == null) {
if (props.isSetLang()) {
props.unsetLang();
}
} else {
props.setLang(lang.toLanguageTag());
}
update(props::isSetLang, props::unsetLang, props::setLang, lang == null ? null : lang.toLanguageTag());
}
public void setAlternativeLanguage(Locale lang) {
if (lang == null) {
if (props.isSetAltLang()) {
props.unsetAltLang();
}
} else {
props.setAltLang(lang.toLanguageTag());
}
update(props::isSetAltLang, props::unsetAltLang, props::setAltLang, lang == null ? null : lang.toLanguageTag());
}
public void setHighlight(XDDFColor color) {
if (color == null) {
if (props.isSetHighlight()) {
props.unsetHighlight();
}
} else {
props.setHighlight(color.getColorContainer());
}
update(props::isSetHighlight, props::unsetHighlight, props::setHighlight, color == null ? null : color.getColorContainer());
}
public void setLineProperties(XDDFLineProperties properties) {
if (properties == null) {
if (props.isSetLn()) {
props.unsetLn();
}
} else {
props.setLn(properties.getXmlObject());
}
update(props::isSetLn, props::unsetLn, props::setLn, properties == null ? null : properties.getXmlObject());
}
public void setBookmark(String bookmark) {
if (bookmark == null) {
if (props.isSetBmk()) {
props.unsetBmk();
}
} else {
props.setBmk(bookmark);
}
update(props::isSetBmk, props::unsetBmk, props::setBmk, bookmark);
}
public XDDFExtensionList getExtensionList() {
@ -356,13 +219,7 @@ public class XDDFRunProperties {
}
public void setExtensionList(XDDFExtensionList list) {
if (list == null) {
if (props.isSetExtLst()) {
props.unsetExtLst();
}
} else {
props.setExtLst(list.getXmlObject());
}
update(props::isSetExtLst, props::unsetExtLst, props::setExtLst, list == null ? null : list.getXmlObject());
}
public XDDFEffectContainer getEffectContainer() {
@ -374,13 +231,7 @@ public class XDDFRunProperties {
}
public void setEffectContainer(XDDFEffectContainer container) {
if (container == null) {
if (props.isSetEffectDag()) {
props.unsetEffectDag();
}
} else {
props.setEffectDag(container.getXmlObject());
}
update(props::isSetEffectDag, props::unsetEffectDag, props::setEffectDag, container == null ? null : container.getXmlObject());
}
public XDDFEffectList getEffectList() {
@ -392,12 +243,14 @@ public class XDDFRunProperties {
}
public void setEffectList(XDDFEffectList list) {
if (list == null) {
if (props.isSetEffectLst()) {
props.unsetEffectLst();
}
} else {
props.setEffectLst(list.getXmlObject());
update(props::isSetEffectLst, props::unsetEffectLst, props::setEffectLst, list == null ? null : list.getXmlObject());
}
private static <T> void update(Supplier<Boolean> isSet, Runnable unset, Consumer<T> setter, T val) {
if (val != null) {
setter.accept(val);
} else if (isSet.get()) {
unset.run();
}
}
}

View File

@ -22,15 +22,15 @@ import org.apache.poi.ss.formula.EvaluationSheet;
import org.apache.poi.ss.formula.FormulaParser;
import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.xssf.usermodel.BaseXSSFEvaluationWorkbook;
import org.apache.poi.util.Internal;
import org.apache.poi.xssf.usermodel.BaseXSSFEvaluationWorkbook;
/**
* SXSSF wrapper around the SXSSF and XSSF workbooks
*/
@Internal
public final class SXSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook {
private final SXSSFWorkbook _uBook;
private final SXSSFWorkbook _sxssfBook;
public static SXSSFEvaluationWorkbook create(SXSSFWorkbook book) {
if (book == null) {
@ -41,23 +41,23 @@ public final class SXSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook {
private SXSSFEvaluationWorkbook(SXSSFWorkbook book) {
super(book.getXSSFWorkbook());
_uBook = book;
_sxssfBook = book;
}
@Override
public int getSheetIndex(EvaluationSheet evalSheet) {
SXSSFSheet sheet = ((SXSSFEvaluationSheet)evalSheet).getSXSSFSheet();
return _uBook.getSheetIndex(sheet);
return _sxssfBook.getSheetIndex(sheet);
}
@Override
public EvaluationSheet getSheet(int sheetIndex) {
return new SXSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
return new SXSSFEvaluationSheet(_sxssfBook.getSheetAt(sheetIndex));
}
@Override
public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
SXSSFCell cell = ((SXSSFEvaluationCell)evalCell).getSXSSFCell();
return FormulaParser.parse(cell.getCellFormula(), this, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
return FormulaParser.parse(cell.getCellFormula(), this, FormulaType.CELL, _sxssfBook.getSheetIndex(cell.getSheet()));
}
}

View File

@ -38,10 +38,6 @@ public enum TableWidthType {
this.type = type;
}
protected STTblWidth.Enum getSTWidthType() {
return this.type;
}
/**
* Get the underlying STTblWidth enum value.
*

View File

@ -1294,7 +1294,7 @@ public class XWPFTable implements IBodyElement, ISDTContents {
protected static void setWidthType(TableWidthType widthType, CTTblWidth ctWidth) {
TableWidthType currentType = getWidthType(ctWidth);
if (!currentType.equals(widthType)) {
STTblWidth.Enum stWidthType = widthType.getSTWidthType();
STTblWidth.Enum stWidthType = widthType.getStWidthType();
ctWidth.setType(stWidthType);
switch (stWidthType.intValue()) {
case STTblWidth.INT_PCT:

View File

@ -521,7 +521,7 @@ public class HemfPlusBrush {
private EmfPlusWrapMode wrapMode;
private Rectangle2D rect = new Rectangle2D.Double();
private Color startColor, endColor;
private AffineTransform transform;
private AffineTransform blendTransform;
private float[] positions;
private Color[] blendColors;
private float[] positionsV;
@ -556,7 +556,7 @@ public class HemfPlusBrush {
size += 4*LittleEndianConsts.INT_SIZE;
if (TRANSFORM.isSet(dataFlags)) {
size += readXForm(leis, (transform = new AffineTransform()));
size += readXForm(leis, (blendTransform = new AffineTransform()));
}
if (isPreset() && (isBlendH() || isBlendV())) {
@ -575,7 +575,7 @@ public class HemfPlusBrush {
HemfDrawProperties prop = ctx.getProperties();
prop.setBrushStyle(HwmfBrushStyle.BS_LINEAR_GRADIENT);
prop.setBrushRect(rect);
prop.setBrushTransform(transform);
prop.setBrushTransform(blendTransform);
// Preset colors and BlendH/V are mutual exclusive
if (isPreset()) {
@ -613,7 +613,7 @@ public class HemfPlusBrush {
m.put("rect", () -> rect);
m.put("startColor", () -> startColor);
m.put("endColor", () -> endColor);
m.put("transform", () -> transform);
m.put("blendTransform", () -> blendTransform);
m.put("positions", () -> positions);
m.put("blendColors", () -> blendColors);
m.put("positionsV", () -> positionsV);
@ -694,7 +694,7 @@ public class HemfPlusBrush {
private Color[] surroundingColor;
private EmfPlusPath boundaryPath;
private Point2D[] boundaryPoints;
private AffineTransform transform;
private AffineTransform blendTransform;
private float[] positions;
private Color[] blendColors;
private float[] blendFactorsH;
@ -761,7 +761,7 @@ public class HemfPlusBrush {
// the path gradient brush. This field MUST be present if the BrushDataTransform flag is set in the
// BrushDataFlags field of the EmfPlusPathGradientBrushData object.
if (TRANSFORM.isSet(dataFlags)) {
size += readXForm(leis, (transform = new AffineTransform()));
size += readXForm(leis, (blendTransform = new AffineTransform()));
}
// An optional blend pattern for the path gradient brush. If this field is present, it MUST contain either
@ -825,7 +825,7 @@ public class HemfPlusBrush {
m.put("surroundingColor", () -> surroundingColor);
m.put("boundaryPath", () -> boundaryPath);
m.put("boundaryPoints", () -> boundaryPoints);
m.put("transform", () -> transform);
m.put("blendTransform", () -> blendTransform);
m.put("positions", () -> positions);
m.put("blendColors", () -> blendColors);
m.put("blendFactorsH", () -> blendFactorsH);
@ -839,7 +839,7 @@ public class HemfPlusBrush {
public static class EmfPlusTextureBrushData implements EmfPlusBrushData {
private int dataFlags;
private EmfPlusWrapMode wrapMode;
private AffineTransform transform;
private AffineTransform brushTransform;
private EmfPlusImage image;
@Override
@ -855,7 +855,7 @@ public class HemfPlusBrush {
int size = 2*LittleEndianConsts.INT_SIZE;
if (TRANSFORM.isSet(dataFlags)) {
size += readXForm(leis, (transform = new AffineTransform()));
size += readXForm(leis, (brushTransform = new AffineTransform()));
}
if (dataSize > size) {
@ -871,7 +871,7 @@ public class HemfPlusBrush {
image.applyObject(ctx, null);
prop.setBrushBitmap(prop.getEmfPlusImage());
prop.setBrushStyle(HwmfBrushStyle.BS_PATTERN);
prop.setBrushTransform(transform);
prop.setBrushTransform(brushTransform);
}
@Override
@ -894,7 +894,7 @@ public class HemfPlusBrush {
return GenericRecordUtil.getGenericProperties(
"dataFlags", () -> dataFlags,
"wrapMode", () -> wrapMode,
"transform", () -> transform,
"brushTransform", () -> brushTransform,
"image", () -> image
);
}

View File

@ -49,11 +49,11 @@ public final class TextRulerAtom extends RecordAtom {
private static final BitField DEFAULT_TAB_SIZE = getInstance(0x0001);
private static final BitField C_LEVELS = getInstance(0x0002);
private static final BitField TAB_STOPS = getInstance(0x0004);
private static final BitField[] LEFT_MARGIN = {
private static final BitField[] LEFT_MARGIN_LVL_MASK = {
getInstance(0x0008), getInstance(0x0010), getInstance(0x0020),
getInstance(0x0040), getInstance(0x0080),
};
private static final BitField[] INDENT = {
private static final BitField[] INDENT_LVL_MASK = {
getInstance(0x0100), getInstance(0x0200), getInstance(0x0400),
getInstance(0x0800), getInstance(0x1000),
};
@ -87,7 +87,7 @@ public final class TextRulerAtom extends RecordAtom {
* @param start the start offset into the byte array.
* @param len the length of the slice in the byte array.
*/
protected TextRulerAtom(final byte[] source, final int start, final int len) {
TextRulerAtom(final byte[] source, final int start, final int len) {
final LittleEndianByteArrayInputStream leis = new LittleEndianByteArrayInputStream(source, start, Math.min(len, MAX_RECORD_LENGTH));
@ -128,8 +128,8 @@ public final class TextRulerAtom extends RecordAtom {
mask |= writeIf(lbos, defaultTabSize, DEFAULT_TAB_SIZE);
mask |= writeIf(lbos, tabStops, TAB_STOPS);
for (int i=0; i<5; i++) {
mask |= writeIf(lbos, leftMargin[i], LEFT_MARGIN[i]);
mask |= writeIf(lbos, indent[i], INDENT[i]);
mask |= writeIf(lbos, leftMargin[i], LEFT_MARGIN_LVL_MASK[i]);
mask |= writeIf(lbos, indent[i], INDENT_LVL_MASK[i]);
}
LittleEndian.putInt(_header, 4, bos.size()+4);
out.write(_header);
@ -147,6 +147,7 @@ public final class TextRulerAtom extends RecordAtom {
return bit.setBoolean(0, isSet);
}
@SuppressWarnings("SameParameterValue")
private static int writeIf(final LittleEndianOutputStream lbos, List<HSLFTabStop> value, BitField bit) {
boolean isSet = false;
if (value != null && !value.isEmpty()) {
@ -167,8 +168,8 @@ public final class TextRulerAtom extends RecordAtom {
tabStops.addAll(HSLFTabStopPropCollection.readTabStops(leis));
}
for (int i=0; i<5; i++) {
leftMargin[i] = readIf(leis, mask, LEFT_MARGIN[i]);
indent[i] = readIf(leis, mask, INDENT[i]);
leftMargin[i] = readIf(leis, mask, LEFT_MARGIN_LVL_MASK[i]);
indent[i] = readIf(leis, mask, INDENT_LVL_MASK[i]);
}
}

View File

@ -552,6 +552,7 @@ public class HwmfGraphics {
switch (valign) {
case TOP:
tx.translate(0, layout.getAscent());
break;
default:
case BASELINE:
break;