| 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 | import org.jopendocument.dom.spreadsheet.CellStyle;
|
|---|
| 24 | import org.jopendocument.dom.spreadsheet.ColumnStyle;
|
|---|
| 25 | import org.jopendocument.dom.spreadsheet.RowStyle;
|
|---|
| 26 | import org.jopendocument.dom.spreadsheet.TableStyle;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * A style:style, see section 14.1. Maintains a map of family to classes.
|
|---|
| 30 | *
|
|---|
| 31 | * @author Sylvain
|
|---|
| 32 | */
|
|---|
| 33 | public class StyleStyle extends ODNode {
|
|---|
| 34 |
|
|---|
| 35 | private static final Map<XMLVersion, Map<String, StyleDesc<?>>> family2Desc;
|
|---|
| 36 | private static final Map<XMLVersion, Map<Class<? extends StyleStyle>, StyleDesc<?>>> class2Desc;
|
|---|
| 37 | private static boolean descsLoaded = false;
|
|---|
| 38 | static {
|
|---|
| 39 | family2Desc = new HashMap<>();
|
|---|
| 40 | class2Desc = new HashMap<>();
|
|---|
| 41 | for (final XMLVersion v : XMLVersion.values()) {
|
|---|
| 42 | family2Desc.put(v, new HashMap<String, StyleDesc<?>>());
|
|---|
| 43 | class2Desc.put(v, new HashMap<Class<? extends StyleStyle>, StyleDesc<?>>());
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | // lazy initialization to avoid circular dependency (i.e. ClassLoader loads PStyle.DESC which
|
|---|
| 48 | // loads StyleStyle which needs PStyle.DESC)
|
|---|
| 49 | private static void loadDescs() {
|
|---|
| 50 | if (!descsLoaded) {
|
|---|
| 51 | registerAllVersions(CellStyle.DESC);
|
|---|
| 52 | registerAllVersions(RowStyle.DESC);
|
|---|
| 53 | registerAllVersions(ColumnStyle.DESC);
|
|---|
| 54 | registerAllVersions(TableStyle.DESC);
|
|---|
| 55 | descsLoaded = true;
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | // until now styles have remained constant through versions
|
|---|
| 60 | private static void registerAllVersions(StyleDesc<? extends StyleStyle> desc) {
|
|---|
| 61 | for (final XMLVersion v : XMLVersion.values()) {
|
|---|
| 62 | if (v == desc.getVersion())
|
|---|
| 63 | register(desc);
|
|---|
| 64 | else
|
|---|
| 65 | register(StyleDesc.copy(desc, v));
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | public static void register(StyleDesc<? extends StyleStyle> desc) {
|
|---|
| 70 | if (family2Desc.get(desc.getVersion()).put(desc.getFamily(), desc) != null)
|
|---|
| 71 | throw new IllegalStateException(desc.getFamily() + " duplicate");
|
|---|
| 72 | if (class2Desc.get(desc.getVersion()).put(desc.getStyleClass(), desc) != null)
|
|---|
| 73 | throw new IllegalStateException(desc.getStyleClass() + " duplicate");
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | static <S extends StyleStyle> StyleDesc<S> getStyleDesc(Class<S> clazz, final XMLVersion version) {
|
|---|
| 77 | return getStyleDesc(clazz, version, true);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | @SuppressWarnings("unchecked")
|
|---|
| 81 | private static <S extends StyleStyle> StyleDesc<S> getStyleDesc(Class<S> clazz, final XMLVersion version, final boolean mustExist) {
|
|---|
| 82 | loadDescs();
|
|---|
| 83 | final Map<Class<? extends StyleStyle>, StyleDesc<?>> map = class2Desc.get(version);
|
|---|
| 84 | if (map.containsKey(clazz))
|
|---|
| 85 | return (StyleDesc<S>) map.get(clazz);
|
|---|
| 86 | else if (mustExist)
|
|---|
| 87 | throw new IllegalArgumentException("unregistered " + clazz + " for version " + version);
|
|---|
| 88 | else
|
|---|
| 89 | return null;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | private final StyleDesc<?> desc;
|
|---|
| 93 | private final ODPackage pkg;
|
|---|
| 94 | private final String name, family;
|
|---|
| 95 | private final XMLVersion ns;
|
|---|
| 96 |
|
|---|
| 97 | public StyleStyle(final ODPackage pkg, final Element styleElem) {
|
|---|
| 98 | super(styleElem);
|
|---|
| 99 | this.pkg = pkg;
|
|---|
| 100 | this.name = this.getElement().getAttributeValue("name", this.getSTYLE());
|
|---|
| 101 | this.family = this.getElement().getAttributeValue("family", this.getSTYLE());
|
|---|
| 102 | this.ns = this.pkg.getVersion();
|
|---|
| 103 | this.desc = getStyleDesc(this.getClass(), this.ns, false);
|
|---|
| 104 | if (this.desc != null && !this.desc.getFamily().equals(this.getFamily()))
|
|---|
| 105 | throw new IllegalArgumentException("expected " + this.desc.getFamily() + " but got " + this.getFamily() + " for " + styleElem);
|
|---|
| 106 | // assert that styleElem is in pkg (and thus have the same version)
|
|---|
| 107 | assert this.pkg.getXMLFile(getElement().getDocument()) != null;
|
|---|
| 108 | assert this.pkg.getVersion() == XMLVersion.getVersion(getElement());
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | protected final Namespace getSTYLE() {
|
|---|
| 112 | return this.getElement().getNamespace("style");
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | public final XMLVersion getNS() { // NO_UCD
|
|---|
| 116 | return this.ns;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | public final String getName() {
|
|---|
| 120 | return this.name;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | public final String getFamily() {
|
|---|
| 124 | return this.family;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | @Override
|
|---|
| 128 | public final boolean equals(Object obj) {
|
|---|
| 129 | if (!(obj instanceof StyleStyle))
|
|---|
| 130 | return false;
|
|---|
| 131 | final StyleStyle o = (StyleStyle) obj;
|
|---|
| 132 | return this.getName().equals(o.getName()) && this.getFamily().equals(o.getFamily());
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | @Override
|
|---|
| 136 | public int hashCode() {
|
|---|
| 137 | return this.getName().hashCode() + this.getFamily().hashCode();
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|