Search in sources :

Example 6 with InvalidFormatException

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

the class POIXMLDocumentPart method getNextPartNumber.

/**
     * Identifies the next available part number for a part of the given type,
     *  if possible, otherwise -1 if none are available.
     * The found (valid) index can then be safely given to
     *  {@link #createRelationship(POIXMLRelation, POIXMLFactory, int)} or
     *  {@link #createRelationship(POIXMLRelation, POIXMLFactory, int, boolean)}
     *  without naming clashes.
     * If parts with other types are already claiming a name for this relationship
     *  type (eg a {@link XSSFRelation#CHART} using the drawing part namespace 
     *  normally used by {@link XSSFRelation#DRAWINGS}), those will be considered
     *  when finding the next spare number.
     *
     * @param descriptor The relationship type to find the part number for
     * @param minIdx The minimum free index to assign, use -1 for any
     * @return The next free part number, or -1 if none available
     */
protected final int getNextPartNumber(POIXMLRelation descriptor, int minIdx) {
    OPCPackage pkg = packagePart.getPackage();
    try {
        String name = descriptor.getDefaultFileName();
        if (name.equals(descriptor.getFileName(9999))) {
            // Non-index based, check if default is free
            PackagePartName ppName = PackagingURIHelper.createPartName(name);
            if (pkg.containPart(ppName)) {
                // Default name already taken, not index based, nothing free
                return -1;
            } else {
                // Default name free
                return 0;
            }
        }
        // Default to searching from 1, unless they asked for 0+
        int idx = (minIdx < 0) ? 1 : minIdx;
        int maxIdx = minIdx + pkg.getParts().size();
        while (idx <= maxIdx) {
            name = descriptor.getFileName(idx);
            PackagePartName ppName = PackagingURIHelper.createPartName(name);
            if (!pkg.containPart(ppName)) {
                return idx;
            }
            idx++;
        }
    } catch (InvalidFormatException e) {
        // Give a general wrapped exception for the problem
        throw new POIXMLException(e);
    }
    return -1;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 7 with InvalidFormatException

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

the class POIXMLProperties method commit.

/**
     * Commit changes to the underlying OPC package
     * 
     * @throws IOException if the properties can't be saved
     * @throws POIXMLException if the properties are erroneous
     */
public void commit() throws IOException {
    if (extPart == null && !NEW_EXT_INSTANCE.toString().equals(ext.props.toString())) {
        try {
            PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/app.xml");
            pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties");
            extPart = pkg.createPart(prtname, "application/vnd.openxmlformats-officedocument.extended-properties+xml");
        } catch (InvalidFormatException e) {
            throw new POIXMLException(e);
        }
    }
    if (custPart == null && !NEW_CUST_INSTANCE.toString().equals(cust.props.toString())) {
        try {
            PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/custom.xml");
            pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties");
            custPart = pkg.createPart(prtname, "application/vnd.openxmlformats-officedocument.custom-properties+xml");
        } catch (InvalidFormatException e) {
            throw new POIXMLException(e);
        }
    }
    if (extPart != null) {
        OutputStream out = extPart.getOutputStream();
        if (extPart.getSize() > 0) {
            extPart.clear();
        }
        ext.props.save(out, DEFAULT_XML_OPTIONS);
        out.close();
    }
    if (custPart != null) {
        OutputStream out = custPart.getOutputStream();
        cust.props.save(out, DEFAULT_XML_OPTIONS);
        out.close();
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) OutputStream(java.io.OutputStream) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 8 with InvalidFormatException

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

the class PackagingURIHelper method getRelationshipPartName.

/**
	 * Build a part name where the relationship should be stored ((ex
	 * /word/document.xml -> /word/_rels/document.xml.rels)
	 *
	 * @param partName
	 *            Source part URI
	 * @return the full path (as URI) of the relation file
	 * @throws InvalidOperationException
	 *             Throws if the specified URI is a relationshp part.
	 */
public static PackagePartName getRelationshipPartName(PackagePartName partName) {
    if (partName == null)
        throw new IllegalArgumentException("partName");
    if (PackagingURIHelper.PACKAGE_ROOT_URI.getPath().equals(partName.getURI().getPath()))
        return PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_PART_NAME;
    if (partName.isRelationshipPartURI())
        throw new InvalidOperationException("Can't be a relationship part");
    String fullPath = partName.getURI().getPath();
    String filename = getFilename(partName.getURI());
    fullPath = fullPath.substring(0, fullPath.length() - filename.length());
    fullPath = combine(fullPath, PackagingURIHelper.RELATIONSHIP_PART_SEGMENT_NAME);
    fullPath = combine(fullPath, filename);
    fullPath = fullPath + PackagingURIHelper.RELATIONSHIP_PART_EXTENSION_NAME;
    PackagePartName retPartName;
    try {
        retPartName = createPartName(fullPath);
    } catch (InvalidFormatException e) {
        // case of return null:
        return null;
    }
    return retPartName;
}
Also used : InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 9 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 10 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)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)72 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