diff options
author | Dominik Stadler <centic@apache.org> | 2021-01-30 18:42:05 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2021-01-30 18:42:05 +0000 |
commit | a10ca15627642bdaa87e54af937da98e0ee1de74 (patch) | |
tree | e2c97d9696ba80e320f1f629b2b66411da2f922e | |
parent | 615de587f7413f59a336c7f7adc52b8408e7b85b (diff) | |
download | poi-a10ca15627642bdaa87e54af937da98e0ee1de74.tar.gz poi-a10ca15627642bdaa87e54af937da98e0ee1de74.zip |
Apply IDE suggestions, add toString(), adjust JavaDoc and simplify code slightly
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1886061 13f79535-47bb-0310-9956-ffa450edef68
8 files changed, 71 insertions, 59 deletions
diff --git a/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java b/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java index d6a7c46d7e..2497eb43cf 100644 --- a/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java @@ -35,10 +35,8 @@ import org.apache.poi.extractor.POITextExtractor; import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor; import org.apache.poi.hssf.extractor.EventBasedExcelExtractor; import org.apache.poi.ooxml.POIXMLException; -import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.ss.extractor.ExcelExtractor; import org.apache.poi.util.IOUtils; -import org.apache.xmlbeans.XmlException; /** * Base class with things that can be run for any supported file handler @@ -161,7 +159,7 @@ public abstract class AbstractFileHandler implements FileHandler { } } - private void handleExtractingAsStream(File file) throws IOException, OpenXML4JException, XmlException { + private void handleExtractingAsStream(File file) throws IOException { try (InputStream stream = new FileInputStream(file)) { try (POITextExtractor streamExtractor = ExtractorFactory.createExtractor(stream)) { assertNotNull(streamExtractor); diff --git a/src/java/org/apache/poi/common/usermodel/Hyperlink.java b/src/java/org/apache/poi/common/usermodel/Hyperlink.java index 0673ab3950..708f08df5d 100644 --- a/src/java/org/apache/poi/common/usermodel/Hyperlink.java +++ b/src/java/org/apache/poi/common/usermodel/Hyperlink.java @@ -16,45 +16,43 @@ ==================================================================== */ package org.apache.poi.common.usermodel; -import org.apache.poi.util.Removal; - /** * Represents a hyperlink. */ public interface Hyperlink { - + /** * Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file, etc. * * @return the address of this hyperlink */ - public String getAddress(); + String getAddress(); /** * Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file, etc. * * @param address the address of this hyperlink */ - public void setAddress(String address); + void setAddress(String address); /** * Return text label for this hyperlink * * @return text to display */ - public String getLabel(); + String getLabel(); /** * Sets text label for this hyperlink * * @param label text label for this hyperlink */ - public void setLabel(String label); + void setLabel(String label); /** * Return the type of this hyperlink * * @return the type of this hyperlink */ - public HyperlinkType getType(); + HyperlinkType getType(); } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java index 6a95fcee5f..203d5c18f2 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -1668,4 +1668,15 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { * Has close been called already? */ public abstract boolean isClosed(); + + @Override + public String toString() { + return "OPCPackage{" + + "packageAccess=" + packageAccess + + ", relationships=" + relationships + + ", packageProperties=" + packageProperties + + ", isDirty=" + isDirty + + //", originalPackagePath='" + originalPackagePath + '\'' + + '}'; + } } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java index 0fdacdd214..4956032f67 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java @@ -51,7 +51,7 @@ public abstract class PackagePart implements RelationshipSource, Comparable<Pack /** * Flag to know if this part is a relationship. */ - private boolean _isRelationshipPart; + private final boolean _isRelationshipPart; /** * Flag to know if this part has been logically deleted. diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationship.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationship.java index c6edc81db2..bec1d00025 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationship.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationship.java @@ -58,42 +58,35 @@ public final class PackageRelationship { /** * Relation id. */ - private String id; + private final String id; /** * Reference to the package. */ - private OPCPackage container; + private final OPCPackage container; /** * Relationship type */ - private String relationshipType; + private final String relationshipType; /** * Part of this relationship source */ - private PackagePart source; + private final PackagePart source; /** * Targeting mode [Internal|External] */ - private TargetMode targetMode; + private final TargetMode targetMode; /** * Target URI */ - private URI targetUri; + private final URI targetUri; /** * Constructor. - * - * @param pkg - * @param sourcePart - * @param targetUri - * @param targetMode - * @param relationshipType - * @param id */ public PackageRelationship(OPCPackage pkg, PackagePart sourcePart, URI targetUri, TargetMode targetMode, String relationshipType, @@ -123,7 +116,7 @@ public final class PackageRelationship { PackageRelationship rel = (PackageRelationship) obj; return (this.id.equals(rel.id) && this.relationshipType.equals(rel.relationshipType) - && (rel.source != null ? rel.source.equals(this.source) : true) + && (rel.source == null || rel.source.equals(this.source)) && this.targetMode == rel.targetMode && this.targetUri .equals(rel.targetUri)); } @@ -207,11 +200,11 @@ public final class PackageRelationship { @Override public String toString() { - return (id == null ? "id=null" : "id=" + id) + - (container == null ? " - container=null" : " - container=" + container) + - (relationshipType == null ? " - relationshipType=null" : " - relationshipType=" + relationshipType) + + return "id=" + id + + " - container=" + container + + " - relationshipType=" + relationshipType + (source == null ? " - source=null" : " - source=" + getSourceURI().toASCIIString()) + - (targetUri == null ? " - target=null" : " - target=" + getTargetURI().toASCIIString()) + + " - target=" + getTargetURI().toASCIIString() + (targetMode == null ? ",targetMode=null" : ",targetMode=" + targetMode); } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java index 1b1cf86154..3d4b1b7268 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java @@ -125,7 +125,7 @@ public class XWPFStyles extends POIXMLDocumentPart { /** * Sets the ctStyles * - * @param styles + * @param styles The CTStyles object to set */ public void setStyles(CTStyles styles) { ctStyles = styles; @@ -160,8 +160,7 @@ public class XWPFStyles extends POIXMLDocumentPart { /** * add a style to the document * - * @param style - * @throws IOException + * @param style The style to add */ public void addStyle(XWPFStyle style) { listStyle.add(style); @@ -190,9 +189,10 @@ public class XWPFStyles extends POIXMLDocumentPart { /** * get the styles which are related to the parameter style and their relatives + * * this method can be used to copy all styles from one document to another document * - * @param style + * @param style The style to look for * @return a list of all styles which were used by this method */ public List<XWPFStyle> getUsedStyleList(XWPFStyle style) { @@ -204,8 +204,12 @@ public class XWPFStyles extends POIXMLDocumentPart { /** * get the styles which are related to parameter style * - * @param style - * @return all Styles of the parameterList + * @param style The style to look for + * @param usedStyleList The list of current style, found + * related styles are added to this list. + * + * @return all Styles of the parameterList, returns the same object as + * the input-parameter usedStyleList */ private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList) { String basisStyleID = style.getBasisStyleID(); @@ -233,20 +237,17 @@ public class XWPFStyles extends POIXMLDocumentPart { protected CTLanguage getCTLanguage() { ensureDocDefaults(); - CTLanguage lang = null; if (defaultRunStyle.getRPr().sizeOfLangArray() > 0) { - lang = defaultRunStyle.getRPr().getLangArray(0); + return defaultRunStyle.getRPr().getLangArray(0); } else { - lang = defaultRunStyle.getRPr().addNewLang(); + return defaultRunStyle.getRPr().addNewLang(); } - - return lang; } /** * Sets the default spelling language on ctStyles DocDefaults parameter * - * @param strSpellingLanguage + * @param strSpellingLanguage the default spelling language to use */ public void setSpellingLanguage(String strSpellingLanguage) { CTLanguage lang = getCTLanguage(); @@ -257,7 +258,7 @@ public class XWPFStyles extends POIXMLDocumentPart { /** * Sets the default East Asia spelling language on ctStyles DocDefaults parameter * - * @param strEastAsia + * @param strEastAsia The default East Asia spelling language to use */ public void setEastAsia(String strEastAsia) { CTLanguage lang = getCTLanguage(); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index 1b68ed2559..4c4cfd7820 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -47,9 +47,9 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; * Cell is the thing that holds the actual content (paragraphs etc) */ public class XWPFTableCell implements IBody, ICell { - private static EnumMap<XWPFVertAlign, STVerticalJc.Enum> alignMap; + private static final EnumMap<XWPFVertAlign, STVerticalJc.Enum> alignMap; // Create a map from the STVerticalJc.Enum values to the XWPF-level enums - private static HashMap<Integer, XWPFVertAlign> stVertAlignTypeMap; + private static final HashMap<Integer, XWPFVertAlign> stVertAlignTypeMap; static { // populate enum maps @@ -64,7 +64,6 @@ public class XWPFTableCell implements IBody, ICell { stVertAlignTypeMap.put(STVerticalJc.INT_CENTER, XWPFVertAlign.CENTER); stVertAlignTypeMap.put(STVerticalJc.INT_BOTH, XWPFVertAlign.BOTH); stVertAlignTypeMap.put(STVerticalJc.INT_BOTTOM, XWPFVertAlign.BOTTOM); - } private final CTTc ctTc; @@ -73,7 +72,7 @@ public class XWPFTableCell implements IBody, ICell { protected List<IBodyElement> bodyElements; protected IBody part; - private XWPFTableRow tableRow; + private final XWPFTableRow tableRow; /** * If a table cell does not include at least one block-level element, then this document shall be considered corrupt diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java index e57fc1255e..e81605fc26 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCCompliancePackageModel.java @@ -17,6 +17,7 @@ package org.apache.poi.openxml4j.opc.compliance; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -73,8 +74,8 @@ class TestOPCCompliancePackageModel { pkg.createPart(name, ContentTypes.XML); assertThrows(InvalidOperationException.class, () -> pkg.createPart(nameDerived, ContentTypes.EXTENSION_GIF), - "A package implementer shall neither create nor recognize a part with a part name derived from another " + - "part name by appending segments to it. [M1.11]"); + "A package implementer shall neither create nor recognize a part with a part name derived from another " + + "part name by appending segments to it. [M1.11]"); pkg.revert(); } } @@ -88,9 +89,9 @@ class TestOPCCompliancePackageModel { void testPartNameDerivationReadingFailure() { String filename = "OPCCompliance_DerivedPartNameFAIL.docx"; assertThrows(InvalidFormatException.class, () -> - OPCPackage.open(POIDataSamples.getOpenXML4JInstance().openResourceAsStream(filename)), - "A package implementer shall neither create nor recognize a part with a part name derived from another" + - " part name by appending segments to it. [M1.11]" + OPCPackage.open(POIDataSamples.getOpenXML4JInstance().openResourceAsStream(filename)), + "A package implementer shall neither create nor recognize a part with a part name derived from another" + + " part name by appending segments to it. [M1.11]" ); } @@ -107,8 +108,8 @@ class TestOPCCompliancePackageModel { pkg.createPart(name1, ContentTypes.XML); assertThrows(PartAlreadyExistsException.class, () -> pkg.createPart(name2, ContentTypes.XML), - "Packages shall not contain equivalent part names and package implementers shall neither create nor " + - "recognize packages with equivalent part names. [M1.12]" + "Packages shall not contain equivalent part names and package implementers shall neither create nor " + + "recognize packages with equivalent part names. [M1.12]" ); pkg.revert(); } @@ -125,8 +126,8 @@ class TestOPCCompliancePackageModel { PackagePartName partName = PackagingURIHelper.createPartName("/word/document.xml"); pkg.createPart(partName, ContentTypes.XML); assertThrows(InvalidOperationException.class, () -> pkg.createPart(partName, ContentTypes.XML), - "Packages shall not contain equivalent part names and package implementers shall neither create nor " + - "recognize packages with equivalent part names. [M1.12]" + "Packages shall not contain equivalent part names and package implementers shall neither create nor " + + "recognize packages with equivalent part names. [M1.12]" ); pkg.revert(); } @@ -134,7 +135,7 @@ class TestOPCCompliancePackageModel { /** * Try to add a relationship to a relationship part. - * + * <p> * Check rule M1.25: The Relationships part shall not have relationships to * any other part. Package implementers shall enforce this requirement upon * the attempt to create such a relationship and shall treat any such @@ -146,10 +147,21 @@ class TestOPCCompliancePackageModel { PackagePartName name1 = PackagingURIHelper.createPartName("/test/_rels/document.xml.rels"); assertThrows(InvalidOperationException.class, - () -> pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT), - "The Relationships part shall not have relationships to any other part [M1.25]" + () -> pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT), + "The Relationships part shall not have relationships to any other part [M1.25]" ); pkg.revert(); } } + + @Test + void testToString() throws IOException { + try (OPCPackage pkg = OPCPackage.create(TESTFILE)) { + assertEquals("OPCPackage{" + + "packageAccess=READ_WRITE, " + + "relationships=null, " + + "packageProperties=Name: /docProps/core.xml - Content Type: application/vnd.openxmlformats-package.core-properties+xml, " + + "isDirty=false}", pkg.toString()); + } + } } |