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