View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.sequencer.java;
25  
26  import java.util.List;
27  import org.modeshape.graph.property.NameFactory;
28  import org.modeshape.graph.property.Path;
29  import org.modeshape.graph.property.PathFactory;
30  import org.modeshape.graph.sequencer.SequencerOutput;
31  import org.modeshape.sequencer.java.metadata.ArrayTypeFieldMetadata;
32  import org.modeshape.sequencer.java.metadata.ModifierMetadata;
33  import org.modeshape.sequencer.java.metadata.Variable;
34  
35  /**
36   * Sequencer for array types.
37   */
38  public class ArrayTypeFieldMetadataSequencer {
39  
40      /**
41       * Sequence all formal parameters of a method.
42       * 
43       * @param output - the {@link SequencerOutput}.
44       * @param nameFactory - the {@link NameFactory}.
45       * @param pathFactory - the {@link PathFactory}.
46       * @param arrayTypeFieldMetadata - the meta data of a array type.
47       * @param methodParamRootPath - Base path of the method declaration.
48       */
49      public static void sequenceMethodFormalParam( SequencerOutput output,
50                                                    NameFactory nameFactory,
51                                                    PathFactory pathFactory,
52                                                    ArrayTypeFieldMetadata arrayTypeFieldMetadata,
53                                                    Path methodParamRootPath ) {
54          Path methodFormalParamRootPath = createRootPath(pathFactory, methodParamRootPath);
55          output.setProperty(methodFormalParamRootPath,
56                             nameFactory.create(JavaMetadataLexicon.ARRAY_TYPE_NAME),
57                             arrayTypeFieldMetadata.getType());
58          Path arrayTypeVariableChildNode = pathFactory.create(pathFactory.create(pathFactory.create(pathFactory.create(methodParamRootPath,
59                                                                                                                        JavaMetadataLexicon.TYPE_CHILD_NODE),
60                                                                                                     JavaMetadataLexicon.ARRAY_TYPE_CHILD_NODE),
61                                                                                  JavaMetadataLexicon.ARRAY_TYPE_VARIABLE),
62                                                               JavaMetadataLexicon.VARIABLE);
63          for (Variable variable : arrayTypeFieldMetadata.getVariables()) {
64              VariableSequencer.sequenceTheVariable(output, nameFactory, variable, arrayTypeVariableChildNode);
65          }
66  
67      }
68  
69      /**
70       * the root path.
71       * 
72       * @param pathFactory the path factory to use
73       * @param basePath - the base path to use to build a root path.
74       * @return the root path, that is compose from other base path.
75       */
76      public static Path createRootPath( PathFactory pathFactory,
77                                         Path basePath ) {
78          return pathFactory.create(pathFactory.create(basePath, JavaMetadataLexicon.TYPE_CHILD_NODE),
79                                    JavaMetadataLexicon.ARRAY_TYPE_CHILD_NODE);
80      }
81  
82      /**
83       * Sequence member data of array type.
84       * 
85       * @param arrayTypeFieldMetadata
86       * @param pathFactory
87       * @param nameFactory
88       * @param output
89       * @param path
90       * @param index
91       */
92      public static void sequenceFieldMemberData( ArrayTypeFieldMetadata arrayTypeFieldMetadata,
93                                                  PathFactory pathFactory,
94                                                  NameFactory nameFactory,
95                                                  SequencerOutput output,
96                                                  Path path,
97                                                  int index ) {
98  
99          // type
100         Path arryTypeChildNode = pathFactory.create(path);
101         output.setProperty(arryTypeChildNode,
102                            nameFactory.create(JavaMetadataLexicon.ARRAY_TYPE_NAME),
103                            arrayTypeFieldMetadata.getType());
104         // modifiers
105         List<ModifierMetadata> modifiers = arrayTypeFieldMetadata.getModifiers();
106         int arrayModifierIndex = 1;
107         for (ModifierMetadata modifierMetadata : modifiers) {
108             Path modifierPath = pathFactory.create(pathFactory.create(path, JavaMetadataLexicon.ARRAY_TYPE_MODIFIER_CHILD_NODE),
109                                                    pathFactory.createSegment(JavaMetadataLexicon.MODIFIER_DECLARATION_CHILD_NODE,
110                                                                              arrayModifierIndex));
111             output.setProperty(modifierPath, nameFactory.create(JavaMetadataLexicon.MODIFIER_NAME), modifierMetadata.getName());
112             arrayModifierIndex++;
113         }
114         // variables
115         List<Variable> variables = arrayTypeFieldMetadata.getVariables();
116         int arrayVariableIndex = 1;
117         for (Variable variable : variables) {
118             Path variablePath = pathFactory.create(pathFactory.create(path, JavaMetadataLexicon.ARRAY_TYPE_VARIABLE),
119                                                    pathFactory.createSegment(JavaMetadataLexicon.VARIABLE, arrayVariableIndex));
120 
121             VariableSequencer.sequenceTheVariable(output, nameFactory, variable, variablePath);
122             arrayVariableIndex++;
123         }
124     }
125 
126 }