X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.api.acr%2Fsrc%2Forg%2Fargeo%2Fapi%2Facr%2FContent.java;h=df5c149e6fc1fc696187d197766ad5f9cf0c2c52;hb=5724ab347ddfba8f2b21cdcc2fa0b8e1e2b4e527;hp=aeea27ef23ff272b2bb0fb0bf4a95c8c65336e1b;hpb=750a3e154c078d6e9fd72d8da950cb325b8ab012;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 aeea27ef2..df5c149e6 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,18 +1,23 @@ 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; /** * A semi-structured content, with attributes, within a hierarchical structure. */ public interface Content extends Iterable, Map { + /** The base of a repository path. */ + String ROOT_PATH = "/"; QName getName(); @@ -26,30 +31,50 @@ 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()); + } - Optional> getMultiple(QName key, Class clss); + // TODO do we really need the helpers below? + + default Object get(String key) { + return get(unqualified(key)); + } + 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) { Class type; try { @@ -57,14 +82,8 @@ 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; } /* @@ -73,38 +92,197 @@ 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(); + /* + * TYPING + */ + 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(); + for (QName cClass : contentClass) { + if (!contentClasses.contains(cClass)) + return false; + } + 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(); + for (QName cClass : contentClass) { + if (contentClasses.contains(cClass)) + return true; + } + 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 */ - default A adapt(Class clss) throws IllegalArgumentException { - throw new IllegalArgumentException("Cannot adapt content " + this + " to " + clss.getName()); + default A adapt(Class clss) { + throw new UnsupportedOperationException("Cannot adapt content " + this + " to " + clss.getName()); + } + + default C open(Class clss) throws IOException { + throw new UnsupportedOperationException("Cannot open content " + this + " as " + clss.getName()); + } + + default CompletableFuture write(Class clss) { + throw new UnsupportedOperationException("Cannot write content " + this + " as " + clss.getName()); + } + + /* + * CHILDREN + */ + + 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 List children(QNamed name) { + return children(name.qName()); + } + + default Optional soleChild(QNamed name) { + return soleChild(name.qName()); + } + + 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 + */ + /** + * Convenience method returning an attribute as a {@link String}. + * + * @param key the attribute name + * @return the attribute value as a {@link String} or null. + * + * @see Object#toString() + */ + default String attr(QName key) { + return get(key, String.class).orElse(null); } - default C open(Class clss) throws IOException, IllegalArgumentException { - throw new IllegalArgumentException("Cannot open content " + this + " as " + clss.getName()); + /** + * Convenience method returning an attribute as a {@link String}. + * + * @param key the attribute name + * @return the attribute value as a {@link String} or null. + * + * @see Object#toString() + */ + default String attr(QNamed key) { + return attr(key.qName()); + } + + /** + * Convenience method returning an attribute as a {@link String}. + * + * @param key the attribute name + * @return the attribute value as a {@link String} or null. + * + * @see Object#toString() + */ + default String attr(String key) { + return attr(unqualified(key)); } /* - * CONVENIENCE METHODS + * CONTEXT + */ + /** + * A content within this repository + * + * @param path either an absolute path or a path relative to this content */ -// default String attr(String key) { -// return get(key, String.class); -// } -// -// default String attr(Object key) { -// return key != null ? attr(key.toString()) : attr(null); -// } -// -// default A get(Object key, Class clss) { -// return key != null ? get(key.toString(), clss) : get(null, clss); -// } + Optional getContent(String path); /* * EXPERIMENTAL UNSUPPORTED