| 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.util.HashMap;
|
|---|
| 19 | import java.util.Map;
|
|---|
| 20 |
|
|---|
| 21 | import org.jdom2.Element;
|
|---|
| 22 | import org.jdom2.Namespace;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Encapsulate all namespaces for a particular version of xml.
|
|---|
| 26 | *
|
|---|
| 27 | * @author ILM Informatique 26 juil. 2004
|
|---|
| 28 | */
|
|---|
| 29 | public enum XMLVersion {
|
|---|
| 30 |
|
|---|
| 31 | // OpenOffice.org 1.x.
|
|---|
| 32 | OOo("OpenOffice.org", Namespace.getNamespace("manifest", "http://openoffice.org/2001/manifest")) {
|
|---|
| 33 | {
|
|---|
| 34 | this.putMandatory(OFFICE_1, STYLE_1, TEXT_1, TABLE_1);
|
|---|
| 35 | this.put("number", NUMBER_1);
|
|---|
| 36 | this.put("draw", DRAW_1);
|
|---|
| 37 | this.put("number", NUMBER_1);
|
|---|
| 38 | this.put("fo", FO_1);
|
|---|
| 39 | this.put("form", "http://openoffice.org/2000/form");
|
|---|
| 40 | this.put("xlink", "http://www.w3.org/1999/xlink");
|
|---|
| 41 | this.put("script", "http://openoffice.org/2000/script");
|
|---|
| 42 | this.put("svg", "http://www.w3.org/2000/svg");
|
|---|
| 43 | this.put("meta", "http://openoffice.org/2000/meta");
|
|---|
| 44 | this.put("dc", "http://purl.org/dc/elements/1.1/");
|
|---|
| 45 | }
|
|---|
| 46 | },
|
|---|
| 47 | // OpenDocument 1.x/OpenOffice.org 2.x.
|
|---|
| 48 | OD("OpenDocument", Namespace.getNamespace("manifest", "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0")) {
|
|---|
| 49 | {
|
|---|
| 50 | this.putMandatory(OFFICE_2, STYLE_2, TEXT_2, TABLE_2);
|
|---|
| 51 | this.put("number", NUMBER_2);
|
|---|
| 52 | this.put("draw", DRAW_2);
|
|---|
| 53 | this.put("number", NUMBER_2);
|
|---|
| 54 | this.put("fo", FO_2);
|
|---|
| 55 | this.put("form", "urn:oasis:names:tc:opendocument:xmlns:form:1.0");
|
|---|
| 56 | this.put("xlink", "http://www.w3.org/1999/xlink");
|
|---|
| 57 | this.put("script", "urn:oasis:names:tc:opendocument:xmlns:script:1.0");
|
|---|
| 58 | this.put("svg", "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0");
|
|---|
| 59 | this.put("meta", "urn:oasis:names:tc:opendocument:xmlns:meta:1.0");
|
|---|
| 60 | this.put("dc", "http://purl.org/dc/elements/1.1/");
|
|---|
| 61 | }
|
|---|
| 62 | };
|
|---|
| 63 |
|
|---|
| 64 | private static final String OFFICE_1 = "http://openoffice.org/2000/office";
|
|---|
| 65 | private static final String STYLE_1 = "http://openoffice.org/2000/style";
|
|---|
| 66 | private static final String TEXT_1 = "http://openoffice.org/2000/text";
|
|---|
| 67 | private static final String NUMBER_1 = "http://openoffice.org/2000/datastyle";
|
|---|
| 68 | private static final String TABLE_1 = "http://openoffice.org/2000/table";
|
|---|
| 69 | private static final String DRAW_1 = "http://openoffice.org/2000/drawing";
|
|---|
| 70 | private static final String FO_1 = "http://www.w3.org/1999/XSL/Format";
|
|---|
| 71 |
|
|---|
| 72 | private static final String OFFICE_2 = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
|
|---|
| 73 | private static final String STYLE_2 = "urn:oasis:names:tc:opendocument:xmlns:style:1.0";
|
|---|
| 74 | private static final String TEXT_2 = "urn:oasis:names:tc:opendocument:xmlns:text:1.0";
|
|---|
| 75 | private static final String NUMBER_2 = "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0";
|
|---|
| 76 | private static final String TABLE_2 = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
|
|---|
| 77 | private static final String DRAW_2 = "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0";
|
|---|
| 78 | private static final String FO_2 = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0";
|
|---|
| 79 |
|
|---|
| 80 | private final Map<String, Namespace> nss;
|
|---|
| 81 |
|
|---|
| 82 | private XMLVersion(String name, Namespace manifest) {
|
|---|
| 83 | this.nss = new HashMap<>(16);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | protected final void putMandatory(String office, String style, String text, String table) {
|
|---|
| 87 | this.put("office", office);
|
|---|
| 88 | this.put("style", style);
|
|---|
| 89 | this.put("text", text);
|
|---|
| 90 | this.put("table", table);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | protected final void put(String prefix, String uri) {
|
|---|
| 94 | this.nss.put(prefix, Namespace.getNamespace(prefix, uri));
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | public final Namespace getNS(String prefix) {
|
|---|
| 98 | if (!this.nss.containsKey(prefix))
|
|---|
| 99 | throw new IllegalStateException("unknown " + prefix + " : " + this.nss.keySet());
|
|---|
| 100 | return this.nss.get(prefix);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | public Namespace getOFFICE() {
|
|---|
| 104 | return this.getNS("office");
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | public Namespace getSTYLE() { // NO_UCD
|
|---|
| 108 | return this.getNS("style");
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | public Namespace getTEXT() { // NO_UCD
|
|---|
| 112 | return this.getNS("text");
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | public Namespace getTABLE() {
|
|---|
| 116 | return this.getNS("table");
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | public Namespace getMETA() {
|
|---|
| 120 | return this.getNS("meta");
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | // *** static public
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Namespaces for OpenOffice.org 1.x.
|
|---|
| 127 | *
|
|---|
| 128 | * @return namespaces for OO.o 1.
|
|---|
| 129 | */
|
|---|
| 130 | public static final XMLVersion getOOo() {
|
|---|
| 131 | return OOo;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Namespaces for OpenDocument/OpenOffice.org 2.x.
|
|---|
| 136 | *
|
|---|
| 137 | * @return namespaces for OpenDocument.
|
|---|
| 138 | */
|
|---|
| 139 | public static final XMLVersion getOD() {
|
|---|
| 140 | return OD;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /**
|
|---|
| 144 | * Find the NS to which belongs the passed namespace.
|
|---|
| 145 | *
|
|---|
| 146 | * @param ns the namespace, eg office=http://openoffice.org/2000/office.
|
|---|
| 147 | * @return the matching NS, eg NS.getOOo(), or <code>null</code> if none is found.
|
|---|
| 148 | */
|
|---|
| 149 | public static final XMLVersion getParent(Namespace ns) {
|
|---|
| 150 | for (XMLVersion v : values()) {
|
|---|
| 151 | if (v.getNS(ns.getPrefix()).equals(ns))
|
|---|
| 152 | return v;
|
|---|
| 153 | }
|
|---|
| 154 | return null;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * Infer the version of an XML element from its namespace.
|
|---|
| 159 | *
|
|---|
| 160 | * @param elem the element to be tested, eg <text:line-break/>.
|
|---|
| 161 | * @return the version.
|
|---|
| 162 | * @throws IllegalArgumentException if the namespace is unknown.
|
|---|
| 163 | */
|
|---|
| 164 | public static final XMLVersion getVersion(Element elem) {
|
|---|
| 165 | final XMLVersion parent = getParent(elem.getNamespace());
|
|---|
| 166 | if (parent == null)
|
|---|
| 167 | throw new IllegalArgumentException(elem + " is not an OpenOffice element.");
|
|---|
| 168 | return parent;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|