* @author Yegor Kozlov
*/
public class POIXMLDocumentPart {
- private static POILogger logger = POILogFactory.getLogger(POIXMLDocumentPart.class);
+ private static final POILogger logger = POILogFactory.getLogger(POIXMLDocumentPart.class);
public static final XmlOptions DEFAULT_XML_OPTIONS;
static {
this.packagePart = part;
this.packageRel = rel;
}
-
+
+ /**
+ * Creates an POIXMLDocumentPart representing the given package part, relationship and parent
+ * Called by {@link #read(POIXMLFactory, java.util.Map)} when reading in an exisiting file.
+ *
+ * @param parent - Parent part
+ * @param part - The package part that holds xml data represenring this sheet.
+ * @param rel - the relationship of the given package part
+ * @see #read(POIXMLFactory, java.util.Map)
+ */
+ public POIXMLDocumentPart(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel){
+ this.relations = new LinkedList<POIXMLDocumentPart>();
+ this.packagePart = part;
+ this.packageRel = rel;
+ this.parent = parent;
+ }
+
/**
* When you open something like a theme, call this to
* re-base the XML Document onto the core child of the
}
if (!context.containsKey(p)) {
- POIXMLDocumentPart childPart = factory.createDocumentPart(rel, p);
+ POIXMLDocumentPart childPart = factory.createDocumentPart(this, rel, p);
childPart.parent = this;
addRelation(childPart);
if(p != null){
* Create a POIXMLDocumentPart from existing package part and relation. This method is called
* from {@link POIXMLDocument#load(POIXMLFactory)} when parsing a document
*
+ * @param parent parent part
* @param rel the package part relationship
* @param part the PackagePart representing the created instance
* @return A new instance of a POIXMLDocumentPart.
*/
- public abstract POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part);
+ public abstract POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part);
/**
* Create a new POIXMLDocumentPart using the supplied descriptor. This method is used when adding new parts
* @author Yegor Kozlov
*/
public final class XSSFFactory extends POIXMLFactory {
- private static POILogger logger = POILogFactory.getLogger(XSSFFactory.class);
+ private static final POILogger logger = POILogFactory.getLogger(XSSFFactory.class);
private XSSFFactory(){
return inst;
}
- public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){
+ @Override
+ public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){
POIXMLRelation descriptor = XSSFRelation.getInstance(rel.getRelationshipType());
if(descriptor == null || descriptor.getRelationClass() == null){
logger.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());
}
}
+ @Override
public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){
try {
Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
PackagePart hdrPart = doc.getPartById(ref.getId());
HdrDocument hdrDoc = HdrDocument.Factory.parse(hdrPart.getInputStream());
CTHdrFtr hdrFtr = hdrDoc.getHdr();
- XWPFHeader hdr = new XWPFHeader(hdrFtr);
+ XWPFHeader hdr = new XWPFHeader(doc, hdrFtr);
// Assign it
Enum type = ref.getType();
// Get the footer
CTHdrFtrRef ref = sectPr.getFooterReferenceArray(i);
PackagePart ftrPart = doc.getPartById(ref.getId());
- XWPFFooter ftr = new XWPFFooter(
+ XWPFFooter ftr = new XWPFFooter(doc,
FtrDocument.Factory.parse(ftrPart.getInputStream()).getFtr());
// Assign it
shapeTextPath.setString(text);
pict.set(group);
// end watermark paragraph
- return new XWPFParagraph(p, null);
+ return new XWPFParagraph(p, doc);
}
}
* the text of the header or footer.
*/
public List<XWPFParagraph> getParagraphs();
-
-
+
/**
* Return the table(s) that holds the text
* of the IBodyPart, for complex cases
* @param cell
*/
XWPFTableCell getTableCell(CTTc cell);
-
+
+ /**
+ * Return XWPFDocument
+ */
+ public XWPFDocument getXWPFDocument();
+
}
protected String author;
protected StringBuffer text;
- public XWPFComment(CTComment comment)
+ public XWPFComment(CTComment comment, XWPFDocument document)
{
text = new StringBuffer();
id = comment.getId().toString();
for(CTP ctp : comment.getPList())
{
- XWPFParagraph p = new XWPFParagraph(ctp, null);
+ XWPFParagraph p = new XWPFParagraph(ctp, document);
text.append(p.getText());
}
}
String relation = p.getPackageRelationship().getRelationshipType();
if(relation.equals(XWPFRelation.STYLES.getRelation())){
this.styles = (XWPFStyles) p;
- }
- else if(relation.equals(XWPFRelation.NUMBERING.getRelation())){
+ } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())){
this.numbering = (XWPFNumbering) p;
-
- }
- else if(relation.equals(XWPFRelation.FOOTER.getRelation())){
+ } else if (relation.equals(XWPFRelation.FOOTER.getRelation())){
footers.add((XWPFFooter)p);
- }
- else if(relation.equals(XWPFRelation.HEADER.getRelation())){
+ } else if (relation.equals(XWPFRelation.HEADER.getRelation())){
headers.add((XWPFHeader)p);
- }
-
- else if(relation.equals(XWPFRelation.COMMENT.getRelation())){
+ } else if (relation.equals(XWPFRelation.COMMENT.getRelation())){
CommentsDocument cmntdoc = CommentsDocument.Factory.parse(p.getPackagePart().getInputStream());
for(CTComment ctcomment : cmntdoc.getComments().getCommentList()) {
- comments.add(new XWPFComment(ctcomment));
+ comments.add(new XWPFComment(ctcomment, this));
}
- }
- else if(relation.equals(XWPFRelation.SETTINGS.getRelation())){
+ } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())){
settings = (XWPFSettings)p;
}
}
/**
* Create a new CTWorkbook with all values set to default
*/
+ @Override
protected void onDocumentCreate() {
hyperlinks = new ArrayList<XWPFHyperlink>();
comments = new ArrayList<XWPFComment>();
/**
* Get the document's embedded files.
*/
+ @Override
public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
List<PackagePart> embedds = new LinkedList<PackagePart>();
}
return tableRow.getTableCell(cell);
}
+
+ public XWPFDocument getXWPFDocument() {
+ return this;
+ }
}//end class
*/
public final class XWPFFactory extends POIXMLFactory {
- private static POILogger logger = POILogFactory.getLogger(XWPFFactory.class);
+ private static final POILogger logger = POILogFactory.getLogger(XWPFFactory.class);
private XWPFFactory(){
return inst;
}
- public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){
+ @Override
+ public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){
POIXMLRelation descriptor = XWPFRelation.getInstance(rel.getRelationshipType());
if(descriptor == null || descriptor.getRelationClass() == null){
logger.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());
try {
Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
- Constructor<? extends POIXMLDocumentPart> constructor = cls.getDeclaredConstructor(PackagePart.class, PackageRelationship.class);
- return constructor.newInstance(part, rel);
+ try {
+ Constructor<? extends POIXMLDocumentPart> constructor = cls.getDeclaredConstructor(POIXMLDocumentPart.class, PackagePart.class, PackageRelationship.class);
+ return constructor.newInstance(parent, part, rel);
+ } catch (NoSuchMethodException e) {
+ Constructor<? extends POIXMLDocumentPart> constructor = cls.getDeclaredConstructor(PackagePart.class, PackageRelationship.class);
+ return constructor.newInstance(part, rel);
+ }
} catch (Exception e){
throw new POIXMLException(e);
}
}
+ @Override
public POIXMLDocumentPart newDocumentPart(POIXMLRelation descriptor){
try {
Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
import javax.xml.namespace.QName;
+import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.xmlbeans.XmlCursor;
* Sketch of XWPF footer class
*/
public class XWPFFooter extends XWPFHeaderFooter {
- public XWPFFooter() {
- super();
- }
-
-
- public XWPFFooter(CTHdrFtr hdrFtr) throws IOException {
- super(hdrFtr);
+ public XWPFFooter() {
+ super();
+ }
+
+ public XWPFFooter(XWPFDocument doc, CTHdrFtr hdrFtr) throws IOException {
+ super(doc, hdrFtr);
bodyElements = new ArrayList<IBodyElement>();
paragraphs = new ArrayList<XWPFParagraph>();
tables = new ArrayList<XWPFTable>();
getAllPictures();
}
- public XWPFFooter(PackagePart part, PackageRelationship rel) throws IOException {
- super(part, rel);
+ public XWPFFooter(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) throws IOException {
+ super(parent, part, rel);
}
/**
public BodyType getPartType() {
return BodyType.FOOTER;
}
-
}
* Sketch of XWPF header class
*/
public class XWPFHeader extends XWPFHeaderFooter {
-
- public XWPFHeader() {
- super();
- }
-
- public XWPFHeader(PackagePart part, PackageRelationship rel) throws IOException {
- super(part, rel);
+ public XWPFHeader() {
+ super();
+ }
+
+ public XWPFHeader(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) throws IOException {
+ super(parent, part, rel);
}
- public XWPFHeader(CTHdrFtr hdrFtr) throws IOException {
- super(hdrFtr);
+ public XWPFHeader(XWPFDocument doc, CTHdrFtr hdrFtr) throws IOException {
+ super(doc, hdrFtr);
paragraphs = new ArrayList<XWPFParagraph>();
tables = new ArrayList<XWPFTable>();
XmlCursor cursor = headerFooter.newCursor();
protected XWPFDocument document;
protected List<IBodyElement> bodyElements;
- protected XWPFHeaderFooter(CTHdrFtr hdrFtr){
+ protected XWPFHeaderFooter(XWPFDocument doc, CTHdrFtr hdrFtr){
+ if (doc==null) {
+ throw new NullPointerException();
+ }
+
+ document = doc;
headerFooter = hdrFtr;
readHdrFtr();
}
- protected XWPFHeaderFooter() {
- this(CTHdrFtr.Factory.newInstance());
- }
- public XWPFHeaderFooter(PackagePart part, PackageRelationship rel) throws IOException {
- super(part, rel);
+ protected XWPFHeaderFooter() {
+ headerFooter = CTHdrFtr.Factory.newInstance();
+ readHdrFtr();
+ }
+
+
+ public XWPFHeaderFooter(POIXMLDocumentPart parent, PackagePart part, PackageRelationship rel) throws IOException {
+ super(parent, part, rel);
this.document = (XWPFDocument)getParent();
- onDocumentRead();
+
+ if (this.document==null) {
+ throw new NullPointerException();
+ }
+
+ onDocumentRead();
}
@Internal
return tableRow.getTableCell(cell);
}
-
+ public XWPFDocument getXWPFDocument() {
+ if (document!=null) {
+ return document;
+ } else {
+ return (XWPFDocument)getParent();
+ }
+ }
}//end class
* Sketch of XWPF paragraph class
*/
public class XWPFParagraph implements IBodyElement{
- private CTP paragraph;
+ private final CTP paragraph;
protected IBody part;
/** For access to the document's hyperlink, comments, tables etc */
protected XWPFDocument document;
private StringBuffer footnoteText = new StringBuffer();
- public XWPFParagraph(CTP prgrph) {
- this(prgrph, null);
- }
-
-
public XWPFParagraph(CTP prgrph, IBody part) {
this.paragraph = prgrph;
this.part = part;
- // We only care about the document (for comments,
- // hyperlinks etc) if we're attached to the
- // core document
- if(part instanceof XWPFDocument) {
- this.document = (XWPFDocument)part;
+ this.document = part.getXWPFDocument();
+
+ if (document==null) {
+ throw new NullPointerException();
}
-
+
runs = new ArrayList<XWPFRun>();
// Get all our child nodes in order, and process them
public class XWPFTableCell implements IBody {
-
- private CTTc ctTc;
+ private final CTTc ctTc;
protected List<XWPFParagraph> paragraphs = null;
protected List<XWPFTable> tables = null;
protected List<IBodyElement> bodyElements = null;
* @see org.apache.poi.xwpf.usermodel.IBody#getPart()
*/
public IBody getPart() {
- return (IBody) tableRow.getTable().getPart();
+ return tableRow.getTable().getPart();
}
}
return tableRow.getTableCell(cell);
}
+
+ public XWPFDocument getXWPFDocument() {
+ return part.getXWPFDocument();
+ }
}// end class
public TestFactory() {
//
}
- public POIXMLDocumentPart createDocumentPart(PackageRelationship rel, PackagePart part){
+ public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackageRelationship rel, PackagePart part){
return new POIXMLDocumentPart(part, rel);
}
CTText t3 = ctR3.addNewT();
t3.setStringValue("Second paragraph for the footer");
- XWPFParagraph p1 = new XWPFParagraph(ctP1);
+ XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);
XWPFParagraph[] pars = new XWPFParagraph[1];
pars[0] = p1;
- XWPFParagraph p2 = new XWPFParagraph(ctP2);
- XWPFParagraph p3 = new XWPFParagraph(ctP3, null);
+ XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
+ XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);
XWPFParagraph[] pars2 = new XWPFParagraph[2];
pars2[0] = p2;
pars2[1] = p3;
assertNotNull(policy.getDefaultFooter());
// ....and that the footer object captured above contains two
// paragraphs of text.
- assertEquals(footer.getParagraphs().size(), 2);
+ assertEquals(2, footer.getParagraphs().size());
// As an additional check, recover the defauls footer and
// make sure that it contains two paragraphs of text and that
paras[i++] = p;
}
- assertEquals(paras.length, 2);
- assertEquals(paras[0].getText(), "First paragraph for the footer");
- assertEquals(paras[1].getText(), "Second paragraph for the footer");
+ assertEquals(2, paras.length);
+ assertEquals("First paragraph for the footer", paras[0].getText());
+ assertEquals("Second paragraph for the footer", paras[1].getText());
}
public void testSetWatermark() {
}
public void testConstructor() {
+ XWPFDocument doc = new XWPFDocument();
CTTbl ctTable = CTTbl.Factory.newInstance();
- XWPFTable xtab = new XWPFTable(ctTable, null);
+ XWPFTable xtab = new XWPFTable(ctTable, doc);
assertNotNull(xtab);
assertEquals(1, ctTable.sizeOfTrArray());
assertEquals(1, ctTable.getTrArray(0).sizeOfTcArray());
assertNotNull(ctTable.getTrArray(0).getTcArray(0).getPArray(0));
ctTable = CTTbl.Factory.newInstance();
- xtab = new XWPFTable(ctTable, null, 3, 2);
+ xtab = new XWPFTable(ctTable, doc, 3, 2);
assertNotNull(xtab);
assertEquals(3, ctTable.sizeOfTrArray());
assertEquals(2, ctTable.getTrArray(0).sizeOfTcArray());
public void testGetText() {
+ XWPFDocument doc = new XWPFDocument();
CTTbl table = CTTbl.Factory.newInstance();
CTRow row = table.addNewTr();
CTTc cell = row.addNewTc();
CTText text = run.addNewT();
text.setStringValue("finally I can write!");
- XWPFTable xtab = new XWPFTable(table, null);
+ XWPFTable xtab = new XWPFTable(table, doc);
assertEquals("finally I can write!\n", xtab.getText());
}
public void testCreateRow() {
+ XWPFDocument doc = new XWPFDocument();
+
CTTbl table = CTTbl.Factory.newInstance();
CTRow r1 = table.addNewTr();
r1.addNewTc().addNewP();
r3.addNewTc().addNewP();
r3.addNewTc().addNewP();
- XWPFTable xtab = new XWPFTable(table, null);
+ XWPFTable xtab = new XWPFTable(table, doc);
assertEquals(3, xtab.getNumberOfRows());
assertNotNull(xtab.getRow(2));
assertEquals(2, table.getTrArray(0).sizeOfTcArray());
//check creation of first row
- xtab = new XWPFTable(CTTbl.Factory.newInstance(), null);
+ xtab = new XWPFTable(CTTbl.Factory.newInstance(), doc);
assertEquals(1, xtab.getCTTbl().getTrArray(0).sizeOfTcArray());
}
public void testSetGetWidth() {
+ XWPFDocument doc = new XWPFDocument();
+
CTTbl table = CTTbl.Factory.newInstance();
table.addNewTblPr().addNewTblW().setW(new BigInteger("1000"));
- XWPFTable xtab = new XWPFTable(table, null);
+ XWPFTable xtab = new XWPFTable(table, doc);
assertEquals(1000, xtab.getWidth());
}
public void testSetGetHeight() {
+ XWPFDocument doc = new XWPFDocument();
+
CTTbl table = CTTbl.Factory.newInstance();
- XWPFTable xtab = new XWPFTable(table, null);
+ XWPFTable xtab = new XWPFTable(table, doc);
XWPFTableRow row = xtab.createRow();
row.setHeight(20);
assertEquals(20, row.getHeight());