| 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.Collection;
|
|---|
| 19 | import java.util.Collections;
|
|---|
| 20 | import java.util.List;
|
|---|
| 21 |
|
|---|
| 22 | import org.jdom2.Element;
|
|---|
| 23 | import org.jopendocument.util.CollectionMap;
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Describe a family of style.
|
|---|
| 27 | *
|
|---|
| 28 | * @author Sylvain CUAZ
|
|---|
| 29 | *
|
|---|
| 30 | * @param <S> type of style
|
|---|
| 31 | */
|
|---|
| 32 | public abstract class StyleDesc<S extends StyleStyle> {
|
|---|
| 33 |
|
|---|
| 34 | public static <C extends StyleStyle> StyleDesc<C> copy(final StyleDesc<C> toClone, final XMLVersion version) {
|
|---|
| 35 | final StyleDesc<C> res = new StyleDesc<C>(toClone.getStyleClass(), version, toClone.getFamily(), toClone.getBaseName()) {
|
|---|
| 36 | @Override
|
|---|
| 37 | public C create(ODPackage pkg, Element e) {
|
|---|
| 38 | return toClone.create(pkg, e);
|
|---|
| 39 | }
|
|---|
| 40 | };
|
|---|
| 41 | res.getRefElementsMap().putAll(toClone.getRefElementsMap());
|
|---|
| 42 | res.getMultiRefElementsMap().putAll(toClone.getMultiRefElementsMap());
|
|---|
| 43 | return res;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | private final Class<S> clazz;
|
|---|
| 47 | // need version since each one might have different attributes and elements (plus we need it
|
|---|
| 48 | // anyway for the XPath, otherwise it fails when searching for an inexistant namespace)
|
|---|
| 49 | private final XMLVersion version;
|
|---|
| 50 | private final String family, baseName;
|
|---|
| 51 | // { attribute -> element }
|
|---|
| 52 | private final CollectionMap<String, String> refElements;
|
|---|
| 53 | private final CollectionMap<String, String> multiRefElements;
|
|---|
| 54 |
|
|---|
| 55 | protected StyleDesc(final Class<S> clazz, final XMLVersion version, String family, String baseName, String ns) {
|
|---|
| 56 | this(clazz, version, family, baseName, ns, Collections.singletonList(ns + ":" + family));
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | protected StyleDesc(final Class<S> clazz, final XMLVersion version, String family, String baseName, String ns, final List<String> refQNames) {
|
|---|
| 60 | this(clazz, version, family, baseName);
|
|---|
| 61 | this.getRefElementsMap().putAll(ns + ":style-name", refQNames);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | protected StyleDesc(final Class<S> clazz, final XMLVersion version, String family, String baseName) {
|
|---|
| 65 | super();
|
|---|
| 66 | this.clazz = clazz;
|
|---|
| 67 | this.version = version;
|
|---|
| 68 | this.family = family;
|
|---|
| 69 | this.baseName = baseName;
|
|---|
| 70 | this.refElements = new CollectionMap<>();
|
|---|
| 71 | // 4 since they are not common
|
|---|
| 72 | this.multiRefElements = new CollectionMap<>(4);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | public abstract S create(ODPackage pkg, Element e);
|
|---|
| 76 |
|
|---|
| 77 | final Class<S> getStyleClass() {
|
|---|
| 78 | return this.clazz;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | public final XMLVersion getVersion() {
|
|---|
| 82 | return this.version;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | public final String getFamily() {
|
|---|
| 86 | return this.family;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public final String getBaseName() {
|
|---|
| 90 | return this.baseName;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * The list of elements that can point to this family of style.
|
|---|
| 95 | *
|
|---|
| 96 | * @return a list of qualified names, e.g. ["text:h", "text:p"].
|
|---|
| 97 | */
|
|---|
| 98 | protected final Collection<String> getRefElements() {
|
|---|
| 99 | return this.getRefElementsMap().values();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | // e.g. { "text:style-name" -> ["text:h", "text:p"] }
|
|---|
| 103 | protected final CollectionMap<String, String> getRefElementsMap() {
|
|---|
| 104 | return this.refElements;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | // e.g. { "table:default-cell-style-name" -> ["table:table-column", "table:table-row"] }
|
|---|
| 108 | protected final CollectionMap<String, String> getMultiRefElementsMap() {
|
|---|
| 109 | return this.multiRefElements;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|