refactored POIXMLFactory and related classes; simplified XSSFRelation - now it's just a set of definitions of XSSF relations, all read/load stuff is not used anymore and was removed. Also, removed Workbook.getCustomPallete() - it's OLE-specific

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@703814 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-10-12 13:10:30 +00:00
parent 1a700918d8
commit 9c094be425
10 changed files with 203 additions and 341 deletions

View File

@ -374,8 +374,6 @@ public interface Workbook {
*/
void removeName(String name);
Palette getCustomPalette();
/**
* Adds a picture to the workbook.
*

View File

@ -22,7 +22,6 @@ import java.util.List;
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;

View File

@ -143,43 +143,35 @@ public class POIXMLDocumentPart {
* Create a new child POIXMLDocumentPart
*
* @param descriptor the part descriptor
* @param cls the Class object identifying the type of instance to create
* @param factory the factory that will create an instance of the requested relation
* @return the created child POIXMLDocumentPart
*/
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls){
return createRelationship(descriptor, cls, -1);
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory){
return createRelationship(descriptor, factory, -1, false);
}
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx){
return createRelationship(descriptor, factory, idx, false);
}
/**
* Create a new child POIXMLDocumentPart
*
* @param descriptor the part descriptor
* @param cls the Class object identifying the type of instance to create
* @param factory the factory that will create an instance of the requested relation
* @param idx part number
* @param noRelation if true, then no relationship is added.
* @return the created child POIXMLDocumentPart
*/
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls, int idx){
return createRelationship(descriptor, cls, idx, false);
}
/**
* Create a new child POIXMLDocumentPart
*
* @param descriptor the part descriptor
* @param cls the Class object identifying the type of instance to create
* @param idx part number
* @param norel if true, then no relationship is added.
* @return the created child POIXMLDocumentPart
*/
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, Class<? extends POIXMLDocumentPart> cls, int idx, boolean norel){
protected POIXMLDocumentPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx, boolean noRelation){
try {
PackagePartName ppName = PackagingURIHelper.createPartName(descriptor.getFileName(idx));
PackageRelationship rel = null;
if(!norel) rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation());
if(!noRelation) rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation());
PackagePart part = packagePart.getPackage().createPart(ppName, descriptor.getContentType());
POIXMLDocumentPart doc = cls.newInstance();
POIXMLDocumentPart doc = factory.newDocumentPart(descriptor);
doc.packageRel = rel;
doc.packagePart = part;
doc.parent = this;
@ -206,7 +198,7 @@ public class POIXMLDocumentPart {
logger.log(POILogger.ERROR, "Skipped invalid entry " + rel.getTargetURI());
continue;
}
POIXMLDocumentPart childPart = factory.create(rel, p);
POIXMLDocumentPart childPart = factory.createDocumentPart(rel, p);
childPart.parent = this;
addRelation(childPart);

View File

@ -25,16 +25,24 @@ import org.openxml4j.opc.PackagePart;
*
* @author Yegor Kozlov
*/
public class POIXMLFactory {
public abstract class POIXMLFactory {
/**
* Creates a new instance of a {@link POIXMLDocumentPart}
* Create a POIXMLDocumentPart from existing package part and relation. This method is called
* from {@link POIXMLDocumentPart#read(POIXMLFactory)} when parsing a document
*
* @param rel the package part relationship
* @param part the PackagePart representing the created instance
* @return A new instance of a POIXMLDocumentPart.
*/
public POIXMLDocumentPart create(PackageRelationship rel, PackagePart part){
return new POIXMLDocumentPart(part, rel);
}
public abstract POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part);
/**
* Create a new POIXMLDocumentPart using the supplied descriptor. This method is used when adding new parts
* to a document, for example, when adding a sheet to a workbook, slide to a presentation, etc.
*
* @param descriptor described the object to create
* @return A new instance of a POIXMLDocumentPart.
*/
public abstract POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor);
}

View File

@ -21,7 +21,7 @@ package org.apache.poi;
*
* @author Yegor Kozlov
*/
public class POIXMLRelation {
public abstract class POIXMLRelation {
/**
* Describes the content stored in a part.
@ -38,6 +38,26 @@ public class POIXMLRelation {
*/
protected String _defaultName;
/**
* Defines what object is used to construct instances of this relationship
*/
private Class<? extends POIXMLDocumentPart> _cls;
/**
* Instantiates a POIXMLRelation.
*
* @param type content type
* @param rel relationship
* @param defaultName default item name
* @param cls defines what object is used to construct instances of this relationship
*/
public POIXMLRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
_type = type;
_relation = rel;
_defaultName = defaultName;
_cls = cls;
}
/**
* Instantiates a POIXMLRelation.
*
@ -46,11 +66,8 @@ public class POIXMLRelation {
* @param defaultName default item name
*/
public POIXMLRelation(String type, String rel, String defaultName) {
_type = type;
_relation = rel;
_defaultName = defaultName;
this(type, rel, defaultName, null);
}
/**
* Return the content type. Content types define a media type, a subtype, and an
* optional set of parameters, as defined in RFC 2616.
@ -93,4 +110,13 @@ public class POIXMLRelation {
}
return _defaultName.replace("#", Integer.toString(index));
}
/**
* Return type of the obejct used to construct instances of this relationship
*
* @return the class of the object used to construct instances of this relation
*/
public Class<? extends POIXMLDocumentPart> getRelationClass(){
return _cls;
}
}

