KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jcr > base > BaseNodeType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jcr.base;
31
32 import javax.jcr.PropertyType;
33 import javax.jcr.Value;
34 import javax.jcr.nodetype.NodeDefinition;
35 import javax.jcr.nodetype.NodeType;
36 import javax.jcr.nodetype.PropertyDefinition;
37 import javax.jcr.version.OnParentVersionAction;
38 import java.util.ArrayList JavaDoc;
39
40 /**
41  * Represents a node type.
42  */

43 public class BaseNodeType implements NodeType {
44   public static final BaseNodeType MIX_REFERENCEABLE;
45   
46   public static final BaseNodeType NT_BASE;
47   
48   public static final BaseNodeType NT_HIERARCHY_NODE;
49   public static final BaseNodeType NT_FILE;
50   public static final BaseNodeType NT_FOLDER;
51   public static final BaseNodeType NT_RESOURCE;
52   
53   private final String JavaDoc _name;
54   
55   private final NodeType []_declaredSuperTypes;
56   
57   private final NodeType []_superTypes;
58
59   private boolean _isMixin;
60   
61   private boolean _hasOrderableChildNodes;
62
63   private String JavaDoc _primaryItemName;
64   
65   private ArrayList JavaDoc<PropertyDefinition> _properties
66     = new ArrayList JavaDoc<PropertyDefinition>();
67
68   private ArrayList JavaDoc<NodeDefinition> _childNodes
69     = new ArrayList JavaDoc<NodeDefinition>();
70
71   public BaseNodeType(String JavaDoc name,
72               NodeType []declaredSuperTypes)
73   {
74     _name = name;
75     _declaredSuperTypes = declaredSuperTypes;
76
77     ArrayList JavaDoc<NodeType> superTypes = new ArrayList JavaDoc<NodeType>();
78
79     for (NodeType type : declaredSuperTypes) {
80       if (! superTypes.contains(type))
81     superTypes.add(type);
82
83       for (NodeType parentType : type.getSupertypes()) {
84     if (! superTypes.contains(parentType))
85       superTypes.add(parentType);
86       }
87     }
88
89     _superTypes = new NodeType[superTypes.size()];
90     superTypes.toArray(_superTypes);
91   }
92
93   public BaseNodeType(String JavaDoc name, NodeType superType)
94   {
95     this(name, new NodeType[] { superType });
96   }
97   
98   /**
99    * Returns the node type's name.
100    */

101   public String JavaDoc getName()
102   {
103     return _name;
104   }
105
106   /**
107    * Returns true for a mixin node type.
108    */

109   public boolean isMixin()
110   {
111     return _isMixin;
112   }
113
114   /**
115    * Set true for a mixin node type.
116    */

117   public void setMixin(boolean isMixin)
118   {
119     _isMixin = isMixin;
120   }
121
122   /**
123    * Returns true if this node type has orderable children.
124    */

125   public boolean hasOrderableChildNodes()
126   {
127     return _hasOrderableChildNodes;
128   }
129
130   /**
131    * SEt true if this node type has orderable children.
132    */

133   public void setHasOrderableChildNodes(boolean hasOrder)
134   {
135     _hasOrderableChildNodes = hasOrder;
136   }
137
138   /**
139    * Returns the main item name.
140    */

141   public String JavaDoc getPrimaryItemName()
142   {
143     return _primaryItemName;
144   }
145
146   /**
147    * Returns the main item type.
148    */

149   public void setPrimaryItemName(String JavaDoc name)
150   {
151     _primaryItemName = name;
152   }
153
154   /**
155    * Returns all supertypes of the node type.
156    */

157   public NodeType[] getSupertypes()
158   {
159     return _superTypes;
160   }
161
162   /**
163    * Returns the immediate supertypes of the node type.
164    */

165   public NodeType[] getDeclaredSupertypes()
166   {
167     return _declaredSuperTypes;
168   }
169
170   /**
171    * Returns true if the given node type is valid.
172    */

173   public boolean isNodeType(String JavaDoc nodeTypeName)
174   {
175     return false;
176   }
177
178   /**
179    * Returns the properties defined for the node.
180    */

181   public PropertyDefinition[] getPropertyDefinitions()
182   {
183     return getDeclaredPropertyDefinitions();
184   }
185   
186   /**
187    * Returns the immediate properties defined for the node.
188    */

189   public PropertyDefinition[] getDeclaredPropertyDefinitions()
190   {
191     PropertyDefinition []props;
192
193     props = new PropertyDefinition[_properties.size()];
194     _properties.toArray(props);
195     
196     return props;
197   }
198
199   /**
200    * Adds a property definition.
201    */

202   public void addProperty(PropertyDefinition prop)
203   {
204     _properties.add(prop);
205   }
206   
207   /**
208    * Returns the allowed children.
209    */

210   public NodeDefinition[] getChildNodeDefinitions()
211   {
212     return getDeclaredChildNodeDefinitions();
213   }
214   
215   /**
216    * Returns the direct children.
217    */

218   public NodeDefinition[] getDeclaredChildNodeDefinitions()
219   {
220     NodeDefinition []children;
221
222     children = new NodeDefinition[_childNodes.size()];
223     _childNodes.toArray(children);
224     
225     return children;
226   }
227
228   /**
229    * Adds a child node.
230    */

231   public void addChildNode(NodeDefinition child)
232   {
233     _childNodes.add(child);
234   }
235
236   /**
237    * Returns true if the given property can be set with the given value.
238    */

239   public boolean canSetProperty(String JavaDoc propertyName, Value value)
240   {
241     return false;
242   }
243   
244   /**
245    * Returns true if the given property can be set with the given value.
246    */

247   public boolean canSetProperty(String JavaDoc propertyName, Value[] values)
248   {
249     return false;
250   }
251   
252   /**
253    * Returns true if this node type can add a child node.
254    */

255   public boolean canAddChildNode(String JavaDoc childNodeName)
256   {
257     return false;
258   }
259   
260   /**
261    * Returns true if this node type can add a child node with the given type..
262    */

263   public boolean canAddChildNode(String JavaDoc childNodeName, String JavaDoc nodeTypeName)
264   {
265     return false;
266   }
267   
268   /**
269    * Returns true if this node type can remove an item.
270    */

271   public boolean canRemoveItem(String JavaDoc itemName)
272   {
273     return false;
274   }
275
276   public int hashCode()
277   {
278     return getName().hashCode();
279   }
280   
281   public boolean equals(Object JavaDoc o)
282   {
283     if (this == o)
284       return true;
285     else if (! (o instanceof BaseNodeType))
286       return false;
287
288     BaseNodeType nodeType = (BaseNodeType) o;
289
290     return getName().equals(nodeType.getName());
291   }
292
293   public String JavaDoc toString()
294   {
295     return "BaseNodeType[" + getName() + "]";
296   }
297
298   static {
299     BasePropertyDefinition prop;
300     BaseNodeDefinition child;
301
302     // mix:referenceable
303

304     MIX_REFERENCEABLE = new BaseNodeType("mix:referenceable", new NodeType[0]);
305
306     // nt:base
307

308     NT_BASE = new BaseNodeType("nt:base", new NodeType[0]);
309
310     prop = new BasePropertyDefinition("jcr:primaryType", NT_BASE,
311                       PropertyType.NAME);
312     prop.setAutoCreated(true);
313     prop.setMandatory(true);
314     prop.setOnParentVersion(OnParentVersionAction.COMPUTE);
315     prop.setProtected(true);
316     NT_BASE.addProperty(prop);
317
318     prop = new BasePropertyDefinition("jcr:mixinTypes", NT_BASE,
319                       PropertyType.NAME);
320     prop.setOnParentVersion(OnParentVersionAction.COMPUTE);
321     prop.setProtected(true);
322     prop.setMultiple(true);
323     NT_BASE.addProperty(prop);
324
325     // nt:unstructured - XXX: skip
326

327     // nt:hierarchyNode
328

329     NT_HIERARCHY_NODE = new BaseNodeType("nt:hierarchyNode", NT_BASE);
330
331     prop = new BasePropertyDefinition("jcr:created", NT_HIERARCHY_NODE,
332                       PropertyType.DATE);
333     prop.setAutoCreated(true);
334     prop.setOnParentVersion(OnParentVersionAction.INITIALIZE);
335     prop.setProtected(true);
336     NT_HIERARCHY_NODE.addProperty(prop);
337
338     // nt:file
339

340     NT_FILE = new BaseNodeType("nt:file", NT_HIERARCHY_NODE);
341     NT_FILE.setPrimaryItemName("jcr:content");
342
343     child = new BaseNodeDefinition("jcr:content", NT_FILE);
344     child.setRequiredPrimaryTypes(new NodeType[] { NT_BASE });
345     child.setMandatory(true);
346     NT_FILE.addChildNode(child);
347
348     // nt:linkedFile - XXX: skip
349

350     // nt:folder
351

352     NT_FOLDER = new BaseNodeType("nt:folder", NT_HIERARCHY_NODE);
353
354     child = new BaseNodeDefinition("*", NT_FOLDER);
355     child.setRequiredPrimaryTypes(new NodeType[] { NT_HIERARCHY_NODE });
356     child.setOnParentVersion(OnParentVersionAction.VERSION);
357     
358     // nt:resource
359

360     NT_RESOURCE = new BaseNodeType("nt:resource",
361                  new NodeType[] { NT_HIERARCHY_NODE,
362                           MIX_REFERENCEABLE });
363     NT_RESOURCE.setPrimaryItemName("jcr:data");
364
365     prop = new BasePropertyDefinition("jcr:encoding", NT_RESOURCE,
366                       PropertyType.STRING);
367     NT_FOLDER.addProperty(prop);
368
369     prop = new BasePropertyDefinition("jcr:mimeType", NT_RESOURCE,
370                       PropertyType.STRING);
371     prop.setMandatory(true);
372     NT_FOLDER.addProperty(prop);
373
374     prop = new BasePropertyDefinition("jcr:data", NT_RESOURCE,
375                       PropertyType.BINARY);
376     prop.setMandatory(true);
377     NT_FOLDER.addProperty(prop);
378
379     prop = new BasePropertyDefinition("jcr:lastModified", NT_RESOURCE,
380                       PropertyType.DATE);
381     prop.setMandatory(true);
382     prop.setOnParentVersion(OnParentVersionAction.IGNORE);
383     NT_FOLDER.addProperty(prop);
384   }
385 }
386
Popular Tags