org.apache.taverna.robundle.manifest.odf.ODFManifest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taverna-robundle Show documentation
Show all versions of taverna-robundle Show documentation
API for dealing with RO Bundles
package org.apache.taverna.robundle.manifest.odf;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import static java.nio.file.Files.createDirectories;
import static java.nio.file.Files.exists;
import static java.nio.file.Files.isRegularFile;
import static java.nio.file.Files.newInputStream;
import static java.nio.file.Files.newOutputStream;
import static java.nio.file.Files.size;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.WARNING;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import oasis.names.tc.opendocument.xmlns.manifest._1.FileEntry;
import oasis.names.tc.opendocument.xmlns.manifest._1.Manifest;
import oasis.names.tc.opendocument.xmlns.manifest._1.ObjectFactory;
import org.apache.taverna.robundle.Bundle;
import org.apache.taverna.robundle.manifest.PathMetadata;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
public class ODFManifest {
public static class ManifestNamespacePrefixMapperJAXB_RI extends
NamespacePrefixMapper {
@Override
public String[] getPreDeclaredNamespaceUris() {
// TODO Auto-generated method stub
return super.getPreDeclaredNamespaceUris();
}
@Override
public String getPreferredPrefix(String namespaceUri,
String suggestion, boolean requirePrefix) {
if (namespaceUri
.equals("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"))
return "manifest";
return suggestion;
}
}
public static final String CONTAINER_XML = "META-INF/container.xml";
private static JAXBContext jaxbContext;
private static Logger logger = Logger.getLogger(ODFManifest.class
.getCanonicalName());
public static final String MANIFEST_XML = "META-INF/manifest.xml";
private static final String ODF_MANIFEST_VERSION = "1.2";
private static boolean warnedPrefixMapper;
public static boolean containsManifest(Bundle bundle) {
return isRegularFile(manifestXmlPath(bundle));
}
protected static synchronized Marshaller createMarshaller()
throws JAXBException {
Marshaller marshaller = getJaxbContext().createMarshaller();
setPrefixMapper(marshaller);
return marshaller;
}
protected static synchronized Unmarshaller createUnMarshaller()
throws JAXBException {
return getJaxbContext().createUnmarshaller();
}
protected static synchronized JAXBContext getJaxbContext()
throws JAXBException {
if (jaxbContext == null) {
jaxbContext = JAXBContext.newInstance(ObjectFactory.class
// ,
// org.oasis_open.names.tc.opendocument.xmlns.container.ObjectFactory.class,
// org.w3._2000._09.xmldsig_.ObjectFactory.class,
// org.w3._2001._04.xmlenc_.ObjectFactory.class
);
}
return jaxbContext;
}
private static Path manifestXmlPath(Bundle bundle) {
return bundle.getRoot().resolve(MANIFEST_XML);
}
protected static void setPrefixMapper(Marshaller marshaller) {
boolean setPrefixMapper = false;
try {
/*
* This only works with JAXB RI, in which case we can set the
* namespace prefix mapper
*/
Class.forName("com.sun.xml.bind.marshaller.NamespacePrefixMapper");
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
new ManifestNamespacePrefixMapperJAXB_RI());
/*
* Note: A similar mapper for the built-in java
* (com.sun.xml.bind.internal.namespacePrefixMapper) is no longer
* included here, as it will not (easily) compile with Maven.
*/
setPrefixMapper = true;
} catch (Exception e) {
logger.log(FINE, "Can't find NamespacePrefixMapper", e);
}
if (!setPrefixMapper && !warnedPrefixMapper) {
logger.info("Could not set prefix mapper (missing or incompatible JAXB) "
+ "- will use prefixes ns0, ns1, ..");
warnedPrefixMapper = true;
}
}
private Bundle bundle;
// protected void prepareContainerXML() throws IOException {
//
//
// /* Check if we should prune */
// Iterator