Search in sources :

Example 36 with InvalidFormatException

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

the class PackagePropertiesUnmarshaller method checkElementForOPCCompliance.

/* OPC Compliance methods */
/**
	 * Check the element for the following OPC compliance rules:
	 * <p>
	 * Rule M4.2: A format consumer shall consider the use of the Markup
	 * Compatibility namespace to be an error.
	 * </p><p>
	 * Rule M4.3: Producers shall not create a document element that contains
	 * refinements to the Dublin Core elements, except for the two specified in
	 * the schema: <dcterms:created> and <dcterms:modified> Consumers shall
	 * consider a document element that violates this constraint to be an error.
	 * </p><p>
	 * Rule M4.4: Producers shall not create a document element that contains
	 * the xml:lang attribute. Consumers shall consider a document element that
	 * violates this constraint to be an error.
	 *  </p><p>
	 * Rule M4.5: Producers shall not create a document element that contains
	 * the xsi:type attribute, except for a <dcterms:created> or
	 * <dcterms:modified> element where the xsi:type attribute shall be present
	 * and shall hold the value dcterms:W3CDTF, where dcterms is the namespace
	 * prefix of the Dublin Core namespace. Consumers shall consider a document
	 * element that violates this constraint to be an error.
	 * </p>
	 */
public void checkElementForOPCCompliance(Element el) throws InvalidFormatException {
    // Check the current element
    NamedNodeMap namedNodeMap = el.getAttributes();
    int namedNodeCount = namedNodeMap.getLength();
    for (int i = 0; i < namedNodeCount; i++) {
        Attr attr = (Attr) namedNodeMap.item(0);
        if (attr.getNamespaceURI().equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
            // Rule M4.2
            if (attr.getValue().equals(PackageNamespaces.MARKUP_COMPATIBILITY))
                throw new InvalidFormatException("OPC Compliance error [M4.2]: A format consumer shall consider the use of the Markup Compatibility namespace to be an error.");
        }
    }
    // Rule M4.3
    String elName = el.getLocalName();
    if (el.getNamespaceURI().equals(PackageProperties.NAMESPACE_DCTERMS))
        if (!(elName.equals(KEYWORD_CREATED) || elName.equals(KEYWORD_MODIFIED)))
            throw new InvalidFormatException("OPC Compliance error [M4.3]: Producers shall not create a document element that contains refinements to the Dublin Core elements, except for the two specified in the schema: <dcterms:created> and <dcterms:modified> Consumers shall consider a document element that violates this constraint to be an error.");
    // Rule M4.4
    if (el.getAttributeNodeNS(XMLConstants.XML_NS_URI, "lang") != null)
        throw new InvalidFormatException("OPC Compliance error [M4.4]: Producers shall not create a document element that contains the xml:lang attribute. Consumers shall consider a document element that violates this constraint to be an error.");
    // Rule M4.5
    if (el.getNamespaceURI().equals(PackageProperties.NAMESPACE_DCTERMS)) {
        // DCTerms namespace only use with 'created' and 'modified' elements
        if (!(elName.equals(KEYWORD_CREATED) || elName.equals(KEYWORD_MODIFIED)))
            throw new InvalidFormatException("Namespace error : " + elName + " shouldn't have the following naemspace -> " + PackageProperties.NAMESPACE_DCTERMS);
        // Check for the 'xsi:type' attribute
        Attr typeAtt = el.getAttributeNodeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type");
        if (typeAtt == null)
            throw new InvalidFormatException("The element '" + elName + "' must have the 'xsi:type' attribute present !");
        // Check for the attribute value => 'dcterms:W3CDTF'
        if (!typeAtt.getValue().equals(el.getPrefix() + ":W3CDTF"))
            throw new InvalidFormatException("The element '" + elName + "' must have the 'xsi:type' attribute with the value '" + el.getPrefix() + ":W3CDTF', but had '" + typeAtt.getValue() + "' !");
    }
    // Check its children
    NodeList childElements = el.getElementsByTagName("*");
    int childElementCount = childElements.getLength();
    for (int i = 0; i < childElementCount; i++) checkElementForOPCCompliance((Element) childElements.item(i));
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Attr(org.w3c.dom.Attr)

Example 37 with InvalidFormatException

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

the class OOXMLURIDereferencer method findPart.

private PackagePart findPart(URI uri) {
    LOG.log(POILogger.DEBUG, "dereference", uri);
    String path = uri.getPath();
    if (path == null || "".equals(path)) {
        LOG.log(POILogger.DEBUG, "illegal part name (expected)", uri);
        return null;
    }
    PackagePartName ppn;
    try {
        ppn = PackagingURIHelper.createPartName(path);
    } catch (InvalidFormatException e) {
        LOG.log(POILogger.WARN, "illegal part name (not expected)", uri);
        return null;
    }
    return signatureConfig.getOpcPackage().getPart(ppn);
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 38 with InvalidFormatException

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

the class ContentTypeManager method removeContentType.

/**
     * <p>
	 * Delete a content type based on the specified part name. If the specified
	 * part name is register with an override content type, then this content
	 * type is remove, else the content type is remove in the default content
	 * type list if it exists and if no part is associated with it yet.
	 * </p><p>
	 * Check rule M2.4: The package implementer shall require that the Content
	 * Types stream contain one of the following for every part in the package:
	 * One matching Default element One matching Override element Both a
	 * matching Default element and a matching Override element, in which case
	 * the Override element takes precedence.
	 * </p>
	 * @param partName
	 *            The part URI associated with the override content type to
	 *            delete.
	 * @exception InvalidOperationException
	 *                Throws if
	 */
public void removeContentType(PackagePartName partName) throws InvalidOperationException {
    if (partName == null)
        throw new IllegalArgumentException("partName");
    /* Override content type */
    if (this.overrideContentType != null && (this.overrideContentType.get(partName) != null)) {
        // Remove the override definition for the specified part.
        this.overrideContentType.remove(partName);
        return;
    }
    /* Default content type */
    String extensionToDelete = partName.getExtension();
    boolean deleteDefaultContentTypeFlag = true;
    if (this.container != null) {
        try {
            for (PackagePart part : this.container.getParts()) {
                if (!part.getPartName().equals(partName) && part.getPartName().getExtension().equalsIgnoreCase(extensionToDelete)) {
                    deleteDefaultContentTypeFlag = false;
                    break;
                }
            }
        } catch (InvalidFormatException e) {
            throw new InvalidOperationException(e.getMessage());
        }
    }
    // Remove the default content type, no other part use this content type.
    if (deleteDefaultContentTypeFlag) {
        this.defaultContentType.remove(extensionToDelete);
    }
    /*
		 * Check rule 2.4: The package implementer shall require that the
		 * Content Types stream contain one of the following for every part in
		 * the package: One matching Default element One matching Override
		 * element Both a matching Default element and a matching Override
		 * element, in which case the Override element takes precedence.
		 */
    if (this.container != null) {
        try {
            for (PackagePart part : this.container.getParts()) {
                if (!part.getPartName().equals(partName) && this.getContentType(part.getPartName()) == null)
                    throw new InvalidOperationException("Rule M2.4 is not respected: Nor a default element or override element is associated with the part: " + part.getPartName().getName());
            }
        } catch (InvalidFormatException e) {
            throw new InvalidOperationException(e.getMessage());
        }
    }
}
Also used : InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 39 with InvalidFormatException

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

the class ZipPackage method getPartsImpl.

/**
     * Retrieves the parts from this package. We assume that the package has not
     * been yet inspect to retrieve all the parts, this method will open the
     * archive and look for all parts contain inside it. If the package part
     * list is not empty, it will be emptied.
     *
     * @return All parts contain in this package.
     * @throws InvalidFormatException if the package is not valid.
     */
@Override
protected PackagePart[] getPartsImpl() throws InvalidFormatException {
    if (this.partList == null) {
        // The package has just been created, we create an empty part
        // list.
        this.partList = new PackagePartCollection();
    }
    if (this.zipArchive == null) {
        return this.partList.sortedValues().toArray(new PackagePart[this.partList.size()]);
    }
    // First we need to parse the content type part
    Enumeration<? extends ZipEntry> entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.getName().equalsIgnoreCase(ContentTypeManager.CONTENT_TYPES_PART_NAME)) {
            try {
                this.contentTypeManager = new ZipContentTypeManager(getZipArchive().getInputStream(entry), this);
            } catch (IOException e) {
                throw new InvalidFormatException(e.getMessage(), e);
            }
            break;
        }
    }
    // At this point, we should have loaded the content type part
    if (this.contentTypeManager == null) {
        // Is it a different Zip-based format?
        int numEntries = 0;
        boolean hasMimetype = false;
        boolean hasSettingsXML = false;
        entries = this.zipArchive.getEntries();
        while (entries.hasMoreElements()) {
            final ZipEntry entry = entries.nextElement();
            final String name = entry.getName();
            if (MIMETYPE.equals(name)) {
                hasMimetype = true;
            }
            if (SETTINGS_XML.equals(name)) {
                hasSettingsXML = true;
            }
            numEntries++;
        }
        if (hasMimetype && hasSettingsXML) {
            throw new ODFNotOfficeXmlFileException("The supplied data appears to be in ODF (Open Document) Format. " + "Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit");
        }
        if (numEntries == 0) {
            throw new NotOfficeXmlFileException("No valid entries or contents found, this is not a valid OOXML " + "(Office Open XML) file");
        }
        // Fallback exception
        throw new InvalidFormatException("Package should contain a content type part [M1.13]");
    }
    // Now create all the relationships
    // (Need to create relationships before other
    //  parts, otherwise we might create a part before
    //  its relationship exists, and then it won't tie up)
    entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        PackagePartName partName = buildPartName(entry);
        if (partName == null) {
            continue;
        }
        // Only proceed for Relationships at this stage
        String contentType = contentTypeManager.getContentType(partName);
        if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
            try {
                PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
                partList.put(partName, part);
            } catch (InvalidOperationException e) {
                throw new InvalidFormatException(e.getMessage(), e);
            }
        }
    }
    // Then we can go through all the other parts
    entries = this.zipArchive.getEntries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        PackagePartName partName = buildPartName(entry);
        if (partName == null) {
            continue;
        }
        String contentType = contentTypeManager.getContentType(partName);
        if (contentType != null && contentType.equals(ContentTypes.RELATIONSHIPS_PART)) {
        // Already handled
        } else if (contentType != null) {
            try {
                PackagePart part = new ZipPackagePart(this, entry, partName, contentType);
                partList.put(partName, part);
            } catch (InvalidOperationException e) {
                throw new InvalidFormatException(e.getMessage(), e);
            }
        } else {
            throw new InvalidFormatException("The part " + partName.getURI().getPath() + " does not have any content type ! Rule: Package require content types when retrieving a part from a package. [M.1.14]");
        }
    }
    return partList.sortedValues().toArray(new PackagePart[partList.size()]);
}
Also used : ODFNotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException) NotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ZipContentTypeManager(org.apache.poi.openxml4j.opc.internal.ZipContentTypeManager) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) ODFNotOfficeXmlFileException(org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException)

