KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > dictionary > M2Model


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.dictionary;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.alfresco.service.cmr.dictionary.DictionaryException;
27 import org.jibx.runtime.BindingDirectory;
28 import org.jibx.runtime.IBindingFactory;
29 import org.jibx.runtime.IMarshallingContext;
30 import org.jibx.runtime.IUnmarshallingContext;
31 import org.jibx.runtime.JiBXException;
32
33
34 /**
35  * Model Definition.
36  *
37  * @author David Caruana
38  *
39  */

40 public class M2Model
41 {
42     private String JavaDoc name = null;
43     private String JavaDoc description = null;
44     private String JavaDoc author = null;
45     private Date JavaDoc published = null;
46     private String JavaDoc version;
47
48     private List JavaDoc<M2Namespace> namespaces = new ArrayList JavaDoc<M2Namespace>();
49     private List JavaDoc<M2Namespace> imports = new ArrayList JavaDoc<M2Namespace>();
50     private List JavaDoc<M2DataType> dataTypes = new ArrayList JavaDoc<M2DataType>();
51     private List JavaDoc<M2Type> types = new ArrayList JavaDoc<M2Type>();
52     private List JavaDoc<M2Aspect> aspects = new ArrayList JavaDoc<M2Aspect>();
53     
54
55     private M2Model()
56     {
57     }
58
59
60     /**
61      * Construct an empty model
62      *
63      * @param name the name of the model
64      * @return the model
65      */

66     public static M2Model createModel(String JavaDoc name)
67     {
68         M2Model model = new M2Model();
69         model.name = name;
70         return model;
71     }
72
73     
74     /**
75      * Construct a model from a dictionary xml specification
76      *
77      * @param xml the dictionary xml
78      * @return the model representation of the xml
79      */

80     public static M2Model createModel(InputStream JavaDoc xml)
81     {
82         try
83         {
84             IBindingFactory factory = BindingDirectory.getFactory(M2Model.class);
85             IUnmarshallingContext context = factory.createUnmarshallingContext();
86             Object JavaDoc obj = context.unmarshalDocument(xml, null);
87             return (M2Model)obj;
88         }
89         catch(JiBXException e)
90         {
91             throw new DictionaryException("Failed to parse model", e);
92         }
93     }
94
95     
96     /**
97      * Render the model to dictionary XML
98      *
99      * @param xml the dictionary xml representation of the model
100      */

101     public void toXML(OutputStream JavaDoc xml)
102     {
103         try
104         {
105             IBindingFactory factory = BindingDirectory.getFactory(M2Model.class);
106             IMarshallingContext context = factory.createMarshallingContext();
107             context.setIndent(4);
108             context.marshalDocument(this, "UTF-8", null, xml);
109         }
110         catch(JiBXException e)
111         {
112             throw new DictionaryException("Failed to create M2 Model", e);
113         }
114     }
115
116     
117     /**
118      * Create a compiled form of this model
119      *
120      * @param dictionaryDAO dictionary DAO
121      * @param namespaceDAO namespace DAO
122      * @return the compiled form of the model
123      */

124     /*package*/ CompiledModel compile(DictionaryDAO dictionaryDAO, NamespaceDAO namespaceDAO)
125     {
126         CompiledModel compiledModel = new CompiledModel(this, dictionaryDAO, namespaceDAO);
127         return compiledModel;
128     }
129
130     
131     public String JavaDoc getName()
132     {
133         return name;
134     }
135     
136     
137     public void setName(String JavaDoc name)
138     {
139         this.name = name;
140     }
141     
142     
143     public String JavaDoc getDescription()
144     {
145         return description;
146     }
147     
148     
149     public void setDescription(String JavaDoc description)
150     {
151         this.description = description;
152     }
153     
154     
155     public String JavaDoc getAuthor()
156     {
157         return author;
158     }
159     
160     
161     public void setAuthor(String JavaDoc author)
162     {
163         this.author = author;
164     }
165     
166     
167     public Date JavaDoc getPublishedDate()
168     {
169         return published;
170     }
171     
172     
173     public void setPublishedDate(Date JavaDoc published)
174     {
175         this.published = published;
176     }
177     
178     
179     public String JavaDoc getVersion()
180     {
181         return version;
182     }
183     
184     
185     public void setVersion(String JavaDoc version)
186     {
187         this.version = version;
188     }
189
190     
191     public M2Type createType(String JavaDoc name)
192     {
193         M2Type type = new M2Type();
194         type.setName(name);
195         types.add(type);
196         return type;
197     }
198     
199     
200     public void removeType(String JavaDoc name)
201     {
202         M2Type type = getType(name);
203         if (type != null)
204         {
205             types.remove(types);
206         }
207     }
208     
209     
210     public List JavaDoc<M2Type> getTypes()
211     {
212         return Collections.unmodifiableList(types);
213     }
214
215     
216     public M2Type getType(String JavaDoc name)
217     {
218         for (M2Type candidate : types)
219         {
220             if (candidate.getName().equals(name))
221             {
222                 return candidate;
223             }
224         }
225         return null;
226     }
227     
228     
229     public M2Aspect createAspect(String JavaDoc name)
230     {
231         M2Aspect aspect = new M2Aspect();
232         aspect.setName(name);
233         aspects.add(aspect);
234         return aspect;
235     }
236     
237     
238     public void removeAspect(String JavaDoc name)
239     {
240         M2Aspect aspect = getAspect(name);
241         if (aspect != null)
242         {
243             aspects.remove(name);
244         }
245     }
246
247     
248     public List JavaDoc<M2Aspect> getAspects()
249     {
250         return Collections.unmodifiableList(aspects);
251     }
252
253     
254     public M2Aspect getAspect(String JavaDoc name)
255     {
256         for (M2Aspect candidate : aspects)
257         {
258             if (candidate.getName().equals(name))
259             {
260                 return candidate;
261             }
262         }
263         return null;
264     }
265     
266     
267     public M2DataType createPropertyType(String JavaDoc name)
268     {
269         M2DataType type = new M2DataType();
270         type .setName(name);
271         dataTypes.add(type);
272         return type;
273     }
274     
275
276     public void removePropertyType(String JavaDoc name)
277     {
278         M2DataType type = getPropertyType(name);
279         if (type != null)
280         {
281             dataTypes.remove(name);
282         }
283     }
284
285
286     public List JavaDoc<M2DataType> getPropertyTypes()
287     {
288         return Collections.unmodifiableList(dataTypes);
289     }
290
291     
292     public M2DataType getPropertyType(String JavaDoc name)
293     {
294         for (M2DataType candidate : dataTypes)
295         {
296             if (candidate.getName().equals(name))
297             {
298                 return candidate;
299             }
300         }
301         return null;
302     }
303
304     
305     public M2Namespace createNamespace(String JavaDoc uri, String JavaDoc prefix)
306     {
307         M2Namespace namespace = new M2Namespace();
308         namespace.setUri(uri);
309         namespace.setPrefix(prefix);
310         namespaces.add(namespace);
311         return namespace;
312     }
313     
314     
315     public void removeNamespace(String JavaDoc uri)
316     {
317         M2Namespace namespace = getNamespace(uri);
318         if (namespace != null)
319         {
320             namespaces.remove(namespace);
321         }
322     }
323
324     
325     public List JavaDoc<M2Namespace> getNamespaces()
326     {
327         return Collections.unmodifiableList(namespaces);
328     }
329
330
331     public M2Namespace getNamespace(String JavaDoc uri)
332     {
333         for (M2Namespace candidate : namespaces)
334         {
335             if (candidate.getUri().equals(uri))
336             {
337                 return candidate;
338             }
339         }
340         return null;
341     }
342     
343     
344     public M2Namespace createImport(String JavaDoc uri, String JavaDoc prefix)
345     {
346         M2Namespace namespace = new M2Namespace();
347         namespace.setUri(uri);
348         namespace.setPrefix(prefix);
349         imports.add(namespace);
350         return namespace;
351     }
352     
353     
354     public void removeImport(String JavaDoc uri)
355     {
356         M2Namespace namespace = getImport(uri);
357         if (namespace != null)
358         {
359             imports.remove(namespace);
360         }
361     }
362
363
364     public List JavaDoc<M2Namespace> getImports()
365     {
366         return Collections.unmodifiableList(imports);
367     }
368
369     
370     public M2Namespace getImport(String JavaDoc uri)
371     {
372         for (M2Namespace candidate : imports)
373         {
374             if (candidate.getUri().equals(uri))
375             {
376                 return candidate;
377             }
378         }
379         return null;
380     }
381
382     
383     // Do not delete: referenced by m2binding.xml
384
private static List JavaDoc createList()
385     {
386         return new ArrayList JavaDoc();
387     }
388
389 }
390
Popular Tags