]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/AbstractContent.java
1cffef40ef5cc77f5be0783bfd5087ba48c1a565
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
.CrName
;
17 import org
.argeo
.api
.acr
.NamespaceUtils
;
18 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
19 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
20 import org
.argeo
.util
.LangUtils
;
22 /** Partial reference implementation of a {@link ProvidedContent}. */
23 public abstract class AbstractContent
extends AbstractMap
<QName
, Object
> implements ProvidedContent
{
24 private final ProvidedSession session
;
27 // private String _path = null;
29 public AbstractContent(ProvidedSession session
) {
30 this.session
= session
;
34 * ATTRIBUTES OPERATIONS
36 // protected abstract Iterable<QName> keys();
38 // protected abstract void removeAttr(QName key);
41 public Set
<Entry
<QName
, Object
>> entrySet() {
42 Set
<Entry
<QName
, Object
>> result
= new AttrSet();
47 public Class
<?
> getType(QName key
) {
52 public boolean isMultiple(QName key
) {
56 @SuppressWarnings("unchecked")
58 public <A
> Optional
<List
<A
>> getMultiple(QName key
, Class
<A
> clss
) {
59 Object value
= get(key
);
61 return Optional
.empty();
62 if (value
instanceof List
) {
64 List
<A
> res
= (List
<A
>) value
;
65 return Optional
.of(res
);
66 } catch (ClassCastException e
) {
67 List
<A
> res
= new ArrayList
<>();
68 List
<?
> lst
= (List
<?
>) value
;
70 for (Object o
: lst
) {
74 return Optional
.of(res
);
75 } catch (ClassCastException e1
) {
76 return Optional
.empty();
82 return Optional
.of(Collections
.singletonList(res
));
83 } catch (ClassCastException e
) {
84 return Optional
.empty();
94 public String
getPath() {
97 List
<Content
> ancestors
= new ArrayList
<>();
98 collectAncestors(ancestors
, this);
99 StringBuilder path
= new StringBuilder();
100 ancestors
: for (Content c
: ancestors
) {
101 QName name
= c
.getName();
102 if (CrName
.root
.qName().equals(name
))
106 path
.append(NamespaceUtils
.toPrefixedName(name
));
107 int siblingIndex
= c
.getSiblingIndex();
108 if (siblingIndex
!= 1)
109 path
.append('[').append(siblingIndex
).append(']');
111 // _path = path.toString();
113 return path
.toString();
116 private void collectAncestors(List
<Content
> ancestors
, Content content
) {
119 ancestors
.add(0, content
);
120 collectAncestors(ancestors
, content
.getParent());
124 public int getDepth() {
125 List
<Content
> ancestors
= new ArrayList
<>();
126 collectAncestors(ancestors
, this);
127 return ancestors
.size();
131 public String
getSessionLocalId() {
140 public ProvidedSession
getSession() {
149 public List
<QName
> getContentClasses() {
150 return new ArrayList
<>();
156 protected boolean isDefaultAttrTypeRequested(Class
<?
> clss
) {
157 // check whether clss is Object.class
158 return clss
.isAssignableFrom(Object
.class);
162 // public String toString() {
163 // return "content " + getPath();
171 // - cannot be modified
173 public Iterator
<Content
> iterator() {
174 return Collections
.emptyIterator();
178 public Content
add(QName name
, QName
... classes
) {
179 throw new UnsupportedOperationException("Content cannot be added.");
183 public void remove() {
184 throw new UnsupportedOperationException("Content cannot be removed.");
187 protected Iterable
<QName
> keys() {
188 return Collections
.emptySet();
192 public <A
> Optional
<A
> get(QName key
, Class
<A
> clss
) {
196 protected void removeAttr(QName key
) {
197 throw new UnsupportedOperationException("Attributes cannot be removed.");
204 class AttrSet
extends AbstractSet
<Entry
<QName
, Object
>> {
207 public Iterator
<Entry
<QName
, Object
>> iterator() {
208 final Iterator
<QName
> keys
= keys().iterator();
209 Iterator
<Entry
<QName
, Object
>> it
= new Iterator
<Map
.Entry
<QName
, Object
>>() {
214 public boolean hasNext() {
215 return keys
.hasNext();
219 public Entry
<QName
, Object
> next() {
222 Optional
<?
> value
= get(key
, Object
.class);
223 assert !value
.isEmpty();
224 AbstractMap
.SimpleEntry
<QName
, Object
> entry
= new SimpleEntry
<>(key
, value
.get());
229 public void remove() {
231 AbstractContent
.this.removeAttr(key
);
233 throw new IllegalStateException("Iteration has not started");
243 return LangUtils
.size(keys());
252 public String
toString() {
253 return "content " + getPath();