Clean-up of generics in XSSFRelation

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@699990 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-09-29 05:40:48 +00:00
parent 29e0439f39
commit 0c602f354f
3 changed files with 1951 additions and 1904 deletions

View File

@ -1,19 +1,43 @@
/** /* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
*/ contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLDocument;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
import org.apache.poi.xssf.model.*; 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.exceptions.InvalidFormatException;
import org.openxml4j.opc.PackagePart; import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackagePartName; import org.openxml4j.opc.PackagePartName;
@ -22,7 +46,11 @@ import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxml4j.opc.PackagingURIHelper; import org.openxml4j.opc.PackagingURIHelper;
import org.openxml4j.opc.TargetMode; import org.openxml4j.opc.TargetMode;
public class XSSFRelation { /**
*
*/
public final class XSSFRelation<W extends XSSFModel> {
public static final XSSFRelation WORKBOOK = new XSSFRelation( public static final XSSFRelation WORKBOOK = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/workbook", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/workbook",
@ -41,13 +69,13 @@ public class XSSFRelation {
"/xl/worksheets/sheet#.xml", "/xl/worksheets/sheet#.xml",
null null
); );
public static final XSSFRelation SHARED_STRINGS = new XSSFRelation( public static final XSSFRelation<SharedStringsTable> SHARED_STRINGS = create(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
"/xl/sharedStrings.xml", "/xl/sharedStrings.xml",
SharedStringsTable.class SharedStringsTable.class
); );
public static final XSSFRelation STYLES = new XSSFRelation( public static final XSSFRelation<StylesTable> STYLES = create(
"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
"/xl/styles.xml", "/xl/styles.xml",
@ -59,7 +87,7 @@ public class XSSFRelation {
"/xl/drawings/drawing#.xml", "/xl/drawings/drawing#.xml",
null null
); );
public static final XSSFRelation VML_DRAWINGS = new XSSFRelation( public static final XSSFRelation<Drawing> VML_DRAWINGS = create(
"application/vnd.openxmlformats-officedocument.vmlDrawing", "application/vnd.openxmlformats-officedocument.vmlDrawing",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
"/xl/drawings/vmlDrawing#.vml", "/xl/drawings/vmlDrawing#.vml",
@ -71,7 +99,7 @@ public class XSSFRelation {
"/xl/media/image#.emf", "/xl/media/image#.emf",
null null
); );
public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation( public static final XSSFRelation<CommentsTable> SHEET_COMMENTS = create(
"application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
"/xl/comments#.xml", "/xl/comments#.xml",
@ -83,38 +111,38 @@ public class XSSFRelation {
null, null,
null null
); );
public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation( public static final XSSFRelation<BinaryPart> OLEEMBEDDINGS = create(
null, null,
POIXMLDocument.OLE_OBJECT_REL_TYPE, POIXMLDocument.OLE_OBJECT_REL_TYPE,
null, null,
BinaryPart.class BinaryPart.class
); );
public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation( public static final XSSFRelation<BinaryPart> PACKEMBEDDINGS = create(
null, null,
POIXMLDocument.PACK_OBJECT_REL_TYPE, POIXMLDocument.PACK_OBJECT_REL_TYPE,
null, null,
BinaryPart.class BinaryPart.class
); );
public static final XSSFRelation VBA_MACROS = new XSSFRelation( public static final XSSFRelation<BinaryPart> VBA_MACROS = create(
"application/vnd.ms-office.vbaProject", "application/vnd.ms-office.vbaProject",
"http://schemas.microsoft.com/office/2006/relationships/vbaProject", "http://schemas.microsoft.com/office/2006/relationships/vbaProject",
"/xl/vbaProject.bin", "/xl/vbaProject.bin",
BinaryPart.class BinaryPart.class
); );
public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation( public static final XSSFRelation<Control> ACTIVEX_CONTROLS = create(
"application/vnd.ms-office.activeX+xml", "application/vnd.ms-office.activeX+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/control", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/control",
"/xl/activeX/activeX#.xml", "/xl/activeX/activeX#.xml",
Control.class Control.class
); );
public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation( public static final XSSFRelation<BinaryPart> ACTIVEX_BINS = create(
"application/vnd.ms-office.activeX", "application/vnd.ms-office.activeX",
"http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary", "http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary",
"/xl/activeX/activeX#.bin", "/xl/activeX/activeX#.bin",
BinaryPart.class BinaryPart.class
); );
public static final XSSFRelation THEME = new XSSFRelation( public static final XSSFRelation<ThemeTable> THEME = create(
"application/vnd.openxmlformats-officedocument.theme+xml", "application/vnd.openxmlformats-officedocument.theme+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
"/xl/theme/theme#.xml", "/xl/theme/theme#.xml",
@ -124,40 +152,61 @@ public class XSSFRelation {
private static POILogger log = POILogFactory.getLogger(XSSFRelation.class); 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 String TYPE; private String _type;
private String REL; private String _relation;
private String DEFAULT_NAME; private String _defaultName;
private Class<? extends XSSFModel> CLASS; private Constructor<W> _constructor;
private final boolean _constructorTakesTwoArgs;
protected XSSFRelation(String TYPE, String REL, String DEFAULT_NAME, Class<? extends XSSFModel> CLASS) { private XSSFRelation(String type, String rel, String defaultName, Class<W> cls) {
this.TYPE = TYPE; _type = type;
this.REL = REL; _relation = rel;
this.DEFAULT_NAME = DEFAULT_NAME; _defaultName = defaultName;
this.CLASS = CLASS; 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;
}
} }
public String getContentType() { return TYPE; } public String getContentType() { return _type; }
public String getRelation() { return REL; } public String getRelation() { return _relation; }
public String getDefaultFileName() { return DEFAULT_NAME; } public String getDefaultFileName() { return _defaultName; }
/** /**
* Does one of these exist for the given core * Does one of these exist for the given core
* package part? * package part?
*/ */
public boolean exists(PackagePart corePart) throws IOException, InvalidFormatException { public boolean exists(PackagePart corePart) throws InvalidFormatException {
if(corePart == null) { if(corePart == null) {
// new file, can't exist // new file, can't exist
return false; return false;
} }
PackageRelationshipCollection prc = PackageRelationshipCollection prc =
corePart.getRelationshipsByType(REL); corePart.getRelationshipsByType(_relation);
Iterator<PackageRelationship> it = prc.iterator(); Iterator<PackageRelationship> it = prc.iterator();
if(it.hasNext()) { return it.hasNext();
return true;
} else {
return false;
}
} }
/** /**
@ -165,11 +214,11 @@ public class XSSFRelation {
* eg /xl/comments4.xml * eg /xl/comments4.xml
*/ */
public String getFileName(int index) { public String getFileName(int index) {
if(DEFAULT_NAME.indexOf("#") == -1) { if(_defaultName.indexOf("#") == -1) {
// Generic filename in all cases // Generic filename in all cases
return getDefaultFileName(); return getDefaultFileName();
} }
return DEFAULT_NAME.replace("#", Integer.toString(index)); return _defaultName.replace("#", Integer.toString(index));
} }
/** /**
@ -179,7 +228,7 @@ public class XSSFRelation {
*/ */
public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException { public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
PackageRelationshipCollection prc = PackageRelationshipCollection prc =
corePart.getRelationshipsByType(REL); corePart.getRelationshipsByType(_relation);
Iterator<PackageRelationship> it = prc.iterator(); Iterator<PackageRelationship> it = prc.iterator();
if(it.hasNext()) { if(it.hasNext()) {
PackageRelationship rel = it.next(); PackageRelationship rel = it.next();
@ -187,7 +236,7 @@ public class XSSFRelation {
PackagePart part = corePart.getPackage().getPart(relName); PackagePart part = corePart.getPackage().getPart(relName);
return part.getInputStream(); return part.getInputStream();
} else { } else {
log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found"); log.log(POILogger.WARN, "No part " + _defaultName + " found");
return null; return null;
} }
} }
@ -196,9 +245,9 @@ public class XSSFRelation {
* Loads all the XSSFModels of this type which are * Loads all the XSSFModels of this type which are
* defined as relationships of the given parent part * defined as relationships of the given parent part
*/ */
public ArrayList<? extends XSSFModel> loadAll(PackagePart parentPart) throws Exception { public List<W> loadAll(PackagePart parentPart) throws Exception {
ArrayList<XSSFModel> found = new ArrayList<XSSFModel>(); List<W> found = new ArrayList<W>();
for(PackageRelationship rel : parentPart.getRelationshipsByType(REL)) { for(PackageRelationship rel : parentPart.getRelationshipsByType(_relation)) {
PackagePart part = XSSFWorkbook.getTargetPart(parentPart.getPackage(), rel); PackagePart part = XSSFWorkbook.getTargetPart(parentPart.getPackage(), rel);
found.add(create(part, rel)); found.add(create(part, rel));
} }
@ -210,9 +259,9 @@ public class XSSFRelation {
* relationship from the specified core (parent) * relationship from the specified core (parent)
* package part. * package part.
*/ */
public XSSFModel load(PackagePart corePart) throws Exception { public W load(PackagePart corePart) throws Exception {
PackageRelationshipCollection prc = PackageRelationshipCollection prc =
corePart.getRelationshipsByType(REL); corePart.getRelationshipsByType(_relation);
Iterator<PackageRelationship> it = prc.iterator(); Iterator<PackageRelationship> it = prc.iterator();
if(it.hasNext()) { if(it.hasNext()) {
PackageRelationship rel = it.next(); PackageRelationship rel = it.next();
@ -220,7 +269,7 @@ public class XSSFRelation {
PackagePart part = corePart.getPackage().getPart(relName); PackagePart part = corePart.getPackage().getPart(relName);
return create(part, rel); return create(part, rel);
} else { } else {
log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found"); log.log(POILogger.WARN, "No part " + _defaultName + " found");
return null; return null;
} }
} }
@ -228,48 +277,61 @@ public class XSSFRelation {
/** /**
* Does the actual Model creation * Does the actual Model creation
*/ */
private XSSFModel create(PackagePart thisPart, PackageRelationship rel) throws Exception { private W create(PackagePart thisPart, PackageRelationship rel)
XSSFModel model = null; throws IOException, InvalidFormatException {
Constructor<? extends XSSFModel> c; if (_constructor == null) {
boolean withString = false; throw new IllegalStateException("Model class not set");
// Find the right constructor
try {
c = CLASS.getConstructor(InputStream.class, String.class);
withString = true;
} catch(NoSuchMethodException e) {
c = CLASS.getConstructor(InputStream.class);
} }
// Instantiate, if we can // Instantiate, if we can
InputStream inp = thisPart.getInputStream(); InputStream inp = thisPart.getInputStream();
if(inp != null) { if (inp == null) {
try { return null; // TODO - is this valid?
if(withString) { }
model = c.newInstance(inp, rel.getId()); Object[] args;
} else { if (_constructorTakesTwoArgs) {
model = c.newInstance(inp); args = new Object[] { inp, rel.getId(), };
} } else {
} finally { args = new Object[] { inp, };
inp.close(); }
} W result;
try {
// Do children, if required try {
if(model instanceof XSSFChildContainingModel) { result = _constructor.newInstance(args);
XSSFChildContainingModel ccm = } catch (IllegalArgumentException e) {
(XSSFChildContainingModel)model; throw new RuntimeException(e);
for(String relType : ccm.getChildrenRelationshipTypes()) { } catch (InstantiationException e) {
for(PackageRelationship cRel : thisPart.getRelationshipsByType(relType)) { throw new RuntimeException(e);
PackagePart childPart = XSSFWorkbook.getTargetPart(thisPart.getPackage(), cRel); } catch (IllegalAccessException e) {
ccm.generateChild(childPart, cRel.getId()); 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;
return model;
} }
/** /**
@ -277,7 +339,7 @@ public class XSSFRelation {
* @return The internal reference ID it was saved at, normally then used as an r:id * @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 { protected String save(XSSFWritableModel model, PackagePart corePart) throws IOException {
return save(model, corePart, DEFAULT_NAME); return save(model, corePart, _defaultName);
} }
/** /**
* Save, with the name generated by the given index * Save, with the name generated by the given index
@ -298,8 +360,8 @@ public class XSSFRelation {
throw new IllegalStateException("Can't create part with name " + name + " for " + model, e); throw new IllegalStateException("Can't create part with name " + name + " for " + model, e);
} }
PackageRelationship rel = PackageRelationship rel =
corePart.addRelationship(ppName, TargetMode.INTERNAL, REL); corePart.addRelationship(ppName, TargetMode.INTERNAL, _relation);
PackagePart part = corePart.getPackage().createPart(ppName, TYPE); PackagePart part = corePart.getPackage().createPart(ppName, _type);
OutputStream out = part.getOutputStream(); OutputStream out = part.getOutputStream();
model.writeTo(out); model.writeTo(out);

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff