1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.codehaus.modello.plugin.velocity;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.codehaus.modello.ModelloRuntimeException;
25 import org.codehaus.modello.model.ModelAssociation;
26 import org.codehaus.modello.model.ModelClass;
27 import org.codehaus.modello.model.ModelField;
28 import org.codehaus.modello.model.Version;
29 import org.codehaus.modello.plugin.AbstractModelloGenerator;
30 import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
31 import org.codehaus.modello.plugins.xml.metadata.XmlClassMetadata;
32 import org.codehaus.modello.plugins.xml.metadata.XmlFieldMetadata;
33 import org.codehaus.plexus.util.StringUtils;
34
35 @SuppressWarnings("unused")
36 public class Helper {
37 private final Version version;
38
39 public Helper(Version version) {
40 this.version = version;
41 }
42
43 public String capitalise(String str) {
44 return StringUtils.isEmpty(str) ? str : Character.toTitleCase(str.charAt(0)) + str.substring(1);
45 }
46
47 public String uncapitalise(String str) {
48 return StringUtils.isEmpty(str) ? str : Character.toLowerCase(str.charAt(0)) + str.substring(1);
49 }
50
51 public String singular(String str) {
52 return AbstractModelloGenerator.singular(str);
53 }
54
55 public List<ModelClass> ancestors(ModelClass clazz) {
56 List<ModelClass> ancestors = new ArrayList<>();
57 for (ModelClass cl = clazz;
58 cl != null;
59 cl = cl.getSuperClass() != null ? cl.getModel().getClass(cl.getSuperClass(), version) : null) {
60 ancestors.add(0, cl);
61 }
62 return ancestors;
63 }
64
65 public XmlClassMetadata xmlClassMetadata(ModelClass clazz) {
66 return (XmlClassMetadata) clazz.getMetadata(XmlClassMetadata.ID);
67 }
68
69 public XmlFieldMetadata xmlFieldMetadata(ModelField field) {
70 return (XmlFieldMetadata) field.getMetadata(XmlFieldMetadata.ID);
71 }
72
73 public XmlAssociationMetadata xmAssociationMetadata(ModelField field) {
74 return (XmlAssociationMetadata) ((ModelAssociation) field).getAssociationMetadata(XmlAssociationMetadata.ID);
75 }
76
77 public boolean isFlatItems(ModelField field) {
78 return field instanceof ModelAssociation && xmAssociationMetadata(field).isFlatItems();
79 }
80
81 public List<ModelField> xmlFields(ModelClass modelClass) {
82 List<ModelClass> classes = new ArrayList<>();
83
84 while (modelClass != null) {
85 classes.add(modelClass);
86 String superClass = modelClass.getSuperClass();
87 if (superClass != null) {
88
89 modelClass = modelClass.getModel().getClass(superClass, version, true);
90 } else {
91 modelClass = null;
92 }
93 }
94 List<ModelField> fields = new ArrayList<>();
95 for (int i = classes.size() - 1; i >= 0; i--) {
96 modelClass = classes.get(i);
97 Iterator<ModelField> parentIter = fields.iterator();
98 fields = new ArrayList<>();
99 for (ModelField field : modelClass.getFields(version)) {
100 XmlFieldMetadata xmlFieldMetadata = (XmlFieldMetadata) field.getMetadata(XmlFieldMetadata.ID);
101 if (xmlFieldMetadata.isTransient()) {
102
103 continue;
104 }
105 if (xmlFieldMetadata.getInsertParentFieldsUpTo() != null) {
106
107 boolean found = false;
108 while (!found && parentIter.hasNext()) {
109 ModelField parentField = parentIter.next();
110 fields.add(parentField);
111 found = parentField.getName().equals(xmlFieldMetadata.getInsertParentFieldsUpTo());
112 }
113 if (!found) {
114
115 throw new ModelloRuntimeException("parent field not found: class "
116 + modelClass.getName() + " xml.insertParentFieldUpTo='"
117 + xmlFieldMetadata.getInsertParentFieldsUpTo() + "'");
118 }
119 }
120 fields.add(field);
121 }
122
123 while (parentIter.hasNext()) {
124 fields.add(parentIter.next());
125 }
126 }
127 return fields;
128 }
129 }