| 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.EnumSet;
|
|---|
| 19 | import java.util.List;
|
|---|
| 20 |
|
|---|
| 21 | import org.jdom2.Attribute;
|
|---|
| 22 | import org.jdom2.Element;
|
|---|
| 23 | import org.jdom2.Namespace;
|
|---|
| 24 |
|
|---|
| 25 | // eg <meta:user-defined meta:name="countOfSomething">5.2</meta:user-defined>
|
|---|
| 26 | public class ODUserDefinedMeta extends ODNode {
|
|---|
| 27 |
|
|---|
| 28 | private static final String ELEM_NAME = "user-defined";
|
|---|
| 29 |
|
|---|
| 30 | static ODUserDefinedMeta create(String name, ODXMLDocument parent) {
|
|---|
| 31 | final Element elem = new Element(ELEM_NAME, parent.getVersion().getMETA());
|
|---|
| 32 | elem.setAttribute("name", name, parent.getVersion().getMETA());
|
|---|
| 33 | final ODUserDefinedMeta res = new ODUserDefinedMeta(elem, parent);
|
|---|
| 34 | res.setValue("");
|
|---|
| 35 | return res;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | @SuppressWarnings("unchecked")
|
|---|
| 39 | static private List<Element> getChildren(final Element metaElem, final Namespace metaNS) {
|
|---|
| 40 | return metaElem.getChildren(ELEM_NAME, metaNS);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | static Element getElement(final Element metaElem, String name, final XMLVersion ns) {
|
|---|
| 44 | final Namespace metaNS = ns.getMETA();
|
|---|
| 45 | for (final Element elem : getChildren(metaElem, metaNS)) {
|
|---|
| 46 | if (name.equals(elem.getAttributeValue("name", metaNS)))
|
|---|
| 47 | return elem;
|
|---|
| 48 | }
|
|---|
| 49 | return null;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | private static final EnumSet<ODValueType> allowedTypes = EnumSet.of(ODValueType.FLOAT, ODValueType.DATE, ODValueType.TIME, ODValueType.BOOLEAN, ODValueType.STRING);
|
|---|
| 53 |
|
|---|
| 54 | private final ODXMLDocument parent;
|
|---|
| 55 |
|
|---|
| 56 | ODUserDefinedMeta(final Element elem, ODXMLDocument parent) {
|
|---|
| 57 | super(elem);
|
|---|
| 58 | this.parent = parent;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | protected final ODXMLDocument getParent() {
|
|---|
| 62 | return this.parent;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | private final XMLVersion getNS() {
|
|---|
| 66 | return this.getParent().getVersion();
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | private final Attribute getValueTypeAttr() {
|
|---|
| 70 | return getValueTypeAttr(true);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | private final Attribute getValueTypeAttr(boolean create) {
|
|---|
| 74 | Attribute res = this.getElement().getAttribute("value-type", this.getNS().getMETA());
|
|---|
| 75 | // oo don't put value-type for strings (eg File/Properties/User)
|
|---|
| 76 | if (res == null && create) {
|
|---|
| 77 | res = new Attribute("value-type", ODValueType.STRING.getName(), this.getNS().getMETA());
|
|---|
| 78 | this.getElement().setAttribute(res);
|
|---|
| 79 | }
|
|---|
| 80 | return res;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | public final void setValue(Object o) {
|
|---|
| 84 | this.setValue(o, ODValueType.forObject(o));
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | public final void setValue(Object o, final ODValueType vt) {
|
|---|
| 88 | if (!allowedTypes.contains(vt))
|
|---|
| 89 | throw new IllegalArgumentException(vt + " is not allowed: " + allowedTypes);
|
|---|
| 90 | if (vt != ODValueType.STRING)
|
|---|
| 91 | this.getValueTypeAttr().setValue(vt.getName());
|
|---|
| 92 | else {
|
|---|
| 93 | // OOo doesn't support value types
|
|---|
| 94 | final Attribute attr = this.getValueTypeAttr(false);
|
|---|
| 95 | if (attr != null)
|
|---|
| 96 | attr.detach();
|
|---|
| 97 | }
|
|---|
| 98 | this.getElement().setText(vt.format(o));
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | }
|
|---|