You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@taverna.apache.org by st...@apache.org on 2015/02/17 21:39:54 UTC
[03/51] [partial] incubator-taverna-language git commit: temporarily
empty repository
http://git-wip-us.apache.org/repos/asf/incubator-taverna-language/blob/316e4a8a/taverna-scufl2-ucfpackage/LICENSE-odfdom.txt
----------------------------------------------------------------------
diff --git a/taverna-scufl2-ucfpackage/LICENSE-odfdom.txt b/taverna-scufl2-ucfpackage/LICENSE-odfdom.txt
deleted file mode 100644
index 29f81d8..0000000
--- a/taverna-scufl2-ucfpackage/LICENSE-odfdom.txt
+++ /dev/null
@@ -1,201 +0,0 @@
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
- 1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
- 2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
- 3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
- 4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
- 5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
- 6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
- 7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
- 8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
- 9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
- END OF TERMS AND CONDITIONS
-
- APPENDIX: How to apply the Apache License to your work.
-
- To apply the Apache License to your work, attach the following
- boilerplate notice, with the fields enclosed by brackets "[]"
- replaced with your own identifying information. (Don't include
- the brackets!) The text should be enclosed in the appropriate
- comment syntax for the file format. We also recommend that a
- file or class name and description of purpose be included on the
- same "printed page" as the copyright notice for easier
- identification within third-party archives.
-
- Copyright [yyyy] [name of copyright owner]
-
- Licensed 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.
http://git-wip-us.apache.org/repos/asf/incubator-taverna-language/blob/316e4a8a/taverna-scufl2-ucfpackage/pom.xml
----------------------------------------------------------------------
diff --git a/taverna-scufl2-ucfpackage/pom.xml b/taverna-scufl2-ucfpackage/pom.xml
deleted file mode 100644
index 5411dc0..0000000
--- a/taverna-scufl2-ucfpackage/pom.xml
+++ /dev/null
@@ -1,105 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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.
--->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.apache.taverna.language</groupId>
- <artifactId>taverna-language</artifactId>
- <version>0.16.1-incubating-SNAPSHOT</version>
- </parent>
- <artifactId>taverna-scufl2-ucfpackage</artifactId>
- <packaging>bundle</packaging>
- <name>Apache Taverna Scufl 2 UCF Package</name>
- <description>Adobe UCF compatible Package API, making a structured ZIP file with manifest</description>
- <dependencies>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>${commons.io.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.jdom</groupId>
- <artifactId>com.springsource.org.jdom</artifactId>
- <version>${jdom.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.jaxen</groupId>
- <artifactId>com.springsource.org.jaxen</artifactId>
- <version>${jaxen.version}</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <licenses>
- <license>
- <name>Apache 2</name>
- <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
- <comments>The package uk.org.taverna.scul2.ucfpackage.impl.odfdom
- contains a patched subset of ODFDOM 0.8.6 -
- http://odftoolkit.org/projects/odfdom/pages/Home
- Copyright 2008, 2010 Oracle and/or its affiliates.
- All rights reserved. Copyright 2009 IBM. All rights reserved.</comments>
- </license>
- </licenses>
-
- <build>
- <plugins>
- <!--
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>jaxb2-maven-plugin</artifactId>
- <executions>
- <execution>
- <id>jaxb</id>
- <phase>generate-sources</phase>
- <goals>
- <goal>xjc</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- -->
- <plugin>
- <groupId>org.jvnet.jaxb2.maven2</groupId>
- <artifactId>maven-jaxb2-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>generate</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
-
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.rat</groupId>
- <artifactId>apache-rat-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- <!-- -->
-</project>
http://git-wip-us.apache.org/repos/asf/incubator-taverna-language/blob/316e4a8a/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
deleted file mode 100644
index b806ffe..0000000
--- a/taverna-scufl2-ucfpackage/src/main/java/org/apache/taverna/scufl2/ucfpackage/UCFPackage.java
+++ /dev/null
@@ -1,664 +0,0 @@
-package org.apache.taverna.scufl2.ucfpackage;
-/*
- *
- * 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.io.File.createTempFile;
-import static java.util.logging.Level.INFO;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
-import java.net.URI;
-import java.nio.charset.Charset;
-import java.util.ArrayList;
-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.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 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.w3c.dom.Document;
-
-
-public class UCFPackage implements Cloneable {
- private static Logger logger = Logger.getLogger(UCFPackage.class.getName());
- 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";
- public static final String MIME_RDF = "application/rdf+xml";
- public static final String MIME_EPUB = "application/epub+zip";
- public static final String MIME_WORKFLOW_BUNDLE = "application/vnd.taverna.workflow-bundle";
- public static final String MIME_DATA_BUNDLE = "application/vnd.taverna.data-bundle";
- public static final String MIME_WORKFLOW_RUN_BUNDLE = "application/vnd.taverna.workflow-run-bundle";
- public static final String MIME_SERVICE_BUNDLE = "application/vnd.taverna.service-bundle";
-
- private static Charset ASCII = Charset.forName("ascii");
- private OdfPackage odfPackage;
- private static JAXBContext jaxbContext;
- private JAXBElement<Container> containerXml;
- private boolean createdContainerXml = false;
- private static ObjectFactory containerFactory = new ObjectFactory();
-
- public UCFPackage() throws IOException {
- try {
- odfPackage = OdfPackage.create();
- parseContainerXML();
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not create empty UCF Package", e);
- }
- // odfPackage.setMediaType(MIME_EPUB);
- }
-
- public UCFPackage(File containerFile) throws IOException {
- open(containerFile);
- }
-
- protected void open(File containerFile) throws IOException {
- try (BufferedInputStream stream = new BufferedInputStream(
- new FileInputStream(containerFile))) {
- open(stream);
- }
- }
-
- public UCFPackage(InputStream inputStream) throws IOException {
- open(inputStream);
- }
-
- protected void open(InputStream inputStream) throws IOException {
- try {
- odfPackage = OdfPackage.loadPackage(inputStream);
- parseContainerXML();
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException(
- "Could not load UCF Package from input stream", e);
- }
- }
-
- @SuppressWarnings("unchecked")
- protected void parseContainerXML() throws IOException {
- createdContainerXml = false;
- InputStream containerStream = getResourceAsInputStream(CONTAINER_XML);
- if (containerStream == null) {
- // Make an empty containerXml
- Container container = containerFactory.createContainer();
- containerXml = containerFactory.createContainer(container);
- createdContainerXml = true;
- return;
- }
- try {
- Unmarshaller unMarshaller = createUnMarshaller();
- containerXml = (JAXBElement<Container>) unMarshaller
- .unmarshal(containerStream);
- } catch (JAXBException e) {
- throw new IOException("Could not parse " + CONTAINER_XML, e);
- }
- }
-
- public String getPackageMediaType() {
- return odfPackage.getMediaType();
- }
-
- public void setPackageMediaType(String mediaType) {
- if (mediaType == null || !mediaType.contains("/"))
- throw new IllegalArgumentException("Invalid media type "
- + mediaType);
- if (!ASCII.newEncoder().canEncode(mediaType))
- throw new IllegalArgumentException("Media type must be ASCII: "
- + mediaType);
- odfPackage.setMediaType(mediaType);
- }
-
- public void save(File packageFile) throws IOException {
- File tempFile = createTempFile("." + packageFile.getName(), ".tmp",
- packageFile.getCanonicalFile().getParentFile());
- prepareAndSave(tempFile);
- boolean renamed = tempFile.renameTo(packageFile);
- if (!renamed && packageFile.exists() && tempFile.exists()) {
- // Could happen on Windows
- if (!packageFile.delete())
- // Could have been permission problem
- throw new IOException("Could not delete existing "
- + packageFile);
- renamed = tempFile.renameTo(packageFile);
- }
- if (!renamed)
- throw new IOException("Could not rename temp file " + tempFile
- + " to " + packageFile);
- }
-
- protected void prepareAndSave(File tempFile) throws IOException {
- if (getPackageMediaType() == null)
- throw new IllegalStateException("Package media type must be set");
-
- // Write using temp file, and do rename in the end
-
- try {
- prepareContainerXML();
- odfPackage.save(tempFile);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not save bundle to " + tempFile, e);
- } finally {
- odfPackage.close();
- }
-
- try {
- open(tempFile);
- } catch (Exception e) {
- throw new IOException("Could not reload package from " + tempFile,
- e);
- }
- }
-
- 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#");
- try (OutputStream outStream = odfPackage
- .insertOutputStream(CONTAINER_XML)) {
- // 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 {
- try {
- odfPackage.insert(stringValue.getBytes(UTF_8), path, mediaType);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not add " + path, e);
- }
- parseContainerXML();
- }
-
- public void addResource(byte[] bytesValue, String path, String mediaType)
- throws IOException {
- try {
- odfPackage.insert(bytesValue, path, mediaType);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not add " + path, e);
- }
- if (path.equals(CONTAINER_XML))
- parseContainerXML();
- }
-
- public void addResource(Document document, String path, String mediaType)
- throws IOException {
- try {
- odfPackage.insert(document, path, mediaType);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not add " + path, e);
- }
- if (path.equals(CONTAINER_XML))
- parseContainerXML();
- }
-
- public void addResource(InputStream inputStream, String path,
- String mediaType) throws IOException {
- try {
- odfPackage.insert(inputStream, path, mediaType);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not add " + path, e);
- }
- if (path.equals(CONTAINER_XML))
- parseContainerXML();
- }
-
- public void addResource(URI uri, String path, String mediaType)
- throws IOException {
- try {
- odfPackage.insert(uri, path, mediaType);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not add " + path, e);
- }
-
- if (path.equals(CONTAINER_XML))
- parseContainerXML();
- }
-
- public String getResourceAsString(String path) throws IOException {
- try {
- return new String(odfPackage.getBytes(path), UTF_8);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not get " + path, e);
- }
- }
-
- public byte[] getResourceAsBytes(String path) throws IOException {
- try {
- return odfPackage.getBytes(path);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not get " + path, e);
- }
- }
-
- public InputStream getResourceAsInputStream(String path) throws IOException {
- try {
- return odfPackage.getInputStream(path);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not get " + path, e);
- }
- }
-
- public Map<String, ResourceEntry> listResources() {
- return listResources("", false);
- }
-
- public Map<String, ResourceEntry> listResources(String folderPath) {
- return listResources(folderPath, false);
- }
-
- protected Map<String, ResourceEntry> listResources(String folderPath,
- boolean recursive) {
- if (!folderPath.isEmpty() && !folderPath.endsWith("/"))
- folderPath = folderPath + "/";
- HashMap<String, ResourceEntry> content = new HashMap<>();
-
- for (Entry<String, OdfFileEntry> entry : odfPackage
- .getManifestEntries().entrySet()) {
- String entryPath = entry.getKey();
- if (!entryPath.startsWith(folderPath))
- continue;
- String subPath = entryPath.substring(folderPath.length(),
- entryPath.length());
- if (subPath.isEmpty())
- // The folder itself
- continue;
- int firstSlash = subPath.indexOf("/");
- if (!recursive && firstSlash > -1
- && firstSlash < subPath.length() - 1)
- /*
- * Children of a folder (note that we'll include the folder
- * itself which ends in /)
- */
- continue;
- content.put(subPath, new ResourceEntry(entry.getValue()));
- }
- return content;
- }
-
- public void removeResource(String path) {
- if (!odfPackage.contains(path))
- return;
- if (path.endsWith("/"))
- for (ResourceEntry childEntry : listResources(path).values())
- removeResource(childEntry.getPath());
- odfPackage.remove(path);
- }
-
- public class ResourceEntry {
- private final String path;
- private final long size;
- private String mediaType;
- private String version;
-
- protected ResourceEntry(OdfFileEntry odfEntry) {
- path = odfEntry.getPath();
- size = odfEntry.getSize();
- mediaType = odfEntry.getMediaType();
- version = odfEntry.getVersion();
- }
-
- public String getPath() {
- return path;
- }
-
- public long getSize() {
- return size;
- }
-
- public String getMediaType() {
- return mediaType;
- }
-
- public boolean isFolder() {
- return path.endsWith("/");
- }
-
- public UCFPackage getUcfPackage() {
- return UCFPackage.this;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof ResourceEntry))
- return false;
- ResourceEntry other = (ResourceEntry) obj;
-
- if (!getUcfPackage().equals(other.getUcfPackage()))
- return false;
- return getPath().equals(other.getPath());
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + getUcfPackage().hashCode();
- result = prime * result + ((path == null) ? 0 : path.hashCode());
- return result;
- }
-
- public String getVersion() {
- return version;
- }
- }
-
- public Map<String, ResourceEntry> listAllResources() {
- return listResources("", true);
- }
-
- public void setRootFile(String path) {
- setRootFile(path, null);
- }
-
- @SuppressWarnings("rawtypes")
- public void setRootFile(String path, String version) {
- ResourceEntry rootFile = getResourceEntry(path);
- if (rootFile == null)
- throw new IllegalArgumentException("Unknown resource: " + path);
- odfPackage.getManifestEntries().get(path).setVersion(version);
-
- 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);
- }
- }
-
- 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;
- }
- // 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.mediaType)
- // Override the mime type in the returned entry
- entry.mediaType = rf.getMediaType();
- rootFiles.add(entry);
- }
- return rootFiles;
- }
-
- public ResourceEntry getResourceEntry(String path) {
- OdfFileEntry odfFileEntry = odfPackage.getManifestEntries().get(path);
- if (odfFileEntry == null)
- return null;
- return new ResourceEntry(odfFileEntry);
- }
-
- @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;
- }
-
- public void save(OutputStream output) throws IOException {
- File tempFile = createTempFile("ucfpackage", ".tmp");
- prepareAndSave(tempFile);
-
- // Copy file to the output
-
- // Note - Should use IOUtils, but we're trying to avoid external dependencies
- try (InputStream inStream = new FileInputStream(tempFile)) {
- byte[] buffer = new byte[8192];
- int n = 0;
- do {
- output.write(buffer, 0, n);
- n = inStream.read(buffer);
- } while (n > -1);
- } finally {
- tempFile.delete();
- }
- }
-
- public OutputStream addResourceUsingOutputStream(String path,
- String mediaType) throws IOException {
- if (path.equals(CONTAINER_XML))
- // as we need to parse it after insertion, this must fail
- throw new IllegalArgumentException("Can't add " + CONTAINER_XML
- + " using OutputStream");
- try {
- return odfPackage.insertOutputStream(path, mediaType);
- } catch (IOException e) {
- throw e;
- } catch (Exception e) {
- throw new IOException("Could not add " + path, e);
- }
- }
-
- @Override
- public UCFPackage clone() {
- final PipedOutputStream outputStream = new PipedOutputStream();
- try {
- try (PipedInputStream inputStream = copyToOutputStream(outputStream)) {
- return new UCFPackage(inputStream);
- }
- } catch (IOException e) {
- throw new RuntimeException("Could not clone UCFPackage", e);
- }
- }
-
- private PipedInputStream copyToOutputStream(
- final PipedOutputStream outputStream) throws IOException {
- PipedInputStream inputStream = new PipedInputStream(outputStream);
- new Thread("Cloning " + this) {
- @Override
- public void run() {
- try {
- try {
- save(outputStream);
- } finally {
- outputStream.close();
- }
- } catch (IOException e) {
- logger.log(INFO,
- "Could not save/close UCF package while cloning", e);
- }
- }
- }.start();
- return inputStream;
- }
-
- public String getRootFileVersion(String rootFile) {
- return getResourceEntry(rootFile).getVersion();
- }
-}