diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2018-05-27 21:59:18 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2018-05-27 21:59:18 +0000 |
commit | 6ce329eca43ad902a132cf86e7b9853f652fc3c1 (patch) | |
tree | 858df04c39e1f14f6ed6e863c4bd366f07529eb4 /src/ooxml/java/org | |
parent | 11457e8f684474e1583fc59477abe52452e66f65 (diff) | |
download | poi-6ce329eca43ad902a132cf86e7b9853f652fc3c1.tar.gz poi-6ce329eca43ad902a132cf86e7b9853f652fc3c1.zip |
#62355 - unsplit packages - 1 - moved classes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1832358 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org')
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/POIXMLDocument.java (renamed from src/ooxml/java/org/apache/poi/POIXMLDocument.java) | 2 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java (renamed from src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java) | 2 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/POIXMLException.java (renamed from src/ooxml/java/org/apache/poi/POIXMLException.java) | 2 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/POIXMLFactory.java (renamed from src/ooxml/java/org/apache/poi/POIXMLFactory.java) | 2 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java (renamed from src/ooxml/java/org/apache/poi/POIXMLProperties.java) | 4 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/POIXMLRelation.java (renamed from src/ooxml/java/org/apache/poi/POIXMLRelation.java) | 2 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/POIXMLTypeLoader.java (renamed from src/ooxml/java/org/apache/poi/POIXMLTypeLoader.java) | 6 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/dev/OOXMLLister.java (renamed from src/ooxml/java/org/apache/poi/dev/OOXMLLister.java) | 2 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java (renamed from src/ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java) | 2 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/extractor/CommandLineTextExtractor.java (renamed from src/ooxml/java/org/apache/poi/extractor/CommandLineTextExtractor.java) | 4 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java (renamed from src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java) | 11 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLPropertiesTextExtractor.java (renamed from src/ooxml/java/org/apache/poi/POIXMLPropertiesTextExtractor.java) | 4 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLTextExtractor.java (renamed from src/ooxml/java/org/apache/poi/POIXMLTextExtractor.java) | 10 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java (renamed from src/ooxml/java/org/apache/poi/util/DocumentHelper.java) | 4 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/util/IdentifierManager.java (renamed from src/ooxml/java/org/apache/poi/util/IdentifierManager.java) | 2 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/util/PackageHelper.java (renamed from src/ooxml/java/org/apache/poi/util/PackageHelper.java) | 5 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java (renamed from src/ooxml/java/org/apache/poi/util/SAXHelper.java) | 4 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedData.java | 104 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java | 410 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java | 275 | ||||
-rw-r--r-- | src/ooxml/java/org/apache/poi/util/OOXMLLite.java | 337 |
21 files changed, 37 insertions, 1157 deletions
diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocument.java index a7eaaf2c87..8925776b9e 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocument.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocument.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml; import java.io.Closeable; import java.io.File; diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java index e977e6ea39..5a368c576a 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml; import java.io.IOException; import java.net.URI; diff --git a/src/ooxml/java/org/apache/poi/POIXMLException.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLException.java index 82832ecff8..d002fa0e84 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLException.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLException.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml; /** * Indicates a generic OOXML error. diff --git a/src/ooxml/java/org/apache/poi/POIXMLFactory.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLFactory.java index 651f40cbc1..ca6cdb30c0 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLFactory.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLFactory.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml; import java.lang.reflect.InvocationTargetException; diff --git a/src/ooxml/java/org/apache/poi/POIXMLProperties.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java index b956b7ee55..04ca65fb21 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLProperties.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java @@ -14,9 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml; -import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS; +import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS; import java.io.IOException; import java.io.InputStream; diff --git a/src/ooxml/java/org/apache/poi/POIXMLRelation.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLRelation.java index 55d162c5f3..c661ce8e20 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLRelation.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLRelation.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml; import java.io.IOException; import java.io.InputStream; diff --git a/src/ooxml/java/org/apache/poi/POIXMLTypeLoader.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLTypeLoader.java index 8578a8333f..123c0b5786 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLTypeLoader.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLTypeLoader.java @@ -15,7 +15,7 @@ limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml; import java.io.File; import java.io.FileInputStream; @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.io.StringReader; -import java.lang.ref.WeakReference; import java.net.URL; import java.util.Collections; import java.util.HashMap; @@ -32,8 +31,7 @@ import java.util.Map; import javax.xml.stream.XMLStreamReader; import org.apache.poi.openxml4j.opc.PackageNamespaces; -import org.apache.poi.util.DocumentHelper; -import org.apache.poi.util.Removal; +import org.apache.poi.ooxml.util.DocumentHelper; import org.apache.xmlbeans.SchemaType; import org.apache.xmlbeans.SchemaTypeLoader; import org.apache.xmlbeans.XmlBeans; diff --git a/src/ooxml/java/org/apache/poi/dev/OOXMLLister.java b/src/ooxml/java/org/apache/poi/ooxml/dev/OOXMLLister.java index cbbca45cf9..177f9f9335 100644 --- a/src/ooxml/java/org/apache/poi/dev/OOXMLLister.java +++ b/src/ooxml/java/org/apache/poi/ooxml/dev/OOXMLLister.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi.dev; +package org.apache.poi.ooxml.dev; import java.io.*; import java.util.ArrayList; diff --git a/src/ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java b/src/ooxml/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java index e8ae9eb103..47ec47055e 100644 --- a/src/ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java +++ b/src/ooxml/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi.dev; +package org.apache.poi.ooxml.dev; import java.io.BufferedOutputStream; import java.io.File; diff --git a/src/ooxml/java/org/apache/poi/extractor/CommandLineTextExtractor.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/CommandLineTextExtractor.java index 264daa028f..999abd46ee 100644 --- a/src/ooxml/java/org/apache/poi/extractor/CommandLineTextExtractor.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/CommandLineTextExtractor.java @@ -14,11 +14,11 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi.extractor; +package org.apache.poi.ooxml.extractor; import java.io.File; -import org.apache.poi.POITextExtractor; +import org.apache.poi.extractor.POITextExtractor; /** * A command line wrapper around {@link ExtractorFactory}, useful diff --git a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java index 9a7765af0d..6603f58582 100644 --- a/src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/ExtractorFactory.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi.extractor; +package org.apache.poi.ooxml.extractor; import java.io.ByteArrayInputStream; import java.io.File; @@ -25,9 +25,9 @@ import java.util.ArrayList; import java.util.Iterator; import org.apache.poi.EncryptedDocumentException; -import org.apache.poi.POIOLE2TextExtractor; -import org.apache.poi.POITextExtractor; -import org.apache.poi.POIXMLTextExtractor; +import org.apache.poi.extractor.POIOLE2TextExtractor; +import org.apache.poi.extractor.POITextExtractor; +import org.apache.poi.extractor.OLE2ExtractorFactory; import org.apache.poi.hsmf.MAPIMessage; import org.apache.poi.hsmf.datatypes.AttachmentChunks; import org.apache.poi.hsmf.extractor.OutlookTextExtactor; @@ -61,7 +61,6 @@ import org.apache.poi.xdgf.extractor.XDGFVisioExtractor; import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFRelation; -import org.apache.poi.xslf.usermodel.XSLFSlideShow; import org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor; import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor; import org.apache.poi.xssf.extractor.XSSFExcelExtractor; @@ -125,7 +124,7 @@ public class ExtractorFactory { * Should this thread use event based extractors is available? * Checks the all-threads one first, then thread specific. */ - protected static boolean getPreferEventExtractor() { + public static boolean getPreferEventExtractor() { return OLE2ExtractorFactory.getPreferEventExtractor(); } diff --git a/src/ooxml/java/org/apache/poi/POIXMLPropertiesTextExtractor.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLPropertiesTextExtractor.java index f0fe9c30f7..47c37e84b4 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLPropertiesTextExtractor.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLPropertiesTextExtractor.java @@ -15,7 +15,7 @@ limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml.extractor; import java.math.BigDecimal; import java.text.DateFormat; @@ -24,6 +24,8 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; +import org.apache.poi.extractor.POITextExtractor; +import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.util.LocaleUtil; import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty; diff --git a/src/ooxml/java/org/apache/poi/POIXMLTextExtractor.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLTextExtractor.java index 003fe353f1..ada32a1cc0 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLTextExtractor.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLTextExtractor.java @@ -15,13 +15,15 @@ limitations under the License. ==================================================================== */ -package org.apache.poi; +package org.apache.poi.ooxml.extractor; import java.io.IOException; -import org.apache.poi.POIXMLProperties.CoreProperties; -import org.apache.poi.POIXMLProperties.CustomProperties; -import org.apache.poi.POIXMLProperties.ExtendedProperties; +import org.apache.poi.extractor.POITextExtractor; +import org.apache.poi.ooxml.POIXMLDocument; +import org.apache.poi.ooxml.POIXMLProperties.CoreProperties; +import org.apache.poi.ooxml.POIXMLProperties.CustomProperties; +import org.apache.poi.ooxml.POIXMLProperties.ExtendedProperties; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.util.ZipSecureFile; diff --git a/src/ooxml/java/org/apache/poi/util/DocumentHelper.java b/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java index 569c5ff719..d79237d8ac 100644 --- a/src/ooxml/java/org/apache/poi/util/DocumentHelper.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/DocumentHelper.java @@ -15,7 +15,7 @@ limitations under the License. ==================================================================== */ -package org.apache.poi.util; +package org.apache.poi.ooxml.util; import java.io.IOException; import java.io.InputStream; @@ -27,6 +27,8 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.events.Namespace; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.ErrorHandler; diff --git a/src/ooxml/java/org/apache/poi/util/IdentifierManager.java b/src/ooxml/java/org/apache/poi/ooxml/util/IdentifierManager.java index a863dabe60..f367473cc3 100644 --- a/src/ooxml/java/org/apache/poi/util/IdentifierManager.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/IdentifierManager.java @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ -package org.apache.poi.util; +package org.apache.poi.ooxml.util; import java.util.LinkedList; import java.util.ListIterator; diff --git a/src/ooxml/java/org/apache/poi/util/PackageHelper.java b/src/ooxml/java/org/apache/poi/ooxml/util/PackageHelper.java index e950323116..1385848428 100644 --- a/src/ooxml/java/org/apache/poi/util/PackageHelper.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/PackageHelper.java @@ -15,13 +15,14 @@ limitations under the License. ==================================================================== */ -package org.apache.poi.util; +package org.apache.poi.ooxml.util; import org.apache.poi.openxml4j.opc.*; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.POIXMLException; +import org.apache.poi.ooxml.POIXMLException; +import org.apache.poi.util.IOUtils; import java.io.*; import java.net.URI; diff --git a/src/ooxml/java/org/apache/poi/util/SAXHelper.java b/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java index b5968d9ff9..630e5540ab 100644 --- a/src/ooxml/java/org/apache/poi/util/SAXHelper.java +++ b/src/ooxml/java/org/apache/poi/ooxml/util/SAXHelper.java @@ -15,7 +15,7 @@ limitations under the License. ==================================================================== */ -package org.apache.poi.util; +package org.apache.poi.ooxml.util; import java.io.IOException; import java.io.StringReader; @@ -26,6 +26,8 @@ import javax.xml.XMLConstants; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; diff --git a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedData.java b/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedData.java deleted file mode 100644 index 0e598b3175..0000000000 --- a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedData.java +++ /dev/null @@ -1,104 +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.ss.extractor; - -import org.apache.poi.ss.usermodel.Shape; - -/** - * A collection of embedded object informations and content - */ -public class EmbeddedData { - private String filename; - private byte[] embeddedData; - private Shape shape; - private String contentType = "binary/octet-stream"; - - public EmbeddedData(String filename, byte[] embeddedData, String contentType) { - setFilename(filename); - setEmbeddedData(embeddedData); - setContentType(contentType); - } - - /** - * @return the filename - */ - public String getFilename() { - return filename; - } - - /** - * Sets the filename - * - * @param filename the filename - */ - public void setFilename(String filename) { - if (filename == null) { - this.filename = "unknown.bin"; - } else { - this.filename = filename.replaceAll("[^/\\\\]*[/\\\\]", "").trim(); - } - } - - /** - * @return the embedded object byte array - */ - public byte[] getEmbeddedData() { - return embeddedData; - } - - /** - * Sets the embedded object as byte array - * - * @param embeddedData the embedded object byte array - */ - public void setEmbeddedData(byte[] embeddedData) { - this.embeddedData = (embeddedData == null) ? null : embeddedData.clone(); - } - - /** - * @return the shape which links to the embedded object - */ - public Shape getShape() { - return shape; - } - - /** - * Sets the shape which links to the embedded object - * - * @param shape the shape - */ - public void setShape(Shape shape) { - this.shape = shape; - } - - /** - * @return the content-/mime-type of the embedded object, the default (if unknown) is {@code binary/octet-stream} - */ - public String getContentType() { - return contentType; - } - - /** - * Sets the content-/mime-type - * - * @param contentType the content-type - */ - public void setContentType(String contentType) { - this.contentType = contentType; - } -}
\ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java b/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java deleted file mode 100644 index 8ea6df28a6..0000000000 --- a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java +++ /dev/null @@ -1,410 +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.ss.extractor; - -import static org.apache.poi.util.StringUtil.endsWithIgnoreCase; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -import org.apache.poi.hpsf.ClassID; -import org.apache.poi.hpsf.ClassIDPredefined; -import org.apache.poi.poifs.filesystem.DirectoryNode; -import org.apache.poi.poifs.filesystem.DocumentInputStream; -import org.apache.poi.poifs.filesystem.Entry; -import org.apache.poi.poifs.filesystem.Ole10Native; -import org.apache.poi.poifs.filesystem.Ole10NativeException; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.apache.poi.ss.usermodel.Drawing; -import org.apache.poi.ss.usermodel.ObjectData; -import org.apache.poi.ss.usermodel.Picture; -import org.apache.poi.ss.usermodel.PictureData; -import org.apache.poi.ss.usermodel.Shape; -import org.apache.poi.ss.usermodel.ShapeContainer; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.util.Beta; -import org.apache.poi.util.IOUtils; -import org.apache.poi.util.LocaleUtil; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; -import org.apache.poi.xssf.usermodel.XSSFObjectData; - -/** - * This extractor class tries to identify various embedded documents within Excel files - * and provide them via a common interface, i.e. the EmbeddedData instances - */ -@Beta -public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> { - private static final POILogger LOG = POILogFactory.getLogger(EmbeddedExtractor.class); - //arbitrarily selected; may need to increase - private static final int MAX_RECORD_LENGTH = 1_000_000; - - // contentType - private static final String CONTENT_TYPE_BYTES = "binary/octet-stream"; - private static final String CONTENT_TYPE_PDF = "application/pdf"; - private static final String CONTENT_TYPE_DOC = "application/msword"; - private static final String CONTENT_TYPE_XLS = "application/vnd.ms-excel"; - - /** - * @return the list of known extractors, if you provide custom extractors, override this method - */ - @Override - public Iterator<EmbeddedExtractor> iterator() { - EmbeddedExtractor[] ee = { - new Ole10Extractor(), new PdfExtractor(), new BiffExtractor(), new OOXMLExtractor(), new FsExtractor() - }; - return Arrays.asList(ee).iterator(); - } - - public EmbeddedData extractOne(DirectoryNode src) throws IOException { - for (EmbeddedExtractor ee : this) { - if (ee.canExtract(src)) { - return ee.extract(src); - } - } - return null; - } - - public EmbeddedData extractOne(Picture src) throws IOException { - for (EmbeddedExtractor ee : this) { - if (ee.canExtract(src)) { - return ee.extract(src); - } - } - return null; - } - - public List<EmbeddedData> extractAll(Sheet sheet) throws IOException { - Drawing<?> patriarch = sheet.getDrawingPatriarch(); - if (null == patriarch){ - return Collections.emptyList(); - } - List<EmbeddedData> embeddings = new ArrayList<>(); - extractAll(patriarch, embeddings); - return embeddings; - } - - protected void extractAll(ShapeContainer<?> parent, List<EmbeddedData> embeddings) throws IOException { - for (Shape shape : parent) { - EmbeddedData data = null; - if (shape instanceof ObjectData) { - ObjectData od = (ObjectData)shape; - try { - if (od.hasDirectoryEntry()) { - data = extractOne((DirectoryNode)od.getDirectory()); - } else { - String contentType = CONTENT_TYPE_BYTES; - if (od instanceof XSSFObjectData) { - contentType = ((XSSFObjectData)od).getObjectPart().getContentType(); - } - data = new EmbeddedData(od.getFileName(), od.getObjectData(), contentType); - } - } catch (Exception e) { - LOG.log(POILogger.WARN, "Entry not found / readable - ignoring OLE embedding", e); - } - } else if (shape instanceof Picture) { - data = extractOne((Picture)shape); - } else if (shape instanceof ShapeContainer) { - extractAll((ShapeContainer<?>)shape, embeddings); - } - - if (data == null) { - continue; - } - - data.setShape(shape); - String filename = data.getFilename(); - String extension = (filename == null || filename.lastIndexOf('.') == -1) ? ".bin" : filename.substring(filename.lastIndexOf('.')); - - // try to find an alternative name - if (filename == null || filename.isEmpty() || filename.startsWith("MBD") || filename.startsWith("Root Entry")) { - filename = shape.getShapeName(); - if (filename != null) { - filename += extension; - } - } - // default to dummy name - if (filename == null || filename.isEmpty()) { - filename = "picture_" + embeddings.size() + extension; - } - filename = filename.trim(); - data.setFilename(filename); - - embeddings.add(data); - } - } - - - public boolean canExtract(DirectoryNode source) { - return false; - } - - public boolean canExtract(Picture source) { - return false; - } - - protected EmbeddedData extract(DirectoryNode dn) throws IOException { - assert(canExtract(dn)); - ByteArrayOutputStream bos = new ByteArrayOutputStream(20000); - try (POIFSFileSystem dest = new POIFSFileSystem()) { - copyNodes(dn, dest.getRoot()); - // start with a reasonable big size - dest.writeFilesystem(bos); - } - - return new EmbeddedData(dn.getName(), bos.toByteArray(), CONTENT_TYPE_BYTES); - } - - protected EmbeddedData extract(Picture source) throws IOException { - return null; - } - - public static class Ole10Extractor extends EmbeddedExtractor { - @Override - public boolean canExtract(DirectoryNode dn) { - ClassID clsId = dn.getStorageClsid(); - return ClassIDPredefined.lookup(clsId) == ClassIDPredefined.OLE_V1_PACKAGE; - } - - @Override - public EmbeddedData extract(DirectoryNode dn) throws IOException { - try { - // TODO: inspect the CompObj record for more details, i.e. the content type - Ole10Native ole10 = Ole10Native.createFromEmbeddedOleObject(dn); - return new EmbeddedData(ole10.getFileName(), ole10.getDataBuffer(), CONTENT_TYPE_BYTES); - } catch (Ole10NativeException e) { - throw new IOException(e); - } - } - } - - static class PdfExtractor extends EmbeddedExtractor { - static ClassID PdfClassID = new ClassID("{B801CA65-A1FC-11D0-85AD-444553540000}"); - @Override - public boolean canExtract(DirectoryNode dn) { - ClassID clsId = dn.getStorageClsid(); - return (PdfClassID.equals(clsId) || dn.hasEntry("CONTENTS")); - } - - @Override - public EmbeddedData extract(DirectoryNode dn) throws IOException { - try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); - InputStream is = dn.createDocumentInputStream("CONTENTS")) { - IOUtils.copy(is, bos); - return new EmbeddedData(dn.getName() + ".pdf", bos.toByteArray(), CONTENT_TYPE_PDF); - } - } - - @Override - public boolean canExtract(Picture source) { - PictureData pd = source.getPictureData(); - return (pd != null && pd.getPictureType() == Workbook.PICTURE_TYPE_EMF); - } - - /** - * Mac Office encodes embedded objects inside the picture, e.g. PDF is part of an EMF. - * If an embedded stream is inside an EMF picture, this method extracts the payload. - * - * @return the embedded data in an EMF picture or null if none is found - */ - @Override - protected EmbeddedData extract(Picture source) throws IOException { - // check for emf+ embedded pdf (poor mans style :( ) - // Mac Excel 2011 embeds pdf files with this method. - PictureData pd = source.getPictureData(); - if (pd == null || pd.getPictureType() != Workbook.PICTURE_TYPE_EMF) { - return null; - } - - // TODO: investigate if this is just an EMF-hack or if other formats are also embedded in EMF - byte pictureBytes[] = pd.getData(); - int idxStart = indexOf(pictureBytes, 0, "%PDF-".getBytes(LocaleUtil.CHARSET_1252)); - if (idxStart == -1) { - return null; - } - - int idxEnd = indexOf(pictureBytes, idxStart, "%%EOF".getBytes(LocaleUtil.CHARSET_1252)); - if (idxEnd == -1) { - return null; - } - - int pictureBytesLen = idxEnd-idxStart+6; - byte[] pdfBytes = IOUtils.safelyAllocate(pictureBytesLen, MAX_RECORD_LENGTH); - System.arraycopy(pictureBytes, idxStart, pdfBytes, 0, pictureBytesLen); - String filename = source.getShapeName().trim(); - if (!endsWithIgnoreCase(filename, ".pdf")) { - filename += ".pdf"; - } - return new EmbeddedData(filename, pdfBytes, CONTENT_TYPE_PDF); - } - - - } - - static class OOXMLExtractor extends EmbeddedExtractor { - @Override - public boolean canExtract(DirectoryNode dn) { - return dn.hasEntry("package"); - } - - @Override - public EmbeddedData extract(DirectoryNode dn) throws IOException { - - ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid()); - - String contentType = null; - String ext = null; - - if (clsId != null) { - contentType = clsId.getContentType(); - ext = clsId.getFileExtension(); - } - - if (contentType == null || ext == null) { - contentType = "application/zip"; - ext = ".zip"; - } - - DocumentInputStream dis = dn.createDocumentInputStream("package"); - byte data[] = IOUtils.toByteArray(dis); - dis.close(); - - return new EmbeddedData(dn.getName()+ext, data, contentType); - } - } - - static class BiffExtractor extends EmbeddedExtractor { - @Override - public boolean canExtract(DirectoryNode dn) { - return canExtractExcel(dn) || canExtractWord(dn); - } - - protected boolean canExtractExcel(DirectoryNode dn) { - ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid()); - return (ClassIDPredefined.EXCEL_V7 == clsId - || ClassIDPredefined.EXCEL_V8 == clsId - || dn.hasEntry("Workbook") /*...*/); - } - - protected boolean canExtractWord(DirectoryNode dn) { - ClassIDPredefined clsId = ClassIDPredefined.lookup(dn.getStorageClsid()); - return (ClassIDPredefined.WORD_V7 == clsId - || ClassIDPredefined.WORD_V8 == clsId - || dn.hasEntry("WordDocument")); - } - - @Override - public EmbeddedData extract(DirectoryNode dn) throws IOException { - EmbeddedData ed = super.extract(dn); - if (canExtractExcel(dn)) { - ed.setFilename(dn.getName() + ".xls"); - ed.setContentType(CONTENT_TYPE_XLS); - } else if (canExtractWord(dn)) { - ed.setFilename(dn.getName() + ".doc"); - ed.setContentType(CONTENT_TYPE_DOC); - } - - return ed; - } - } - - static class FsExtractor extends EmbeddedExtractor { - @Override - public boolean canExtract(DirectoryNode dn) { - return true; - } - @Override - public EmbeddedData extract(DirectoryNode dn) throws IOException { - EmbeddedData ed = super.extract(dn); - ed.setFilename(dn.getName() + ".ole"); - // TODO: read the content type from CombObj stream - return ed; - } - } - - protected static void copyNodes(DirectoryNode src, DirectoryNode dest) throws IOException { - for (Entry e : src) { - if (e instanceof DirectoryNode) { - DirectoryNode srcDir = (DirectoryNode)e; - DirectoryNode destDir = (DirectoryNode)dest.createDirectory(srcDir.getName()); - destDir.setStorageClsid(srcDir.getStorageClsid()); - copyNodes(srcDir, destDir); - } else { - try (InputStream is = src.createDocumentInputStream(e)) { - dest.createDocument(e.getName(), is); - } - } - } - } - - - - /** - * Knuth-Morris-Pratt Algorithm for Pattern Matching - * Finds the first occurrence of the pattern in the text. - */ - private static int indexOf(byte[] data, int offset, byte[] pattern) { - int[] failure = computeFailure(pattern); - - int j = 0; - if (data.length == 0) { - return -1; - } - - for (int i = offset; i < data.length; i++) { - while (j > 0 && pattern[j] != data[i]) { - j = failure[j - 1]; - } - if (pattern[j] == data[i]) { j++; } - if (j == pattern.length) { - return i - pattern.length + 1; - } - } - return -1; - } - - /** - * Computes the failure function using a boot-strapping process, - * where the pattern is matched against itself. - */ - private static int[] computeFailure(byte[] pattern) { - int[] failure = new int[pattern.length]; - - int j = 0; - for (int i = 1; i < pattern.length; i++) { - while (j > 0 && pattern[j] != pattern[i]) { - j = failure[j - 1]; - } - if (pattern[j] == pattern[i]) { - j++; - } - failure[i] = j; - } - - return failure; - } - - -} diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java deleted file mode 100644 index 1a4c2cb1d5..0000000000 --- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ /dev/null @@ -1,275 +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.ss.usermodel; - -import java.io.BufferedInputStream; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; - -import org.apache.poi.EmptyFileException; -import org.apache.poi.EncryptedDocumentException; -import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.openxml4j.opc.PackageAccess; -import org.apache.poi.poifs.crypt.Decryptor; -import org.apache.poi.poifs.filesystem.DirectoryNode; -import org.apache.poi.poifs.filesystem.DocumentFactoryHelper; -import org.apache.poi.poifs.filesystem.FileMagic; -import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; -import org.apache.poi.poifs.filesystem.OfficeXmlFileException; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.apache.poi.util.IOUtils; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; - -/** - * Factory for creating the appropriate kind of Workbook - * (be it {@link HSSFWorkbook} or {@link XSSFWorkbook}), - * by auto-detecting from the supplied input. - */ -public class WorkbookFactory { - /** - * Creates a HSSFWorkbook from the given POIFSFileSystem - * <p>Note that in order to properly release resources the - * Workbook should be closed after use. - */ - public static Workbook create(POIFSFileSystem fs) throws IOException { - return new HSSFWorkbook(fs); - } - - /** - * Creates a HSSFWorkbook from the given NPOIFSFileSystem - * <p>Note that in order to properly release resources the - * Workbook should be closed after use. - */ - public static Workbook create(NPOIFSFileSystem fs) throws IOException { - try { - return create(fs, null); - } catch (InvalidFormatException e) { - // Special case of OOXML-in-POIFS which is broken - throw new IOException(e); - } - } - - /** - * Creates a Workbook from the given NPOIFSFileSystem, which may - * be password protected - * - * @param fs The {@link NPOIFSFileSystem} to read the document from - * @param password The password that should be used or null if no password is necessary. - * - * @return The created Workbook - * - * @throws IOException if an error occurs while reading the data - * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook} - */ - private static Workbook create(final NPOIFSFileSystem fs, String password) throws IOException, InvalidFormatException { - DirectoryNode root = fs.getRoot(); - - // Encrypted OOXML files go inside OLE2 containers, is this one? - if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { - InputStream stream = DocumentFactoryHelper.getDecryptedStream(fs, password); - - OPCPackage pkg = OPCPackage.open(stream); - return create(pkg); - } - - // If we get here, it isn't an encrypted XLSX file - // So, treat it as a regular HSSF XLS one - boolean passwordSet = false; - if (password != null) { - Biff8EncryptionKey.setCurrentUserPassword(password); - passwordSet = true; - } - try { - return new HSSFWorkbook(root, true); - } finally { - if (passwordSet) { - Biff8EncryptionKey.setCurrentUserPassword(null); - } - } - } - - /** - * Creates a XSSFWorkbook from the given OOXML Package - * - * <p>Note that in order to properly release resources the - * Workbook should be closed after use.</p> - * - * @param pkg The {@link OPCPackage} opened for reading data. - * - * @return The created Workbook - * - * @throws IOException if an error occurs while reading the data - */ - public static Workbook create(OPCPackage pkg) throws IOException { - return new XSSFWorkbook(pkg); - } - - /** - * Creates the appropriate HSSFWorkbook / XSSFWorkbook from - * the given InputStream. - * - * <p>Your input stream MUST either support mark/reset, or - * be wrapped as a {@link BufferedInputStream}! Note that - * using an {@link InputStream} has a higher memory footprint - * than using a {@link File}.</p> - * - * <p>Note that in order to properly release resources the - * Workbook should be closed after use. Note also that loading - * from an InputStream requires more memory than loading - * from a File, so prefer {@link #create(File)} where possible. - * - * @param inp The {@link InputStream} to read data from. - * - * @return The created Workbook - * - * @throws IOException if an error occurs while reading the data - * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook} - * @throws EncryptedDocumentException If the workbook given is password protected - */ - public static Workbook create(InputStream inp) throws IOException, InvalidFormatException, EncryptedDocumentException { - return create(inp, null); - } - - /** - * Creates the appropriate HSSFWorkbook / XSSFWorkbook from - * the given InputStream, which may be password protected.<p> - * - * Note that using an {@link InputStream} has a higher memory footprint - * than using a {@link File}.<p> - * - * Note that in order to properly release resources the - * Workbook should be closed after use. Note also that loading - * from an InputStream requires more memory than loading - * from a File, so prefer {@link #create(File)} where possible. - * - * @param inp The {@link InputStream} to read data from. - * @param password The password that should be used or null if no password is necessary. - * - * @return The created Workbook - * - * @throws IOException if an error occurs while reading the data - * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook} - * @throws EncryptedDocumentException If the wrong password is given for a protected file - * @throws EmptyFileException If an empty stream is given - */ - public static Workbook create(InputStream inp, String password) throws IOException, InvalidFormatException, EncryptedDocumentException { - InputStream is = FileMagic.prepareToCheckMagic(inp); - - FileMagic fm = FileMagic.valueOf(is); - - switch (fm) { - case OLE2: - NPOIFSFileSystem fs = new NPOIFSFileSystem(is); - return create(fs, password); - case OOXML: - return new XSSFWorkbook(OPCPackage.open(is)); - default: - throw new InvalidFormatException("Your InputStream was neither an OLE2 stream, nor an OOXML stream"); - } - } - - /** - * Creates the appropriate HSSFWorkbook / XSSFWorkbook from - * the given File, which must exist and be readable. - * <p>Note that in order to properly release resources the - * Workbook should be closed after use. - * - * @param file The file to read data from. - * - * @return The created Workbook - * - * @throws IOException if an error occurs while reading the data - * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook} - * @throws EncryptedDocumentException If the workbook given is password protected - */ - public static Workbook create(File file) throws IOException, InvalidFormatException, EncryptedDocumentException { - return create(file, null); - } - - /** - * Creates the appropriate HSSFWorkbook / XSSFWorkbook from - * the given File, which must exist and be readable, and - * may be password protected - * <p>Note that in order to properly release resources the - * Workbook should be closed after use. - * - * @param file The file to read data from. - * @param password The password that should be used or null if no password is necessary. - * - * @return The created Workbook - * - * @throws IOException if an error occurs while reading the data - * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook} - * @throws EncryptedDocumentException If the wrong password is given for a protected file - * @throws EmptyFileException If an empty stream is given - */ - public static Workbook create(File file, String password) throws IOException, InvalidFormatException, EncryptedDocumentException { - return create(file, password, false); - } - - /** - * Creates the appropriate HSSFWorkbook / XSSFWorkbook from - * the given File, which must exist and be readable, and - * may be password protected - * <p>Note that in order to properly release resources the - * Workbook should be closed after use. - * - * @param file The file to read data from. - * @param password The password that should be used or null if no password is necessary. - * @param readOnly If the Workbook should be opened in read-only mode to avoid writing back - * changes when the document is closed. - * - * @return The created Workbook - * - * @throws IOException if an error occurs while reading the data - * @throws InvalidFormatException if the contents of the file cannot be parsed into a {@link Workbook} - * @throws EncryptedDocumentException If the wrong password is given for a protected file - * @throws EmptyFileException If an empty stream is given - */ - public static Workbook create(File file, String password, boolean readOnly) throws IOException, InvalidFormatException, EncryptedDocumentException { - if (! file.exists()) { - throw new FileNotFoundException(file.toString()); - } - - try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly)) { - return create(fs, password); - } catch(OfficeXmlFileException e) { - // opening as .xls failed => try opening as .xlsx - OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE); // NOSONAR - try { - return new XSSFWorkbook(pkg); - } catch (Exception ioe) { - // ensure that file handles are closed - use revert() to not re-write the file - pkg.revert(); - // do not pkg.close(); - - if (ioe instanceof IOException) { - throw (IOException)ioe; - } else if (ioe instanceof RuntimeException) { - throw (RuntimeException)ioe; - } else { - throw new IOException(ioe); - } - } - } - } -} diff --git a/src/ooxml/java/org/apache/poi/util/OOXMLLite.java b/src/ooxml/java/org/apache/poi/util/OOXMLLite.java deleted file mode 100644 index 06c57c464b..0000000000 --- a/src/ooxml/java/org/apache/poi/util/OOXMLLite.java +++ /dev/null @@ -1,337 +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.util; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.net.URL; -import java.security.AccessController; -import java.security.CodeSource; -import java.security.PrivilegedAction; -import java.security.ProtectionDomain; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.Vector; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.regex.Pattern; - -import junit.framework.TestCase; - -import org.junit.Test; -import org.junit.internal.TextListener; -import org.junit.runner.Description; -import org.junit.runner.JUnitCore; -import org.junit.runner.Result; - -/** - * Build a 'lite' version of the ooxml-schemas.jar - * - * @author Yegor Kozlov - */ -public final class OOXMLLite { - private static final Pattern SCHEMA_PATTERN = Pattern.compile("schemaorg_apache_xmlbeans/(system|element)/.*\\.xsb"); - - /** - * Destination directory to copy filtered classes - */ - private File _destDest; - - /** - * Directory with the compiled ooxml tests - */ - private File _testDir; - - /** - * Reference to the ooxml-schemas.jar - */ - private File _ooxmlJar; - - - OOXMLLite(String dest, String test, String ooxmlJar) { - _destDest = new File(dest); - _testDir = new File(test); - _ooxmlJar = new File(ooxmlJar); - } - - public static void main(String[] args) throws IOException { - System.out.println("Free memory (bytes): " + - Runtime.getRuntime().freeMemory()); - long maxMemory = Runtime.getRuntime().maxMemory(); - System.out.println("Maximum memory (bytes): " + - (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory)); - System.out.println("Total memory (bytes): " + - Runtime.getRuntime().totalMemory()); - - String dest = null, test = null, ooxml = null; - - for (int i = 0; i < args.length; i++) { - switch (args[i]) { - case "-dest": - dest = args[++i]; - break; - case "-test": - test = args[++i]; - break; - case "-ooxml": - ooxml = args[++i]; - break; - } - } - OOXMLLite builder = new OOXMLLite(dest, test, ooxml); - builder.build(); - } - - void build() throws IOException { - List<Class<?>> lst = new ArrayList<>(); - //collect unit tests - String exclude = StringUtil.join("|", - "BaseTestXWorkbook", - "BaseTestXSheet", - "BaseTestXRow", - "BaseTestXCell", - "BaseTestXSSFPivotTable", - "TestSXSSFWorkbook\\$\\d", - "TestUnfixedBugs", - "MemoryUsage", - "TestDataProvider", - "TestDataSamples", - "All.+Tests", - "ZipFileAssert", - "AesZipFileZipEntrySource", - "TempFileRecordingSXSSFWorkbookWithCustomZipEntrySource", - "PkiTestUtils", - "TestCellFormatPart\\$\\d", - "TestSignatureInfo\\$\\d", - "TestCertificateEncryption\\$CertData", - "TestPOIXMLDocument\\$OPCParser", - "TestPOIXMLDocument\\$TestFactory", - "TestXSLFTextParagraph\\$DrawTextParagraphProxy", - "TestXSSFExportToXML\\$\\d", - "TestXSSFExportToXML\\$DummyEntityResolver", - "TestFormulaEvaluatorOnXSSF\\$Result", - "TestFormulaEvaluatorOnXSSF\\$SS", - "TestMultiSheetFormulaEvaluatorOnXSSF\\$Result", - "TestMultiSheetFormulaEvaluatorOnXSSF\\$SS", - "TestXSSFBugs\\$\\d", - "AddImageBench", - "AddImageBench_jmhType_B\\d", - "AddImageBench_benchCreatePicture_jmhTest", - "TestEvilUnclosedBRFixingInputStream\\$EvilUnclosedBRFixingInputStream", - "TempFileRecordingSXSSFWorkbookWithCustomZipEntrySource\\$TempFileRecordingSheetDataWriterWithDecorator", - "TestXSSFBReader\\$1", - "TestXSSFBReader\\$TestSheetHandler", - "TestFormulaEvaluatorOnXSSF\\$1", - "TestMultiSheetFormulaEvaluatorOnXSSF\\$1", - "TestZipPackagePropertiesMarshaller\\$1", - "SLCommonUtils", - "TestPPTX2PNG\\$1", - "TestMatrixFormulasFromXMLSpreadsheet\\$1", - "TestMatrixFormulasFromXMLSpreadsheet\\$Navigator", - "TestPOIXMLDocument\\$UncaughtHandler", - "TestOleShape\\$Api", - "TestOleShape\\$1", - "TestPOIXMLDocument\\$1", - "TestXMLSlideShow\\$1", - "TestXMLSlideShow\\$BufAccessBAOS", - "TestXDDFChart\\$1", - "TestOOXMLLister\\$1", - "TestOOXMLPrettyPrint\\$1" - ); - System.out.println("Collecting unit tests from " + _testDir); - collectTests(_testDir, _testDir, lst, ".+.class$", ".+(" + exclude + ").class"); - System.out.println("Found " + lst.size() + " classes"); - - //run tests - JUnitCore jUnitCore = new JUnitCore(); - jUnitCore.addListener(new TextListener(System.out) { - private final Set<String> classes = new HashSet<>(); - private int count; - - @Override - public void testStarted(Description description) { - // count how many test-classes we already saw - classes.add(description.getClassName()); - count++; - if(count % 100 == 0) { - System.out.println(); - System.out.println(classes.size() + "/" + lst.size() + ": " + description.getDisplayName()); - } - - super.testStarted(description); - } - }); - Result result = jUnitCore.run(lst.toArray(new Class<?>[0])); - if (!result.wasSuccessful()) { - throw new RuntimeException("Tests did not succeed, cannot build ooxml-lite jar"); - } - - //see what classes from the ooxml-schemas.jar are loaded - System.out.println("Copying classes to " + _destDest); - Map<String, Class<?>> classes = getLoadedClasses(_ooxmlJar.getName()); - for (Class<?> cls : classes.values()) { - String className = cls.getName(); - String classRef = className.replace('.', '/') + ".class"; - File destFile = new File(_destDest, classRef); - IOUtils.copy(cls.getResourceAsStream('/' + classRef), destFile); - - if(cls.isInterface()){ - /// Copy classes and interfaces declared as members of this class - for(Class<?> fc : cls.getDeclaredClasses()){ - className = fc.getName(); - classRef = className.replace('.', '/') + ".class"; - destFile = new File(_destDest, classRef); - IOUtils.copy(fc.getResourceAsStream('/' + classRef), destFile); - } - } - } - - //finally copy the compiled .xsb files - System.out.println("Copying .xsb resources"); - try (JarFile jar = new JarFile(_ooxmlJar)) { - for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) { - JarEntry je = e.nextElement(); - if (SCHEMA_PATTERN.matcher(je.getName()).matches()) { - File destFile = new File(_destDest, je.getName()); - IOUtils.copy(jar.getInputStream(je), destFile); - } - } - } - } - - private static boolean checkForTestAnnotation(Class<?> testclass) { - for (Method m : testclass.getDeclaredMethods()) { - if(m.isAnnotationPresent(Test.class)) { - return true; - } - } - - // also check super classes - if(testclass.getSuperclass() != null) { - for (Method m : testclass.getSuperclass().getDeclaredMethods()) { - if(m.isAnnotationPresent(Test.class)) { - return true; - } - } - } - - System.out.println("Class " + testclass.getName() + " does not derive from TestCase and does not have a @Test annotation"); - - // Should we also look at superclasses to find cases - // where we have abstract base classes with derived tests? - // if(checkForTestAnnotation(testclass.getSuperclass())) return true; - - return false; - } - - /** - * Recursively collect classes from the supplied directory - * - * @param arg the directory to search in - * @param out output - * @param ptrn the pattern (regexp) to filter found files - */ - private static void collectTests(File root, File arg, List<Class<?>> out, String ptrn, String exclude) { - if (arg.isDirectory()) { - File files[] = arg.listFiles(); - if (files != null) { - for (File f : files) { - collectTests(root, f, out, ptrn, exclude); - } - } - } else { - String path = arg.getAbsolutePath(); - String prefix = root.getAbsolutePath(); - String cls = path.substring(prefix.length() + 1).replace(File.separator, "."); - if(!cls.matches(ptrn)) return; - if (cls.matches(exclude)) return; - //ignore inner classes defined in tests - if (cls.indexOf('$') != -1) { - System.out.println("Inner class " + cls + " not included"); - return; - } - - cls = cls.replace(".class", ""); - - try { - Class<?> testclass = Class.forName(cls); - if (TestCase.class.isAssignableFrom(testclass) - || checkForTestAnnotation(testclass)) { - out.add(testclass); - } - } catch (Throwable e) { // NOSONAR - System.out.println("Class " + cls + " is not in classpath"); - } - } - } - - /** - * - * @param ptrn the pattern to filter output - * @return the classes loaded by the system class loader keyed by class name - */ - @SuppressWarnings("unchecked") - private static Map<String, Class<?>> getLoadedClasses(String ptrn) { - // make the field accessible, we defer this from static initialization to here to - // allow JDKs which do not have this field (e.g. IBM JDK) to at least load the class - // without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550 - final Field _classes = AccessController.doPrivileged(new PrivilegedAction<Field>() { - @SuppressForbidden("TODO: Reflection works until Java 8 on Oracle/Sun JDKs, but breaks afterwards (different classloader types, access checks)") - public Field run() { - try { - Field fld = ClassLoader.class.getDeclaredField("classes"); - fld.setAccessible(true); - return fld; - } catch (Exception e) { - throw new RuntimeException(e); - } - - } - }); - - ClassLoader appLoader = ClassLoader.getSystemClassLoader(); - try { - Vector<Class<?>> classes = (Vector<Class<?>>) _classes.get(appLoader); - Map<String, Class<?>> map = new HashMap<>(); - for (Class<?> cls : classes) { - // e.g. proxy-classes, ... - ProtectionDomain pd = cls.getProtectionDomain(); - if (pd == null) continue; - CodeSource cs = pd.getCodeSource(); - if (cs == null) continue; - URL loc = cs.getLocation(); - if (loc == null) continue; - - String jar = loc.toString(); - if (jar.contains(ptrn)) { - map.put(cls.getName(), cls); - } - } - return map; - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } -} |