| 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 | /*
|
|---|
| 17 | * Créé le 28 oct. 2004
|
|---|
| 18 | */
|
|---|
| 19 | package org.jopendocument.dom;
|
|---|
| 20 |
|
|---|
| 21 | import java.util.ArrayList;
|
|---|
| 22 | import java.util.HashMap;
|
|---|
| 23 | import java.util.List;
|
|---|
| 24 | import java.util.Map;
|
|---|
| 25 |
|
|---|
| 26 | import org.jdom2.Document;
|
|---|
| 27 | import org.jdom2.Element;
|
|---|
| 28 | import org.jdom2.Namespace;
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * An OpenDocument XML document, like content.xml ou styles.xml.
|
|---|
| 32 | *
|
|---|
| 33 | * @author Sylvain CUAZ
|
|---|
| 34 | */
|
|---|
| 35 | public class ODXMLDocument {
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * All top-level elements that an office document may contain. Note that only the single xml
|
|---|
| 39 | * representation (office:document) contains all of them.
|
|---|
| 40 | */
|
|---|
| 41 | private static final Map<XMLVersion, List<Element>> ELEMS_ORDER;
|
|---|
| 42 | static {
|
|---|
| 43 | ELEMS_ORDER = new HashMap<>(2);
|
|---|
| 44 | ELEMS_ORDER.put(XMLVersion.getOOo(), createChildren(XMLVersion.getOOo()));
|
|---|
| 45 | ELEMS_ORDER.put(XMLVersion.getOD(), createChildren(XMLVersion.getOD()));
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | private static final List<Element> createChildren(XMLVersion ins) {
|
|---|
| 49 | final Namespace ns = ins.getOFFICE();
|
|---|
| 50 | final List<Element> res = new ArrayList<>(8);
|
|---|
| 51 | res.add(new Element("meta", ns));
|
|---|
| 52 | res.add(new Element("settings", ns));
|
|---|
| 53 | res.add(new Element("script", ns));
|
|---|
| 54 | res.add(new Element("font-decls", ns));
|
|---|
| 55 | res.add(new Element("styles", ns));
|
|---|
| 56 | res.add(new Element("automatic-styles", ns));
|
|---|
| 57 | res.add(new Element("master-styles", ns));
|
|---|
| 58 | res.add(new Element("body", ns));
|
|---|
| 59 | return res;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | // namespaces for the name attributes
|
|---|
| 63 | static private final Map<String, String> namePrefixes;
|
|---|
| 64 | static {
|
|---|
| 65 | namePrefixes = new HashMap<>();
|
|---|
| 66 | namePrefixes.put("table:table", "table");
|
|---|
| 67 | namePrefixes.put("text:a", "office");
|
|---|
| 68 | namePrefixes.put("draw:text-box", "draw");
|
|---|
| 69 | namePrefixes.put("draw:image", "draw");
|
|---|
| 70 | namePrefixes.put("draw:frame", "draw");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | private final Document content;
|
|---|
| 74 | private final XMLVersion version;
|
|---|
| 75 | private final ChildCreator childCreator;
|
|---|
| 76 |
|
|---|
| 77 | // before making it public, assure that content is really of version "version"
|
|---|
| 78 | // eg by checking some namespace
|
|---|
| 79 | protected ODXMLDocument(final Document content, final XMLVersion version) {
|
|---|
| 80 | if (content == null)
|
|---|
| 81 | throw new NullPointerException("null document");
|
|---|
| 82 | this.content = content;
|
|---|
| 83 | this.version = version;
|
|---|
| 84 | this.childCreator = new ChildCreator(this.content.getRootElement(), ELEMS_ORDER.get(this.getVersion()));
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | public ODXMLDocument(Document content) {
|
|---|
| 88 | this(content, XMLVersion.getVersion(content.getRootElement()));
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | public ODXMLDocument(ODXMLDocument doc) {
|
|---|
| 92 | this((Document) doc.content.clone(), doc.version);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | public Document getDocument() {
|
|---|
| 96 | return this.content;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | public final XMLVersion getVersion() {
|
|---|
| 100 | return this.version;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | // *** children
|
|---|
| 104 |
|
|---|
| 105 | public final Element getChild(String childName) {
|
|---|
| 106 | return this.getChild(childName, false);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Return the asked child, optionally creating it.
|
|---|
| 111 | *
|
|---|
| 112 | * @param childName the name of the child.
|
|---|
| 113 | * @param create whether it should be created in case it doesn't exist.
|
|---|
| 114 | * @return the asked child or <code>null</code> if it doesn't exist and create is
|
|---|
| 115 | * <code>false</code>
|
|---|
| 116 | */
|
|---|
| 117 | public Element getChild(String childName, boolean create) {
|
|---|
| 118 | return this.childCreator.getChild(this.getVersion().getOFFICE(), childName, create);
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|