1 package org.codehaus.modello.plugins.xml;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import org.codehaus.modello.ModelloRuntimeException;
8 import org.codehaus.modello.model.ModelClass;
9 import org.codehaus.modello.model.ModelField;
10 import org.codehaus.modello.model.Version;
11 import org.codehaus.modello.plugin.AbstractModelloGenerator;
12 import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
13 import org.codehaus.modello.plugins.xml.metadata.XmlClassMetadata;
14 import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 class XmlModelHelpers {
44
45
46
47
48
49
50 static String resolveTagName(ModelClass modelClass) {
51 XmlClassMetadata xmlClassMetadata = (XmlClassMetadata) modelClass.getMetadata(XmlClassMetadata.ID);
52
53 String tagName;
54 if ((xmlClassMetadata == null) || (xmlClassMetadata.getTagName() == null)) {
55 tagName = AbstractModelloGenerator.uncapitalise(modelClass.getName());
56 } else {
57
58 tagName = xmlClassMetadata.getTagName();
59 }
60 return tagName;
61 }
62
63
64
65
66
67
68
69
70 static String resolveTagName(ModelField modelField, XmlFieldMetadata xmlFieldMetadata) {
71 String tagName;
72 if ((xmlFieldMetadata == null) || (xmlFieldMetadata.getTagName() == null)) {
73 tagName = modelField.getName();
74 } else {
75
76 tagName = xmlFieldMetadata.getTagName();
77 }
78 return tagName;
79 }
80
81
82
83
84
85
86
87
88 static String resolveTagName(String fieldTagName, XmlAssociationMetadata xmlAssociationMetadata) {
89 String tagName;
90 if ((xmlAssociationMetadata == null) || (xmlAssociationMetadata.getTagName() == null)) {
91 tagName = AbstractModelloGenerator.singular(fieldTagName);
92 } else {
93
94 tagName = xmlAssociationMetadata.getTagName();
95 }
96 return tagName;
97 }
98
99
100
101
102
103
104
105 static ModelField getContentField(List<ModelField> modelFields) {
106 if (modelFields == null) {
107 return null;
108 }
109 for (ModelField field : modelFields) {
110 XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata(XmlFieldMetadata.ID);
111
112 if (xmlFieldMetadata.isContent()) {
113 return field;
114 }
115 }
116 return null;
117 }
118
119
120
121
122
123
124
125
126 static List<ModelField> getXmlAttributeFields(List<ModelField> modelFields) {
127 List<ModelField> xmlAttributeFields = new ArrayList<ModelField>();
128
129 for (ModelField field : modelFields) {
130 XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata(XmlFieldMetadata.ID);
131
132 if (xmlFieldMetadata.isAttribute()) {
133 xmlAttributeFields.add(field);
134 }
135 }
136
137 return xmlAttributeFields;
138 }
139
140
141
142
143
144
145
146
147 static List<ModelField> getFieldsForXml(ModelClass modelClass, Version version) {
148 List<ModelClass> classes = new ArrayList<ModelClass>();
149
150
151 while (modelClass != null) {
152 classes.add(modelClass);
153
154 String superClass = modelClass.getSuperClass();
155 if (superClass != null) {
156
157 modelClass = modelClass.getModel().getClass(superClass, version, true);
158 } else {
159 modelClass = null;
160 }
161 }
162
163 List<ModelField> fields = new ArrayList<ModelField>();
164
165 for (int i = classes.size() - 1; i >= 0; i--) {
166 modelClass = classes.get(i);
167
168 Iterator<ModelField> parentIter = fields.iterator();
169
170 fields = new ArrayList<ModelField>();
171
172 for (ModelField field : modelClass.getFields(version)) {
173 XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata(XmlFieldMetadata.ID);
174
175 if (xmlFieldMetadata.isTransient()) {
176
177 continue;
178 }
179
180 if (xmlFieldMetadata.getInsertParentFieldsUpTo() != null) {
181
182 boolean found = false;
183
184 while (!found && parentIter.hasNext()) {
185 ModelField parentField = parentIter.next();
186
187 fields.add(parentField);
188
189 found = parentField.getName().equals(xmlFieldMetadata.getInsertParentFieldsUpTo());
190 }
191
192 if (!found) {
193
194 throw new ModelloRuntimeException("parent field not found: class " + modelClass.getName()
195 + " xml.insertParentFieldUpTo='" + xmlFieldMetadata.getInsertParentFieldsUpTo() + "'");
196 }
197 }
198
199 fields.add(field);
200 }
201
202
203 while (parentIter.hasNext()) {
204 fields.add(parentIter.next());
205 }
206 }
207
208 return fields;
209 }
210 }