]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/AbstractContent.java
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
;
26 private String _path
= null;
28 public AbstractContent(ProvidedSession session
) {
29 this.session
= session
;
33 * ATTRIBUTES OPERATIONS
35 // protected abstract Iterable<QName> keys();
37 // protected abstract void removeAttr(QName key);
40 public Set
<Entry
<QName
, Object
>> entrySet() {
41 Set
<Entry
<QName
, Object
>> result
= new AttrSet();
46 public Class
<?
> getType(QName key
) {
51 public boolean isMultiple(QName key
) {
55 @SuppressWarnings("unchecked")
57 public <A
> Optional
<List
<A
>> getMultiple(QName key
, Class
<A
> clss
) {
58 Object value
= get(key
);
61 if (value
instanceof List
) {
63 List
<A
> res
= (List
<A
>) value
;
64 return Optional
.of(res
);
65 } catch (ClassCastException e
) {
66 List
<A
> res
= new ArrayList
<>();
67 List
<?
> lst
= (List
<?
>) value
;
69 for (Object o
: lst
) {
73 return Optional
.of(res
);
74 } catch (ClassCastException e1
) {
75 return Optional
.empty();
81 return Optional
.of(Collections
.singletonList(res
));
82 } catch (ClassCastException e
) {
83 return Optional
.empty();
93 public String
getPath() {
96 List
<Content
> ancestors
= new ArrayList
<>();
97 collectAncestors(ancestors
, this);
98 StringBuilder path
= new StringBuilder();
99 for (Content c
: ancestors
) {
100 QName name
= c
.getName();
102 if (!CrName
.root
.qName().equals(name
))
103 path
.append('/').append(name
);
105 _path
= path
.toString();
109 private void collectAncestors(List
<Content
> ancestors
, Content content
) {
112 ancestors
.add(0, content
);
113 collectAncestors(ancestors
, content
.getParent());
117 public int getDepth() {
118 List
<Content
> ancestors
= new ArrayList
<>();
119 collectAncestors(ancestors
, this);
120 return ancestors
.size();
124 public String
getSessionLocalId() {
133 public ProvidedSession
getSession() {
142 public List
<QName
> getContentClasses() {
143 return new ArrayList
<>();
149 protected boolean isDefaultAttrTypeRequested(Class
<?
> clss
) {
150 // check whether clss is Object.class
151 return clss
.isAssignableFrom(Object
.class);
155 // public String toString() {
156 // return "content " + getPath();
164 // - cannot be modified
166 public Iterator
<Content
> iterator() {
167 return Collections
.emptyIterator();
171 public Content
add(QName name
, QName
... classes
) {
172 throw new UnsupportedOperationException("Content cannot be added.");
176 public void remove() {
177 throw new UnsupportedOperationException("Content cannot be removed.");
180 protected Iterable
<QName
> keys() {
181 return Collections
.emptySet();
185 public <A
> Optional
<A
> get(QName key
, Class
<A
> clss
) {
189 protected void removeAttr(QName key
) {
190 throw new UnsupportedOperationException("Attributes cannot be removed.");
197 class AttrSet
extends AbstractSet
<Entry
<QName
, Object
>> {
200 public Iterator
<Entry
<QName
, Object
>> iterator() {
201 final Iterator
<QName
> keys
= keys().iterator();
202 Iterator
<Entry
<QName
, Object
>> it
= new Iterator
<Map
.Entry
<QName
, Object
>>() {
207 public boolean hasNext() {
208 return keys
.hasNext();
212 public Entry
<QName
, Object
> next() {
215 Optional
<?
> value
= get(key
, Object
.class);
216 assert !value
.isEmpty();
217 AbstractMap
.SimpleEntry
<QName
, Object
> entry
= new SimpleEntry
<>(key
, value
.get());
222 public void remove() {
224 AbstractContent
.this.removeAttr(key
);
226 throw new IllegalStateException("Iteration has not started");
236 return LangUtils
.size(keys());
245 public String
toString() {
246 return "content " + getPath();