]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/AbstractContent.java
fd4ef4cfc2c8dddc6870c91d2df8184a4c202289
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
.spi
.ProvidedContent
;
18 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
19 import org
.argeo
.util
.LangUtils
;
21 /** Partial reference implementation of a {@link ProvidedContent}. */
22 public abstract class AbstractContent
extends AbstractMap
<QName
, Object
> implements ProvidedContent
{
23 private final ProvidedSession session
;
25 public AbstractContent(ProvidedSession session
) {
26 this.session
= session
;
30 * ATTRIBUTES OPERATIONS
32 // protected abstract Iterable<QName> keys();
34 // protected abstract void removeAttr(QName key);
37 public Set
<Entry
<QName
, Object
>> entrySet() {
38 Set
<Entry
<QName
, Object
>> result
= new AttrSet();
43 public Class
<?
> getType(QName key
) {
48 public boolean isMultiple(QName key
) {
52 @SuppressWarnings("unchecked")
54 public <A
> Optional
<List
<A
>> getMultiple(QName key
, Class
<A
> clss
) {
55 Object value
= get(key
);
58 if (value
instanceof List
) {
60 List
<A
> res
= (List
<A
>) value
;
61 return Optional
.of(res
);
62 } catch (ClassCastException e
) {
63 List
<A
> res
= new ArrayList
<>();
64 List
<?
> lst
= (List
<?
>) value
;
66 for (Object o
: lst
) {
70 return Optional
.of(res
);
71 } catch (ClassCastException e1
) {
72 return Optional
.empty();
78 return Optional
.of(Collections
.singletonList(res
));
79 } catch (ClassCastException e
) {
80 return Optional
.empty();
90 public String
getPath() {
91 List
<Content
> ancestors
= new ArrayList
<>();
92 collectAncestors(ancestors
, this);
93 StringBuilder path
= new StringBuilder();
94 for (Content c
: ancestors
) {
95 QName name
= c
.getName();
97 if (!CrName
.root
.qName().equals(name
))
98 path
.append('/').append(name
);
100 return path
.toString();
103 private void collectAncestors(List
<Content
> ancestors
, Content content
) {
106 ancestors
.add(0, content
);
107 collectAncestors(ancestors
, content
.getParent());
111 public int getDepth() {
112 List
<Content
> ancestors
= new ArrayList
<>();
113 collectAncestors(ancestors
, this);
114 return ancestors
.size();
118 public String
getSessionLocalId() {
127 public ProvidedSession
getSession() {
136 public List
<QName
> getContentClasses() {
137 return new ArrayList
<>();
143 protected boolean isDefaultAttrTypeRequested(Class
<?
> clss
) {
144 // check whether clss is Object.class
145 return clss
.isAssignableFrom(Object
.class);
149 // public String toString() {
150 // return "content " + getPath();
158 // - cannot be modified
160 public Iterator
<Content
> iterator() {
161 return Collections
.emptyIterator();
165 public Content
add(QName name
, QName
... classes
) {
166 throw new UnsupportedOperationException("Content cannot be added.");
170 public void remove() {
171 throw new UnsupportedOperationException("Content cannot be removed.");
174 protected Iterable
<QName
> keys() {
175 return Collections
.emptySet();
179 public <A
> Optional
<A
> get(QName key
, Class
<A
> clss
) {
183 protected void removeAttr(QName key
) {
184 throw new UnsupportedOperationException("Attributes cannot be removed.");
191 class AttrSet
extends AbstractSet
<Entry
<QName
, Object
>> {
194 public Iterator
<Entry
<QName
, Object
>> iterator() {
195 final Iterator
<QName
> keys
= keys().iterator();
196 Iterator
<Entry
<QName
, Object
>> it
= new Iterator
<Map
.Entry
<QName
, Object
>>() {
201 public boolean hasNext() {
202 return keys
.hasNext();
206 public Entry
<QName
, Object
> next() {
209 Optional
<?
> value
= get(key
, Object
.class);
210 assert !value
.isEmpty();
211 AbstractMap
.SimpleEntry
<QName
, Object
> entry
= new SimpleEntry
<>(key
, value
.get());
216 public void remove() {
218 AbstractContent
.this.removeAttr(key
);
220 throw new IllegalStateException("Iteration has not started");
230 return LangUtils
.size(keys());