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