| 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.text.ParseException;
|
|---|
| 19 | import java.util.ArrayList;
|
|---|
| 20 | import java.util.Calendar;
|
|---|
| 21 | import java.util.Date;
|
|---|
| 22 | import java.util.HashMap;
|
|---|
| 23 | import java.util.List;
|
|---|
| 24 | import java.util.Map;
|
|---|
| 25 |
|
|---|
| 26 | import org.jdom2.Element;
|
|---|
| 27 | import org.jdom2.Namespace;
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * OpenDocument metadata, obtained through {@link ODPackage#getMeta()}.
|
|---|
| 31 | *
|
|---|
| 32 | * @author Sylvain
|
|---|
| 33 | * @see "section 3 of OpenDocument v1.1"
|
|---|
| 34 | */
|
|---|
| 35 | public class ODMeta extends ODNode {
|
|---|
| 36 |
|
|---|
| 37 | static ODMeta create(ODXMLDocument parent) {
|
|---|
| 38 | final Element meta = parent.getChild("meta");
|
|---|
| 39 | return meta == null ? null : new ODMeta(meta, parent);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | private static final Map<XMLVersion, List<Element>> ELEMS_ORDER;
|
|---|
| 43 | static {
|
|---|
| 44 | ELEMS_ORDER = new HashMap<>(2);
|
|---|
| 45 | ELEMS_ORDER.put(XMLVersion.getOOo(), createChildren(XMLVersion.getOOo()));
|
|---|
| 46 | ELEMS_ORDER.put(XMLVersion.getOD(), createChildren(XMLVersion.getOD()));
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | private static final List<Element> createChildren(XMLVersion ins) {
|
|---|
| 50 | final Namespace meta = ins.getMETA();
|
|---|
| 51 | final Namespace dc = ins.getNS("dc");
|
|---|
| 52 | final List<Element> res = new ArrayList<>(8);
|
|---|
| 53 | res.add(new Element("generator", meta));
|
|---|
| 54 | res.add(new Element("title", dc));
|
|---|
| 55 | res.add(new Element("description", dc));
|
|---|
| 56 | res.add(new Element("subject", dc));
|
|---|
| 57 | res.add(new Element("keyword", meta));
|
|---|
| 58 | res.add(new Element("initial-creator", meta));
|
|---|
| 59 | res.add(new Element("creator", dc));
|
|---|
| 60 | res.add(new Element("printed-by", meta));
|
|---|
| 61 | res.add(new Element("creation-date", meta));
|
|---|
| 62 | res.add(new Element("date", dc));
|
|---|
| 63 | res.add(new Element("print-date", meta));
|
|---|
| 64 | res.add(new Element("template", meta));
|
|---|
| 65 | res.add(new Element("auto-reload", meta));
|
|---|
| 66 | res.add(new Element("hyperlink-behaviour", meta));
|
|---|
| 67 | res.add(new Element("language", dc));
|
|---|
| 68 | res.add(new Element("editing-cycles", meta));
|
|---|
| 69 | res.add(new Element("editing-duration", meta));
|
|---|
| 70 | res.add(new Element("document-statistic", meta));
|
|---|
| 71 | res.add(new Element("user-defined", meta));
|
|---|
| 72 | return res;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | // *** instance
|
|---|
| 76 |
|
|---|
| 77 | private final ODXMLDocument parent;
|
|---|
| 78 | private final ChildCreator childCreator;
|
|---|
| 79 |
|
|---|
| 80 | private ODMeta(final Element elem, ODXMLDocument parent) {
|
|---|
| 81 | super(elem);
|
|---|
| 82 | this.parent = parent;
|
|---|
| 83 | this.childCreator = new ChildCreator(this.getElement(), ELEMS_ORDER.get(this.getNS()));
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | protected final ODXMLDocument getParent() {
|
|---|
| 87 | return this.parent;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | private final XMLVersion getNS() {
|
|---|
| 91 | return this.getParent().getVersion();
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | public final String getInitialCreator() { // NO_UCD
|
|---|
| 95 | return this.getMetaChild("initial-creator").getTextTrim();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | public final String getCreator() { // NO_UCD
|
|---|
| 99 | return this.getDCChild("creator").getTextTrim();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | public final Calendar getCreationDate() { // NO_UCD
|
|---|
| 103 | return this.getDateChild("creation-date", this.getNS().getMETA());
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | public final Calendar getModifDate() { // NO_UCD
|
|---|
| 107 | return this.getDateChild("date", this.getNS().getNS("dc"));
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | public final String getLanguage() { // NO_UCD
|
|---|
| 111 | return this.getDCChild("language").getTextTrim();
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * Return the metadata with the passed name, optionnaly creating it.
|
|---|
| 116 | *
|
|---|
| 117 | * @param name the name of user metadata.
|
|---|
| 118 | * @param create <code>true</code> if it should be created.
|
|---|
| 119 | * @return the requested metadata, or <code>null</code> if none is found and <code>create</code>
|
|---|
| 120 | * is <code>false</code>.
|
|---|
| 121 | */
|
|---|
| 122 | public final ODUserDefinedMeta getUserMeta(String name, boolean create) {
|
|---|
| 123 | final Element userElem = ODUserDefinedMeta.getElement(this.getElement(), name, this.getNS());
|
|---|
| 124 | if (userElem != null)
|
|---|
| 125 | return new ODUserDefinedMeta(userElem, this.getParent());
|
|---|
| 126 | else if (create) {
|
|---|
| 127 | final ODUserDefinedMeta res = ODUserDefinedMeta.create(name, this.getParent());
|
|---|
| 128 | this.getElement().addContent(res.getElement());
|
|---|
| 129 | return res;
|
|---|
| 130 | } else
|
|---|
| 131 | return null;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | // * getChild
|
|---|
| 135 |
|
|---|
| 136 | public final Element getMetaChild(final String name) {
|
|---|
| 137 | return this.getChild(name, this.getNS().getMETA());
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | public final Element getDCChild(final String name) {
|
|---|
| 141 | return this.getChild(name, this.getNS().getNS("dc"));
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | private final Element getChild(final String name, final Namespace ns) {
|
|---|
| 145 | return this.childCreator.getChild(ns, name, true);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | private final Calendar getDateChild(final String name, final Namespace ns) {
|
|---|
| 149 | final String date = this.getChild(name, ns).getTextTrim();
|
|---|
| 150 | if (date.length() == 0)
|
|---|
| 151 | return null;
|
|---|
| 152 | else {
|
|---|
| 153 | final Calendar cal = Calendar.getInstance();
|
|---|
| 154 | try {
|
|---|
| 155 | cal.setTime((Date) OOUtils.DATE_FORMAT.parseObject(date));
|
|---|
| 156 | } catch (ParseException e) {
|
|---|
| 157 | throw new IllegalStateException("wrong date: " + date, e);
|
|---|
| 158 | }
|
|---|
| 159 | return cal;
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|