View Javadoc
1   package org.codehaus.modello.plugin.velocity;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // get the full inheritance
98          while ( modelClass != null )
99          {
100             classes.add( modelClass );
101             String superClass = modelClass.getSuperClass();
102             if ( superClass != null )
103             {
104                 // superClass can be located outside (not generated by modello)
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                     // just ignore xml.transient fields
124                     continue;
125                 }
126                 if ( xmlFieldMetadata.getInsertParentFieldsUpTo() != null )
127                 {
128                     // insert fields from parent up to the specified field
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                         // interParentFieldsUpTo not found
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             // add every remaining fields from parent class
147             while ( parentIter.hasNext() )
148             {
149                 fields.add( parentIter.next() );
150             }
151         }
152         return fields;
153     }
154 }