Moved container.xml handling to robundle Project: http://git-wip-us.apache.org/repos/asf/incubator-taverna-language/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-taverna-language/commit/60575c8b Tree: http://git-wip-us.apache.org/repos/asf/incubator-taverna-language/tree/60575c8b Diff: http://git-wip-us.apache.org/repos/asf/incubator-taverna-language/diff/60575c8b
Branch: refs/heads/ucfpackage-robundle Commit: 60575c8b015f6469270e3fd107bed58ff3fb1d50 Parents: 20af454 Author: Stian Soiland-Reyes <[email protected]> Authored: Thu Jun 19 17:51:16 2014 +0100 Committer: Stian Soiland-Reyes <[email protected]> Committed: Thu Jul 2 15:18:33 2015 +0100 ---------------------------------------------------------------------- .../taverna/scufl2/ucfpackage/UCFPackage.java | 317 ++----------------- 1 file changed, 21 insertions(+), 296 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-taverna-language/blob/60575c8b/taverna-scufl2-ucfpackage/src/main/java/org/apache/taverna/scufl2/ucfpackage/UCFPackage.java ---------------------------------------------------------------------- diff --git a/taverna-scufl2-ucfpackage/src/main/java/org/apache/taverna/scufl2/ucfpackage/UCFPackage.java b/taverna-scufl2-ucfpackage/src/main/java/org/apache/taverna/scufl2/ucfpackage/UCFPackage.java index 440d27e..b417461 100644 --- a/taverna-scufl2-ucfpackage/src/main/java/org/apache/taverna/scufl2/ucfpackage/UCFPackage.java +++ b/taverna-scufl2-ucfpackage/src/main/java/org/apache/taverna/scufl2/ucfpackage/UCFPackage.java @@ -32,8 +32,6 @@ import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.net.URI; -import java.nio.charset.Charset; -import java.nio.file.DirectoryStream; import java.nio.file.FileVisitResult; import java.nio.file.FileVisitor; import java.nio.file.Files; @@ -42,18 +40,12 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; //import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; @@ -63,10 +55,10 @@ import javax.xml.transform.stream.StreamResult; //import org.apache.taverna.scufl2.ucfpackage.impl.odfdom.pkg.OdfPackage; //import org.apache.taverna.scufl2.ucfpackage.impl.odfdom.pkg.manifest.OdfFileEntry; -import org.oasis_open.names.tc.opendocument.xmlns.container.Container; -import org.oasis_open.names.tc.opendocument.xmlns.container.Container.RootFiles; -import org.oasis_open.names.tc.opendocument.xmlns.container.ObjectFactory; -import org.oasis_open.names.tc.opendocument.xmlns.container.RootFile; +//import org.oasis_open.names.tc.opendocument.xmlns.container.Container; +//import org.oasis_open.names.tc.opendocument.xmlns.container.Container.RootFiles; +//import org.oasis_open.names.tc.opendocument.xmlns.container.ObjectFactory; +//import org.oasis_open.names.tc.opendocument.xmlns.container.RootFile; import org.purl.wf4ever.robundle.Bundle; import org.purl.wf4ever.robundle.Bundles; import org.purl.wf4ever.robundle.manifest.Manifest; @@ -79,7 +71,6 @@ public class UCFPackage implements Cloneable { private static Logger logger = Logger.getLogger(UCFPackage.class.getName()); private static final URI VERSION_BASE = URI.create("http://ns.taverna.org.uk/2010/scufl2/"); private static final String CONTAINER_XML = "META-INF/container.xml"; - private static final Charset UTF_8 = Charset.forName("utf-8"); public static final String MIME_BINARY = "application/octet-stream"; public static final String MIME_TEXT_PLAIN = "text/plain"; public static final String MIME_TEXT_XML = "text/xml"; @@ -87,13 +78,8 @@ public class UCFPackage implements Cloneable { public static final String MIME_EPUB = "application/epub+zip"; public static final String MIME_WORKFLOW_BUNDLE = "application/vnd.taverna.workflow-bundle"; - private static Charset ASCII = Charset.forName("ascii"); -// private OdfPackage odfPackage; - private static JAXBContext jaxbContext; - private JAXBElement<Container> containerXml; - private boolean createdContainerXml = false; private Bundle bundle; - private static ObjectFactory containerFactory = new ObjectFactory(); + public UCFPackage() throws IOException { try { @@ -101,7 +87,6 @@ public class UCFPackage implements Cloneable { //odfPackage = OdfPackage.create(); bundle.getManifest().populateFromBundle(); bundle.getManifest().writeAsODFManifest(); - parseContainerXML(); } catch (IOException e) { throw e; } catch (Exception e) { @@ -117,7 +102,6 @@ public class UCFPackage implements Cloneable { protected void open(File containerFile) throws IOException { bundle = Bundles.openBundleReadOnly(containerFile.toPath()); - parseContainerXML(); } public UCFPackage(InputStream inputStream) throws IOException { @@ -126,7 +110,6 @@ public class UCFPackage implements Cloneable { protected UCFPackage(Bundle bundle) throws IOException { this.bundle = bundle; - parseContainerXML(); } protected void open(InputStream inputStream) throws IOException { @@ -135,8 +118,6 @@ public class UCFPackage implements Cloneable { Files.copy(inputStream, bundlePath); bundle = Bundles.openBundle(bundlePath); bundle.setDeleteOnClose(true); - - parseContainerXML(); } catch (IOException e) { throw e; } catch (Exception e) { @@ -145,26 +126,6 @@ public class UCFPackage implements Cloneable { } } - @SuppressWarnings("unchecked") - protected void parseContainerXML() throws IOException { - createdContainerXml = false; - if (getResourceEntry(CONTAINER_XML) == null) { - // Make an empty containerXml - Container container = containerFactory.createContainer(); - containerXml = containerFactory.createContainer(container); - createdContainerXml = true; - return; - } - try { - InputStream containerStream = getResourceAsInputStream(CONTAINER_XML); - Unmarshaller unMarshaller = createUnMarshaller(); - containerXml = (JAXBElement<Container>) unMarshaller - .unmarshal(containerStream); - } catch (JAXBException e) { - throw new IOException("Could not parse " + CONTAINER_XML, e); - } - } - public String getPackageMediaType() { try { return Bundles.getMimeType(bundle); @@ -182,8 +143,6 @@ public class UCFPackage implements Cloneable { } public void save(File packageFile) throws IOException { - prepareContainerXML(); - Path source = bundle.getSource(); boolean deleteOnClose = bundle.isDeleteOnClose(); bundle.setDeleteOnClose(false); @@ -193,134 +152,6 @@ public class UCFPackage implements Cloneable { bundle.setDeleteOnClose(deleteOnClose); } - protected void prepareContainerXML() throws IOException { - if (containerXml == null || createdContainerXml - && containerXml.getValue().getRootFilesOrAny() == null) - return; - - /* Check if we should prune <rootFiles> */ - Iterator<Object> iterator = containerXml.getValue().getRootFilesOrAny() - .iterator(); - boolean foundAlready = false; - while (iterator.hasNext()) { - Object anyOrRoot = iterator.next(); - if (!(anyOrRoot instanceof JAXBElement)) - continue; - @SuppressWarnings("rawtypes") - JAXBElement elem = (JAXBElement) anyOrRoot; - if (!elem.getDeclaredType().equals(RootFiles.class)) - continue; - RootFiles rootFiles = (RootFiles) elem.getValue(); - if (foundAlready - || (rootFiles.getOtherAttributes().isEmpty() && rootFiles - .getAnyOrRootFile().isEmpty())) { - // Delete it! - System.err.println("Deleting unneccessary <rootFiles>"); - iterator.remove(); - } - foundAlready = true; - } - - Marshaller marshaller; - try { - marshaller = createMarshaller(); - // XMLStreamWriter xmlStreamWriter = XMLOutputFactory - // .newInstance().createXMLStreamWriter(outStream); - // xmlStreamWriter.setDefaultNamespace(containerElem.getName() - // .getNamespaceURI()); - // - // xmlStreamWriter.setPrefix("dsig", - // "http://www.w3.org/2000/09/xmldsig#"); - // xmlStreamWriter.setPrefix("xmlenc", - // "http://www.w3.org/2001/04/xmlenc#"); - Path containerPath = writableBundlePath(CONTAINER_XML); - outStream = Files.newOutputStream(containerPath); - - // FIXME: Set namespace prefixes and default namespace - - marshaller.setProperty("jaxb.formatted.output", true); - - // TODO: Ensure using default namespace - marshaller.marshal(containerXml, outStream); - - } catch (IOException e) { - throw e; - } catch (Exception e) { - throw new IOException("Could not parse " + CONTAINER_XML, e); - } - } - - protected static synchronized Marshaller createMarshaller() - throws JAXBException { - return getJaxbContext().createMarshaller(); - } - - protected static synchronized Unmarshaller createUnMarshaller() - throws JAXBException { - return getJaxbContext().createUnmarshaller(); - } - - protected static synchronized JAXBContext getJaxbContext() - throws JAXBException { - if (jaxbContext == null) - jaxbContext = JAXBContext - .newInstance( - 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; - } - - public void addResource(String stringValue, String path, String mediaType) - throws IOException { - Path bundlePath = writableBundlePath(path); - Bundles.setStringValue(bundlePath, stringValue); - Manifest manifest = bundle.getManifest(); - manifest.getAggregation(bundlePath).setMediatype(mediaType); - } - - public void addResource(byte[] bytesValue, String path, String mediaType) - throws IOException { - - Path bundlePath = writableBundlePath(path); - Files.write(bundlePath, bytesValue); - Manifest manifest = bundle.getManifest(); - manifest.getAggregation(bundlePath).setMediatype(mediaType); - } - - private Path writableBundlePath(String path) { - Path bundlePath = bundle.getRoot().resolve(path); - if (bundlePath.getParent() != null) { - try { - Files.createDirectories(bundlePath.getParent()); - } catch (IOException e) { - throw new RuntimeException("Could not create parent directories of " + path, e); - } - } - return bundlePath; - } - - @Deprecated - public void addResource(Document document, String path, String mediaType) - throws IOException { - - Path bundlePath = writableBundlePath(path); - TransformerFactory tFactory = TransformerFactory.newInstance(); - Transformer transformer; - try { - transformer = tFactory.newTransformer(); - } catch (TransformerConfigurationException e) { - throw new IOException("Can't create XML transformer to save " - + path, e); - } - - DOMSource source = new DOMSource(document); - try (OutputStream outStream = Files.newOutputStream(bundlePath)) { - StreamResult result = new StreamResult(outStream); - transformer.transform(source, result); - } catch (TransformerException e) { - throw new IOException("Can't save XML to " + path, e); - } if (path.equals(CONTAINER_XML)) { parseContainerXML(); @@ -527,115 +358,34 @@ public class UCFPackage implements Cloneable { } public void setRootFile(String path) { - setRootFile(path, null); + bundle.addRootFile(bundle.getPath(path)); } @SuppressWarnings("rawtypes") public void setRootFile(String path, String version) { - ResourceEntry rootFile = getResourceEntry(path); - if (rootFile == null) - throw new IllegalArgumentException("Unknown resource: " + path); - } - - URI conformsTo = null; - if (version != null) { - conformsTo = VERSION_BASE.resolve(version); - PathMetadata aggregation; - try { - aggregation = bundle.getManifest().getAggregation(rootFile.path); - } catch (IOException e) { - throw new RuntimeException("Can't get root file aggregation for " + path, e); - } - aggregation.setConformsTo(conformsTo); - } - - Container container = containerXml.getValue(); - - RootFiles rootFiles = getRootFiles(container); - String mediaType = rootFile.getMediaType(); - boolean foundExisting = false; - // Check any existing files for matching path/mime type - Iterator<Object> anyOrRootIt = rootFiles.getAnyOrRootFile().iterator(); - while (anyOrRootIt.hasNext()) { - Object anyOrRoot = anyOrRootIt.next(); - if (anyOrRoot instanceof JAXBElement) - anyOrRoot = ((JAXBElement) anyOrRoot).getValue(); - if (!(anyOrRoot instanceof RootFile)) - continue; - RootFile rootFileElem = (RootFile) anyOrRoot; - if (!rootFileElem.getFullPath().equals(path) - && !rootFileElem.getMediaType().equals(mediaType)) - // Different path and media type - ignore - continue; - if (foundExisting) { - // Duplicate path/media type, we'll remove it - anyOrRootIt.remove(); - continue; - } - rootFileElem.setFullPath(rootFile.getPath()); - if (mediaType != null) - rootFileElem.setMediaType(mediaType); - - foundExisting = true; - } - if (!foundExisting) { - RootFile rootFileElem = containerFactory.createRootFile(); - rootFileElem.setFullPath(rootFile.getPath()); - rootFileElem.setMediaType(mediaType); - rootFiles.getAnyOrRootFile().add( - containerFactory - .createContainerRootFilesRootFile(rootFileElem)); - // rootFiles.getAnyOrRootFile().add(rootFileElem); + setRootFile(path); + if (version == null) { + return; } - } - - protected RootFiles getRootFiles(Container container) { - for (Object o : container.getRootFilesOrAny()) { - if (o instanceof JAXBElement) { - @SuppressWarnings("rawtypes") - JAXBElement jaxbElement = (JAXBElement) o; - o = jaxbElement.getValue(); - } - if (o instanceof RootFiles) - return (RootFiles) o; + try { + PathMetadata metadata = bundle.getManifest().getAggregation(bundle.getPath(path)); + metadata.setConformsTo(VERSION_BASE.resolve(version)); + } catch (IOException e) { + throw new RuntimeException("Could not set root file: "+ path, e); } - // Not found - add it - RootFiles rootFiles = containerFactory.createContainerRootFiles(); - container.getRootFilesOrAny().add( - containerFactory.createContainerRootFiles(rootFiles)); - return rootFiles; } - @SuppressWarnings("rawtypes") public List<ResourceEntry> getRootFiles() { - ArrayList<UCFPackage.ResourceEntry> rootFiles = new ArrayList<>(); - if (containerXml == null) - return rootFiles; - - RootFiles rootFilesElem = getRootFiles(containerXml.getValue()); - for (Object anyOrRoot : rootFilesElem.getAnyOrRootFile()) { - if (anyOrRoot instanceof JAXBElement) - anyOrRoot = ((JAXBElement) anyOrRoot).getValue(); - if (!(anyOrRoot instanceof RootFile)) - continue; - RootFile rf = (RootFile) anyOrRoot; - ResourceEntry entry = getResourceEntry(rf.getFullPath()); - if (rf.getMediaType() != null - && rf.getMediaType() != entry.getMediaType()) { - // Override the mime type in the returned entry - PathMetadata aggr; - try { - aggr = bundle.getManifest().getAggregation(entry.path); - } catch (IOException e) { - throw new RuntimeException("Can't get aggregation for " + entry, e); - } - aggr.setMediatype(rf.getMediaType()); + List<ResourceEntry> files = new ArrayList<>(); + for (Path path : bundle.getRootFiles()) { + if (Files.exists(path)) { + files.add(new ResourceEntry(path)); } - rootFiles.add(entry); } - return rootFiles; + return files; } + public ResourceEntry getResourceEntry(String path) { Path bundlePath = bundle.getRoot().resolve(path); if (Files.exists(bundlePath)) { @@ -646,32 +396,14 @@ public class UCFPackage implements Cloneable { @SuppressWarnings("rawtypes") public void unsetRootFile(String path) { - Container container = containerXml.getValue(); - RootFiles rootFiles = getRootFiles(container); - Iterator<Object> anyOrRootIt = rootFiles.getAnyOrRootFile().iterator(); - while (anyOrRootIt.hasNext()) { - Object anyOrRoot = anyOrRootIt.next(); - if (anyOrRoot instanceof JAXBElement) - anyOrRoot = ((JAXBElement) anyOrRoot).getValue(); - if (!(anyOrRoot instanceof RootFile)) - continue; - RootFile rootFileElem = (RootFile) anyOrRoot; - if (rootFileElem.getFullPath().equals(path)) - anyOrRootIt.remove(); - } - } - - protected JAXBElement<Container> getContainerXML() { - return containerXml; + bundle.removeAsRootFile(bundle.getPath(path)); } public void save(OutputStream output) throws IOException { - prepareContainerXML(); Path source = bundle.getSource(); boolean deleteOnClose = bundle.isDeleteOnClose(); bundle.setDeleteOnClose(false); Bundles.closeBundle(bundle); - Files.copy(source, output); // Re-open the original source (usually a tmpfile) @@ -693,13 +425,6 @@ public class UCFPackage implements Cloneable { @Override public UCFPackage clone() { - - try { - prepareContainerXML(); - } catch (IOException e) { - throw new RuntimeException("Could not prepare " + CONTAINER_XML, e); - } - Path source = bundle.getSource(); boolean deleteOnClose = bundle.isDeleteOnClose(); bundle.setDeleteOnClose(false);
