fixes and misc refactorings suggested by Intellij

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@806426 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2009-08-21 06:08:31 +00:00
parent 4076768ed3
commit ba518d2221
22 changed files with 64 additions and 297 deletions

View File

@ -28,7 +28,7 @@ public abstract class POIXMLFactory {
/**
* Create a POIXMLDocumentPart from existing package part and relation. This method is called
* from {@link POIXMLDocumentPart#read(POIXMLFactory)} when parsing a document
* from {@link POIXMLDocument#load(POIXMLFactory)} when parsing a document
*
* @param rel the package part relationship
* @param part the PackagePart representing the created instance

View File

@ -48,7 +48,7 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
/**
* Returns the core document properties, eg author
*/
public String getCorePropertiesText() throws IOException, OpenXML4JException, XmlException {
public String getCorePropertiesText() {
StringBuffer text = new StringBuffer();
PackagePropertiesPart props =
document.getProperties().getCoreProperties().getUnderlyingProperties();
@ -79,7 +79,7 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
* Returns the extended document properties, eg
* application
*/
public String getExtendedPropertiesText() throws IOException, OpenXML4JException, XmlException {
public String getExtendedPropertiesText() {
StringBuffer text = new StringBuffer();
org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties
props = document.getProperties().getExtendedProperties().getUnderlyingProperties();
@ -106,7 +106,7 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
* Returns the custom document properties, if
* there are any
*/
public String getCustomPropertiesText() throws IOException, OpenXML4JException, XmlException {
public String getCustomPropertiesText() {
StringBuffer text = new StringBuffer();
org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties
props = document.getProperties().getCustomProperties().getUnderlyingProperties();

View File

@ -38,19 +38,19 @@ public abstract class POIXMLTextExtractor extends POITextExtractor {
/**
* Returns the core document properties
*/
public CoreProperties getCoreProperties() throws IOException, OpenXML4JException, XmlException {
public CoreProperties getCoreProperties() {
return document.getProperties().getCoreProperties();
}
/**
* Returns the extended document properties
*/
public ExtendedProperties getExtendedProperties() throws IOException, OpenXML4JException, XmlException {
public ExtendedProperties getExtendedProperties() {
return document.getProperties().getExtendedProperties();
}
/**
* Returns the custom document properties
*/
public CustomProperties getCustomProperties() throws IOException, OpenXML4JException, XmlException {
public CustomProperties getCustomProperties() {
return document.getProperties().getCustomProperties();
}

View File

@ -682,7 +682,7 @@ public abstract class OPCPackage implements RelationshipSource {
throw new IllegalArgumentException("partName");
}
if (contentType == null || contentType == "") {
if (contentType == null || contentType.equals("")) {
throw new IllegalArgumentException("contentType");
}
@ -703,7 +703,7 @@ public abstract class OPCPackage implements RelationshipSource {
// A format consumer shall consider more than one core properties
// relationship for a package to be an error. If present, the
// relationship shall target the Core Properties part.
if (contentType == ContentTypes.CORE_PROPERTIES_PART) {
if (contentType.equals(ContentTypes.CORE_PROPERTIES_PART)) {
if (this.packageProperties != null)
throw new InvalidOperationException(
"OPC Compliance error [M4.1]: you try to add more than one core properties relationship in the package !");
@ -1200,16 +1200,12 @@ public abstract class OPCPackage implements RelationshipSource {
/**
* @see org.apache.poi.openxml4j.opc.RelationshipSource#isRelationshipExists(org.apache.poi.openxml4j.opc.PackageRelationship)
*/
@SuppressWarnings("finally")
public boolean isRelationshipExists(PackageRelationship rel) {
try {
for (PackageRelationship r : this.getRelationships()) {
if (r == rel)
return true;
}
} finally {
return false;
}
for (PackageRelationship r : this.getRelationships()) {
if (r == rel)
return true;
}
return false;
}
/**

View File

@ -444,16 +444,16 @@ public abstract class PackagePart implements RelationshipSource {
* else returns <b>false</b>
* @see org.apache.poi.openxml4j.opc.RelationshipSource#isRelationshipExists(org.apache.poi.openxml4j.opc.PackageRelationship)
*/
@SuppressWarnings("finally")
public boolean isRelationshipExists(PackageRelationship rel) {
try {
for (PackageRelationship r : this.getRelationships()) {
if (r == rel)
return true;
}
} finally {
return false;
}
try {
for (PackageRelationship r : this.getRelationships()) {
if (r == rel)
return true;
}
} catch (InvalidFormatException e){
;
}
return false;
}
/**

View File

@ -233,7 +233,7 @@ public final class PackagePartName implements Comparable<PackagePartName> {
*/
private static void throwExceptionIfPartNameHaveInvalidSegments(URI partUri)
throws InvalidFormatException {
if (partUri == null || "".equals(partUri)) {
if (partUri == null) {
throw new IllegalArgumentException("partUri");
}

View File

@ -120,8 +120,8 @@ public final class PackageRelationship {
return false;
}
PackageRelationship rel = (PackageRelationship) obj;
return (this.id == rel.id
&& this.relationshipType == rel.relationshipType
return (this.id.equals(rel.id)
&& this.relationshipType.equals(rel.relationshipType)
&& (rel.source != null ? rel.source.equals(this.source) : true)
&& this.targetMode == rel.targetMode && this.targetUri
.equals(rel.targetUri));
@ -217,7 +217,7 @@ public final class PackageRelationship {
sb.append(container == null ? " - container=null" : " - container="
+ container.toString());
sb.append(relationshipType == null ? " - relationshipType=null"
: " - relationshipType=" + relationshipType.toString());
: " - relationshipType=" + relationshipType);
sb.append(source == null ? " - source=null" : " - source="
+ getSourceURI().toASCIIString());
sb.append(targetUri == null ? " - target=null" : " - target="

View File

@ -620,8 +620,8 @@ public final class PackagingURIHelper {
if (partName == null)
throw new IllegalArgumentException("partName");
if (PackagingURIHelper.PACKAGE_ROOT_URI.getPath() == partName.getURI()
.getPath())
if (PackagingURIHelper.PACKAGE_ROOT_URI.getPath().equals(
partName.getURI().getPath()) )
return PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_PART_NAME;
if (partName.isRelationshipPartURI())

View File

@ -574,10 +574,10 @@ public final class PackagePropertiesPart extends PackagePart implements
* @param d
* The Date to convert.
* @return The formated date or null.
* @see java.util.SimpleDateFormat
* @see java.text.SimpleDateFormat
*/
private String getDateValue(Nullable<Date> d) {
if (d == null || d.equals("")) {
if (d == null) {
return "";
}
SimpleDateFormat df = new SimpleDateFormat(

View File

@ -113,7 +113,7 @@ public final class ZipHelper {
if (opcItemName == null)
throw new IllegalArgumentException("opcItemName");
String retVal = new String(opcItemName);
String retVal = opcItemName;
while (retVal.startsWith(FORWARD_SLASH))
retVal = retVal.substring(1);
return retVal;
@ -131,7 +131,7 @@ public final class ZipHelper {
if (opcItemName == null)
throw new IllegalArgumentException("opcItemName");
String retVal = new String(opcItemName);
String retVal = opcItemName;
while (retVal.startsWith(FORWARD_SLASH))
retVal = retVal.substring(1);
try {

View File

@ -20,8 +20,6 @@ package org.apache.poi.xssf.dev;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import java.io.*;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
@ -43,9 +41,6 @@ public final class XSSFDump {
}
public static void dump(ZipFile zip) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
String zipname = zip.getName();
int sep = zipname.lastIndexOf('.');
File root = new File(zipname.substring(0, sep));

View File

@ -99,7 +99,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
for(int i=0; i<workbook.getNumberOfSheets(); i++) {
XSSFSheet sheet = workbook.getSheetAt(i);
if(includeSheetNames) {
text.append(workbook.getSheetName(i) + "\n");
text.append(workbook.getSheetName(i)).append("\n");
}
// Header(s), if present
@ -137,7 +137,7 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor implements org.apach
// Replace any newlines with spaces, otherwise it
// breaks the output
String commentText = comment.getString().getString().replace('\n', ' ');
text.append(" Comment by "+comment.getAuthor()+": "+commentText);
text.append(" Comment by ").append(comment.getAuthor()).append(": ").append(commentText);
}
if(ri.hasNext())

View File

@ -121,19 +121,14 @@ public class StylesTable extends POIXMLDocumentPart {
for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) {
borders.add(new XSSFCellBorder(border));
}
if(doc.getStyleSheet().getCellXfs() != null)
for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) {
xfs.add(xf);
}
if(doc.getStyleSheet().getCellStyleXfs() != null)
for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) {
styleXfs.add(xf);
}
// dxf
if(doc.getStyleSheet().getDxfs() != null)
for (CTDxf dxf : doc.getStyleSheet().getDxfs().getDxfArray()) {
dxfs.add(dxf);
}
CTCellXfs cellXfs = doc.getStyleSheet().getCellXfs();
if(cellXfs != null) xfs.addAll(Arrays.asList(cellXfs.getXfArray()));
CTCellStyleXfs cellStyleXfs = doc.getStyleSheet().getCellStyleXfs();
if(cellStyleXfs != null) styleXfs.addAll(Arrays.asList(cellStyleXfs.getXfArray()));
CTDxfs styleDxfs = doc.getStyleSheet().getDxfs();
if(styleDxfs != null) dxfs.addAll(Arrays.asList(styleDxfs.getDxfArray()));
} catch (XmlException e) {
throw new IOException(e.getLocalizedMessage());

View File

@ -1,67 +0,0 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.model;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.openxml4j.opc.PackagePart;
/**
* Common interface for XSSF models, which have (typically
* binary) children to them.
* One example is a VmlDrawing (Drawing), which can have
* raw images associated with it.
*/
public interface XSSFChildContainingModel extends XSSFModel {
/**
* Returns the relationship type of any children we
* expect
*/
public String[] getChildrenRelationshipTypes();
/**
* Called for each matching child, so that the
* appropriate model or usermodel thing can be
* created for it.
* @param childPart The PackagePart of the child
* @param childRelId the ID of the relationship the child comes from
*/
public void generateChild(PackagePart childPart, String childRelId);
/**
* Returns the number of children contained, which
* will need to be written out.
*/
public int getNumberOfChildren();
/**
* Called for each child in turn when writing out,
* which returns a WritableChild for the child in
* that position. The WritableChild has enough
* information to be able to write it out.
* @param index A zero based index of this child
*/
public WritableChild getChildForWriting(int index);
static class WritableChild {
private XSSFWritableModel model;
private XSSFRelation relation;
public WritableChild(XSSFWritableModel model, XSSFRelation rel) {
this.model = model;
this.relation = rel;
}
public XSSFWritableModel getModel() { return model; }
public XSSFRelation getRelation() { return relation; }
}
}

View File

@ -1,34 +0,0 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.model;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.xssf.usermodel.XSSFRelation;
/**
* Common interface for XSSF models, which deal with
* parts of the xssf file.
* These should also implement a constructor of
* (InputStream is), so they can be used with
* {@link XSSFRelation}
*/
public interface XSSFModel extends XSSFWritableModel {
/** Read from the given InputStream */
public void readFrom(InputStream is) throws IOException;
}

View File

@ -1,37 +0,0 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.model;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.xssf.usermodel.XSSFRelation;
/**
* A very stripped down XSSF models interface,
* which only allows writing out. Normally only
* used with the children of a
* {@link XSSFChildContainingModel}.
* Most proper models will go for {@link XSSFModel}
* instead of this one.
* {@link XSSFRelation} needs classes to implement
* this, so that it can write them out.
*/
public interface XSSFWritableModel {
/** Write to the supplied OutputStream, with default options */
public void writeTo(OutputStream out) throws IOException;
}

View File

@ -1,72 +0,0 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.model.XSSFWritableModel;
import org.apache.poi.POIXMLException;
import org.apache.poi.openxml4j.opc.PackagePart;
/**
*
* @author Nick Burch
*/
public final class XSSFActiveXData implements PictureData, XSSFWritableModel {
private final PackagePart _packagePart;
private final String _originalId;
public XSSFActiveXData(PackagePart packagePart, String originalId) {
_packagePart = packagePart;
_originalId = originalId;
}
public XSSFActiveXData(PackagePart packagePart) {
this(packagePart, null);
}
public String getOriginalId() {
return _originalId;
}
public PackagePart getPart() {
return _packagePart;
}
public void writeTo(OutputStream out) throws IOException {
IOUtils.copy(_packagePart.getInputStream(), out);
}
public byte[] getData() {
// TODO - is this right?
// Are there headers etc?
try {
return IOUtils.toByteArray(_packagePart.getInputStream());
} catch(IOException e) {
throw new POIXMLException(e);
}
}
public String suggestFileExtension() {
return _packagePart.getPartName().getExtension();
}
}

View File

@ -857,7 +857,7 @@ public final class XSSFCell implements Cell {
int sstIndex = Integer.parseInt(_cell.getV());
XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
String text = rt.getString();
return Boolean.valueOf(text).booleanValue();
return Boolean.valueOf(text);
case CELL_TYPE_NUMERIC:
return Double.parseDouble(_cell.getV()) != 0;

View File

@ -311,7 +311,7 @@ public class XSSFCellStyle implements CellStyle {
*/
public short getBottomBorderColor() {
XSSFColor clr = getBottomBorderXSSFColor();
return clr == null ? IndexedColors.BLACK.getIndex() : (short)clr.getIndexed();
return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
}
/**
@ -359,7 +359,7 @@ public class XSSFCellStyle implements CellStyle {
*/
public short getFillBackgroundColor() {
XSSFColor clr = getFillBackgroundXSSFColor();
return clr == null ? IndexedColors.AUTOMATIC.getIndex() : (short)clr.getIndexed();
return clr == null ? IndexedColors.AUTOMATIC.getIndex() : clr.getIndexed();
}
/**
@ -391,7 +391,7 @@ public class XSSFCellStyle implements CellStyle {
*/
public short getFillForegroundColor() {
XSSFColor clr = getFillForegroundXSSFColor();
return clr == null ? IndexedColors.AUTOMATIC.getIndex() : (short)clr.getIndexed();
return clr == null ? IndexedColors.AUTOMATIC.getIndex() : clr.getIndexed();
}
/**
@ -508,7 +508,7 @@ public class XSSFCellStyle implements CellStyle {
*/
public short getLeftBorderColor() {
XSSFColor clr = getLeftBorderXSSFColor();
return clr == null ? IndexedColors.BLACK.getIndex() : (short)clr.getIndexed();
return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
}
/**
@ -543,7 +543,7 @@ public class XSSFCellStyle implements CellStyle {
*/
public short getRightBorderColor() {
XSSFColor clr = getRightBorderXSSFColor();
return clr == null ? IndexedColors.BLACK.getIndex() : (short)clr.getIndexed();
return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
}
/**
* Get the color to use for the right border
@ -586,7 +586,7 @@ public class XSSFCellStyle implements CellStyle {
*/
public short getTopBorderColor() {
XSSFColor clr = getTopBorderXSSFColor();
return clr == null ? IndexedColors.BLACK.getIndex() : (short)clr.getIndexed();
return clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndexed();
}
/**

View File

@ -1369,13 +1369,13 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
boolean styleChanged = style != null
&& ci.getStyle() != style.intValue();
&& ci.getStyle() != style;
boolean levelChanged = level != null
&& ci.getOutlineLevel() != level.intValue();
&& ci.getOutlineLevel() != level;
boolean hiddenChanged = hidden != null
&& ci.getHidden() != hidden.booleanValue();
&& ci.getHidden() != hidden;
boolean collapsedChanged = collapsed != null
&& ci.getCollapsed() != collapsed.booleanValue();
&& ci.getCollapsed() != collapsed;
boolean columnChanged = levelChanged || hiddenChanged
|| collapsedChanged || styleChanged;
if (!columnChanged) {
@ -1828,11 +1828,10 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
/**
* @param rowIndex the zero based row index to expand
* @param rowNumber the zero based row index to expand
*/
private void expandRow(int rowNumber) {
int idx = rowNumber;
if (idx == -1)
if (rowNumber == -1)
return;
XSSFRow row = getRow(rowNumber);
// If it is already expanded do nothing.
@ -1840,10 +1839,10 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
return;
// Find the start of the group.
int startIdx = findStartOfRowOutlineGroup(idx);
int startIdx = findStartOfRowOutlineGroup(rowNumber);
// Find the end of the group.
int endIdx = findEndOfRowOutlineGroup(idx);
int endIdx = findEndOfRowOutlineGroup(rowNumber);
// expand:
// collapsed must be unset
@ -1856,7 +1855,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
// is the enclosing group
// hidden bit only is altered for this outline level. ie. don't
// un-collapse contained groups
if (!isRowGroupHiddenByParent(idx)) {
if (!isRowGroupHiddenByParent(rowNumber)) {
for (int i = startIdx; i < endIdx; i++) {
if (row.getCTRow().getOutlineLevel() == getRow(i).getCTRow()
.getOutlineLevel()) {
@ -1871,7 +1870,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
/**
* @param rowIndex the zero based row index to find from
* @param row the zero based row index to find from
*/
public int findEndOfRowOutlineGroup(int row) {
int level = getRow(row).getCTRow().getOutlineLevel();
@ -1886,7 +1885,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
/**
* @param rowIndex the zero based row index to find from
* @param row the zero based row index to find from
*/
private boolean isRowGroupHiddenByParent(int row) {
// Look out outline details of end
@ -1922,7 +1921,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
}
/**
* @param rowIndex the zero based row index to find from
* @param row the zero based row index to find from
*/
private boolean isRowGroupCollapsed(int row) {
int collapseRow = findEndOfRowOutlineGroup(row) + 1;

View File

@ -28,6 +28,7 @@ import org.apache.poi.POIXMLProperties.CoreProperties;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.XWPFTestDataSamples;
/**
* Test setting extended and custom OOXML properties
@ -37,15 +38,7 @@ public final class TestPOIXMLProperties extends TestCase {
private CoreProperties _coreProperties;
public void setUp() throws Exception{
File sampleFile = new File(
System.getProperty("HWPF.testdata.path") +
File.separator + "documentProperties.docx"
);
assertTrue(sampleFile.exists());
XWPFDocument sampleDoc;
sampleDoc = new XWPFDocument(
POIXMLDocument.openPackage(sampleFile.toString())
);
XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("documentProperties.docx");
_props = sampleDoc.getProperties();
_coreProperties = _props.getCoreProperties();
assertNotNull(_props);

View File

@ -696,7 +696,6 @@ public class TestWrite extends TestCase
*/
private void testRecreate(final File f)
{
System.out.println("Recreating file \"" + f + "\"");
try
{
/* Read the POI filesystem's property set streams: */