KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > schema > impl > SchemaImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.schema.impl;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.definition.Visibility;
25 import org.apache.hivemind.parse.BaseAnnotationHolder;
26 import org.apache.hivemind.schema.AttributeModel;
27 import org.apache.hivemind.schema.ElementModel;
28 import org.apache.hivemind.schema.Schema;
29 import org.apache.hivemind.util.UniqueHashMap;
30
31 /**
32  * Implementation of {@link org.apache.hivemind.schema.Schema}.
33  *
34  * @author Howard Lewis Ship
35  */

36 public class SchemaImpl extends BaseAnnotationHolder implements Schema
37 {
38     private List JavaDoc _elementModels;
39
40     private List JavaDoc _shareableElementModels;
41
42     /** @since 1.1 */
43     private Visibility _visibility = Visibility.PUBLIC;
44
45     /** @since 1.1 */
46     private String JavaDoc _moduleId;
47
48     /** @since 1.1 */
49     private String JavaDoc _id;
50     
51     private String JavaDoc _rootElementClassName;
52     
53     public SchemaImpl(String JavaDoc moduleId)
54     {
55         _moduleId = moduleId;
56     }
57
58     /**
59      * @since 1.1
60      */

61     public String JavaDoc getModuleId()
62     {
63         return _moduleId;
64     }
65
66     /**
67      * @since 1.1
68      */

69     public String JavaDoc getId()
70     {
71         return _id;
72     }
73     
74     public String JavaDoc getFullyQualifiedId()
75     {
76         return getModuleId() + "." + _id;
77     }
78
79     /**
80      * @since 1.1
81      */

82     public Visibility getVisibility()
83     {
84         return _visibility;
85     }
86
87     /** @since 1.1 */
88     public boolean visibleToModule(String JavaDoc moduleId)
89     {
90         if (_visibility == Visibility.PUBLIC)
91             return true;
92
93         return getModuleId().equals(moduleId);
94     }
95
96     public void addElementModel(ElementModel model)
97     {
98         if (_elementModels == null)
99             _elementModels = new ArrayList JavaDoc();
100
101         _elementModels.add(model);
102         _shareableElementModels = null;
103     }
104
105     public List JavaDoc getElementModel()
106     {
107         if (_shareableElementModels == null)
108             _shareableElementModels = _elementModels == null ? Collections.EMPTY_LIST : Collections
109                     .unmodifiableList(_elementModels);
110
111         return _shareableElementModels;
112     }
113
114     /**
115      * @see org.apache.hivemind.schema.Schema#canInstancesBeKeyed()
116      */

117     public boolean canInstancesBeKeyed()
118     {
119         boolean emptyModel = _elementModels == null || _elementModels.isEmpty();
120
121         if (emptyModel)
122             return false;
123
124         boolean result = false;
125         for (Iterator JavaDoc i = _elementModels.iterator(); i.hasNext();)
126         {
127             ElementModel model = (ElementModel) i.next();
128
129             if (model.getKeyAttribute() != null)
130                 result = true;
131             
132             // check for unique attribute
133
for (Iterator JavaDoc j = model.getAttributeModels().iterator(); j.hasNext();)
134             {
135                 AttributeModel attributeModel = (AttributeModel) j.next();
136
137                 if (attributeModel.isUnique())
138                     result = true;
139             }
140         }
141
142         return result;
143     }
144     
145     /**
146      * @return true if instances are keyed and the key is unique (attribute unique="true" used)
147      */

148     public boolean isKeyUnique()
149     {
150         boolean emptyModel = _elementModels == null || _elementModels.isEmpty();
151
152         if (emptyModel)
153             return false;
154
155         boolean result = false;
156         for (Iterator JavaDoc i = _elementModels.iterator(); i.hasNext();)
157         {
158             ElementModel model = (ElementModel) i.next();
159
160             // check for unique attribute
161
for (Iterator JavaDoc j = model.getAttributeModels().iterator(); j.hasNext();)
162             {
163                 AttributeModel attributeModel = (AttributeModel) j.next();
164
165                 if (attributeModel.isUnique())
166                     result = true;
167             }
168         }
169
170         return result;
171     }
172     
173     /**
174      * Called by the {@link org.apache.hivemind.parse.DescriptorParser} to make sure that key
175      * attributes specified by the top-level elements actually are defined.
176      */

177     public void validateKeyAttributes()
178     {
179         if (_elementModels == null)
180             return;
181
182         for (Iterator JavaDoc i = _elementModels.iterator(); i.hasNext();)
183         {
184             ElementModel em = (ElementModel) i.next();
185
186             String JavaDoc key = em.getKeyAttribute();
187
188             if (key == null)
189                 continue;
190
191             AttributeModel keyAm = em.getAttributeModel(key);
192
193             if (keyAm == null)
194                 throw new ApplicationRuntimeException("Key attribute \'" + key + "\' of element \'"
195                         + em.getElementName() + "\' never declared.", em.getLocation(), null);
196         }
197     }
198
199     /**
200      * @since 1.1
201      */

202     public void setVisibility(Visibility visibility)
203     {
204         _visibility = visibility;
205     }
206
207     /**
208      * @since 1.1
209      */

210     public void setId(String JavaDoc id)
211     {
212         _id = id;
213     }
214
215     public String JavaDoc getDefiningModuleId()
216     {
217         return _moduleId;
218     }
219
220     public String JavaDoc getRootElementClassName()
221     {
222         if (_rootElementClassName == null) {
223             return getDefaultRootElementClassName();
224         }
225         return _rootElementClassName;
226     }
227
228     /**
229      * @return returns the default root element class. For backward compatibility it is either ArrayList
230      * or in the case of a keyed elements, a HashMap.
231      */

232     private String JavaDoc getDefaultRootElementClassName()
233     {
234         // if one element of the schema has defined a key-attribute, then use a map
235
// in that case {@link InvokeParentRule} does the rest to handle element addition correctly
236
if (canInstancesBeKeyed()) {
237             if (isKeyUnique()) {
238                 return UniqueHashMap.class.getName();
239             } else {
240                 return HashMap JavaDoc.class.getName();
241             }
242         }
243         else return ArrayList JavaDoc.class.getName();
244     }
245
246     public void setRootElementClassName(String JavaDoc rootElementClassName)
247     {
248         _rootElementClassName = rootElementClassName;
249     }
250
251 }
Popular Tags