use of org.apache.poi.openxml4j.opc.internal.ContentType 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.opc.internal.ContentType in project poi by apache.
the class PackagePart method setContentType.
/**
* Set the content type.
*
* @param contentType
* the contentType to set
*
* @throws InvalidFormatException
* Throws if the content type is not valid.
* @throws InvalidOperationException
* Throws if you try to change the content type whereas this
* part is already attached to a package.
*/
public void setContentType(String contentType) throws InvalidFormatException {
if (_container == null) {
_contentType = new ContentType(contentType);
} else {
_container.unregisterPartAndContentType(_partName);
_contentType = new ContentType(contentType);
_container.registerPartAndContentType(this);
}
}
use of org.apache.poi.openxml4j.opc.internal.ContentType in project poi by apache.
the class TestContentType method testFileWithContentTypeParams.
/**
* Check that we can open a file where there are valid
* parameters on a content type
*/
public void testFileWithContentTypeParams() throws Exception {
InputStream is = OpenXML4JTestDataSamples.openSampleStream("ContentTypeHasParameters.ooxml");
OPCPackage p = OPCPackage.open(is);
final String typeResqml = "application/x-resqml+xml";
// Check the types on everything
for (PackagePart part : p.getParts()) {
final String contentType = part.getContentType();
final ContentType details = part.getContentTypeDetails();
final int length = details.getParameterKeys().length;
final boolean hasParameters = details.hasParameters();
// _rels type doesn't have any params
if (part.isRelationshipPart()) {
assertEquals(ContentTypes.RELATIONSHIPS_PART, contentType);
assertEquals(ContentTypes.RELATIONSHIPS_PART, details.toString());
assertEquals(false, hasParameters);
assertEquals(0, length);
} else // Core type doesn't have any params
if (part.getPartName().toString().equals("/docProps/core.xml")) {
assertEquals(ContentTypes.CORE_PROPERTIES_PART, contentType);
assertEquals(ContentTypes.CORE_PROPERTIES_PART, details.toString());
assertEquals(false, hasParameters);
assertEquals(0, length);
} else // Global Crs types do have params
if (part.getPartName().toString().equals("/global1dCrs.xml")) {
assertTrue(part.getContentType().startsWith(typeResqml));
assertEquals(typeResqml, details.toString(false));
assertEquals(true, hasParameters);
assertContains("version=2.0", details.toString());
assertContains("type=obj_global1dCrs", details.toString());
assertEquals(2, length);
assertEquals("2.0", details.getParameter("version"));
assertEquals("obj_global1dCrs", details.getParameter("type"));
} else if (part.getPartName().toString().equals("/global2dCrs.xml")) {
assertTrue(part.getContentType().startsWith(typeResqml));
assertEquals(typeResqml, details.toString(false));
assertEquals(true, hasParameters);
assertContains("version=2.0", details.toString());
assertContains("type=obj_global2dCrs", details.toString());
assertEquals(2, length);
assertEquals("2.0", details.getParameter("version"));
assertEquals("obj_global2dCrs", details.getParameter("type"));
} else // Other thingy
if (part.getPartName().toString().equals("/myTestingGuid.xml")) {
assertTrue(part.getContentType().startsWith(typeResqml));
assertEquals(typeResqml, details.toString(false));
assertEquals(true, hasParameters);
assertContains("version=2.0", details.toString());
assertContains("type=obj_tectonicBoundaryFeature", details.toString());
assertEquals(2, length);
assertEquals("2.0", details.getParameter("version"));
assertEquals("obj_tectonicBoundaryFeature", details.getParameter("type"));
} else // That should be it!
{
fail("Unexpected part " + part);
}
}
}
Aggregations