Search in sources :

Example 11 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class ContentTypeManager method parseContentTypesFile.

/**
	 * Parse the content types part.
	 *
	 * @throws InvalidFormatException
	 *             Throws if the content type doesn't exist or the XML format is
	 *             invalid.
	 */
private void parseContentTypesFile(InputStream in) throws InvalidFormatException {
    try {
        Document xmlContentTypetDoc = DocumentHelper.readDocument(in);
        // Default content types
        NodeList defaultTypes = xmlContentTypetDoc.getDocumentElement().getElementsByTagNameNS(TYPES_NAMESPACE_URI, DEFAULT_TAG_NAME);
        int defaultTypeCount = defaultTypes.getLength();
        for (int i = 0; i < defaultTypeCount; i++) {
            Element element = (Element) defaultTypes.item(i);
            String extension = element.getAttribute(EXTENSION_ATTRIBUTE_NAME);
            String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME);
            addDefaultContentType(extension, contentType);
        }
        // Overriden content types
        NodeList overrideTypes = xmlContentTypetDoc.getDocumentElement().getElementsByTagNameNS(TYPES_NAMESPACE_URI, OVERRIDE_TAG_NAME);
        int overrideTypeCount = overrideTypes.getLength();
        for (int i = 0; i < overrideTypeCount; i++) {
            Element element = (Element) overrideTypes.item(i);
            URI uri = new URI(element.getAttribute(PART_NAME_ATTRIBUTE_NAME));
            PackagePartName partName = PackagingURIHelper.createPartName(uri);
            String contentType = element.getAttribute(CONTENT_TYPE_ATTRIBUTE_NAME);
            addOverrideContentType(partName, contentType);
        }
    } catch (URISyntaxException urie) {
        throw new InvalidFormatException(urie.getMessage());
    } catch (SAXException e) {
        throw new InvalidFormatException(e.getMessage());
    } catch (IOException e) {
        throw new InvalidFormatException(e.getMessage());
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Document(org.w3c.dom.Document) URI(java.net.URI) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) SAXException(org.xml.sax.SAXException)

Example 12 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class XSLFSlideShow method getSlideComments.

/**
	 * Returns all the comments for the given slide
	 */
@Internal
public CTCommentList getSlideComments(CTSlideIdListEntry slide) throws IOException, XmlException {
    PackageRelationshipCollection commentRels;
    PackagePart slidePart = getSlidePart(slide);
    try {
        commentRels = slidePart.getRelationshipsByType(XSLFRelation.COMMENTS.getRelation());
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
    if (commentRels.size() == 0) {
        // No comments for this slide
        return null;
    }
    if (commentRels.size() > 1) {
        throw new IllegalStateException("Expecting 0 or 1 comments for a slide, but found " + commentRels.size());
    }
    try {
        PackagePart cPart = slidePart.getRelatedPart(commentRels.getRelationship(0));
        CmLstDocument commDoc = CmLstDocument.Factory.parse(cPart.getInputStream(), DEFAULT_XML_OPTIONS);
        return commDoc.getCmLst();
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
}
Also used : CmLstDocument(org.openxmlformats.schemas.presentationml.x2006.main.CmLstDocument) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Internal(org.apache.poi.util.Internal)

Example 13 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class XSLFSlideShow method getNodesPart.

/**
	 * Gets the PackagePart of the notes for the
	 *  given slide, or null if there isn't one.
	 */
public PackagePart getNodesPart(CTSlideIdListEntry parentSlide) throws IOException, XmlException {
    PackageRelationshipCollection notes;
    PackagePart slidePart = getSlidePart(parentSlide);
    try {
        notes = slidePart.getRelationshipsByType(XSLFRelation.NOTES.getRelation());
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
    if (notes.size() == 0) {
        // No notes for this slide
        return null;
    }
    if (notes.size() > 1) {
        throw new IllegalStateException("Expecting 0 or 1 notes for a slide, but found " + notes.size());
    }
    try {
        return slidePart.getRelatedPart(notes.getRelationship(0));
    } catch (InvalidFormatException e) {
        throw new IllegalStateException(e);
    }
}
Also used : PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 14 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class XSSFSheet method initHyperlinks.

/**
     * Read hyperlink relations, link them with CTHyperlink beans in this worksheet
     * and initialize the internal array of XSSFHyperlink objects
     */
private void initHyperlinks() {
    hyperlinks = new ArrayList<XSSFHyperlink>();
    if (!worksheet.isSetHyperlinks()) {
        return;
    }
    try {
        PackageRelationshipCollection hyperRels = getPackagePart().getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation());
        // Turn each one into a XSSFHyperlink
        for (CTHyperlink hyperlink : worksheet.getHyperlinks().getHyperlinkArray()) {
            PackageRelationship hyperRel = null;
            if (hyperlink.getId() != null) {
                hyperRel = hyperRels.getRelationshipByID(hyperlink.getId());
            }
            hyperlinks.add(new XSSFHyperlink(hyperlink, hyperRel));
        }
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) POIXMLException(org.apache.poi.POIXMLException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 15 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class XSSFWorkbook method setVBAProject.

/**
     * Adds a vbaProject.bin file to the workbook.  This will change the workbook
     * type if necessary.
     *
     * @throws IOException
     */
public void setVBAProject(InputStream vbaProjectStream) throws IOException {
    if (!isMacroEnabled()) {
        setWorkbookType(XSSFWorkbookType.XLSM);
    }
    PackagePartName ppName;
    try {
        ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    OPCPackage opc = getPackage();
    OutputStream outputStream;
    if (!opc.containPart(ppName)) {
        POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
        outputStream = relationship.getPackagePart().getOutputStream();
    } else {
        PackagePart part = opc.getPart(ppName);
        outputStream = part.getOutputStream();
    }
    try {
        IOUtils.copy(vbaProjectStream, outputStream);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)71 IOException (java.io.IOException)24 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)22 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)18 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 InputStream (java.io.InputStream)12 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)11 Test (org.junit.Test)10 URI (java.net.URI)9 POIXMLException (org.apache.poi.POIXMLException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ArrayList (java.util.ArrayList)7 TikaException (org.apache.tika.exception.TikaException)7 XmlException (org.apache.xmlbeans.XmlException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 FileNotFoundException (java.io.FileNotFoundException)6 HashMap (java.util.HashMap)6 Workbook (org.apache.poi.ss.usermodel.Workbook)6