KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > model > ModelDef


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

17
18 import java.util.*;
19
20 import xdoclet.modules.ojb.constraints.*;
21
22 /**
23  * Defines the model (class descriptors etc.).
24  *
25  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
26  */

27 public class ModelDef extends DefBase
28 {
29     /** The class definitions keyed by their names */
30     private SortedMap _classDefs = new TreeMap();
31     /** The tables keyed by their names */
32     private SortedMap _tableDefs = new TreeMap();
33
34     /**
35      * Creates a new model object.
36      */

37     public ModelDef()
38     {
39         super("");
40     }
41
42     /**
43      * Determines whether this model contains a class descriptor of the given name.
44      *
45      * @param qualifiedName The qualified name
46      * @return <code>true</code> if such a class descriptor exists in this model
47      */

48     public boolean hasClass(String JavaDoc qualifiedName)
49     {
50         return _classDefs.containsKey(qualifiedName.replace('$', '.'));
51     }
52
53     /**
54      * Returns the class descriptor of the given name contained in this model. The name can be both
55      * a fully qualified name as per java spec or a classloader-compatible full name (which uses
56      * '$' for inner/nested classes).
57      *
58      * @param qualifiedName The qualified name
59      * @return The class descriptor or <code>null</code> if there is no such class in this model
60      */

61     public ClassDescriptorDef getClass(String JavaDoc qualifiedName)
62     {
63         return (ClassDescriptorDef)_classDefs.get(qualifiedName.replace('$', '.'));
64     }
65
66     /**
67      * Adds the class descriptor to this model.
68      *
69      * @param classDef The class descriptor
70      * @return The class descriptor or <code>null</code> if there is no such class in this model
71      */

72     public void addClass(ClassDescriptorDef classDef)
73     {
74         classDef.setOwner(this);
75         // Regardless of the format of the class name, we're using the fully qualified format
76
// This is safe because of the package & class naming constraints of the Java language
77
_classDefs.put(classDef.getQualifiedName(), classDef);
78     }
79
80     /**
81      * Returns all classes in this model.
82      *
83      * @return An iterator of all classes
84      */

85     public Iterator getClasses()
86     {
87         return _classDefs.values().iterator();
88     }
89
90     /**
91      * Returns the number of classes contained in this model.
92      *
93      * @return The number of classes
94      */

95     public int getNumClasses()
96     {
97         return _classDefs.size();
98     }
99
100     /**
101      * Processes all classes (flattens the hierarchy such that every class has declarations for all fields,
102      * references,collections that it will have in the descriptor) and applies modifications (removes ignored
103      * features, changes declarations).
104      *
105      * @throws ConstraintException If a constraint has been violated
106      */

107     public void process() throws ConstraintException
108     {
109         ClassDescriptorDef classDef;
110
111         // process all classes
112
for (Iterator it = getClasses(); it.hasNext();)
113         {
114             classDef = (ClassDescriptorDef)it.next();
115             if (!classDef.hasBeenProcessed())
116             {
117                 classDef.process();
118             }
119         }
120     }
121
122     /**
123      * Checks constraints on this model.
124      *
125      * @param checkLevel The amount of checks to perform
126      * @throws ConstraintException If a constraint has been violated
127      */

128     public void checkConstraints(String JavaDoc checkLevel) throws ConstraintException
129     {
130         // check constraints now after all classes have been processed
131
for (Iterator it = getClasses(); it.hasNext();)
132         {
133             ((ClassDescriptorDef)it.next()).checkConstraints(checkLevel);
134         }
135         // additional model constraints that either deal with bigger parts of the model or
136
// can only be checked after the individual classes have been checked (e.g. specific
137
// attributes have been ensured)
138
new ModelConstraints().check(this, checkLevel);
139     }
140 }
141
Popular Tags