use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class SignatureInfo method getSignatureParts.
/**
* @return a signature part for each signature document.
* the parts can be validated independently.
*/
public Iterable<SignaturePart> getSignatureParts() {
signatureConfig.init(true);
return new Iterable<SignaturePart>() {
public Iterator<SignaturePart> iterator() {
return new Iterator<SignaturePart>() {
OPCPackage pkg = signatureConfig.getOpcPackage();
Iterator<PackageRelationship> sigOrigRels = pkg.getRelationshipsByType(PackageRelationshipTypes.DIGITAL_SIGNATURE_ORIGIN).iterator();
Iterator<PackageRelationship> sigRels = null;
PackagePart sigPart = null;
public boolean hasNext() {
while (sigRels == null || !sigRels.hasNext()) {
if (!sigOrigRels.hasNext())
return false;
sigPart = pkg.getPart(sigOrigRels.next());
LOG.log(POILogger.DEBUG, "Digital Signature Origin part", sigPart);
try {
sigRels = sigPart.getRelationshipsByType(PackageRelationshipTypes.DIGITAL_SIGNATURE).iterator();
} catch (InvalidFormatException e) {
LOG.log(POILogger.WARN, "Reference to signature is invalid.", e);
}
}
return true;
}
public SignaturePart next() {
PackagePart sigRelPart = null;
do {
try {
if (!hasNext())
throw new NoSuchElementException();
sigRelPart = sigPart.getRelatedPart(sigRels.next());
LOG.log(POILogger.DEBUG, "XML Signature part", sigRelPart);
} catch (InvalidFormatException e) {
LOG.log(POILogger.WARN, "Reference to signature is invalid.", e);
}
} while (sigPart == null);
return new SignaturePart(sigRelPart);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class OPCPackage method getParts.
/**
* Load the parts of the archive if it has not been done yet. The
* relationships of each part are not loaded.
*
* Note - Rule M4.1 states that there may only ever be one Core
* Properties Part, but Office produced files will sometimes
* have multiple! As Office ignores all but the first, we relax
* Compliance with Rule M4.1, and ignore all others silently too.
*
* @return All this package's parts.
* @throws InvalidFormatException if the package is not valid.
*/
public ArrayList<PackagePart> getParts() throws InvalidFormatException {
throwExceptionIfWriteOnly();
// If the part list is null, we parse the package to retrieve all parts.
if (partList == null) {
/* Variables use to validate OPC Compliance */
// Check rule M4.1 -> A format consumer shall consider more than
// one core properties relationship for a package to be an error
// (We just log it and move on, as real files break this!)
boolean hasCorePropertiesPart = false;
boolean needCorePropertiesPart = true;
PackagePart[] parts = this.getPartsImpl();
this.partList = new PackagePartCollection();
for (PackagePart part : parts) {
if (partList.containsKey(part._partName)) {
throw new InvalidFormatException("A part with the name '" + part._partName + "' already exist : Packages shall not contain equivalent " + "part names and package implementers shall neither create " + "nor recognize packages with equivalent part names. [M1.12]");
}
// Check OPC compliance rule M4.1
if (part.getContentType().equals(ContentTypes.CORE_PROPERTIES_PART)) {
if (!hasCorePropertiesPart) {
hasCorePropertiesPart = true;
} else {
logger.log(POILogger.WARN, "OPC Compliance error [M4.1]: " + "there is more than one core properties relationship in the package! " + "POI will use only the first, but other software may reject this file.");
}
}
PartUnmarshaller partUnmarshaller = partUnmarshallers.get(part._contentType);
if (partUnmarshaller != null) {
UnmarshallContext context = new UnmarshallContext(this, part._partName);
try {
PackagePart unmarshallPart = partUnmarshaller.unmarshall(context, part.getInputStream());
partList.put(unmarshallPart._partName, unmarshallPart);
// and ignore any subsequent ones
if (unmarshallPart instanceof PackagePropertiesPart && hasCorePropertiesPart && needCorePropertiesPart) {
this.packageProperties = (PackagePropertiesPart) unmarshallPart;
needCorePropertiesPart = false;
}
} catch (IOException ioe) {
logger.log(POILogger.WARN, "Unmarshall operation : IOException for " + part._partName);
continue;
} catch (InvalidOperationException invoe) {
throw new InvalidFormatException(invoe.getMessage(), invoe);
}
} else {
try {
partList.put(part._partName, part);
} catch (InvalidOperationException e) {
throw new InvalidFormatException(e.getMessage(), e);
}
}
}
}
return new ArrayList<PackagePart>(partList.sortedValues());
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class OPCPackage method init.
/**
* Initialize the package instance.
*/
private void init() {
this.partMarshallers = new HashMap<ContentType, PartMarshaller>(5);
this.partUnmarshallers = new HashMap<ContentType, PartUnmarshaller>(2);
try {
// Add 'default' unmarshaller
this.partUnmarshallers.put(new ContentType(ContentTypes.CORE_PROPERTIES_PART), new PackagePropertiesUnmarshaller());
// Add default marshaller
this.defaultPartMarshaller = new DefaultMarshaller();
// TODO Delocalize specialized marshallers
this.partMarshallers.put(new ContentType(ContentTypes.CORE_PROPERTIES_PART), new ZipPackagePropertiesMarshaller());
} catch (InvalidFormatException e) {
// Should never happen
throw new OpenXML4JRuntimeException("Package.init() : this exception should never happen, " + "if you read this message please send a mail to the developers team. : " + e.getMessage(), e);
}
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class OPCPackage method removePart.
/**
* Remove a part in this package. If this part is relationship part, then
* delete all relationships in the source part.
*
* @param partName
* The part name of the part to remove.
*/
public void removePart(PackagePartName partName) {
throwExceptionIfReadOnly();
if (partName == null || !this.containPart(partName)) {
throw new IllegalArgumentException("partName");
}
// Delete the specified part from the package.
if (this.partList.containsKey(partName)) {
this.partList.get(partName).setDeleted(true);
this.removePartImpl(partName);
this.partList.remove(partName);
} else {
this.removePartImpl(partName);
}
// Delete content type
this.contentTypeManager.removeContentType(partName);
// the source part.
if (partName.isRelationshipPartURI()) {
URI sourceURI = PackagingURIHelper.getSourcePartUriFromRelationshipPartUri(partName.getURI());
PackagePartName sourcePartName;
try {
sourcePartName = PackagingURIHelper.createPartName(sourceURI);
} catch (InvalidFormatException e) {
logger.log(POILogger.ERROR, "Part name URI '" + sourceURI + "' is not valid ! This message is not intended to be displayed !");
return;
}
if (sourcePartName.getURI().equals(PackagingURIHelper.PACKAGE_ROOT_URI)) {
clearRelationships();
} else if (containPart(sourcePartName)) {
PackagePart part = getPart(sourcePartName);
if (part != null) {
part.clearRelationships();
}
}
}
this.isDirty = true;
}
use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.
the class PackagePart method getRelatedPart.
/**
* Get the PackagePart that is the target of a relationship.
*
* @param rel A relationship from this part to another one
* @return The target part of the relationship
*/
public PackagePart getRelatedPart(PackageRelationship rel) throws InvalidFormatException {
// Ensure this is one of ours
if (!isRelationshipExists(rel)) {
throw new IllegalArgumentException("Relationship " + rel + " doesn't start with this part " + _partName);
}
// Get the target URI, excluding any relative fragments
URI target = rel.getTargetURI();
if (target.getFragment() != null) {
String t = target.toString();
try {
target = new URI(t.substring(0, t.indexOf('#')));
} catch (URISyntaxException e) {
throw new InvalidFormatException("Invalid target URI: " + target);
}
}
// Turn that into a name, and fetch
PackagePartName relName = PackagingURIHelper.createPartName(target);
PackagePart part = _container.getPart(relName);
if (part == null) {
throw new IllegalArgumentException("No part found for relationship " + rel);
}
return part;
}
Aggregations