]>
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
.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 CmsContent
{
25 private final ProvidedSession session
;
28 // private String _path = null;
30 public AbstractContent(ProvidedSession session
) {
31 this.session
= session
;
35 * ATTRIBUTES MAP IMPLEMENTATION
38 public Set
<Entry
<QName
, Object
>> entrySet() {
39 Set
<Entry
<QName
, Object
>> result
= new AttrSet();
44 public Object
get(Object key
) {
45 return get((QName
) key
, Object
.class).orElse(null);
49 * ATTRIBUTES OPERATIONS
52 public Class
<?
> getType(QName key
) {
57 public boolean isMultiple(QName key
) {
61 @SuppressWarnings("unchecked")
63 public <A
> List
<A
> getMultiple(QName key
, Class
<A
> clss
) {
64 Object value
= get(key
);
66 return new ArrayList
<>();
67 if (value
instanceof List
) {
68 if (clss
.isAssignableFrom(Object
.class))
69 return (List
<A
>) value
;
70 List
<A
> res
= new ArrayList
<>();
71 List
<?
> lst
= (List
<?
>) value
;
72 for (Object o
: lst
) {
73 A item
= CrAttributeType
.cast(clss
, o
).get();
78 A res
= CrAttributeType
.cast(clss
, value
).get();
79 return Collections
.singletonList(res
);
88 public String
getPath() {
91 List
<Content
> ancestors
= new ArrayList
<>();
92 collectAncestors(ancestors
, this);
93 StringBuilder path
= new StringBuilder();
94 ancestors
: for (Content c
: ancestors
) {
95 QName name
= c
.getName();
96 if (CrName
.root
.qName().equals(name
))
99 path
.append(PATH_SEPARATOR
);
100 path
.append(NamespaceUtils
.toPrefixedName(name
));
101 int siblingIndex
= c
.getSiblingIndex();
102 if (siblingIndex
!= 1)
103 path
.append('[').append(siblingIndex
).append(']');
105 // _path = path.toString();
107 return path
.toString();
110 private void collectAncestors(List
<Content
> ancestors
, Content content
) {
113 ancestors
.add(0, content
);
114 collectAncestors(ancestors
, content
.getParent());
118 public int getDepth() {
119 List
<Content
> ancestors
= new ArrayList
<>();
120 collectAncestors(ancestors
, this);
121 return ancestors
.size();
125 public boolean isRoot() {
126 return CrName
.root
.qName().equals(getName());
130 public String
getSessionLocalId() {
139 public ProvidedSession
getSession() {
148 public List
<QName
> getContentClasses() {
149 return new ArrayList
<>();
155 // protected boolean isDefaultAttrTypeRequested(Class<?> clss) {
156 // // check whether clss is Object.class
157 // return clss.isAssignableFrom(Object.class);
161 // public String toString() {
162 // return "content " + getPath();
170 // - cannot be modified
172 public Iterator
<Content
> iterator() {
173 return Collections
.emptyIterator();
177 public Content
add(QName name
, QName
... classes
) {
178 throw new UnsupportedOperationException("Content cannot be added.");
182 public void remove() {
183 throw new UnsupportedOperationException("Content cannot be removed.");
186 protected Iterable
<QName
> keys() {
187 return Collections
.emptySet();
191 public <A
> Optional
<A
> get(QName key
, Class
<A
> clss
) {
192 return Optional
.empty();
195 protected void removeAttr(QName key
) {
196 throw new UnsupportedOperationException("Attributes cannot be removed.");
203 class AttrSet
extends AbstractSet
<Entry
<QName
, Object
>> {
206 public Iterator
<Entry
<QName
, Object
>> iterator() {
207 final Iterator
<QName
> keys
= keys().iterator();
208 Iterator
<Entry
<QName
, Object
>> it
= new Iterator
<Map
.Entry
<QName
, Object
>>() {
213 public boolean hasNext() {
214 return keys
.hasNext();
218 public Entry
<QName
, Object
> next() {
221 Optional
<?
> value
= get(key
, Object
.class);
222 assert !value
.isEmpty();
223 AbstractMap
.SimpleEntry
<QName
, Object
> entry
= new SimpleEntry
<>(key
, value
.get());
228 public void remove() {
230 AbstractContent
.this.removeAttr(key
);
232 throw new IllegalStateException("Iteration has not started");
242 return LangUtils
.size(keys());
251 public String
toString() {
252 return "content " + getPath();