X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.acr%2Fsrc%2Forg%2Fargeo%2Fapi%2Facr%2FContent.java;h=f52ab31b8f25223e109b4781707502f924cd5ebd;hb=f3cb3bed85cb528d919193ce6abb2bb8d05faf03;hp=0fdc14fe30e9f0146afbc8f8dde42187086d0200;hpb=55d1a78150b6be0004f6bcb28703bcdd6daf55a1;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.api.acr/src/org/argeo/api/acr/Content.java b/org.argeo.api.acr/src/org/argeo/api/acr/Content.java index 0fdc14fe3..f52ab31b8 100644 --- a/org.argeo.api.acr/src/org/argeo/api/acr/Content.java +++ b/org.argeo.api.acr/src/org/argeo/api/acr/Content.java @@ -1,13 +1,15 @@ package org.argeo.api.acr; +import static org.argeo.api.acr.NamespaceUtils.unqualified; + import java.io.Closeable; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.CompletableFuture; -import javax.xml.XMLConstants; import javax.xml.namespace.QName; /** @@ -27,29 +29,48 @@ public interface Content extends Iterable, Map { Optional get(QName key, Class clss); - default Object get(String key) { - if (key.indexOf(':') >= 0) - throw new IllegalArgumentException("Name " + key + " has a prefix"); - return get(new QName(XMLConstants.NULL_NS_URI, key, XMLConstants.DEFAULT_NS_PREFIX)); + Class getType(QName key); + + boolean isMultiple(QName key); + + List getMultiple(QName key, Class clss); + + /* + * ATTRIBUTES OPERATION HELPERS + */ + default boolean containsKey(QNamed key) { + return containsKey(key.qName()); } - default Object put(String key, Object value) { - if (key.indexOf(':') >= 0) - throw new IllegalArgumentException("Name " + key + " has a prefix"); - return put(new QName(XMLConstants.NULL_NS_URI, key, XMLConstants.DEFAULT_NS_PREFIX), value); + default Optional get(QNamed key, Class clss) { + return get(key.qName(), clss); } - default Object remove(String key) { - if (key.indexOf(':') >= 0) - throw new IllegalArgumentException("Name " + key + " has a prefix"); - return remove(new QName(XMLConstants.NULL_NS_URI, key, XMLConstants.DEFAULT_NS_PREFIX)); + default Object get(QNamed key) { + return get(key.qName()); } - Class getType(QName key); + default Object put(QNamed key, Object value) { + return put(key.qName(), value); + } - boolean isMultiple(QName key); + default Object remove(QNamed key) { + return remove(key.qName()); + } + + // TODO do we really need the helpers below? + + default Object get(String key) { + return get(unqualified(key)); + } - Optional> getMultiple(QName key, Class clss); + default Object put(String key, Object value) { + return put(unqualified(key), value); + } + + default Object remove(String key) { + return remove(unqualified(key)); + } @SuppressWarnings("unchecked") default List getMultiple(QName key) { @@ -59,14 +80,15 @@ public interface Content extends Iterable, Map { } catch (ClassCastException e) { throw new IllegalArgumentException("Requested type is not the default type"); } - Optional> res = getMultiple(key, type); - if (res == null) - return null; - else { - if (res.isEmpty()) - throw new IllegalStateException("Metadata " + key + " is not availabel as list of type " + type); - return res.get(); - } + List res = getMultiple(key, type); + return res; +// if (res == null) +// return null; +// else { +// if (res.isEmpty()) +// throw new IllegalStateException("Metadata " + key + " is not availabel as list of type " + type); +// return res.get(); +// } } /* @@ -82,9 +104,7 @@ public interface Content extends Iterable, Map { Content add(QName name, QName... classes); default Content add(String name, QName... classes) { - if (name.indexOf(':') >= 0) - throw new IllegalArgumentException("Name " + name + " has a prefix"); - return add(new QName(XMLConstants.NULL_NS_URI, name, XMLConstants.DEFAULT_NS_PREFIX), classes); + return add(unqualified(name), classes); } void remove(); @@ -94,6 +114,10 @@ public interface Content extends Iterable, Map { */ List getContentClasses(); + default void addContentClasses(QName... contentClass) { + throw new UnsupportedOperationException("Adding content classes to " + getPath() + " is not supported"); + } + /** AND */ default boolean isContentClass(QName... contentClass) { List contentClasses = getContentClasses(); @@ -104,6 +128,14 @@ public interface Content extends Iterable, Map { return true; } + /** AND */ + default boolean isContentClass(QNamed... contentClass) { + List lst = new ArrayList<>(); + for (QNamed qNamed : contentClass) + lst.add(qNamed.qName()); + return isContentClass(lst.toArray(new QName[lst.size()])); + } + /** OR */ default boolean hasContentClass(QName... contentClass) { List contentClasses = getContentClasses(); @@ -114,6 +146,22 @@ public interface Content extends Iterable, Map { return false; } + /** OR */ + default boolean hasContentClass(QNamed... contentClass) { + List lst = new ArrayList<>(); + for (QNamed qNamed : contentClass) + lst.add(qNamed.qName()); + return hasContentClass(lst.toArray(new QName[lst.size()])); + } + + /* + * SIBLINGS + */ + + default int getSiblingIndex() { + return 1; + } + /* * DEFAULT METHODS */ @@ -130,22 +178,84 @@ public interface Content extends Iterable, Map { } /* - * CONVENIENCE METHODS + * CHILDREN */ - default String attr(String key) { - Object obj = get(key); - if (obj == null) - return null; - return obj.toString(); + default boolean hasChild(QName name) { + for (Content child : this) { + if (child.getName().equals(name)) + return true; + } + return false; + } + + default boolean hasChild(QNamed name) { + return hasChild(name.qName()); } + default Content anyOrAddChild(QName name, QName... classes) { + Content child = anyChild(name); + if (child != null) + return child; + return this.add(name, classes); + } + + default Content anyOrAddChild(String name, QName... classes) { + return anyOrAddChild(unqualified(name), classes); + } + + /** Any child with this name, or null if there is none */ + default Content anyChild(QName name) { + for (Content child : this) { + if (child.getName().equals(name)) + return child; + } + return null; + } + + default List children(QName name) { + List res = new ArrayList<>(); + for (Content child : this) { + if (child.getName().equals(name)) + res.add(child); + } + return res; + } + + default Optional soleChild(QName name) { + List res = children(name); + if (res.isEmpty()) + return Optional.empty(); + if (res.size() > 1) + throw new IllegalStateException(this + " has multiple children with name " + name); + return Optional.of(res.get(0)); + } + + default Content child(QName name) { + return soleChild(name).orElseThrow(); + } + + default Content child(QNamed name) { + return child(name.qName()); + } + + /* + * ATTR AS STRING + */ default String attr(QName key) { + // TODO check String type? Object obj = get(key); if (obj == null) return null; return obj.toString(); + } + + default String attr(QNamed key) { + return attr(key.qName()); + } + default String attr(String key) { + return attr(unqualified(key)); } // // default String attr(Object key) {