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
This commit is contained in:
Dominik Stadler 2021-01-30 18:42:05 +00:00
parent 615de587f7
commit a10ca15627
8 changed files with 71 additions and 59 deletions

View File

@ -35,10 +35,8 @@ import org.apache.poi.extractor.POITextExtractor;
import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor; import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;
import org.apache.poi.hssf.extractor.EventBasedExcelExtractor; import org.apache.poi.hssf.extractor.EventBasedExcelExtractor;
import org.apache.poi.ooxml.POIXMLException; import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.ss.extractor.ExcelExtractor; import org.apache.poi.ss.extractor.ExcelExtractor;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.xmlbeans.XmlException;
/** /**
* Base class with things that can be run for any supported file handler * 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 (InputStream stream = new FileInputStream(file)) {
try (POITextExtractor streamExtractor = ExtractorFactory.createExtractor(stream)) { try (POITextExtractor streamExtractor = ExtractorFactory.createExtractor(stream)) {
assertNotNull(streamExtractor); assertNotNull(streamExtractor);

View File

@ -16,45 +16,43 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.common.usermodel; package org.apache.poi.common.usermodel;
import org.apache.poi.util.Removal;
/** /**
* Represents a hyperlink. * Represents a hyperlink.
*/ */
public interface Hyperlink { public interface Hyperlink {
/** /**
* Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file, etc. * Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file, etc.
* *
* @return the address of this hyperlink * @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. * 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 * @param address the address of this hyperlink
*/ */
public void setAddress(String address); void setAddress(String address);
/** /**
* Return text label for this hyperlink * Return text label for this hyperlink
* *
* @return text to display * @return text to display
*/ */
public String getLabel(); String getLabel();
/** /**
* Sets text label for this hyperlink * Sets text label for this hyperlink
* *
* @param label 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
* *
* @return the type of this hyperlink * @return the type of this hyperlink
*/ */
public HyperlinkType getType(); HyperlinkType getType();
} }

View File

@ -1668,4 +1668,15 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
* Has close been called already? * Has close been called already?
*/ */
public abstract boolean isClosed(); public abstract boolean isClosed();
@Override
public String toString() {
return "OPCPackage{" +
"packageAccess=" + packageAccess +
", relationships=" + relationships +
", packageProperties=" + packageProperties +
", isDirty=" + isDirty +
//", originalPackagePath='" + originalPackagePath + '\'' +
'}';
}
} }

View File

@ -51,7 +51,7 @@ public abstract class PackagePart implements RelationshipSource, Comparable<Pack
/** /**
* Flag to know if this part is a relationship. * 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. * Flag to know if this part has been logically deleted.

View File

@ -58,42 +58,35 @@ public final class PackageRelationship {
/** /**
* Relation id. * Relation id.
*/ */
private String id; private final String id;
/** /**
* Reference to the package. * Reference to the package.
*/ */
private OPCPackage container; private final OPCPackage container;
/** /**
* Relationship type * Relationship type
*/ */
private String relationshipType; private final String relationshipType;
/** /**
* Part of this relationship source * Part of this relationship source
*/ */
private PackagePart source; private final PackagePart source;
/** /**
* Targeting mode [Internal|External] * Targeting mode [Internal|External]
*/ */
private TargetMode targetMode; private final TargetMode targetMode;
/** /**
* Target URI * Target URI
*/ */
private URI targetUri; private final URI targetUri;
/** /**
* Constructor. * Constructor.
*
* @param pkg
* @param sourcePart
* @param targetUri
* @param targetMode
* @param relationshipType
* @param id
*/ */
public PackageRelationship(OPCPackage pkg, PackagePart sourcePart, public PackageRelationship(OPCPackage pkg, PackagePart sourcePart,
URI targetUri, TargetMode targetMode, String relationshipType, URI targetUri, TargetMode targetMode, String relationshipType,
@ -123,7 +116,7 @@ public final class PackageRelationship {
PackageRelationship rel = (PackageRelationship) obj; PackageRelationship rel = (PackageRelationship) obj;
return (this.id.equals(rel.id) return (this.id.equals(rel.id)
&& this.relationshipType.equals(rel.relationshipType) && 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 && this.targetMode == rel.targetMode && this.targetUri
.equals(rel.targetUri)); .equals(rel.targetUri));
} }
@ -207,11 +200,11 @@ public final class PackageRelationship {
@Override @Override
public String toString() { public String toString() {
return (id == null ? "id=null" : "id=" + id) + return "id=" + id +
(container == null ? " - container=null" : " - container=" + container) + " - container=" + container +
(relationshipType == null ? " - relationshipType=null" : " - relationshipType=" + relationshipType) + " - relationshipType=" + relationshipType +
(source == null ? " - source=null" : " - source=" + getSourceURI().toASCIIString()) + (source == null ? " - source=null" : " - source=" + getSourceURI().toASCIIString()) +
(targetUri == null ? " - target=null" : " - target=" + getTargetURI().toASCIIString()) + " - target=" + getTargetURI().toASCIIString() +
(targetMode == null ? ",targetMode=null" : ",targetMode=" + targetMode); (targetMode == null ? ",targetMode=null" : ",targetMode=" + targetMode);
} }
} }

