View Javadoc
1   package org.codehaus.modello.plugin.stax;
2   
3   /*
4    * Copyright (c) 2004, Jason van Zyl
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import org.codehaus.modello.ModelloException;
31  import org.codehaus.modello.model.Model;
32  import org.codehaus.modello.model.ModelAssociation;
33  import org.codehaus.modello.model.ModelClass;
34  import org.codehaus.modello.model.ModelField;
35  import org.codehaus.modello.plugins.xml.AbstractXmlJavaGenerator;
36  import org.codehaus.modello.plugins.xml.metadata.XmlAssociationMetadata;
37  
38  /**
39   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
40   */
41  public abstract class AbstractStaxGenerator extends AbstractXmlJavaGenerator {
42      private Set<ModelClass> parts;
43  
44      protected void initialize(Model model, Properties parameters) throws ModelloException {
45          super.initialize(model, parameters);
46  
47          parts = null;
48      }
49  
50      protected ModelField getReferenceIdentifierField(ModelAssociation association) throws ModelloException {
51          XmlAssociationMetadata xmlAssocMetadata =
52                  (XmlAssociationMetadata) association.getAssociationMetadata(XmlAssociationMetadata.ID);
53  
54          ModelField referenceIdentifierField = null;
55          if (xmlAssocMetadata.isReference()) {
56              String associationName = association.getName();
57  
58              ModelClass modelClass = association.getModelClass();
59              if (!isClassInModel(association.getTo(), modelClass.getModel())) {
60                  throw new ModelloException("Can't use xml.reference on the '" + associationName + "' association of '"
61                          + modelClass.getName() + "' because the target class '" + association.getTo()
62                          + "' is not in the model");
63              }
64  
65              List<ModelField> identifierFields = association.getToClass().getIdentifierFields(getGeneratedVersion());
66              if (identifierFields.size() == 1) {
67                  referenceIdentifierField = identifierFields.get(0);
68              } else {
69                  referenceIdentifierField = new DummyIdModelField();
70                  referenceIdentifierField.setName("modello.refid");
71              }
72          }
73          return referenceIdentifierField;
74      }
75  
76      protected boolean isAssociationPartToClass(ModelClass modelClass) {
77          if (parts == null) {
78              parts = new HashSet<ModelClass>();
79              for (ModelClass clazz : modelClass.getModel().getClasses(getGeneratedVersion())) {
80                  for (ModelField modelField : clazz.getFields(getGeneratedVersion())) {
81                      if (modelField instanceof ModelAssociation) {
82                          ModelAssociation assoc = (ModelAssociation) modelField;
83  
84                          XmlAssociationMetadata xmlAssocMetadata =
85                                  (XmlAssociationMetadata) assoc.getAssociationMetadata(XmlAssociationMetadata.ID);
86  
87                          if (xmlAssocMetadata.isReference()) {
88                              parts.add(assoc.getToClass());
89                          }
90                      }
91                  }
92              }
93          }
94          return parts.contains(modelClass);
95      }
96  }