1 package xdoclet.sdk.xgg;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.LinkedList;
10
11 import xdoclet.sdk.xgg.binding.Child;
12 import xdoclet.sdk.xgg.binding.Element;
13 import xdoclet.sdk.xgg.binding.SubElement;
14
15 /***
16 * This class is a data object for Velocity used during generation of beans
17 * that represent XML elements.
18 *
19 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a>
20 * @author <a href="mailto:letiemble at users.sourceforge.net">Laurent Etiemble</a>
21 * @created 7 mai 2003
22 * @version $Revision: 1.3 $
23 */
24 public final class MergedClass {
25 private final Map _referers = new HashMap();
26 private final List _elements = new ArrayList();
27
28 private final Map _childIdMethodMap = new HashMap();
29 /*** Properly ordered children */
30 private final List _children = new LinkedList();
31 private Child _lastAddedChild = null;
32
33 /***
34 * Adds an element to this class model. This potentially adds more methods.
35 *
36 * @param element The element to add
37 */
38 public void addElement(Element element) {
39 _elements.add(element);
40 for (Iterator children = element.getChildren().iterator(); children.hasNext(); ) {
41 Child child = (Child) children.next();
42 addChild(child);
43 }
44
45 for (Iterator referers = element.getReferers().iterator(); referers.hasNext(); ) {
46 SubElement referer = (SubElement) referers.next();
47 addReferer(referer);
48 }
49 }
50
51
52 /***
53 * Returns the elements that were used to build this class. This is only
54 * used in the template to list all grammars that have the element
55 * that correspond to this class.
56 *
57 * @return a Collection of {@link Element}.
58 */
59 public Collection getElements() {
60 return _elements;
61 }
62
63
64 /***
65 * Gets the Java name of the first element
66 *
67 * @return The Java name
68 */
69 public String getJavaName() {
70 Element element0 = (Element) _elements.get(0);
71 return element0.getJavaName();
72 }
73
74
75 /***
76 * Returns the method models.
77 *
78 * @return a Collection of {@link MergedMethod}.
79 */
80 public List getMergedMethods() {
81 List result = new ArrayList();
82 int n = 0;
83 for( Iterator children = _children.iterator(); children.hasNext(); ) {
84 Child child = (Child) children.next();
85 MergedMethod mergedMethod = (MergedMethod) _childIdMethodMap.get( child.getId() );
86 if( mergedMethod == null ) {
87 throw new IllegalStateException( "No method for " + child.getId() );
88 }
89 mergedMethod.setIndex( n++ );
90 result.add( mergedMethod );
91 }
92 return result;
93 }
94
95
96 /***
97 * Returns the class model referers.
98 *
99 * @return a Collection of {@link Referer}.
100 */
101 public Collection getReferers() {
102 return _referers.values();
103 }
104
105
106 /***
107 * Gets the regular expression pattern used for validation
108 *
109 * @return The regular expression pattern
110 */
111 public String getRegexp() {
112 Element element0 = (Element) _elements.get(0);
113 return element0.getRegexp();
114 }
115
116
117 /***
118 * This will return the XML name of the first element. This should
119 * be the actual XML name of the first element as would be seen in
120 * an XML document.
121 *
122 * @return the XML name of the first element.
123 */
124 public String getXmlName() {
125 Element element0 = (Element) _elements.get(0);
126 return element0.getXmlName();
127 }
128
129
130 /***
131 * Returns whether or not this class model has a PC Data
132 *
133 * @return True if this class model has a PC Data
134 */
135 public boolean isPcData() {
136 Element element0 = (Element) _elements.get(0);
137 return element0.isPcData();
138 }
139
140
141 /***
142 * Adds a child to this class model. A child represents a method, which
143 * in turn represents an attribute or a sub element.
144 *
145 * @param child The child to add
146 */
147 private void addChild(Child child) {
148 // the key acoounts for type (element or attribute) and name.
149 MergedMethod mergedMethod = (MergedMethod) _childIdMethodMap.get(child.getId());
150 if (mergedMethod == null) {
151 // add a new one
152 mergedMethod = new MergedMethod(child);
153 } else {
154 // add property to the existing model.
155 mergedMethod.addChild(child);
156 }
157 _childIdMethodMap.put(child.getId(), mergedMethod);
158
159 if( !_children.contains( child ) ) {
160 // Now insert the child at the right position if it's new.
161 int insertPosition;
162 if( _lastAddedChild == null ) {
163 insertPosition = 0;
164 } else {
165 insertPosition = _children.indexOf( _lastAddedChild ) + 1;
166 }
167 _children.add( insertPosition, child );
168 }
169 _lastAddedChild = child;
170 }
171
172 /***
173 * Adds a referer to this class model
174 *
175 * @param subElement The referer to add
176 */
177 private void addReferer(SubElement subElement) {
178 getReferer(subElement).addSubElement(subElement);
179 }
180
181
182 /***
183 * Gets the referer associated to a given sub element
184 *
185 * @param subElement The sub element
186 * @return The referer
187 */
188 private Referer getReferer(SubElement subElement) {
189 Referer referer = (Referer) _referers.get(subElement.getContainer().getXmlName());
190 if (referer == null) {
191 referer = new Referer();
192 _referers.put(subElement.getContainer().getXmlName(), referer);
193 }
194 return referer;
195 }
196 }
This page was automatically generated by Maven