View File

@ -125,7 +125,7 @@ public class XWPFStyles extends POIXMLDocumentPart {
/** /**
* Sets the ctStyles * Sets the ctStyles
* *
* @param styles * @param styles The CTStyles object to set
*/ */
public void setStyles(CTStyles styles) { public void setStyles(CTStyles styles) {
ctStyles = styles; ctStyles = styles;
@ -160,8 +160,7 @@ public class XWPFStyles extends POIXMLDocumentPart {
/** /**
* add a style to the document * add a style to the document
* *
* @param style * @param style The style to add
* @throws IOException
*/ */
public void addStyle(XWPFStyle style) { public void addStyle(XWPFStyle style) {
listStyle.add(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 * 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 * 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 * @return a list of all styles which were used by this method
*/ */
public List<XWPFStyle> getUsedStyleList(XWPFStyle style) { public List<XWPFStyle> getUsedStyleList(XWPFStyle style) {
@ -204,8 +204,12 @@ public class XWPFStyles extends POIXMLDocumentPart {
/** /**
* get the styles which are related to parameter style * get the styles which are related to parameter style
* *
* @param style * @param style The style to look for
* @return all Styles of the parameterList * @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) { private List<XWPFStyle> getUsedStyleList(XWPFStyle style, List<XWPFStyle> usedStyleList) {
String basisStyleID = style.getBasisStyleID(); String basisStyleID = style.getBasisStyleID();
@ -233,20 +237,17 @@ public class XWPFStyles extends POIXMLDocumentPart {
protected CTLanguage getCTLanguage() { protected CTLanguage getCTLanguage() {
ensureDocDefaults(); ensureDocDefaults();
CTLanguage lang = null;
if (defaultRunStyle.getRPr().sizeOfLangArray() > 0) { if (defaultRunStyle.getRPr().sizeOfLangArray() > 0) {
lang = defaultRunStyle.getRPr().getLangArray(0); return defaultRunStyle.getRPr().getLangArray(0);
} else { } else {
lang = defaultRunStyle.getRPr().addNewLang(); return defaultRunStyle.getRPr().addNewLang();
} }
return lang;
} }
/** /**
* Sets the default spelling language on ctStyles DocDefaults parameter * Sets the default spelling language on ctStyles DocDefaults parameter
* *
* @param strSpellingLanguage * @param strSpellingLanguage the default spelling language to use
*/ */
public void setSpellingLanguage(String strSpellingLanguage) { public void setSpellingLanguage(String strSpellingLanguage) {
CTLanguage lang = getCTLanguage(); CTLanguage lang = getCTLanguage();
@ -257,7 +258,7 @@ public class XWPFStyles extends POIXMLDocumentPart {
/** /**
* Sets the default East Asia spelling language on ctStyles DocDefaults parameter * 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) { public void setEastAsia(String strEastAsia) {
CTLanguage lang = getCTLanguage(); CTLanguage lang = getCTLanguage();

View File

@ -47,9 +47,9 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
* Cell is the thing that holds the actual content (paragraphs etc) * Cell is the thing that holds the actual content (paragraphs etc)
*/ */
public class XWPFTableCell implements IBody, ICell { 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 // 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 { static {
// populate enum maps // populate enum maps
@ -64,7 +64,6 @@ public class XWPFTableCell implements IBody, ICell {
stVertAlignTypeMap.put(STVerticalJc.INT_CENTER, XWPFVertAlign.CENTER); stVertAlignTypeMap.put(STVerticalJc.INT_CENTER, XWPFVertAlign.CENTER);
stVertAlignTypeMap.put(STVerticalJc.INT_BOTH, XWPFVertAlign.BOTH); stVertAlignTypeMap.put(STVerticalJc.INT_BOTH, XWPFVertAlign.BOTH);
stVertAlignTypeMap.put(STVerticalJc.INT_BOTTOM, XWPFVertAlign.BOTTOM); stVertAlignTypeMap.put(STVerticalJc.INT_BOTTOM, XWPFVertAlign.BOTTOM);
} }
private final CTTc ctTc; private final CTTc ctTc;
@ -73,7 +72,7 @@ public class XWPFTableCell implements IBody, ICell {
protected List<IBodyElement> bodyElements; protected List<IBodyElement> bodyElements;
protected IBody part; 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 * If a table cell does not include at least one block-level element, then this document shall be considered corrupt

View File

@ -17,6 +17,7 @@
package org.apache.poi.openxml4j.opc.compliance; 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.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -73,8 +74,8 @@ class TestOPCCompliancePackageModel {
pkg.createPart(name, ContentTypes.XML); pkg.createPart(name, ContentTypes.XML);
assertThrows(InvalidOperationException.class, () -> pkg.createPart(nameDerived, ContentTypes.EXTENSION_GIF), 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 " + "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]"); "part name by appending segments to it. [M1.11]");
pkg.revert(); pkg.revert();
} }
} }
@ -88,9 +89,9 @@ class TestOPCCompliancePackageModel {
void testPartNameDerivationReadingFailure() { void testPartNameDerivationReadingFailure() {
String filename = "OPCCompliance_DerivedPartNameFAIL.docx"; String filename = "OPCCompliance_DerivedPartNameFAIL.docx";
assertThrows(InvalidFormatException.class, () -> assertThrows(InvalidFormatException.class, () ->
OPCPackage.open(POIDataSamples.getOpenXML4JInstance().openResourceAsStream(filename)), OPCPackage.open(POIDataSamples.getOpenXML4JInstance().openResourceAsStream(filename)),
"A package implementer shall neither create nor recognize a part with a part name derived from another" + "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]" " part name by appending segments to it. [M1.11]"
); );
} }
@ -107,8 +108,8 @@ class TestOPCCompliancePackageModel {
pkg.createPart(name1, ContentTypes.XML); pkg.createPart(name1, ContentTypes.XML);
assertThrows(PartAlreadyExistsException.class, () -> pkg.createPart(name2, ContentTypes.XML), assertThrows(PartAlreadyExistsException.class, () -> pkg.createPart(name2, ContentTypes.XML),
"Packages shall not contain equivalent part names and package implementers shall neither create nor " + "Packages shall not contain equivalent part names and package implementers shall neither create nor " +
"recognize packages with equivalent part names. [M1.12]" "recognize packages with equivalent part names. [M1.12]"
); );
pkg.revert(); pkg.revert();
} }
@ -125,8 +126,8 @@ class TestOPCCompliancePackageModel {
PackagePartName partName = PackagingURIHelper.createPartName("/word/document.xml"); PackagePartName partName = PackagingURIHelper.createPartName("/word/document.xml");
pkg.createPart(partName, ContentTypes.XML); pkg.createPart(partName, ContentTypes.XML);
assertThrows(InvalidOperationException.class, () -> 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 " + "Packages shall not contain equivalent part names and package implementers shall neither create nor " +
"recognize packages with equivalent part names. [M1.12]" "recognize packages with equivalent part names. [M1.12]"
); );
pkg.revert(); pkg.revert();
} }
@ -134,7 +135,7 @@ class TestOPCCompliancePackageModel {
/** /**
* Try to add a relationship to a relationship part. * Try to add a relationship to a relationship part.
* * <p>
* Check rule M1.25: The Relationships part shall not have relationships to * Check rule M1.25: The Relationships part shall not have relationships to
* any other part. Package implementers shall enforce this requirement upon * any other part. Package implementers shall enforce this requirement upon
* the attempt to create such a relationship and shall treat any such * 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"); PackagePartName name1 = PackagingURIHelper.createPartName("/test/_rels/document.xml.rels");
assertThrows(InvalidOperationException.class, assertThrows(InvalidOperationException.class,
() -> pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT), () -> pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT),
"The Relationships part shall not have relationships to any other part [M1.25]" "The Relationships part shall not have relationships to any other part [M1.25]"
); );
pkg.revert(); 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());
}
}
} }