*/
void removeName(String name);
- Palette getCustomPalette();
-
/**
* Adds a picture to the workbook.
*
import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IOUtils;
-import org.apache.poi.util.PackageHelper;
import org.apache.xmlbeans.XmlException;
import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.exceptions.OpenXML4JException;
* Create a new child POIXMLDocumentPart\r
*\r
* @param descriptor the part descriptor\r
- * @param cls the Class object identifying the type of instance to create\r
+ * @param factory the factory that will create an instance of the requested relation\r
* @return the created child POIXMLDocumentPart\r
*/\r
- protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls){\r
- return createRelationship(descriptor, cls, -1);\r
+ protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory){\r
+ return createRelationship(descriptor, factory, -1, false);\r
}\r
\r
- /**\r
- * Create a new child POIXMLDocumentPart\r
- *\r
- * @param descriptor the part descriptor\r
- * @param cls the Class object identifying the type of instance to create\r
- * @param idx part number\r
- * @return the created child POIXMLDocumentPart\r
- */\r
- protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls, int idx){\r
- return createRelationship(descriptor, cls, idx, false);\r
+ protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx){\r
+ return createRelationship(descriptor, factory, idx, false);\r
}\r
\r
/**\r
* Create a new child POIXMLDocumentPart\r
*\r
* @param descriptor the part descriptor\r
- * @param cls the Class object identifying the type of instance to create\r
+ * @param factory the factory that will create an instance of the requested relation\r
* @param idx part number\r
- * @param norel if true, then no relationship is added. \r
+ * @param noRelation if true, then no relationship is added.\r
* @return the created child POIXMLDocumentPart\r
*/\r
- protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls, int idx, boolean norel){\r
+ protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx, boolean noRelation){\r
try {\r
\r
PackagePartName ppName = PackagingURIHelper.createPartName(descriptor.getFileName(idx));\r
PackageRelationship rel = null;\r
- if(!norel) rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation());\r
+ if(!noRelation) rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation());\r
\r
PackagePart part = packagePart.getPackage().createPart(ppName, descriptor.getContentType());\r
- POIXMLDocumentPart doc = cls.newInstance();\r
+ POIXMLDocumentPart doc = factory.newDocumentPart(descriptor);\r
doc.packageRel = rel;\r
doc.packagePart = part;\r
doc.parent = this;\r
logger.log(POILogger.ERROR, "Skipped invalid entry " + rel.getTargetURI());\r
continue;\r
}\r
- POIXMLDocumentPart childPart = factory.create(rel, p);\r
+ POIXMLDocumentPart childPart = factory.createDocumentPart(rel, p);\r
childPart.parent = this;\r
addRelation(childPart);\r
\r
*\r
* @author Yegor Kozlov\r
*/\r
-public class POIXMLFactory {\r
+public abstract class POIXMLFactory {\r
\r
/**\r
- * Creates a new instance of a {@link POIXMLDocumentPart}\r
+ * Create a POIXMLDocumentPart from existing package part and relation. This method is called\r
+ * from {@link POIXMLDocumentPart#read(POIXMLFactory)} when parsing a document\r
*\r
* @param rel the package part relationship\r
* @param part the PackagePart representing the created instance\r
* @return A new instance of a POIXMLDocumentPart.\r
*/\r
- public POIXMLDocumentPart create(PackageRelationship rel, PackagePart part){\r
- return new POIXMLDocumentPart(part, rel);\r
- }\r
+ public abstract POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part);\r
+\r
+ /**\r
+ * Create a new POIXMLDocumentPart using the supplied descriptor. This method is used when adding new parts\r
+ * to a document, for example, when adding a sheet to a workbook, slide to a presentation, etc.\r
+ *\r
+ * @param descriptor described the object to create\r
+ * @return A new instance of a POIXMLDocumentPart.\r
+ */\r
+ public abstract POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor);\r
}\r
*\r
* @author Yegor Kozlov\r
*/\r
-public class POIXMLRelation {\r
+public abstract class POIXMLRelation {\r
\r
/**\r
* Describes the content stored in a part.\r
*/\r
protected String _defaultName;\r
\r
+ /**\r
+ * Defines what object is used to construct instances of this relationship\r
+ */\r
+ private Class<? extends POIXMLDocumentPart> _cls;\r
+\r
/**\r
* Instantiates a POIXMLRelation.\r
*\r
* @param type content type\r
* @param rel relationship\r
* @param defaultName default item name\r
+ * @param cls defines what object is used to construct instances of this relationship\r
*/\r
- public POIXMLRelation(String type, String rel, String defaultName) {\r
+ public POIXMLRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {\r
_type = type;\r
_relation = rel;\r
_defaultName = defaultName;\r
+ _cls = cls;\r
}\r
\r
+ /**\r
+ * Instantiates a POIXMLRelation.\r
+ *\r
+ * @param type content type\r
+ * @param rel relationship\r
+ * @param defaultName default item name\r
+ */\r
+ public POIXMLRelation(String type, String rel, String defaultName) {\r
+ this(type, rel, defaultName, null);\r
+ }\r
/**\r
* Return the content type. Content types define a media type, a subtype, and an\r
* optional set of parameters, as defined in RFC 2616.\r
}\r
return _defaultName.replace("#", Integer.toString(index));\r
}\r
+\r
+ /**\r
+ * Return type of the obejct used to construct instances of this relationship\r
+ *\r
+ * @return the class of the object used to construct instances of this relation\r
+ */\r
+ public Class<? extends POIXMLDocumentPart> getRelationClass(){\r
+ return _cls;\r
+ }\r
}\r
import org.apache.poi.POIXMLDocumentPart;\r
import org.apache.poi.POIXMLFactory;\r
import org.apache.poi.POIXMLException;\r
-import org.apache.poi.xssf.model.SharedStringsTable;\r
-import org.apache.poi.xssf.model.StylesTable;\r
-import org.apache.poi.xssf.model.CommentsTable;\r
+import org.apache.poi.POIXMLRelation;\r
+import org.apache.poi.util.POILogger;\r
+import org.apache.poi.util.POILogFactory;\r
import org.openxml4j.opc.PackageRelationship;\r
import org.openxml4j.opc.PackagePart;\r
\r
-import java.util.Map;\r
-import java.util.HashMap;\r
import java.lang.reflect.Constructor;\r
\r
/**\r
* @author Yegor Kozlov\r
*/\r
public class XSSFFactory extends POIXMLFactory {\r
- protected static Map<String, Class> parts = new HashMap<String, Class>();\r
- static {\r
- parts.put(XSSFRelation.WORKSHEET.getRelation(), XSSFSheet.class);\r
- parts.put(XSSFRelation.SHARED_STRINGS.getRelation(), SharedStringsTable.class);\r
- parts.put(XSSFRelation.STYLES.getRelation(), StylesTable.class);\r
- parts.put(XSSFRelation.SHEET_COMMENTS.getRelation(), CommentsTable.class);\r
- parts.put(XSSFRelation.DRAWINGS.getRelation(), XSSFDrawing.class);\r
- parts.put(XSSFRelation.IMAGES.getRelation(), XSSFPictureData.class);\r
+ private static POILogger logger = POILogFactory.getLogger(XSSFFactory.class);\r
+\r
+ private XSSFFactory(){\r
+\r
+ }\r
+\r
+ public static XSSFFactory getInstance(){\r
+ return new XSSFFactory();\r
}\r
\r
- public POIXMLDocumentPart create(PackageRelationship rel, PackagePart p){\r
- Class cls = parts.get(rel.getRelationshipType());\r
- if(cls == null) return super.create(rel, p);\r
+ public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){\r
+ XSSFRelation descriptor = XSSFRelation.getInstance(rel.getRelationshipType());\r
+ if(descriptor == null || descriptor.getRelationClass() == null){\r
+ logger.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());\r
+ return new POIXMLDocumentPart(part, rel);\r
+ }\r
\r
try {\r
+ Class cls = descriptor.getRelationClass();\r
Constructor<? extends POIXMLDocumentPart> constructor = cls.getConstructor(PackagePart.class, PackageRelationship.class);\r
- return constructor.newInstance(p, rel);\r
+ return constructor.newInstance(part, rel);\r
+ } catch (Exception e){\r
+ throw new POIXMLException(e);\r
+ }\r
+ }\r
+\r
+ public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){\r
+ try {\r
+ Class cls = descriptor.getRelationClass();\r
+ Constructor<? extends POIXMLDocumentPart> constructor = cls.getConstructor();\r
+ return constructor.newInstance();\r
} catch (Exception e){\r
throw new POIXMLException(e);\r
}\r
}\r
+\r
}\r
protected static final POIXMLRelation[] RELATIONS;
static {
RELATIONS = new POIXMLRelation[8];
- RELATIONS[Workbook.PICTURE_TYPE_EMF] = new POIXMLRelation("image/x-emf", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.emf");
- RELATIONS[Workbook.PICTURE_TYPE_WMF] = new POIXMLRelation("image/x-wmf", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.wmf");
- RELATIONS[Workbook.PICTURE_TYPE_PICT] = new POIXMLRelation("image/pict", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.pict");
- RELATIONS[Workbook.PICTURE_TYPE_JPEG] = new POIXMLRelation("image/jpeg", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.jpeg");
- RELATIONS[Workbook.PICTURE_TYPE_PNG] = new POIXMLRelation("image/png", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.png");
- RELATIONS[Workbook.PICTURE_TYPE_DIB] = new POIXMLRelation("image/dib", XSSFRelation.IMAGES.getRelation(), "/xl/media/image#.dib");
+ RELATIONS[Workbook.PICTURE_TYPE_EMF] = XSSFRelation.IMAGE_EMF;
+ RELATIONS[Workbook.PICTURE_TYPE_WMF] = XSSFRelation.IMAGE_WMF;
+ RELATIONS[Workbook.PICTURE_TYPE_PICT] = XSSFRelation.IMAGE_PICT;
+ RELATIONS[Workbook.PICTURE_TYPE_JPEG] = XSSFRelation.IMAGE_JPEG;
+ RELATIONS[Workbook.PICTURE_TYPE_PNG] = XSSFRelation.IMAGE_PNG;
+ RELATIONS[Workbook.PICTURE_TYPE_DIB] = XSSFRelation.IMAGE_DIB;
}
/**
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStream;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
import java.util.Iterator;
-import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLRelation;
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.xssf.model.StylesTable;
+import org.apache.poi.xssf.model.SharedStringsTable;
+import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
-import org.apache.poi.xssf.model.BinaryPart;
-import org.apache.poi.xssf.model.CommentsTable;
-import org.apache.poi.xssf.model.Control;
-import org.apache.poi.xssf.model.Drawing;
-import org.apache.poi.xssf.model.SharedStringsTable;
-import org.apache.poi.xssf.model.StylesTable;
-import org.apache.poi.xssf.model.ThemeTable;
-import org.apache.poi.xssf.model.XSSFChildContainingModel;
-import org.apache.poi.xssf.model.XSSFModel;
-import org.apache.poi.xssf.model.XSSFWritableModel;
import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackagePartName;
import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxml4j.opc.PackagingURIHelper;
-import org.openxml4j.opc.TargetMode;
/**
*
*/
-public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
+public final class XSSFRelation extends POIXMLRelation {
+
+ private static POILogger log = POILogFactory.getLogger(XSSFRelation.class);
+
+ /**
+ * A map to lookup POIXMLRelation by its relation type
+ */
+ protected static Map<String, XSSFRelation> _table = new HashMap<String, XSSFRelation>();
+
public static final XSSFRelation WORKBOOK = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
"/xl/worksheets/sheet#.xml",
- null
+ XSSFSheet.class
);
- public static final XSSFRelation<SharedStringsTable> SHARED_STRINGS = create(
+ public static final XSSFRelation SHARED_STRINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
"/xl/sharedStrings.xml",
SharedStringsTable.class
);
- public static final XSSFRelation<StylesTable> STYLES = create(
+ public static final XSSFRelation STYLES = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
"/xl/styles.xml",
"application/vnd.openxmlformats-officedocument.drawing+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing",
"/xl/drawings/drawing#.xml",
- null
+ XSSFDrawing.class
);
- public static final XSSFRelation<Drawing> VML_DRAWINGS = create(
+ public static final XSSFRelation VML_DRAWINGS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.vmlDrawing",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
"/xl/drawings/vmlDrawing#.vml",
- Drawing.class
+ null
);
public static final XSSFRelation IMAGES = new XSSFRelation(
- //client will substitute $type and $ext with the appropriate values depending on the passed data
- "image/$type",
+ null,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
- "/xl/media/image#.$ext",
- null
+ null,
+ XSSFPictureData.class
);
- public static final XSSFRelation<CommentsTable> SHEET_COMMENTS = create(
+ public static final XSSFRelation IMAGE_EMF = new XSSFRelation(
+ "image/x-emf",
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/xl/media/image#.emf",
+ XSSFPictureData.class
+ );
+ public static final XSSFRelation IMAGE_WMF = new XSSFRelation(
+ "image/x-wmf",
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/xl/media/image#.wmf",
+ XSSFPictureData.class
+ );
+ public static final XSSFRelation IMAGE_PICT = new XSSFRelation(
+ "image/pict",
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/xl/media/image#.pict",
+ XSSFPictureData.class
+ );
+ public static final XSSFRelation IMAGE_JPEG = new XSSFRelation(
+ "image/jpeg",
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/xl/media/image#.jpeg",
+ XSSFPictureData.class
+ );
+ public static final XSSFRelation IMAGE_PNG = new XSSFRelation(
+ "image/png",
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/xl/media/image#.png",
+ XSSFPictureData.class
+ );
+ public static final XSSFRelation IMAGE_DIB = new XSSFRelation(
+ "image/dib",
+ "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
+ "/xl/media/image#.dib",
+ XSSFPictureData.class
+ );
+
+ public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
"/xl/comments#.xml",
null,
null
);
- public static final XSSFRelation<BinaryPart> OLEEMBEDDINGS = create(
+ public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation(
null,
POIXMLDocument.OLE_OBJECT_REL_TYPE,
null,
- BinaryPart.class
+ null
);
- public static final XSSFRelation<BinaryPart> PACKEMBEDDINGS = create(
+ public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation(
null,
POIXMLDocument.PACK_OBJECT_REL_TYPE,
null,
- BinaryPart.class
+ null
);
- public static final XSSFRelation<BinaryPart> VBA_MACROS = create(
+ public static final XSSFRelation VBA_MACROS = new XSSFRelation(
"application/vnd.ms-office.vbaProject",
"http://schemas.microsoft.com/office/2006/relationships/vbaProject",
"/xl/vbaProject.bin",
- BinaryPart.class
+ null
);
- public static final XSSFRelation<Control> ACTIVEX_CONTROLS = create(
+ public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation(
"application/vnd.ms-office.activeX+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/control",
"/xl/activeX/activeX#.xml",
- Control.class
+ null
);
- public static final XSSFRelation<BinaryPart> ACTIVEX_BINS = create(
+ public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation(
"application/vnd.ms-office.activeX",
"http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary",
"/xl/activeX/activeX#.bin",
- BinaryPart.class
+ null
);
- public static final XSSFRelation<ThemeTable> THEME = create(
+ public static final XSSFRelation THEME = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.theme+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
"/xl/theme/theme#.xml",
- ThemeTable.class
+ null
);
- private static POILogger log = POILogFactory.getLogger(XSSFRelation.class);
-
- private static <R extends XSSFModel> XSSFRelation<R> create(String type, String rel, String defaultName, Class<R> cls) {
- return new XSSFRelation<R>(type, rel, defaultName, cls);
- }
-
- private Constructor<W> _constructor;
- private final boolean _constructorTakesTwoArgs;
-
- private XSSFRelation(String type, String rel, String defaultName, Class<W> cls) {
- super(type, rel, defaultName);
- if (cls == null) {
- _constructor = null;
- _constructorTakesTwoArgs = false;
- } else {
- Constructor<W> c;
- boolean twoArg;
-
- // Find the right constructor
- try {
- c = cls.getConstructor(InputStream.class, String.class);
- twoArg = true;
- } catch(NoSuchMethodException e) {
- try {
- c = cls.getConstructor(InputStream.class);
- twoArg = false;
- } catch(NoSuchMethodException e2) {
- throw new RuntimeException(e2);
- }
- }
- _constructor = c;
- _constructorTakesTwoArgs = twoArg;
- }
- }
+ private XSSFRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
+ super(type, rel, defaultName, cls);
- /**
- * Does one of these exist for the given core
- * package part?
- */
- public boolean exists(PackagePart corePart) throws InvalidFormatException {
- if(corePart == null) {
- // new file, can't exist
- return false;
- }
-
- PackageRelationshipCollection prc =
- corePart.getRelationshipsByType(_relation);
- Iterator<PackageRelationship> it = prc.iterator();
- return it.hasNext();
- }
-
- /**
- * Returns the filename for the nth one of these,
- * eg /xl/comments4.xml
- */
- public String getFileName(int index) {
- if(_defaultName.indexOf("#") == -1) {
- // Generic filename in all cases
- return getDefaultFileName();
- }
- return _defaultName.replace("#", Integer.toString(index));
- }
+ if(cls != null && !_table.containsKey(rel)) _table.put(rel, this);
+ }
- /**
- * Fetches the InputStream to read the contents, based
+ /**
+ * Fetches the InputStream to read the contents, based
* of the specified core part, for which we are defined
* as a suitable relationship
*/
return null;
}
}
-
- /**
- * Loads all the XSSFModels of this type which are
- * defined as relationships of the given parent part
- */
- public List<W> loadAll(PackagePart parentPart) throws Exception {
- List<W> found = new ArrayList<W>();
- for(PackageRelationship rel : parentPart.getRelationshipsByType(_relation)) {
- PackagePart part = XSSFWorkbook.getTargetPart(parentPart.getPackage(), rel);
- found.add(create(part, rel));
- }
- return found;
- }
-
- /**
- * Load a single Model, which is defined as a suitable
- * relationship from the specified core (parent)
- * package part.
- */
- public W load(PackagePart corePart) throws Exception {
- PackageRelationshipCollection prc =
- corePart.getRelationshipsByType(_relation);
- Iterator<PackageRelationship> it = prc.iterator();
- if(it.hasNext()) {
- PackageRelationship rel = it.next();
- PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
- PackagePart part = corePart.getPackage().getPart(relName);
- return create(part, rel);
- } else {
- log.log(POILogger.WARN, "No part " + _defaultName + " found");
- return null;
- }
- }
-
- /**
- * Does the actual Model creation
- */
- private W create(PackagePart thisPart, PackageRelationship rel)
- throws IOException, InvalidFormatException {
-
- if (_constructor == null) {
- throw new IllegalStateException("Model class not set");
- }
- // Instantiate, if we can
- InputStream inp = thisPart.getInputStream();
- if (inp == null) {
- return null; // TODO - is this valid?
- }
- Object[] args;
- if (_constructorTakesTwoArgs) {
- args = new Object[] { inp, rel.getId(), };
- } else {
- args = new Object[] { inp, };
- }
- W result;
- try {
- try {
- result = _constructor.newInstance(args);
- } catch (IllegalArgumentException e) {
- throw new RuntimeException(e);
- } catch (InstantiationException e) {
- throw new RuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new RuntimeException(e);
- } catch (InvocationTargetException e) {
- Throwable t = e.getTargetException();
- if (t instanceof IOException) {
- throw (IOException)t;
- }
- if (t instanceof RuntimeException) {
- throw (RuntimeException)t;
- }
- throw new RuntimeException(t);
- }
- } finally {
- inp.close();
- }
-
- // Do children, if required
- if(result instanceof XSSFChildContainingModel) {
- XSSFChildContainingModel ccm =
- (XSSFChildContainingModel)result;
- for(String relType : ccm.getChildrenRelationshipTypes()) {
- for(PackageRelationship cRel : thisPart.getRelationshipsByType(relType)) {
- PackagePart childPart = XSSFWorkbook.getTargetPart(thisPart.getPackage(), cRel);
- ccm.generateChild(childPart, cRel.getId());
- }
- }
- }
-
-
- return result;
- }
-
- /**
- * Save, with the default name
- * @return The internal reference ID it was saved at, normally then used as an r:id
- */
- protected String save(XSSFWritableModel model, PackagePart corePart) throws IOException {
- return save(model, corePart, _defaultName);
- }
- /**
- * Save, with the name generated by the given index
- * @return The internal reference ID it was saved at, normally then used as an r:id
- */
- protected String save(XSSFWritableModel model, PackagePart corePart, int index) throws IOException {
- return save(model, corePart, getFileName(index));
- }
- /**
- * Save, with the specified name
- * @return The internal reference ID it was saved at, normally then used as an r:id
- */
- protected String save(XSSFWritableModel model, PackagePart corePart, String name) throws IOException {
- PackagePartName ppName = null;
- try {
- ppName = PackagingURIHelper.createPartName(name);
- } catch(InvalidFormatException e) {
- throw new IllegalStateException("Can't create part with name " + name + " for " + model, e);
- }
- PackageRelationship rel =
- corePart.addRelationship(ppName, TargetMode.INTERNAL, _relation);
- PackagePart part = corePart.getPackage().createPart(ppName, _type);
-
- OutputStream out = part.getOutputStream();
- model.writeTo(out);
- out.close();
-
- // Do children, if required
- if(model instanceof XSSFChildContainingModel) {
- XSSFChildContainingModel ccm =
- (XSSFChildContainingModel)model;
- // Loop over each child, writing it out
- int numChildren = ccm.getNumberOfChildren();
- for(int i=0; i<numChildren; i++) {
- XSSFChildContainingModel.WritableChild child =
- ccm.getChildForWriting(i);
- child.getRelation().save(
- child.getModel(),
- part,
- (i+1)
- );
- }
- }
-
- return rel.getId();
- }
+
+
+ /**
+ * Get POIXMLRelation by relation type
+ *
+ * @param rel relation type, for example,
+ * <code>http://schemas.openxmlformats.org/officeDocument/2006/relationships/image</code>
+ * @return registered POIXMLRelation or null if not found
+ */
+ public static XSSFRelation getInstance(String rel){
+ return _table.get(rel);
+ }
}
import java.util.*;
import javax.xml.namespace.QName;
-import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.util.PaneInformation;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.Footer;
import org.apache.poi.ss.usermodel.Header;
-import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
protected CommentsSource sheetComments;
protected CTMergeCells ctMergeCells;
-
- protected List<Control> controls;
-
-
public static final short LeftMargin = 0;
public static final short RightMargin = 1;
public static final short TopMargin = 2;
public List<Control> getControls()
{
- return controls;
+ return null;
}
/**
if(ctDrawing == null) {
//drawingNumber = #drawings.size() + 1
int drawingNumber = getPackagePart().getPackage().getPartsByRelationshipType(XSSFRelation.DRAWINGS.getRelation()).size() + 1;
- drawing = (XSSFDrawing)createRelationship(XSSFRelation.DRAWINGS, XSSFDrawing.class, drawingNumber);
+ drawing = (XSSFDrawing)createRelationship(XSSFRelation.DRAWINGS, XSSFFactory.getInstance(), drawingNumber);
String relId = drawing.getPackageRelationship().getId();
//add CT_Drawing element which indicates that this sheet contains drawing components built on the drawingML platform.
public short getLeftCol() {
String cellRef = worksheet.getSheetViews().getSheetViewArray(0).getTopLeftCell();
CellReference cellReference = new CellReference(cellRef);
- return (short)cellReference.getCol();
+ return cellReference.getCol();
}
public double getMargin(short margin) {
* @see #setZoom(int)
*/
public void setZoom(int numerator, int denominator) {
- Float result = new Float(numerator)/new Float(denominator)*100;
- setZoom(result.intValue());
+ int zoom = 100*numerator/denominator;
+ setZoom(zoom);
}
/**
public void ungroupRow(int fromRow, int toRow) {
for(int i=fromRow;i<=toRow;i++){
- XSSFRow xrow=(XSSFRow)getRow(i-1);
+ XSSFRow xrow=getRow(i-1);
if(xrow!=null){
CTRow ctrow=xrow.getCTRow();
short outlinelevel=ctrow.getOutlineLevel();
private void setSheetFormatPrOutlineLevelRow(){
short maxLevelRow=getMaxOutlineLevelRows();
- getSheetTypeSheetFormatPr().setOutlineLevelRow((short)(maxLevelRow));
+ getSheetTypeSheetFormatPr().setOutlineLevelRow(maxLevelRow);
}
private void setSheetFormatPrOutlineLevelCol(){
short maxLevelCol=getMaxOutlineLevelCols();
- getSheetTypeSheetFormatPr().setOutlineLevelCol((short)(maxLevelCol));
+ getSheetTypeSheetFormatPr().setOutlineLevelCol(maxLevelCol);
}
protected CTSheetViews getSheetTypeSheetViews() {
private CommentsSource getComments() {
if (sheetComments == null) {
- sheetComments = (CommentsTable)createRelationship(XSSFRelation.SHEET_COMMENTS, CommentsTable.class, (int)sheet.getSheetId());
+ sheetComments = (CommentsTable)createRelationship(XSSFRelation.SHEET_COMMENTS, XSSFFactory.getInstance(), (int)sheet.getSheetId());
}
return sheetComments;
}
import java.io.IOException;
import java.io.OutputStream;
-import java.io.InputStream;
-import java.io.ByteArrayInputStream;
import java.util.*;
import javax.xml.namespace.QName;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLDocumentPart;
-import org.apache.poi.ss.usermodel.Palette;
-import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.PackageHelper;
-import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.model.*;
import org.apache.poi.POIXMLException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
-import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.exceptions.OpenXML4JException;
import org.openxml4j.opc.*;
import org.openxml4j.opc.Package;
public XSSFWorkbook(Package pkg) throws IOException {
super();
if(pkg.getPackageAccess() == PackageAccess.READ){
- //current implementation of OpenXML4J is funny.
+ //YK: current implementation of OpenXML4J is funny.
//Packages opened by Package.open(InputStream is) are read-only,
//there is no way to change or even save such an instance in a OutputStream.
//The workaround is to create a copy via a temp file
try {
//build the POIXMLDocumentPart tree, this workbook is the root
- read(new XSSFFactory());
+ read(XSSFFactory.getInstance());
PackagePart corePart = getCorePart();
if(sharedStringSource == null) {
//Create SST if it is missing
- sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, SharedStringsTable.class);
+ sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
}
// Process the named ranges
bv.setActiveTab(0);
workbook.addNewSheets();
- sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, SharedStringsTable.class);
- stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, StylesTable.class);
+ sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
+ stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
namedRanges = new LinkedList<XSSFName>();
sheets = new LinkedList<XSSFSheet>();
*/
public int addPicture(byte[] pictureData, int format) {
int imageNumber = getAllPictures().size() + 1;
- XSSFPictureData img = (XSSFPictureData)createRelationship(XSSFPictureData.RELATIONS[format], XSSFPictureData.class, imageNumber, true);
+ XSSFPictureData img = (XSSFPictureData)createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true);
try {
OutputStream out = img.getPackagePart().getOutputStream();
out.write(pictureData);
throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
int sheetNumber = getNumberOfSheets() + 1;
- XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFSheet.class, sheetNumber);
+ XSSFSheet wrapper = (XSSFSheet)createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber);
CTSheet sheet = addSheet(sheetname);
wrapper.sheet = sheet;
return stylesSource.getStyleAt(idx);
}
- public Palette getCustomPalette() {
- // TODO Auto-generated method stub
- return null;
- }
-
/**
* Get the font at the given index number
*
* @return XSSFFont at the index
*/
public XSSFFont getFontAt(short idx) {
- return (XSSFFont)stylesSource.getFontAt(idx);
+ return stylesSource.getFontAt(idx);
}
/**