| 1 | /*
|
|---|
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * The contents of this file are subject to the terms of the GNU
|
|---|
| 7 | * General Public License Version 3 only ("GPL").
|
|---|
| 8 | * You may not use this file except in compliance with the License.
|
|---|
| 9 | * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
|
|---|
| 10 | * See the License for the specific language governing permissions and limitations under the License.
|
|---|
| 11 | *
|
|---|
| 12 | * When distributing the software, include this License Header Notice in each file.
|
|---|
| 13 | *
|
|---|
| 14 | */
|
|---|
| 15 |
|
|---|
| 16 | package org.jopendocument.dom;
|
|---|
| 17 |
|
|---|
| 18 | import java.io.File;
|
|---|
| 19 | import java.util.HashMap;
|
|---|
| 20 | import java.util.HashSet;
|
|---|
| 21 | import java.util.Map;
|
|---|
| 22 | import java.util.Set;
|
|---|
| 23 |
|
|---|
| 24 | import org.jdom2.Document;
|
|---|
| 25 | import org.jopendocument.util.CopyUtils;
|
|---|
| 26 | import org.jopendocument.util.FileUtils;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * An OpenDocument package, ie a zip containing XML documents and their associated files.
|
|---|
| 30 | *
|
|---|
| 31 | * @author ILM Informatique 2 août 2004
|
|---|
| 32 | */
|
|---|
| 33 | public class ODPackage {
|
|---|
| 34 |
|
|---|
| 35 | private static final Set<String> subdocNames;
|
|---|
| 36 | static {
|
|---|
| 37 | subdocNames = new HashSet<>();
|
|---|
| 38 | // section 2.1 of OpenDocument-v1.1-os.odt
|
|---|
| 39 | subdocNames.add("content.xml");
|
|---|
| 40 | subdocNames.add("styles.xml");
|
|---|
| 41 | subdocNames.add("meta.xml");
|
|---|
| 42 | subdocNames.add("settings.xml");
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | private final Map<String, ODPackageEntry> files;
|
|---|
| 46 | private File file;
|
|---|
| 47 |
|
|---|
| 48 | public ODPackage() {
|
|---|
| 49 | this.files = new HashMap<>();
|
|---|
| 50 | this.file = null;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public ODPackage(ODPackage o) {
|
|---|
| 54 | this();
|
|---|
| 55 | // ATTN this works because, all files are read upfront
|
|---|
| 56 | for (final String name : o.getEntries()) {
|
|---|
| 57 | final ODPackageEntry entry = o.getEntry(name);
|
|---|
| 58 | final Object data = entry.getData();
|
|---|
| 59 | final Object myData;
|
|---|
| 60 | if (data instanceof byte[])
|
|---|
| 61 | // assume byte[] are immutable
|
|---|
| 62 | myData = data;
|
|---|
| 63 | else if (data instanceof ODSingleXMLDocument) {
|
|---|
| 64 | myData = new ODSingleXMLDocument((ODSingleXMLDocument) data, this);
|
|---|
| 65 | } else {
|
|---|
| 66 | myData = CopyUtils.copy(data);
|
|---|
| 67 | }
|
|---|
| 68 | this.putFile(name, myData, entry.getType(), entry.isCompressed());
|
|---|
| 69 | }
|
|---|
| 70 | this.file = o.file;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * The version of this package, <code>null</code> if it cannot be found (eg this package is
|
|---|
| 75 | * empty, or contains no xml).
|
|---|
| 76 | *
|
|---|
| 77 | * @return the version of this package, can be <code>null</code>.
|
|---|
| 78 | */
|
|---|
| 79 | public final XMLVersion getVersion() {
|
|---|
| 80 | final ODXMLDocument content = this.getContent();
|
|---|
| 81 | if (content == null)
|
|---|
| 82 | return null;
|
|---|
| 83 | else
|
|---|
| 84 | return content.getVersion();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | // *** getter on files
|
|---|
| 89 |
|
|---|
| 90 | public final Set<String> getEntries() {
|
|---|
| 91 | return this.files.keySet();
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | public final ODPackageEntry getEntry(String entry) {
|
|---|
| 95 | return this.files.get(entry);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | protected final Object getData(String entry) {
|
|---|
| 99 | final ODPackageEntry e = this.getEntry(entry);
|
|---|
| 100 | return e == null ? null : e.getData();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | public final ODXMLDocument getXMLFile(String xmlEntry) {
|
|---|
| 104 | return (ODXMLDocument) this.getData(xmlEntry);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | public final ODXMLDocument getXMLFile(final Document doc) {
|
|---|
| 108 | for (final String s : subdocNames) {
|
|---|
| 109 | final ODXMLDocument xmlFile = getXMLFile(s);
|
|---|
| 110 | if (xmlFile != null && xmlFile.getDocument() == doc) {
|
|---|
| 111 | return xmlFile;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | return null;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | public final ODXMLDocument getContent() {
|
|---|
| 118 | return this.getXMLFile("content.xml");
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | public final ODMeta getMeta() { // NO_UCD
|
|---|
| 122 | final ODMeta meta;
|
|---|
| 123 | if (this.getEntries().contains("meta.xml"))
|
|---|
| 124 | meta = ODMeta.create(this.getXMLFile("meta.xml"));
|
|---|
| 125 | else
|
|---|
| 126 | meta = ODMeta.create(this.getContent());
|
|---|
| 127 | return meta;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * Return an XML document.
|
|---|
| 132 | *
|
|---|
| 133 | * @param xmlEntry the filename, eg "styles.xml".
|
|---|
| 134 | * @return the matching document, or <code>null</code> if there's none.
|
|---|
| 135 | */
|
|---|
| 136 | public Document getDocument(String xmlEntry) {
|
|---|
| 137 | final ODXMLDocument xml = this.getXMLFile(xmlEntry);
|
|---|
| 138 | return xml == null ? null : xml.getDocument();
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | // *** setter
|
|---|
| 142 |
|
|---|
| 143 | public void putFile(String entry, Object data) {
|
|---|
| 144 | this.putFile(entry, data, null);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | public void putFile(final String entry, final Object data, final String mediaType) {
|
|---|
| 148 | this.putFile(entry, data, mediaType, true);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | public void putFile(final String entry, final Object data, final String mediaType, final boolean compress) {
|
|---|
| 152 | if (entry == null)
|
|---|
| 153 | throw new NullPointerException("null name");
|
|---|
| 154 | final Object myData;
|
|---|
| 155 | if (subdocNames.contains(entry)) {
|
|---|
| 156 | final ODXMLDocument oodoc;
|
|---|
| 157 | if (data instanceof Document)
|
|---|
| 158 | oodoc = new ODXMLDocument((Document) data);
|
|---|
| 159 | else
|
|---|
| 160 | oodoc = (ODXMLDocument) data;
|
|---|
| 161 | // si le package est vide n'importe quelle version convient
|
|---|
| 162 | if (this.getVersion() != null && !oodoc.getVersion().equals(this.getVersion()))
|
|---|
| 163 | throw new IllegalArgumentException("version mismatch " + this.getVersion() + " != " + oodoc);
|
|---|
| 164 | myData = oodoc;
|
|---|
| 165 | } else if (data != null && !(data instanceof byte[]))
|
|---|
| 166 | throw new IllegalArgumentException("should be byte[] for " + entry + ": " + data);
|
|---|
| 167 | else
|
|---|
| 168 | myData = data;
|
|---|
| 169 | final String inferredType = mediaType != null ? mediaType : FileUtils.findMimeType(entry);
|
|---|
| 170 | this.files.put(entry, new ODPackageEntry(entry, inferredType, myData, compress));
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|