use of org.apache.poi.xwpf.usermodel.XWPFRelation in project poi by apache.
the class XWPFHeaderFooterPolicy method createFooter.
/**
* Creates a new footer of the specified type, to which the
* supplied (and previously unattached!) paragraphs are
* added to.
*/
public XWPFFooter createFooter(Enum type, XWPFParagraph[] pars) {
XWPFFooter footer = getFooter(type);
if (footer == null) {
FtrDocument ftrDoc = FtrDocument.Factory.newInstance();
XWPFRelation relation = XWPFRelation.FOOTER;
int i = getRelationIndex(relation);
XWPFFooter wrapper = (XWPFFooter) doc.createRelationship(relation, XWPFFactory.getInstance(), i);
wrapper.setXWPFDocument(doc);
String pStyle = "Footer";
CTHdrFtr ftr = buildFtr(type, pStyle, wrapper, pars);
wrapper.setHeaderFooter(ftr);
ftrDoc.setFtr(ftr);
assignFooter(wrapper, type);
footer = wrapper;
}
return footer;
}
use of org.apache.poi.xwpf.usermodel.XWPFRelation in project tika by apache.
the class XWPFEventBasedWordExtractor method handleDocumentPart.
private void handleDocumentPart(PackagePart documentPart, StringBuilder sb) throws IOException, SAXException {
//load the numbering/list manager and styles from the main document part
XWPFNumbering numbering = loadNumbering(documentPart);
XWPFListManager xwpfListManager = new XWPFListManager(numbering);
//headers
try {
PackageRelationshipCollection headersPRC = documentPart.getRelationshipsByType(XWPFRelation.HEADER.getRelation());
if (headersPRC != null) {
for (int i = 0; i < headersPRC.size(); i++) {
PackagePart header = documentPart.getRelatedPart(headersPRC.getRelationship(i));
handlePart(header, xwpfListManager, sb);
}
}
} catch (InvalidFormatException e) {
LOG.warn("Invalid format", e);
}
//main document
handlePart(documentPart, xwpfListManager, sb);
//for now, just dump other components at end
for (XWPFRelation rel : new XWPFRelation[] { XWPFRelation.FOOTNOTE, XWPFRelation.COMMENT, XWPFRelation.FOOTER, XWPFRelation.ENDNOTE }) {
try {
PackageRelationshipCollection prc = documentPart.getRelationshipsByType(rel.getRelation());
if (prc != null) {
for (int i = 0; i < prc.size(); i++) {
PackagePart packagePart = documentPart.getRelatedPart(prc.getRelationship(i));
handlePart(packagePart, xwpfListManager, sb);
}
}
} catch (InvalidFormatException e) {
LOG.warn("Invalid format", e);
}
}
}
use of org.apache.poi.xwpf.usermodel.XWPFRelation in project poi by apache.
the class XWPFHeaderFooterPolicy method createHeader.
/**
* Creates a new header of the specified type, to which the
* supplied (and previously unattached!) paragraphs are
* added to.
*/
public XWPFHeader createHeader(Enum type, XWPFParagraph[] pars) {
XWPFHeader header = getHeader(type);
if (header == null) {
HdrDocument hdrDoc = HdrDocument.Factory.newInstance();
XWPFRelation relation = XWPFRelation.HEADER;
int i = getRelationIndex(relation);
XWPFHeader wrapper = (XWPFHeader) doc.createRelationship(relation, XWPFFactory.getInstance(), i);
wrapper.setXWPFDocument(doc);
String pStyle = "Header";
CTHdrFtr hdr = buildHdr(type, pStyle, wrapper, pars);
wrapper.setHeaderFooter(hdr);
hdrDoc.setHdr(hdr);
assignHeader(wrapper, type);
header = wrapper;
}
return header;
}
use of org.apache.poi.xwpf.usermodel.XWPFRelation in project poi by apache.
the class ExtractorFactory method createExtractor.
/**
* Tries to determine the actual type of file and produces a matching text-extractor for it.
*
* @param pkg An {@link OPCPackage}.
* @return A {@link POIXMLTextExtractor} for the given file.
* @throws IOException If an error occurs while reading the file
* @throws OpenXML4JException If an error parsing the OpenXML file format is found.
* @throws XmlException If an XML parsing error occurs.
* @throws IllegalArgumentException If no matching file type could be found.
*/
public static POIXMLTextExtractor createExtractor(OPCPackage pkg) throws IOException, OpenXML4JException, XmlException {
try {
// Check for the normal Office core document
PackageRelationshipCollection core;
core = pkg.getRelationshipsByType(CORE_DOCUMENT_REL);
// If nothing was found, try some of the other OOXML-based core types
if (core.size() == 0) {
// Could it be an OOXML-Strict one?
core = pkg.getRelationshipsByType(STRICT_DOCUMENT_REL);
}
if (core.size() == 0) {
// Could it be a visio one?
core = pkg.getRelationshipsByType(VISIO_DOCUMENT_REL);
if (core.size() == 1)
return new XDGFVisioExtractor(pkg);
}
// Should just be a single core document, complain if not
if (core.size() != 1) {
throw new IllegalArgumentException("Invalid OOXML Package received - expected 1 core document, found " + core.size());
}
// Grab the core document part, and try to identify from that
final PackagePart corePart = pkg.getPart(core.getRelationship(0));
final String contentType = corePart.getContentType();
// Is it XSSF?
for (XSSFRelation rel : XSSFExcelExtractor.SUPPORTED_TYPES) {
if (rel.getContentType().equals(contentType)) {
if (getPreferEventExtractor()) {
return new XSSFEventBasedExcelExtractor(pkg);
}
return new XSSFExcelExtractor(pkg);
}
}
// Is it XWPF?
for (XWPFRelation rel : XWPFWordExtractor.SUPPORTED_TYPES) {
if (rel.getContentType().equals(contentType)) {
return new XWPFWordExtractor(pkg);
}
}
// Is it XSLF?
for (XSLFRelation rel : XSLFPowerPointExtractor.SUPPORTED_TYPES) {
if (rel.getContentType().equals(contentType)) {
return new XSLFPowerPointExtractor(pkg);
}
}
// special handling for SlideShow-Theme-files,
if (XSLFRelation.THEME_MANAGER.getContentType().equals(contentType)) {
return new XSLFPowerPointExtractor(new XSLFSlideShow(pkg));
}
// How about xlsb?
for (XSSFRelation rel : XSSFBEventBasedExcelExtractor.SUPPORTED_TYPES) {
if (rel.getContentType().equals(contentType)) {
return new XSSFBEventBasedExcelExtractor(pkg);
}
}
throw new IllegalArgumentException("No supported documents found in the OOXML package (found " + contentType + ")");
} catch (IOException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
throw e;
} catch (OpenXML4JException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
throw e;
} catch (XmlException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
throw e;
} catch (RuntimeException e) {
// ensure that we close the package again if there is an error opening it, however
// we need to revert the package to not re-write the file via close(), which is very likely not wanted for a TextExtractor!
pkg.revert();
throw e;
}
}
use of org.apache.poi.xwpf.usermodel.XWPFRelation in project tika by apache.
the class OOXMLExtractorFactory method trySXWPF.
private static POIXMLTextExtractor trySXWPF(OPCPackage pkg) throws XmlException, OpenXML4JException, IOException {
PackageRelationshipCollection packageRelationshipCollection = pkg.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
if (packageRelationshipCollection.size() == 0) {
packageRelationshipCollection = pkg.getRelationshipsByType("http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument");
}
if (packageRelationshipCollection.size() == 0) {
return null;
}
PackagePart corePart = pkg.getPart(packageRelationshipCollection.getRelationship(0));
String targetContentType = corePart.getContentType();
for (XWPFRelation relation : XWPFWordExtractor.SUPPORTED_TYPES) {
if (targetContentType.equals(relation.getContentType())) {
return new XWPFEventBasedWordExtractor(pkg);
}
}
return null;
}
Aggregations