1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.sequencer.ddl.node;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import org.modeshape.graph.ExecutionContext;
29 import org.modeshape.graph.JcrLexicon;
30 import org.modeshape.graph.JcrNtLexicon;
31 import org.modeshape.graph.property.Name;
32 import org.modeshape.graph.property.Path;
33 import org.modeshape.graph.property.Property;
34 import org.modeshape.graph.property.basic.LongValueFactory;
35 import org.modeshape.graph.property.basic.SimpleNamespaceRegistry;
36 import org.modeshape.graph.property.basic.StringValueFactory;
37
38
39
40
41 public class AstNodeFactory {
42
43
44
45 private ExecutionContext context;
46
47 private LongValueFactory longFactory;
48 private StringValueFactory stringFactory;
49
50
51
52
53 public AstNodeFactory( ExecutionContext context ) {
54 super();
55 this.context = context;
56 this.stringFactory = new StringValueFactory(new SimpleNamespaceRegistry(), Path.URL_DECODER, Path.URL_ENCODER);
57 this.longFactory = new LongValueFactory(Path.URL_DECODER, stringFactory);
58 }
59
60 public AstNodeFactory() {
61 this.context = new ExecutionContext();
62 }
63
64
65
66
67
68
69
70 public Name name( String name ) {
71 assert name != null;
72 return context.getValueFactories().getNameFactory().create(name);
73 }
74
75
76
77
78
79
80
81 public AstNode node( String name ) {
82 assert name != null;
83
84 AstNode node = new AstNode(name(name));
85 node.setProperty(JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.UNSTRUCTURED);
86
87 return node;
88 }
89
90
91
92
93
94
95
96
97
98 public AstNode node( String name,
99 AstNode parent,
100 Object... types ) {
101 assert name != null;
102 assert parent != null;
103 assert types != null;
104 assert types.length > 0;
105
106 AstNode node = new AstNode(name(name));
107 node.setProperty(JcrLexicon.MIXIN_TYPES, types);
108 node.setProperty(JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.UNSTRUCTURED);
109 node.setParent(parent);
110 return node;
111 }
112
113
114
115
116
117
118
119
120
121 public AstNode node( String name,
122 AstNode parent,
123 Name type ) {
124 assert name != null;
125 assert parent != null;
126 assert type != null;
127
128 AstNode node = new AstNode(name(name));
129 node.setProperty(JcrLexicon.MIXIN_TYPES, type);
130 node.setProperty(JcrLexicon.PRIMARY_TYPE, JcrNtLexicon.UNSTRUCTURED);
131 node.setParent(parent);
132 return node;
133 }
134
135
136
137
138
139
140
141 public void setType( AstNode node,
142 Name type ) {
143 assert node != null;
144 assert type != null;
145 node.setProperty(JcrLexicon.MIXIN_TYPES, type);
146 }
147
148
149
150
151
152
153
154
155 public List<AstNode> getChildrenForType( AstNode astNode,
156 Name nodeType ) {
157 assert astNode != null;
158 assert nodeType != null;
159
160 List<AstNode> childrenOfType = new ArrayList<AstNode>();
161 for (AstNode child : astNode.getChildren()) {
162 if (hasMixinType(child, nodeType)) {
163 childrenOfType.add(child);
164 }
165 }
166
167 return childrenOfType;
168 }
169
170
171
172
173
174
175
176
177
178 public AstNode getChildforNameAndType( AstNode astNode,
179 String name,
180 Name nodeType ) {
181 assert astNode != null;
182 assert name != null;
183 assert nodeType != null;
184
185 for (AstNode child : astNode.getChildren()) {
186 if (hasMixinType(child.getProperty(JcrLexicon.MIXIN_TYPES), nodeType)) {
187 if (name.equalsIgnoreCase(child.getName().getString())) {
188 return child;
189 }
190 }
191 }
192 return null;
193 }
194
195
196
197
198
199
200
201
202 private boolean hasMixinType( Property mixins,
203 Name mixinType ) {
204 assert mixins != null;
205 assert mixinType != null;
206
207 for (Object prop : mixins.getValuesAsArray()) {
208 if (prop instanceof Name) {
209 if (prop.equals(mixinType)) {
210 return true;
211 }
212 }
213 }
214
215 return false;
216 }
217
218
219
220
221
222
223
224
225 public boolean hasMixinType( AstNode node,
226 Name mixinType ) {
227 assert node != null;
228 assert mixinType != null;
229 return hasMixinType(node.getProperty(JcrLexicon.MIXIN_TYPES), mixinType);
230 }
231
232 public int createInt( String stringValue ) {
233 return longFactory.create(stringValue).intValue();
234 }
235
236 }