View File

@ -19,14 +19,12 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.POIXMLFactory;
import org.apache.poi.POIXMLException;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.POIXMLRelation;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.POILogFactory;
import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.PackagePart;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Constructor;
/**
@ -35,25 +33,40 @@ import java.lang.reflect.Constructor;
* @author Yegor Kozlov
*/
public class XSSFFactory extends POIXMLFactory {
protected static Map<String, Class> parts = new HashMap<String, Class>();
static {
parts.put(XSSFRelation.WORKSHEET.getRelation(), XSSFSheet.class);
parts.put(XSSFRelation.SHARED_STRINGS.getRelation(), SharedStringsTable.class);
parts.put(XSSFRelation.STYLES.getRelation(), StylesTable.class);
parts.put(XSSFRelation.SHEET_COMMENTS.getRelation(), CommentsTable.class);
parts.put(XSSFRelation.DRAWINGS.getRelation(), XSSFDrawing.class);
parts.put(XSSFRelation.IMAGES.getRelation(), XSSFPictureData.class);
private static POILogger logger = POILogFactory.getLogger(XSSFFactory.class);
private XSSFFactory(){
}
public POIXMLDocumentPart create(PackageRelationship rel, PackagePart p){
Class cls = parts.get(rel.getRelationshipType());
if(cls == null) return super.create(rel, p);
public static XSSFFactory getInstance(){
return new XSSFFactory();
}
public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){
XSSFRelation descriptor = XSSFRelation.getInstance(rel.getRelationshipType());
if(descriptor == null || descriptor.getRelationClass() == null){
logger.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());
return new POIXMLDocumentPart(part, rel);
}
try {
Class cls = descriptor.getRelationClass();
Constructor<? extends POIXMLDocumentPart> constructor = cls.getConstructor(PackagePart.class, PackageRelationship.class);
return constructor.newInstance(p, rel);
return constructor.newInstance(part, rel);
} catch (Exception e){
throw new POIXMLException(e);
}
}
public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){
try {
Class cls = descriptor.getRelationClass();
Constructor<? extends POIXMLDocumentPart> constructor = cls.getConstructor();
return constructor.newInstance();
} catch (Exception e){
throw new POIXMLException(e);
}
}
}

View File

@ -39,12 +39,12 @@ public class XSSFPictureData extends POIXMLDocumentPart implements PictureData {
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;
}
/**

View File

@ -18,39 +18,37 @@ package org.apache.poi.xssf.usermodel;
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",
@ -68,15 +66,15 @@ public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
"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",
@ -86,22 +84,58 @@ public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
"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",
@ -113,111 +147,54 @@ public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
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 XSSFRelation(String type, String rel, String defaultName, Class<? extends POIXMLDocumentPart> cls) {
super(type, rel, defaultName, cls);
if(cls != null && !_table.containsKey(rel)) _table.put(rel, this);
}
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;
}
}
/**
* 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));
}
/**
* 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
*/
@ -235,150 +212,16 @@ public final class XSSFRelation<W extends XSSFModel> extends POIXMLRelation {
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);
}
}

View File

@ -22,13 +22,11 @@ import java.io.OutputStream;
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;
@ -71,10 +69,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
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;
@ -167,7 +161,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
public List<Control> getControls()
{
return controls;
return null;
}
/**
@ -267,7 +261,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
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.
@ -632,7 +626,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
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) {
@ -1220,8 +1214,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
* @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);
}
/**
@ -1368,7 +1362,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
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();
@ -1384,12 +1378,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
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() {
@ -1519,7 +1513,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
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;
}

View File

@ -19,14 +19,10 @@ package org.apache.poi.xssf.usermodel;
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;
@ -34,12 +30,10 @@ import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
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;
@ -131,7 +125,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
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
@ -155,7 +149,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
try {
//build the POIXMLDocumentPart tree, this workbook is the root
read(new XSSFFactory());
read(XSSFFactory.getInstance());
PackagePart corePart = getCorePart();
@ -202,7 +196,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
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
@ -240,8 +234,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
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>();
@ -273,7 +267,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
*/
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);
@ -392,7 +386,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
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;
@ -496,11 +490,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
return stylesSource.getStyleAt(idx);
}
public Palette getCustomPalette() {
// TODO Auto-generated method stub
return null;
}
/**
* Get the font at the given index number
*
@ -508,7 +497,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* @return XSSFFont at the index
*/
public XSSFFont getFontAt(short idx) {
return (XSSFFont)stylesSource.getFontAt(idx);
return stylesSource.getFontAt(idx);
}
/**