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;
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 org.apache.poi.POIXMLDocument;
import org.apache.poi.util.POILogFactory;
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.opc.PackagePart;
import org.openxml4j.opc.PackagePartName;
@ -22,7 +46,11 @@ import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxml4j.opc.PackagingURIHelper;
import org.openxml4j.opc.TargetMode;
public class XSSFRelation {
/**
*
*/
public final class XSSFRelation<W extends XSSFModel> {
public static final XSSFRelation WORKBOOK = new XSSFRelation(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/workbook",
@ -41,13 +69,13 @@ public class XSSFRelation {
"/xl/worksheets/sheet#.xml",
null
);
public static final XSSFRelation SHARED_STRINGS = new XSSFRelation(
public static final XSSFRelation<SharedStringsTable> SHARED_STRINGS = create(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
"/xl/sharedStrings.xml",
SharedStringsTable.class
);
public static final XSSFRelation STYLES = new XSSFRelation(
public static final XSSFRelation<StylesTable> STYLES = create(
"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
"/xl/styles.xml",
@ -59,7 +87,7 @@ public class XSSFRelation {
"/xl/drawings/drawing#.xml",
null
);
public static final XSSFRelation VML_DRAWINGS = new XSSFRelation(
public static final XSSFRelation<Drawing> VML_DRAWINGS = create(
"application/vnd.openxmlformats-officedocument.vmlDrawing",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing",
"/xl/drawings/vmlDrawing#.vml",
@ -71,7 +99,7 @@ public class XSSFRelation {
"/xl/media/image#.emf",
null
);
public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation(
public static final XSSFRelation<CommentsTable> SHEET_COMMENTS = create(
"application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
"/xl/comments#.xml",
@ -83,38 +111,38 @@ public class XSSFRelation {
null,
null
);
public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation(
public static final XSSFRelation<BinaryPart> OLEEMBEDDINGS = create(
null,
POIXMLDocument.OLE_OBJECT_REL_TYPE,
null,
BinaryPart.class
);
public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation(
public static final XSSFRelation<BinaryPart> PACKEMBEDDINGS = create(
null,
POIXMLDocument.PACK_OBJECT_REL_TYPE,
null,
BinaryPart.class
);
public static final XSSFRelation VBA_MACROS = new XSSFRelation(
public static final XSSFRelation<BinaryPart> VBA_MACROS = create(
"application/vnd.ms-office.vbaProject",
"http://schemas.microsoft.com/office/2006/relationships/vbaProject",
"/xl/vbaProject.bin",
BinaryPart.class
);
public static final XSSFRelation ACTIVEX_CONTROLS = new XSSFRelation(
public static final XSSFRelation<Control> ACTIVEX_CONTROLS = create(
"application/vnd.ms-office.activeX+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/control",
"/xl/activeX/activeX#.xml",
Control.class
);
public static final XSSFRelation ACTIVEX_BINS = new XSSFRelation(
public static final XSSFRelation<BinaryPart> ACTIVEX_BINS = create(
"application/vnd.ms-office.activeX",
"http://schemas.microsoft.com/office/2006/relationships/activeXControlBinary",
"/xl/activeX/activeX#.bin",
BinaryPart.class
);
public static final XSSFRelation THEME = new XSSFRelation(
public static final XSSFRelation<ThemeTable> THEME = create(
"application/vnd.openxmlformats-officedocument.theme+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
"/xl/theme/theme#.xml",
@ -124,40 +152,61 @@ public class XSSFRelation {
private static POILogger log = POILogFactory.getLogger(XSSFRelation.class);
private String TYPE;
private String REL;
private String DEFAULT_NAME;
private Class<? extends XSSFModel> CLASS;
protected XSSFRelation(String TYPE, String REL, String DEFAULT_NAME, Class<? extends XSSFModel> CLASS) {
this.TYPE = TYPE;
this.REL = REL;
this.DEFAULT_NAME = DEFAULT_NAME;
this.CLASS = 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);
}
public String getContentType() { return TYPE; }
public String getRelation() { return REL; }
public String getDefaultFileName() { return DEFAULT_NAME; }
private String _type;
private String _relation;
private String _defaultName;
private Constructor<W> _constructor;
private final boolean _constructorTakesTwoArgs;
private XSSFRelation(String type, String rel, String defaultName, Class<W> cls) {
_type = type;
_relation = rel;
_defaultName = 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;
}
}
public String getContentType() { return _type; }
public String getRelation() { return _relation; }
public String getDefaultFileName() { return _defaultName; }
/**
* Does one of these exist for the given core
* package part?
*/
public boolean exists(PackagePart corePart) throws IOException, InvalidFormatException {
public boolean exists(PackagePart corePart) throws InvalidFormatException {
if(corePart == null) {
// new file, can't exist
return false;
}
PackageRelationshipCollection prc =
corePart.getRelationshipsByType(REL);
corePart.getRelationshipsByType(_relation);
Iterator<PackageRelationship> it = prc.iterator();
if(it.hasNext()) {
return true;
} else {
return false;
}
return it.hasNext();
}
/**
@ -165,11 +214,11 @@ public class XSSFRelation {
* eg /xl/comments4.xml
*/
public String getFileName(int index) {
if(DEFAULT_NAME.indexOf("#") == -1) {
if(_defaultName.indexOf("#") == -1) {
// Generic filename in all cases
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 {
PackageRelationshipCollection prc =
corePart.getRelationshipsByType(REL);
corePart.getRelationshipsByType(_relation);
Iterator<PackageRelationship> it = prc.iterator();
if(it.hasNext()) {
PackageRelationship rel = it.next();
@ -187,7 +236,7 @@ public class XSSFRelation {
PackagePart part = corePart.getPackage().getPart(relName);
return part.getInputStream();
} else {
log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found");
log.log(POILogger.WARN, "No part " + _defaultName + " found");
return null;
}
}
@ -196,9 +245,9 @@ public class XSSFRelation {
* Loads all the XSSFModels of this type which are
* defined as relationships of the given parent part
*/
public ArrayList<? extends XSSFModel> loadAll(PackagePart parentPart) throws Exception {
ArrayList<XSSFModel> found = new ArrayList<XSSFModel>();
for(PackageRelationship rel : parentPart.getRelationshipsByType(REL)) {
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));
}
@ -210,9 +259,9 @@ public class XSSFRelation {
* relationship from the specified core (parent)
* package part.
*/
public XSSFModel load(PackagePart corePart) throws Exception {
public W load(PackagePart corePart) throws Exception {
PackageRelationshipCollection prc =
corePart.getRelationshipsByType(REL);
corePart.getRelationshipsByType(_relation);
Iterator<PackageRelationship> it = prc.iterator();
if(it.hasNext()) {
PackageRelationship rel = it.next();
@ -220,7 +269,7 @@ public class XSSFRelation {
PackagePart part = corePart.getPackage().getPart(relName);
return create(part, rel);
} else {
log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found");
log.log(POILogger.WARN, "No part " + _defaultName + " found");
return null;
}
}
@ -228,37 +277,51 @@ public class XSSFRelation {
/**
* Does the actual Model creation
*/
private XSSFModel create(PackagePart thisPart, PackageRelationship rel) throws Exception {
XSSFModel model = null;
private W create(PackagePart thisPart, PackageRelationship rel)
throws IOException, InvalidFormatException {
Constructor<? extends XSSFModel> c;
boolean withString = false;
// Find the right constructor
try {
c = CLASS.getConstructor(InputStream.class, String.class);
withString = true;
} catch(NoSuchMethodException e) {
c = CLASS.getConstructor(InputStream.class);
if (_constructor == null) {
throw new IllegalStateException("Model class not set");
}
// Instantiate, if we can
InputStream inp = thisPart.getInputStream();
if(inp != null) {
try {
if(withString) {
model = c.newInstance(inp, rel.getId());
if (inp == null) {
return null; // TODO - is this valid?
}
Object[] args;
if (_constructorTakesTwoArgs) {
args = new Object[] { inp, rel.getId(), };
} else {
model = c.newInstance(inp);
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(model instanceof XSSFChildContainingModel) {
if(result instanceof XSSFChildContainingModel) {
XSSFChildContainingModel ccm =
(XSSFChildContainingModel)model;
(XSSFChildContainingModel)result;
for(String relType : ccm.getChildrenRelationshipTypes()) {
for(PackageRelationship cRel : thisPart.getRelationshipsByType(relType)) {
PackagePart childPart = XSSFWorkbook.getTargetPart(thisPart.getPackage(), cRel);
@ -266,10 +329,9 @@ public class XSSFRelation {
}
}
}
}
return model;
return result;
}
/**
@ -277,7 +339,7 @@ public class XSSFRelation {
* @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, DEFAULT_NAME);
return save(model, corePart, _defaultName);
}
/**
* 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);
}
PackageRelationship rel =
corePart.addRelationship(ppName, TargetMode.INTERNAL, REL);
PackagePart part = corePart.getPackage().createPart(ppName, TYPE);
corePart.addRelationship(ppName, TargetMode.INTERNAL, _relation);
PackagePart part = corePart.getPackage().createPart(ppName, _type);
OutputStream out = part.getOutputStream();
model.writeTo(out);

View File

@ -19,15 +19,17 @@ package org.apache.poi.xssf.usermodel;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.hssf.util.PaneInformation;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
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.Hyperlink;
import org.apache.poi.ss.usermodel.Patriarch;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
@ -43,12 +45,9 @@ import org.apache.xmlbeans.XmlOptions;
import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTControls;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomProperties;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
@ -58,23 +57,18 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOutlinePr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetup;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPane;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPrintOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRecord;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSelection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetProtection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetView;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetViews;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState;
/**
@ -98,8 +92,8 @@ public class XSSFSheet implements Sheet {
protected CTMergeCells ctMergeCells;
protected ArrayList<Drawing> drawings;
protected ArrayList<Control> controls;
protected List<Drawing> drawings;
protected List<Control> controls;
public static final short LeftMargin = 0;
@ -109,18 +103,18 @@ public class XSSFSheet implements Sheet {
public static final short HeaderMargin = 4;
public static final short FooterMargin = 5;
public XSSFSheet(CTSheet sheet, CTWorksheet worksheet, XSSFWorkbook workbook, CommentsSource sheetComments, ArrayList<Drawing> drawings, ArrayList<Control> controls) {
public XSSFSheet(CTSheet sheet, CTWorksheet worksheet, XSSFWorkbook workbook, CommentsSource sheetComments, List<Drawing> drawings, List<Control> controls) {
this(sheet, worksheet, workbook, sheetComments);
this.drawings = drawings;
this.controls = controls;
}
public ArrayList<Drawing> getDrawings()
public List<Drawing> getDrawings()
{
return drawings;
}
public ArrayList<Control> getControls()
public List<Control> getControls()
{
return controls;
}
@ -1574,9 +1568,4 @@ public class XSSFSheet implements Sheet {
}
return getDefaultSheetView().getPane();
}
}

View File

@ -20,7 +20,6 @@ package org.apache.poi.xssf.usermodel;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@ -76,7 +75,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
private SharedStringSource sharedStringSource;
private StylesSource stylesSource;
private List<? extends XSSFModel> themes = new LinkedList<ThemeTable>();
private List<ThemeTable> themes = new LinkedList<ThemeTable>();
private MissingCellPolicy missingCellPolicy = Row.RETURN_NULL_AND_BLANK;
@ -113,22 +112,20 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
try {
// Load shared strings
this.sharedStringSource = (SharedStringSource)
XSSFRelation.SHARED_STRINGS.load(getCorePart());
sharedStringSource = XSSFRelation.SHARED_STRINGS.load(getCorePart());
} catch(Exception e) {
throw new IOException("Unable to load shared strings - " + e.toString());
}
try {
// Load styles source
this.stylesSource = (StylesSource)
XSSFRelation.STYLES.load(getCorePart());
stylesSource = XSSFRelation.STYLES.load(getCorePart());
} catch(Exception e) {
e.printStackTrace();
throw new IOException("Unable to load styles - " + e.toString());
}
try {
// Load themes
this.themes = XSSFRelation.THEME.loadAll(getCorePart());
themes = XSSFRelation.THEME.loadAll(getCorePart());
} catch(Exception e) {
throw new IOException("Unable to load shared strings - " + e.toString());
}
@ -142,21 +139,21 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
// Load child streams of the sheet
ArrayList<? extends XSSFModel> childModels;
List<CommentsTable> childModels;
CommentsSource comments = null;
ArrayList<Drawing> drawings;
ArrayList<Control> controls;
List<Drawing> drawings;
List<Control> controls;
try {
// Get the comments for the sheet, if there are any
childModels = XSSFRelation.SHEET_COMMENTS.loadAll(part);
if(childModels.size() > 0) {
comments = (CommentsSource)childModels.get(0);
comments = childModels.get(0);
}
// Get the drawings for the sheet, if there are any
drawings = (ArrayList<Drawing>)XSSFRelation.VML_DRAWINGS.loadAll(part);
drawings = XSSFRelation.VML_DRAWINGS.loadAll(part);
// Get the activeX controls for the sheet, if there are any
controls = (ArrayList<Control>)XSSFRelation.ACTIVEX_CONTROLS.loadAll(part);
controls = XSSFRelation.ACTIVEX_CONTROLS.loadAll(part);
} catch(Exception e) {
throw new RuntimeException("Unable to construct child part",e);
}
@ -737,7 +734,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
// Copy VBA Macros if present
if(XSSFRelation.VBA_MACROS.exists( getCorePart() )) {
try {
XSSFModel vba = XSSFRelation.VBA_MACROS.load(getCorePart());
BinaryPart vba = XSSFRelation.VBA_MACROS.load(getCorePart());
XSSFRelation.VBA_MACROS.save(vba, corePart);
} catch(Exception e) {
throw new RuntimeException("Unable to copy vba macros over", e);
@ -792,5 +789,4 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
return false;
}
}