View Javadoc

1   /*	
2   	Copyright 2007-2014 Fraunhofer IGD, http://www.igd.fraunhofer.de
3   	Fraunhofer-Gesellschaft - Institute for Computer Graphics Research
4   	
5   	See the NOTICE file distributed with this work for additional 
6   	information regarding copyright ownership
7   	
8   	Licensed under the Apache License, Version 2.0 (the "License");
9   	you may not use this file except in compliance with the License.
10  	You may obtain a copy of the License at
11  	
12  	  http://www.apache.org/licenses/LICENSE-2.0
13  	
14  	Unless required by applicable law or agreed to in writing, software
15  	distributed under the License is distributed on an "AS IS" BASIS,
16  	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  	See the License for the specific language governing permissions and
18  	limitations under the License.
19   */
20  package org.universAAL.middleware.rdf;
21  
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.Hashtable;
25  import java.util.List;
26  
27  import org.universAAL.middleware.container.utils.LogUtils;
28  import org.universAAL.middleware.datarep.SharedResources;
29  
30  /**
31   * A Resource that can not be modified.
32   * 
33   * @author Carsten Stockloew
34   */
35  public final class UnmodifiableResource extends Resource {
36  
37      private Resource res;
38  
39      public UnmodifiableResource(Resource r) {
40  	res = r;
41      }
42  
43      /**
44       * Get an unmodifiable version of the given object. If the parameter is a
45       * {@link Resource}, an {@link UnmodifiableResource} is returned. If the
46       * parameter is a {@link java.util.List}, an
47       * {@link UnmodifiableResourceList} is returned.
48       * 
49       * @param o
50       *            The object for which an unmodifiable version should be
51       *            returned.
52       * @return The unmodifiable version.
53       */
54      public static Object getUnmodifiable(Object o) {
55  	if (o instanceof Resource)
56  	    return new UnmodifiableResource((Resource) o);
57  	else if (o instanceof List)
58  	    return new UnmodifiableResourceList((List) o);
59  
60  	return o;
61      }
62  
63      /** @see org.universAAL.middleware.rdf.Resource#changeProperty(String, Object) */
64      public boolean changeProperty(String propURI, Object value) {
65  	LogUtils.logDebug(SharedResources.moduleContext,
66  		UnmodifiableResource.class, "changeProperty",
67  		new String[] { "Can not change an unmodifiable resource." },
68  		null);
69  	return false;
70      }
71  
72      /** @see org.universAAL.middleware.rdf.Resource#getProperty(String) */
73      public final Object getProperty(String propURI) {
74  	Object o = res.getProperty(propURI);
75  	return getUnmodifiable(o);
76      }
77  
78      /** @see org.universAAL.middleware.rdf.Resource#setProperty(String, Object) */
79      public boolean setProperty(String propURI, Object value) {
80  	LogUtils.logDebug(SharedResources.moduleContext,
81  		UnmodifiableResource.class, "setProperty",
82  		new String[] { "Can not change an unmodifiable resource." },
83  		null);
84  	return false;
85      }
86  
87      /**
88       * @see org.universAAL.middleware.rdf.Resource#setPropertyPath(String[],
89       *      Object, boolean)
90       */
91      public boolean setPropertyPath(String[] propPath, Object value,
92  	    boolean force) {
93  	LogUtils.logDebug(SharedResources.moduleContext,
94  		UnmodifiableResource.class, "setPropertyPath",
95  		new String[] { "Can not change an unmodifiable resource." },
96  		null);
97  	return false;
98      }
99  
100     /**
101      * @see org.universAAL.middleware.rdf.Resource#setPropertyPath(String[],
102      *      Object)
103      */
104     public boolean setPropertyPath(String[] propPath, Object value) {
105 	LogUtils.logDebug(SharedResources.moduleContext,
106 		UnmodifiableResource.class, "setPropertyPath",
107 		new String[] { "Can not change an unmodifiable resource." },
108 		null);
109 	return false;
110     }
111 
112     /**
113      * @see org.universAAL.middleware.rdf.Resource#setPropertyPathFromOffset(String[],
114      *      int, Object, boolean)
115      */
116     public boolean setPropertyPathFromOffset(String[] propPath, int fromIndex,
117 	    Object value, boolean force) {
118 	LogUtils.logDebug(SharedResources.moduleContext,
119 		UnmodifiableResource.class, "setPropertyPathFromOffset",
120 		new String[] { "Can not change an unmodifiable resource." },
121 		null);
122 	return false;
123     }
124 
125     public List asList() {
126 	return new UnmodifiableResourceList(res.asList());
127     }
128 
129     public void asList(List l) {
130 	ArrayList l2 = new ArrayList();
131 	res.asList(l2);
132 	for (int i = 0; i < l2.size(); i++) {
133 	    Object o = l2.get(i);
134 	    l.add(getUnmodifiable(o));
135 	}
136     }
137 
138     public Resource copy(boolean isXMLLitera) {
139 	return new UnmodifiableResource(res.copy(isXMLLitera));
140     }
141 
142     public Resource deepCopy() {
143 	return res.deepCopy();
144     }
145 
146     public boolean equals(Object other) {
147 	if (this == other)
148 	    return true;
149 	if (getURI().equals(((Resource) other).getURI()))
150 	    return true;
151 	return res.equals(other);
152     }
153 
154     public String getOrConstructLabel(String type) {
155 	return res.getOrConstructLabel(type);
156     }
157 
158     public int getPropSerializationType(String propURI) {
159 	return res.getPropSerializationType(propURI);
160     }
161 
162     public String getResourceComment() {
163 	return res.getResourceComment();
164     }
165 
166     public String getResourceLabel() {
167 	return res.getResourceLabel();
168     }
169 
170     public Object getStaticFieldValue(String fieldName, Object defaultValue) {
171 	// TODO correct?
172 	return res.getStaticFieldValue(fieldName, defaultValue);
173     }
174 
175     public int hashCode() {
176 	return res.hashCode();
177     }
178 
179     public boolean hasProperty(String propURI) {
180 	return res.hasProperty(propURI);
181     }
182 
183     public boolean isClosedCollection(String propURI) {
184 	return res.isClosedCollection(propURI);
185     }
186 
187     public boolean isWellFormed() {
188 	return res.isWellFormed();
189     }
190 
191     public boolean representsQualifiedURI() {
192 	return res.representsQualifiedURI();
193     }
194 
195     public boolean serializesAsXMLLiteral() {
196 	return res.serializesAsXMLLiteral();
197     }
198 
199     public void setResourceComment(String comment) {
200     }
201 
202     public void setResourceLabel(String label) {
203     }
204 
205     public String toString() {
206 	return res.toString();
207     }
208 
209     public String toStringRecursive() {
210 	return res.toStringRecursive();
211     }
212 
213     public String toStringRecursive(String prefix, boolean prefixAtStart,
214 	    Hashtable visitedElements) {
215 	return res.toStringRecursive(prefix, prefixAtStart, visitedElements);
216     }
217 
218     public int numberOfProperties() {
219 	return res.numberOfProperties();
220     }
221 
222     /** @see org.universAAL.middleware.rdf.Resource#isAnon() */
223     public boolean isAnon() {
224 	return res.isAnon();
225     }
226 
227     /** @see org.universAAL.middleware.rdf.Resource#hasQualifiedName() */
228     public final boolean hasQualifiedName() {
229 	return res.hasQualifiedName();
230     }
231 
232     /** @see org.universAAL.middleware.rdf.Resource#getURI() */
233     public final String getURI() {
234 	return res.getURI();
235     }
236 
237     /** @see org.universAAL.middleware.rdf.Resource#getPropertyURIs() */
238     public final Enumeration getPropertyURIs() {
239 	return res.getPropertyURIs();
240     }
241 
242     /** @see org.universAAL.middleware.rdf.Resource#addType(String, boolean) */
243     public final boolean addType(String typeURI, boolean blockFurtherTypes) {
244 	return false;
245     }
246 
247     /** @see org.universAAL.middleware.rdf.Resource#getLocalName() */
248     public final String getLocalName() {
249 	return res.getLocalName();
250     }
251 
252     /** @see org.universAAL.middleware.rdf.Resource#getNamespace() */
253     public final String getNamespace() {
254 	return res.getNamespace();
255     }
256 
257     /** @see org.universAAL.middleware.rdf.Resource#getType() */
258     public final String getType() {
259 	return res.getType();
260     }
261 
262     /** @see org.universAAL.middleware.rdf.Resource#getTypes() */
263     public final String[] getTypes() {
264 	return res.getTypes();
265     }
266 
267     public final Class getClassOfUnmodifiable() {
268 	return res.getClass();
269     }
270 
271     public final boolean instanceOf(Class c) {
272 	return c.isAssignableFrom(res.getClass());
273     }
274 }