Example 40 with InvalidFormatException

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

the class PackagePartName method checkPCharCompliance.

/**
	 * Throws an exception if a segment any characters other than pchar
	 * characters. [M1.6]
	 *
	 * Throws an exception if a segment contain percent-encoded forward slash
	 * ('/'), or backward slash ('\') characters. [M1.7]
	 *
	 * Throws an exception if a segment contain percent-encoded unreserved
	 * characters. [M1.8]
	 *
	 * @param segment
	 *            The segment to check
	 */
private static void checkPCharCompliance(String segment) throws InvalidFormatException {
    boolean errorFlag;
    final int length = segment.length();
    for (int i = 0; i < length; ++i) {
        char c = segment.charAt(i);
        errorFlag = true;
        // Check for digit or letter
        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
            errorFlag = false;
        } else {
            // Check "-", ".", "_", "~"
            for (int j = 0; j < RFC3986_PCHAR_UNRESERVED_SUP.length; ++j) {
                if (c == RFC3986_PCHAR_UNRESERVED_SUP[j].charAt(0)) {
                    errorFlag = false;
                    break;
                }
            }
            // Check ":", "@"
            for (int j = 0; errorFlag && j < RFC3986_PCHAR_AUTHORIZED_SUP.length; ++j) {
                if (c == RFC3986_PCHAR_AUTHORIZED_SUP[j].charAt(0)) {
                    errorFlag = false;
                }
            }
            // Check "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="
            for (int j = 0; errorFlag && j < RFC3986_PCHAR_SUB_DELIMS.length; ++j) {
                if (c == RFC3986_PCHAR_SUB_DELIMS[j].charAt(0)) {
                    errorFlag = false;
                }
            }
        }
        if (errorFlag && c == '%') {
            // now ( '%' HEXDIGIT HEXDIGIT)
            if (((length - i) < 2)) {
                throw new InvalidFormatException("The segment " + segment + " contain invalid encoded character !");
            }
            // If not percent encoded character error occur then reset the
            // flag -> the character is valid
            errorFlag = false;
            // Decode the encoded character
            char decodedChar = (char) Integer.parseInt(segment.substring(i + 1, i + 3), 16);
            i += 2;
            /* Check rule M1.7 */
            if (decodedChar == '/' || decodedChar == '\\')
                throw new InvalidFormatException("A segment shall not contain percent-encoded forward slash ('/'), or backward slash ('\') characters. [M1.7]");
            // Check for unreserved character like define in RFC3986
            if ((decodedChar >= 'A' && decodedChar <= 'Z') || (decodedChar >= 'a' && decodedChar <= 'z') || (decodedChar >= '0' && decodedChar <= '9'))
                errorFlag = true;
            // Check for unreserved character "-", ".", "_", "~"
            for (int j = 0; !errorFlag && j < RFC3986_PCHAR_UNRESERVED_SUP.length; ++j) {
                if (c == RFC3986_PCHAR_UNRESERVED_SUP[j].charAt(0)) {
                    errorFlag = true;
                    break;
                }
            }
            if (errorFlag)
                throw new InvalidFormatException("A segment shall not contain percent-encoded unreserved characters. [M1.8]");
        }
        if (errorFlag)
            throw new InvalidFormatException("A segment shall not hold any characters other than pchar characters. [M1.6]");
    }
}
Also used : InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

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