]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - AbstractContent.java
54d7410155961e6f1522f88fff4a1eddf7509e97
1 package org
.argeo
.cms
.acr
;
3 import java
.util
.AbstractMap
;
4 import java
.util
.AbstractSet
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Collections
;
7 import java
.util
.Iterator
;
10 import java
.util
.Optional
;
13 import javax
.xml
.namespace
.QName
;
15 import org
.argeo
.api
.acr
.Content
;
16 import org
.argeo
.api
.acr
.CrAttributeType
;
17 import org
.argeo
.api
.acr
.CrName
;
18 import org
.argeo
.api
.acr
.NamespaceUtils
;
19 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
20 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
21 import org
.argeo
.cms
.util
.LangUtils
;
23 /** Partial reference implementation of a {@link ProvidedContent}. */
24 public abstract class AbstractContent
extends AbstractMap
<QName
, Object
> implements ProvidedContent
{
25 private final ProvidedSession session
;
28 // private String _path = null;
30 public AbstractContent(ProvidedSession session
) {
31 this.session
= session
;
35 * ATTRIBUTES OPERATIONS
37 // protected abstract Iterable<QName> keys();
39 // protected abstract void removeAttr(QName key);
42 public Set
<Entry
<QName
, Object
>> entrySet() {
43 Set
<Entry
<QName
, Object
>> result
= new AttrSet();
48 public Class
<?
> getType(QName key
) {
53 public boolean isMultiple(QName key
) {
57 @SuppressWarnings("unchecked")
59 public <A
> List
<A
> getMultiple(QName key
, Class
<A
> clss
) {
60 Object value
= get(key
);
62 return new ArrayList
<>();
63 if (value
instanceof List
) {
64 if (clss
.isAssignableFrom(Object
.class))
65 return (List
<A
>) value
;
66 List
<A
> res
= new ArrayList
<>();
67 List
<?
> lst
= (List
<?
>) value
;
68 for (Object o
: lst
) {
69 A item
= CrAttributeType
.cast(clss
, o
).get();
74 A res
= CrAttributeType
.cast(clss
, value
).get();
75 return Collections
.singletonList(res
);
84 public String
getPath() {
87 List
<Content
> ancestors
= new ArrayList
<>();
88 collectAncestors(ancestors
, this);
89 StringBuilder path
= new StringBuilder();
90 ancestors
: for (Content c
: ancestors
) {
91 QName name
= c
.getName();
92 if (CrName
.root
.qName().equals(name
))
96 path
.append(NamespaceUtils
.toPrefixedName(name
));
97 int siblingIndex
= c
.getSiblingIndex();
98 if (siblingIndex
!= 1)
99 path
.append('[').append(siblingIndex
).append(']');
101 // _path = path.toString();
103 return path
.toString();
106 private void collectAncestors(List
<Content
> ancestors
, Content content
) {
109 ancestors
.add(0, content
);
110 collectAncestors(ancestors
, content
.getParent());
114 public int getDepth() {
115 List
<Content
> ancestors
= new ArrayList
<>();
116 collectAncestors(ancestors
, this);
117 return ancestors
.size();
121 public boolean isRoot() {
122 return CrName
.root
.qName().equals(getName());
126 public String
getSessionLocalId() {
135 public ProvidedSession
getSession() {
144 public List
<QName
> getContentClasses() {
145 return new ArrayList
<>();
151 // protected boolean isDefaultAttrTypeRequested(Class<?> clss) {
152 // // check whether clss is Object.class
153 // return clss.isAssignableFrom(Object.class);
157 // public String toString() {
158 // return "content " + getPath();
166 // - cannot be modified
168 public Iterator
<Content
> iterator() {
169 return Collections
.emptyIterator();
173 public Content
add(QName name
, QName
... classes
) {
174 throw new UnsupportedOperationException("Content cannot be added.");
178 public void remove() {
179 throw new UnsupportedOperationException("Content cannot be removed.");
182 protected Iterable
<QName
> keys() {
183 return Collections
.emptySet();
187 public <A
> Optional
<A
> get(QName key
, Class
<A
> clss
) {
188 return Optional
.empty();
191 protected void removeAttr(QName key
) {
192 throw new UnsupportedOperationException("Attributes cannot be removed.");
199 class AttrSet
extends AbstractSet
<Entry
<QName
, Object
>> {
202 public Iterator
<Entry
<QName
, Object
>> iterator() {
203 final Iterator
<QName
> keys
= keys().iterator();
204 Iterator
<Entry
<QName
, Object
>> it
= new Iterator
<Map
.Entry
<QName
, Object
>>() {
209 public boolean hasNext() {
210 return keys
.hasNext();
214 public Entry
<QName
, Object
> next() {
217 Optional
<?
> value
= get(key
, Object
.class);
218 assert !value
.isEmpty();
219 AbstractMap
.SimpleEntry
<QName
, Object
> entry
= new SimpleEntry
<>(key
, value
.get());
224 public void remove() {
226 AbstractContent
.this.removeAttr(key
);
228 throw new IllegalStateException("Iteration has not started");
238 return LangUtils
.size(keys());
247 public String
toString() {
248 return "content " + getPath();