]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.xml.sax/src/org/simantics/xml/sax/SchemaConversionBase.java
Handling ComplexTypes defined with SimpleContent
[simantics/interop.git] / org.simantics.xml.sax / src / org / simantics / xml / sax / SchemaConversionBase.java
1 package org.simantics.xml.sax;\r
2 \r
3 import java.util.ArrayDeque;\r
4 import java.util.ArrayList;\r
5 import java.util.Deque;\r
6 import java.util.HashMap;\r
7 import java.util.HashSet;\r
8 import java.util.List;\r
9 import java.util.Map;\r
10 import java.util.Set;\r
11 \r
12 import javax.xml.bind.JAXBElement;\r
13 import javax.xml.namespace.QName;\r
14 \r
15 import org.simantics.utils.datastructures.BijectionMap;\r
16 import org.simantics.xml.sax.SchemaElement.ElementType;\r
17 import org.simantics.xml.sax.SchemaObject.ObjectType;\r
18 import org.simantics.xml.sax.configuration.AttributeComposition;\r
19 import org.simantics.xml.sax.configuration.Configuration;\r
20 import org.simantics.xml.sax.configuration.IDProvider;\r
21 import org.simantics.xml.sax.configuration.IDReference;\r
22 import org.simantics.xml.sax.configuration.OrderedChild;\r
23 import org.simantics.xml.sax.configuration.Rename;\r
24 import org.simantics.xml.sax.configuration.UnrecognizedChildElement;\r
25 import org.w3._2001.xmlschema.All;\r
26 import org.w3._2001.xmlschema.Annotated;\r
27 import org.w3._2001.xmlschema.Any;\r
28 import org.w3._2001.xmlschema.Attribute;\r
29 import org.w3._2001.xmlschema.AttributeGroup;\r
30 import org.w3._2001.xmlschema.ComplexContent;\r
31 import org.w3._2001.xmlschema.ComplexType;\r
32 import org.w3._2001.xmlschema.Element;\r
33 import org.w3._2001.xmlschema.ExplicitGroup;\r
34 import org.w3._2001.xmlschema.ExtensionType;\r
35 import org.w3._2001.xmlschema.LocalComplexType;\r
36 import org.w3._2001.xmlschema.LocalElement;\r
37 import org.w3._2001.xmlschema.NamedAttributeGroup;\r
38 import org.w3._2001.xmlschema.OpenAttrs;\r
39 import org.w3._2001.xmlschema.Restriction;\r
40 import org.w3._2001.xmlschema.Schema;\r
41 import org.w3._2001.xmlschema.SimpleContent;\r
42 import org.w3._2001.xmlschema.SimpleType;\r
43 import org.w3._2001.xmlschema.TopLevelAttribute;\r
44 import org.w3._2001.xmlschema.TopLevelComplexType;\r
45 import org.w3._2001.xmlschema.TopLevelElement;\r
46 import org.w3._2001.xmlschema.TopLevelSimpleType;\r
47 \r
48 public abstract class SchemaConversionBase {\r
49         \r
50         protected Schema schema;\r
51         protected Configuration configuration;\r
52         \r
53         protected static final String SCHEMA_NS = "http://www.w3.org/2001/XMLSchema";\r
54         protected static final String CONVERSION_NS = "http://www.simantics.org/Layer0";\r
55         \r
56         protected Map<String,Map<String,TypeEntry>> typeMap;\r
57         \r
58         public SchemaConversionBase(Configuration configuration) {\r
59                 this.configuration = configuration;\r
60                 typeMap = new HashMap<String, Map<String,TypeEntry>>();\r
61                 \r
62                 Map<String,TypeEntry> schemaTypes = new HashMap<String, SchemaConversionBase.TypeEntry>();\r
63                 typeMap.put(SCHEMA_NS, schemaTypes);\r
64                 Map<String,TypeEntry> l0Types = new HashMap<String, SchemaConversionBase.TypeEntry>();\r
65                 typeMap.put(CONVERSION_NS, l0Types);\r
66                 \r
67                 schemaTypes.put("string",               new TypeEntry("L0.String",                      "Bindings.STRING", "java.lang.String", "","","","",""));\r
68                 schemaTypes.put("NMTOKEN",              new TypeEntry("L0.String",                      "Bindings.STRING", "java.lang.String", "","","","",""));\r
69                 schemaTypes.put("token",                new TypeEntry("L0.String",                      "Bindings.STRING", "java.lang.String", "","","","",""));\r
70                 schemaTypes.put("ID",                   new TypeEntry("L0.String",                      "Bindings.STRING", "java.lang.String", "","","","","",true));\r
71                 schemaTypes.put("IDREF",                new TypeEntry("L0.String",                      "Bindings.STRING", "java.lang.String", "","","","",""));\r
72                 schemaTypes.put("date",                 new TypeEntry("XML.Date",                       "org.simantics.xml.sax.base.datatypes.literal.Date.BINDING", "org.simantics.xml.sax.base.datatypes.literal.Date", "","org.simantics.xml.sax.base.datatypes.literal.Date.parseDate(",")","(",").toString()"));\r
73                 schemaTypes.put("time",                 new TypeEntry("XML.Time",                       "org.simantics.xml.sax.base.datatypes.literal.Time.BINDING", "org.simantics.xml.sax.base.datatypes.literal.Time", "","org.simantics.xml.sax.base.datatypes.literal.Time.parseTime(",")","(",").toString()"));\r
74                 schemaTypes.put("dateTime",             new TypeEntry("XML.DateTime",           "org.simantics.xml.sax.base.datatypes.literal.DateTime.BINDING", "org.simantics.xml.sax.base.datatypes.literal.DateTime", "","org.simantics.xml.sax.base.datatypes.literal.DateTime.parseDateTime(",")","(",").toString()"));\r
75                 schemaTypes.put("anyURI",               new TypeEntry("L0.URI",                         "Bindings.STRING", "java.lang.String", "","","","",""));\r
76                 schemaTypes.put("double",               new TypeEntry("L0.Double",                      "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")"));\r
77                 schemaTypes.put("float",                new TypeEntry("L0.Float",                       "Bindings.FLOAT",  "float",  "java.lang.Float.NaN","java.lang.Float.parseFloat(",")","java.lang.Float.toString(",")"));\r
78                 schemaTypes.put("decimal",              new TypeEntry("L0.Double",                      "Bindings.DOUBLE", "double", "java.lang.Double.NaN","java.lang.Double.parseDouble(",")","java.lang.Double.toString(",")"));\r
79                 schemaTypes.put("boolean",              new TypeEntry("L0.Boolean",                     "Bindings.BOOLEAN", "boolean", "false","java.lang.Boolean.parseBoolean(",")","java.lang.Boolean.toString(",")"));\r
80                 schemaTypes.put("integer",              new TypeEntry("L0.Integer",             "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
81                 schemaTypes.put("positiveInteger", new TypeEntry("L0.Integer",          "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
82                 schemaTypes.put("nonPositiveInteger", new TypeEntry("L0.Integer",       "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
83                 schemaTypes.put("nonNegativeInteger", new TypeEntry("L0.Integer",       "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
84                 schemaTypes.put("negativeInteger", new TypeEntry("L0.Integer",          "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
85                 schemaTypes.put("unsignedInt",  new TypeEntry("L0.Integer",             "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
86                 schemaTypes.put("int",                  new TypeEntry("L0.Integer",                     "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
87                 schemaTypes.put("short",                new TypeEntry("L0.Integer",                     "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
88                 schemaTypes.put("unsignedShort",new TypeEntry("L0.Integer",                     "Bindings.INTEGER", "int", "0","java.lang.Integer.parseInt(",")","java.lang.Integer.toString(",")"));\r
89                 schemaTypes.put("byte",                 new TypeEntry("L0.Byte",                        "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")"));\r
90                 schemaTypes.put("unsignedByte", new TypeEntry("L0.Byte",                        "Bindings.BYTE", "byte", "0","java.lang.Byte.parseByte(",")","java.lang.Byte.toString(",")"));\r
91                 schemaTypes.put("long",                 new TypeEntry("L0.Long",                        "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")"));\r
92                 schemaTypes.put("unsignedLong", new TypeEntry("L0.Long",                        "Bindings.LONG", "long", "0","java.lang.Long.parseLong(",")","java.lang.Long.toString(",")"));\r
93                 schemaTypes.put("base64Binary", new TypeEntry("L0.ByteArray",           "Bindings.BYTE_ARRAY", "byte[]", "new byte[0]","",".getBytes(org.simantics.databoard.util.binary.UTF8.CHARSET)","new java.lang.String(",", org.simantics.databoard.util.binary.UTF8.CHARSET)"));\r
94                 \r
95                 \r
96                 \r
97                 l0Types.put("doubleArray",              new TypeEntry("L0.DoubleArray",  "Bindings.DOUBLE_ARRAY", "double[]", null,null,null,"java.lang.Double.toString(",")"));\r
98                 l0Types.put("stringArray",              new TypeEntry("L0.StringArray",  "Bindings.STRING_ARRAY", "string[]", null,null,null,"",""));\r
99         }\r
100         \r
101         \r
102         protected TypeEntry getTypeEntry(QName type) {\r
103                 Map<String,TypeEntry> types = typeMap.get(type.getNamespaceURI());\r
104                 if (types == null)\r
105                         return null;\r
106                 TypeEntry entry = types.get(type.getLocalPart());\r
107                 return entry;\r
108         }\r
109         protected TypeEntry getTypeEntry(String type) {\r
110                 for (Map<String,TypeEntry> types : typeMap.values()) {\r
111                         TypeEntry entry = types.get(type);\r
112                         if (entry != null)\r
113                                 return entry;\r
114                 }\r
115                 return null;\r
116         }\r
117         \r
118         protected String getL0TypeFromPrimitiveType(QName primitiveType) {\r
119                 TypeEntry entry = getTypeEntry(primitiveType);\r
120                 if (entry == null)\r
121                         return null;\r
122                 return entry.l0Type;\r
123         }\r
124         \r
125         protected String getL0Type(QName primitiveType) {\r
126                 String type = getL0TypeFromPrimitiveType(primitiveType);\r
127                 if (type != null)\r
128                         return type;\r
129                 SchemaObject simpleTypeObj = simpleTypeName.get(primitiveType.getLocalPart());\r
130                 if (simpleTypeObj == null)\r
131                         return null;\r
132                 SimpleType simpleType = simpleTypeObj.getSimpleType();\r
133                 while (simpleType != null) {\r
134                         QName base = simpleType.getRestriction().getBase();\r
135                         if (base != null)\r
136                                 return getL0Type(base);\r
137                         simpleType = simpleType.getRestriction().getSimpleType();\r
138                 }\r
139                 return null;\r
140         }\r
141         \r
142         protected String getBindingFromPrimitiveType(QName primitiveType) {\r
143                 TypeEntry entry = getTypeEntry(primitiveType);\r
144                 if (entry == null)\r
145                         return null;\r
146                 return entry.binding;\r
147         }\r
148         \r
149         protected String getJavaTypeFromPrimitiveType(QName primitiveType) {\r
150                 TypeEntry entry = getTypeEntry(primitiveType);\r
151                 if (entry == null)\r
152                         return null;\r
153                 return entry.javaType;\r
154         }\r
155         \r
156         \r
157         protected void handle(Schema schema) {  \r
158                 this.schema = schema;\r
159                 preload();\r
160                 \r
161                 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {\r
162                         if (attrs instanceof TopLevelAttribute) {\r
163                                 handle((TopLevelAttribute)attrs);\r
164                                 \r
165                         } else if (attrs instanceof TopLevelComplexType) {\r
166                                 handleComplexType(complexTypes.get(attrs));\r
167                         } else if (attrs instanceof TopLevelElement) {\r
168                                 handleElement(elements.get(attrs));\r
169                         } else if (attrs instanceof TopLevelSimpleType) {\r
170                                 handleSimpleType(simpleTypes.get(attrs));\r
171                         } else if (attrs instanceof NamedAttributeGroup) {\r
172                                 handle((NamedAttributeGroup)attrs);\r
173                         } else {\r
174                                 System.out.println(attrs.getClass().getName());\r
175                         }\r
176                 }\r
177         }\r
178         \r
179         protected Map<String,SchemaObject> elementName = new HashMap<String, SchemaObject>();\r
180         protected Map<String,SchemaObject> complexTypeName = new HashMap<String, SchemaObject>();\r
181         protected Map<String,SchemaObject> simpleTypeName = new HashMap<String, SchemaObject>();\r
182         protected Map<Element,SchemaObject> elements = new HashMap<Element, SchemaObject>();\r
183         protected Map<ComplexType,SchemaObject> complexTypes = new HashMap<ComplexType, SchemaObject>();\r
184         protected Map<SimpleType,SchemaObject> simpleTypes = new HashMap<SimpleType, SchemaObject>();\r
185         \r
186         \r
187         protected SchemaObject getWithName(SchemaObject referrer, String name) {\r
188                 SchemaObject obj = elementName.get(name);\r
189                 if (obj == null)\r
190                         obj = complexTypeName.get(name);\r
191                 if (obj == null)\r
192                         obj = simpleTypeName.get(name);\r
193                 if (obj == null) {\r
194                         throw new RuntimeException("Cannot locate referred type " + name + " when handling " + referrer.getName());\r
195                 }\r
196                 return obj;\r
197         }\r
198         \r
199         protected SchemaObject getWithObj(SchemaObject referrer, OpenAttrs attrs) {\r
200                 SchemaObject obj = null;\r
201                 if (attrs instanceof Element)\r
202                         obj = elements.get(attrs);\r
203                 else if (attrs instanceof ComplexType)\r
204                         obj = complexTypes.get(attrs);\r
205                 else if (attrs instanceof SimpleType) \r
206                         obj = simpleTypes.get(attrs);\r
207                 if (obj == null){\r
208                         throw new RuntimeException("Cannot locate referred object " + attrs + " when handling " + referrer.getName());\r
209                 }\r
210                 return obj;\r
211         }\r
212         \r
213         private void preload() {\r
214                 Deque<SchemaObject> stack = new ArrayDeque<SchemaObject>();\r
215                 //stack.addAll(schema.getSimpleTypeOrComplexTypeOrGroup());\r
216                 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {\r
217                         if (attrs instanceof Element) {\r
218                                 Element element = (Element)attrs;\r
219                                 SchemaObject obj = new SchemaObject(element);\r
220                                 obj.setRename(getRename(element));\r
221                                 stack.push(obj);\r
222                         } else if (attrs instanceof ComplexType) {\r
223                                 ComplexType complexType = (ComplexType)attrs;\r
224                                 SchemaObject obj = new SchemaObject(complexType);\r
225                                 obj.setRename(getRename(complexType));\r
226                                 stack.push(obj);\r
227                         } else if (attrs instanceof SimpleType) {\r
228                                 SimpleType simpleType = (SimpleType)attrs;\r
229                                 SchemaObject obj = new SchemaObject(simpleType);\r
230                                 stack.push(obj);\r
231                         }\r
232                 }\r
233                 \r
234                 while (!stack.isEmpty()) {\r
235                         SchemaObject object = stack.pop();\r
236                         if (object.getType() == ObjectType.COMPLEX_TYPE) {\r
237                                 ComplexType ct = object.getComplexType();\r
238                                 if (ct.getName() != null && ct.getName().length() > 0 && ct instanceof TopLevelComplexType)\r
239                                         complexTypeName.put(ct.getName(), object);\r
240                                 complexTypes.put(ct, object);\r
241                                 if (ct.getChoice() != null) {\r
242                                         preload(object,ct.getChoice(), stack);\r
243                                 }\r
244                                 if (ct.getSequence() != null) {\r
245                                         preload(object,ct.getSequence(), stack);\r
246                                 }\r
247                                 if (ct.getAll() != null) {\r
248                                         preload(object,ct.getAll(), stack);\r
249                                 }\r
250                                 if (ct.getGroup() != null)\r
251                                         throw new RuntimeException("Groups not supported");\r
252                                 if (ct.getComplexContent() != null) {\r
253                                         ComplexContent cc = ct.getComplexContent();\r
254                                         ExtensionType extensionType = cc.getExtension();\r
255                                         if (extensionType != null) {\r
256                                                 if (extensionType.getChoice() != null) {\r
257                                                         preload(object,extensionType.getChoice(), stack);\r
258                                                 }\r
259                                                 if (extensionType.getSequence()!= null) {\r
260                                                         preload(object,extensionType.getSequence(), stack);\r
261                                                 }\r
262                                                 if (extensionType.getAll()!= null) {\r
263                                                         preload(object,extensionType.getAll(), stack);\r
264                                                 }\r
265                                                 if (extensionType.getGroup() != null)\r
266                                                         throw new RuntimeException("Groups not supported");\r
267                                         }\r
268                                 }\r
269                                 if (ct.getSimpleContent() != null) {\r
270                                         SimpleContent cc = ct.getSimpleContent();\r
271                                         ExtensionType extensionType = cc.getExtension();\r
272                                         if (extensionType != null) {\r
273                                                 if (extensionType.getChoice() != null) {\r
274                                                         preload(object,extensionType.getChoice(), stack);\r
275                                                 }\r
276                                                 if (extensionType.getSequence()!= null) {\r
277                                                         preload(object,extensionType.getSequence(), stack);\r
278                                                 }\r
279                                                 if (extensionType.getAll()!= null) {\r
280                                                         preload(object,extensionType.getAll(), stack);\r
281                                                 }\r
282                                                 if (extensionType.getGroup() != null)\r
283                                                         throw new RuntimeException("Groups not supported");\r
284                                         }\r
285                                 }\r
286                         } else if (object.getType() == ObjectType.ELEMENT) {\r
287                                 Element e = object.getElement();\r
288                                 if (e instanceof TopLevelElement)\r
289                                         elementName.put(e.getName(), object);\r
290                                 elements.put(e, object);\r
291                                 if (e.getComplexType() != null)\r
292                                         stack.push(new SchemaObject(object,e.getComplexType()));\r
293                                 if (e.getSimpleType() != null)\r
294                                         stack.push(new SchemaObject(object,e.getSimpleType()));\r
295                         } else if (object.getType() == ObjectType.SIMPLE_TYPE) {\r
296                                 SimpleType e = object.getSimpleType();\r
297                                 if (e instanceof TopLevelSimpleType)\r
298                                         simpleTypeName.put(e.getName(), object);\r
299                                 simpleTypes.put(e, object);\r
300                         }\r
301                 }\r
302         }\r
303         \r
304         private void preload(SchemaObject parent,ExplicitGroup eg, Deque<SchemaObject> stack) {\r
305                 for (Object o : eg.getParticle()) {\r
306                         if (o instanceof JAXBElement<?>) {\r
307                                 JAXBElement<?> element = (JAXBElement<?>)o;\r
308                                 Object elemValue = element.getValue();\r
309                                 if (elemValue instanceof Element) {\r
310                                         SchemaObject obj = new SchemaObject(parent,(Element)elemValue);\r
311                                         obj.setRename(getRename((Element)elemValue));\r
312                                         stack.add(obj);\r
313                                 } else if (elemValue instanceof All) {\r
314                                         preload(parent,(All)elemValue, stack);\r
315                                 } else if (elemValue instanceof ExplicitGroup) {\r
316                                         preload(parent,(ExplicitGroup)elemValue, stack);\r
317                                 } else {\r
318                                         throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName());\r
319                                 }\r
320                         } else if (o instanceof Any){\r
321                                 \r
322                         } else {\r
323                                 throw new RuntimeException("Unknown ExplicitGroup reference " + o.getClass().getName());\r
324                         }\r
325                 }\r
326         }\r
327         \r
328         protected void handle(TopLevelAttribute topLevelAttribute) {\r
329                 handle(null, topLevelAttribute);\r
330         }\r
331         \r
332         protected void handleSimpleType(SchemaObject topLevelSimpleType) {\r
333                 handleSimpleType(null,topLevelSimpleType);\r
334         }\r
335         \r
336         protected void handle(NamedAttributeGroup namedAttributeGroup){\r
337                 handle(null, namedAttributeGroup);\r
338         }\r
339         \r
340         protected QName getComplexTypeBase(ComplexType complexType) {\r
341                 if (complexType == null)\r
342                         return null;\r
343                 ComplexContent complexContent = complexType.getComplexContent();\r
344                 if (complexContent != null) {\r
345                         ExtensionType extensionType = complexContent.getExtension();\r
346                         if (extensionType != null) {\r
347                                 QName type = extensionType.getBase();\r
348                                 return type;\r
349                         }\r
350                 }\r
351 //              SimpleContent simpleContent = complexType.getSimpleContent();\r
352 //              if (simpleContent != null) {\r
353 //                      ExtensionType extensionType = simpleContent.getExtension();\r
354 //                      if (extensionType != null) {\r
355 //                              QName type = extensionType.getBase();\r
356 //                              return type;\r
357 //                      }\r
358 //              }\r
359                 return null;\r
360         }\r
361         \r
362         protected QName getSimpleTypeBase(SimpleType simpleType) {\r
363                 if (simpleType == null)\r
364                         return null;\r
365                 return simpleType.getRestriction().getBase();\r
366         }\r
367         \r
368         protected QName getElementBase(Element element) {\r
369                 ComplexType complexType = element.getComplexType();\r
370                 SimpleType simpleType = element.getSimpleType();\r
371                 if (complexType != null)\r
372                         return getComplexTypeBase(complexType);\r
373                 if (simpleType != null) {\r
374                         return getSimpleTypeBase(simpleType);\r
375                 }\r
376                 return null;\r
377         }\r
378         \r
379 \r
380         \r
381         \r
382         \r
383         \r
384         \r
385         \r
386         private void handleAttributes(SchemaObject complexType, List<Annotated> attributeOrAttributeGroup) {\r
387                 //name = getComplexTypePrefix()+complexType.getName()\r
388                 \r
389                 Set<Annotated> handled = handleAttributeCompositions(complexType,attributeOrAttributeGroup);\r
390                 for (Annotated annotated : attributeOrAttributeGroup) {\r
391                         if (handled.contains(annotated))\r
392                                 continue;\r
393                         if (annotated instanceof Attribute) {\r
394                                 handle(complexType,(Attribute)annotated);\r
395                         } else if (annotated instanceof AttributeGroup){\r
396                                 handle(complexType,(AttributeGroup)annotated);\r
397                                 //comment("AttributeGroup " + ((AttributeGroup)annotated).getRef().getLocalPart());\r
398                         } else {\r
399                                 throw new RuntimeException();\r
400                         }\r
401                 }\r
402         }\r
403         \r
404         protected abstract void handleAttributes(SchemaObject simpleTypeObj);\r
405         \r
406         protected void handleExtensionAttributes(SchemaObject complexType) {\r
407                 ComplexContent complexContent = complexType.getComplexType().getComplexContent();\r
408                 if (complexContent != null) {\r
409                         ExtensionType extensionType = complexContent.getExtension();\r
410                         if (extensionType != null) {\r
411                                 handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup());\r
412                         }\r
413                 }\r
414                 SimpleContent simpleContent = complexType.getComplexType().getSimpleContent();\r
415                 if (simpleContent != null) {\r
416                         ExtensionType extensionType = simpleContent.getExtension();\r
417                         if (extensionType != null) {\r
418                                 handleAttributes(complexType, extensionType.getAttributeOrAttributeGroup());\r
419                         }\r
420                 }\r
421         }\r
422         \r
423         \r
424         \r
425         protected void handleComplexTypeAttributes(SchemaObject complexType) {          \r
426                 handleAttributes(complexType,complexType.getComplexType().getAttributeOrAttributeGroup());\r
427         }\r
428         \r
429         protected void handleElementComplexTypeAttributes(SchemaObject complexType) {\r
430                 if (complexType != null) {\r
431                         handleComplexTypeAttributes(complexType);\r
432                         handleExtensionAttributes(complexType);\r
433                 }\r
434         }\r
435         \r
436         protected void handleElementSimpleTypeAttributes(SchemaObject simpleType) {\r
437                 if (simpleType != null) {\r
438                         handleAttributes(simpleType);\r
439                 }\r
440         }\r
441         \r
442         protected Set<Annotated> handleAttributeCompositions(SchemaObject obj, List<Annotated> attributeOrAttributeGroup) {\r
443                 \r
444                 Set<Annotated> handled = new HashSet<Annotated>();\r
445                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
446                         if (e.getValue() instanceof AttributeComposition) {\r
447                                 AttributeComposition composition = (AttributeComposition)e.getValue();\r
448                                 if (composition.getAttribute().size() < 2)\r
449                                         throw new RuntimeException("Attribute Composition is not valid");\r
450                                 BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> map = new BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated>();\r
451                                 for (org.simantics.xml.sax.configuration.Attribute a : composition.getAttribute()) {\r
452                                         for (Annotated annotated : attributeOrAttributeGroup) {\r
453                                                 if (annotated instanceof Attribute) {\r
454                                                         Attribute attribute = (Attribute)annotated;\r
455                                                         QName type = getBaseType(attribute);\r
456                                                         if (a.getName().equals(attribute.getName()) && type != null && a.getType().equals(type.getLocalPart())) {\r
457                                                                 map.map(a, attribute);\r
458                                                         }\r
459                                                 }\r
460                                         }\r
461                                 }\r
462                                 if (composition.getAttribute().size() == map.size()) {\r
463                                         handled.addAll(map.getRightSet());\r
464                                         handleAttributeComposition(obj, composition, map);      \r
465                                 }\r
466                         }\r
467                 }\r
468                 return handled;\r
469         }\r
470         \r
471         protected QName getBaseType(Attribute attribute) {\r
472                 if (attribute.getType() != null)\r
473                         return attribute.getType();\r
474                 if (attribute.getRef() != null)\r
475                         return attribute.getRef();\r
476                 SimpleType simpleType = attribute.getSimpleType();\r
477                 if (simpleType != null) {\r
478                         Restriction restriction = simpleType.getRestriction();\r
479                         if (restriction != null)\r
480                                 if (restriction.getBase() != null)\r
481                                         return restriction.getBase();\r
482                 }\r
483                 return null;\r
484         }\r
485         \r
486         protected QName getPrimitiveType(Attribute attribute) {\r
487                 QName type = getBaseType(attribute);\r
488                 String b = getBindingFromPrimitiveType(type);\r
489                 while (b==null && type != null) {\r
490                         SchemaObject baseType = simpleTypeName.get(type.getLocalPart());\r
491                         if (baseType != null) {\r
492                                 Restriction restriction = baseType.getSimpleType().getRestriction();\r
493                                 if (restriction != null)\r
494                                         if (restriction.getBase() != null) {\r
495                                                 type = restriction.getBase();\r
496                                                 b = getBindingFromPrimitiveType(type);\r
497                                         }\r
498                         }\r
499                 }\r
500                 return type;\r
501         }\r
502         \r
503         protected Attribute getRefAttribute(QName ref) {\r
504                 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {\r
505                         if (attrs instanceof TopLevelAttribute) {\r
506                                 TopLevelAttribute attribute = (TopLevelAttribute)attrs;\r
507                                 if (attribute.getName().equals(ref.getLocalPart()))\r
508                                         return attribute;\r
509                         }\r
510                 }\r
511                 return null;\r
512         }\r
513         \r
514         protected abstract void handleAttributeComposition(SchemaObject obj, AttributeComposition composition, BijectionMap<org.simantics.xml.sax.configuration.Attribute, Annotated> attributes);\r
515         \r
516         \r
517         \r
518         \r
519         protected void handleComplexType(SchemaObject complexType) {\r
520                 handleComplexTypeAttributes(complexType);\r
521                 handleComplexTypeExtension(complexType);\r
522                 handleExtensionAttributes(complexType);\r
523         }\r
524         \r
525         protected enum RefType{Element,Reference,Type};\r
526         \r
527         protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement element, String refName, RefType refType);\r
528         protected abstract void handleIndicator(SchemaObject parent, SchemaElement indicator, SchemaElement any);\r
529         protected abstract void handle(SchemaObject parent, SchemaElement indicator, List<SchemaElement> elements);\r
530         \r
531         protected void handle(SchemaObject parent, ExplicitGroup eg, SchemaElement.ElementType indicator) {\r
532                 handle(parent, new SchemaElement(eg, indicator));\r
533         }\r
534         \r
535         protected void handle(SchemaObject parent, SchemaElement indicator) {\r
536                 \r
537                 \r
538                 List<SchemaElement> elements = new ArrayList<SchemaElement>();\r
539                 List<SchemaElement> choices = new ArrayList<SchemaElement>();\r
540                 List<SchemaElement> sequences = new ArrayList<SchemaElement>();\r
541                 List<SchemaElement> alls = new ArrayList<SchemaElement>();\r
542                 List<SchemaElement> anys = new ArrayList<SchemaElement>();\r
543                 \r
544                 for (Object o : indicator.getGroup().getParticle()) {\r
545                         if (o instanceof JAXBElement<?>) {\r
546                                 JAXBElement<?> element = (JAXBElement<?>)o;\r
547                                 Object elemValue = element.getValue();\r
548                                 if (elemValue instanceof LocalElement) {\r
549                                         LocalElement localElement = (LocalElement)elemValue;\r
550                                         elements.add(new SchemaElement(indicator,localElement, ElementType.ELEMENT));\r
551                                 } else if (elemValue instanceof All) {\r
552                                         alls.add(new SchemaElement(indicator,(All)elemValue, ElementType.ALL));\r
553                                 } else if (elemValue instanceof ExplicitGroup) {\r
554                                         QName qname = element.getName();\r
555                                         if ("choice".equals(qname.getLocalPart())) {\r
556                                                 choices.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.CHOICE));\r
557                                         } else if ("sequence".equals(qname.getLocalPart())) {\r
558                                                 sequences.add(new SchemaElement(indicator,(ExplicitGroup)elemValue, ElementType.SEQUENCE));\r
559                                         }\r
560                                 } else {\r
561                                         throw new RuntimeException("Unknown ExplicitGroup element " + elemValue.getClass().getName());\r
562                                 }\r
563                         } else if (o instanceof Any){\r
564                                 anys.add(new SchemaElement(indicator,(Any)o, ElementType.ANY));\r
565                         } else {\r
566                                 throw new RuntimeException("Unknown ExplicitGroup reference " + o.getClass().getName());\r
567                         }\r
568                 }\r
569                 \r
570                 if (elements.size() == 0 && choices.size() == 0 && sequences.size() == 0 && alls.size() == 0 && anys.size() == 0) {\r
571                         return;\r
572                 }\r
573                 \r
574                 if (indicator.getType() == SchemaElement.ElementType.SEQUENCE) {\r
575                         if (indicator.getRestriction().single()) {\r
576                                 if (elements.size() > 0) {\r
577                                         for (SchemaElement e : sequences) {\r
578                                                 handle(parent, e);\r
579                                         }\r
580                                         for (SchemaElement c : choices) {\r
581                                                 handle(parent, c);\r
582                                         }\r
583                                         \r
584                                         for (SchemaElement c : alls) {\r
585                                                 handle(parent, c);\r
586                                         }\r
587                                         handle(parent, indicator, elements);\r
588                                         for (SchemaElement a : anys) {\r
589                                                 handleIndicator(parent, indicator, a);\r
590                                         }\r
591                                 } else {\r
592                                         if (sequences.size() > 0) {\r
593                                                 throw new RuntimeException("Cannot handle Sequence with inner Sequences");\r
594                                         }\r
595                                         for (SchemaElement c : choices) {\r
596                                                 handle(parent, c);\r
597                                         }\r
598                                         for (SchemaElement a : anys) {\r
599                                                 handleIndicator(parent, indicator, a);\r
600                                         }\r
601                                 }\r
602                         } else {\r
603                                 if (sequences.size() > 0 || choices.size() > 0 || alls.size() > 0) {\r
604                                         throw new RuntimeException("Cannot handle Sequence with inner ExplicitGroups");\r
605                                 }\r
606                                 handle(parent, indicator, elements);\r
607                                 for (SchemaElement a : anys) {\r
608                                         handleIndicator(parent, indicator, a);\r
609                                 }\r
610                         }\r
611                 \r
612                 } else if (indicator.getType() == SchemaElement.ElementType.CHOICE){\r
613                         if (indicator.getRestriction().single()) {\r
614                                 if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0) {\r
615                                         throw new RuntimeException("Cannot handle Choice that contains something else than Elements");\r
616                                 }\r
617                                 handle(parent, indicator, elements);\r
618                                 \r
619                         } else {\r
620                                 if (sequences.size() > 0 || choices.size() > 0) {\r
621                                         throw new RuntimeException("Cannot handle Choice with inner ExplicitGroups");\r
622                                 }\r
623                                 handle(parent, indicator,  elements);\r
624                                 for (SchemaElement a : anys) {\r
625                                         handleIndicator(parent, indicator, a);\r
626                                 }\r
627                         }\r
628                 } else if (indicator.getType() == ElementType.ALL) {\r
629                         if (sequences.size()> 0 || choices.size() > 0 || alls.size() > 0 || anys.size() > 0) {\r
630                                 throw new RuntimeException("Cannot handle All that contains something else than Elements");\r
631                         }\r
632                         if (!indicator.getRestriction().single()) {\r
633                                 throw new RuntimeException("All indicator must have maxOccurs=1");\r
634                         }\r
635                         handle(parent, indicator, elements);\r
636                 }\r
637         }\r
638         \r
639         \r
640         protected void handle(SchemaObject parent, SchemaElement indicator, SchemaElement element) {\r
641                 Element localElement = element.getElement();\r
642                 if (localElement.getName() != null) {\r
643                         SchemaObject eObj = elements.get(localElement); // FIXME: handleIndicator should be refactored, not this methdof must be overridden in JavaGenerator to handle renaming of Elements properly\r
644                         //String refName = eObj.getName();//localElement.getName();\r
645                         QName refType = localElement.getType();\r
646                         if (refType != null)\r
647                                 //handleIndicator(parent, indicator, element, false, refName, refType);\r
648                                 handleIndicator(parent, indicator, element, null, RefType.Type);\r
649                         else {\r
650                                 handleElement(eObj);\r
651                                 //handleIndicator(parent, indicator, element, false, refName, localElement);\r
652                                 handleIndicator(parent, indicator, element, null, RefType.Element);\r
653                         }\r
654                 } else if (localElement.getRef() != null) {\r
655                         //QName refType = localElement.getRef();\r
656                         //handleIndicator(parent, indicator,element, true, refType.getLocalPart(), refType);\r
657                         handleIndicator(parent, indicator,element, null, RefType.Reference);\r
658                 }\r
659         }\r
660         \r
661         protected String getElementName(Element localElement) {\r
662                 if (localElement.getName() != null) {\r
663                         String refName = localElement.getName();\r
664                         QName refType = localElement.getType();\r
665                         if (refType != null)\r
666                                 return refName;\r
667                 } else if (localElement.getRef() != null) {\r
668                         QName refType = localElement.getRef();\r
669                         if (refType != null)\r
670                                 return refType.getLocalPart();\r
671                 }\r
672                 return null;\r
673         }\r
674         \r
675         protected String getChoiceName(List<SchemaElement> elements) {\r
676                 if (elements.size() == 1) {\r
677                         return getElementName(elements.get(0).getElement());\r
678                 }\r
679                 List<String> names = new ArrayList<String>();\r
680                 for (SchemaElement e : elements) {\r
681                         String name = getElementName(e.getElement());\r
682                         if (name != null)\r
683                                 names.add(name);\r
684                 }\r
685                 String name = "";\r
686                 for (int i = 0; i < names.size(); i++) {\r
687                         if (i == 0)\r
688                                 name = names.get(i);\r
689                         else\r
690                                 name += "Or"+names.get(i);\r
691                 }\r
692                 return name;\r
693         }\r
694         \r
695         \r
696         protected abstract void handle(SchemaObject parent, Attribute attribute) ;\r
697         protected abstract void handle(SchemaObject parent, AttributeGroup attribute) ;\r
698         \r
699         protected abstract void handleSimpleType(SchemaObject parent, SchemaObject simpleType);\r
700         \r
701         \r
702         \r
703         protected void handleComplexTypeExtension(SchemaObject complexTypeObj) {\r
704                 ComplexType complexType = complexTypeObj.getComplexType();\r
705                 if (complexType != null) {\r
706                         if (complexType.getChoice() != null)\r
707                                 handle(complexTypeObj, complexType.getChoice(), SchemaElement.ElementType.CHOICE);\r
708                         if (complexType.getSequence() != null)\r
709                                 handle(complexTypeObj, complexType.getSequence(), SchemaElement.ElementType.SEQUENCE);\r
710                         if (complexType.getAll() != null)\r
711                                 handle(complexTypeObj, complexType.getAll(), SchemaElement.ElementType.ALL);\r
712                         if (complexType.getGroup() != null)\r
713                                 throw new RuntimeException("Groups not supported");\r
714                         ComplexContent complexContent = complexType.getComplexContent();\r
715                         if (complexContent != null) {\r
716                                 ExtensionType extensionType = complexContent.getExtension();\r
717                                 if (extensionType != null) {\r
718                                         if (extensionType.getChoice() != null) {\r
719                                                 handle(complexTypeObj, extensionType.getChoice(), SchemaElement.ElementType.CHOICE);\r
720                                         }\r
721                                         if (extensionType.getSequence()!= null) {\r
722                                                 handle(complexTypeObj, extensionType.getSequence(), SchemaElement.ElementType.SEQUENCE);\r
723                                         }\r
724                                         if (extensionType.getAll()!= null) {\r
725                                                 handle(complexTypeObj, extensionType.getAll(), SchemaElement.ElementType.ALL);\r
726                                         }\r
727                                         if (extensionType.getGroup() != null) {\r
728                                                 throw new RuntimeException("Groups not supported");\r
729                                         }\r
730                                 }\r
731                         }\r
732 //                      SimpleContent simpleContent = complexType.getSimpleContent();\r
733 //                      if (simpleContent != null) {\r
734 //                              ExtensionType extensionType = simpleContent.getExtension();\r
735 //                      }\r
736                 }\r
737         }\r
738         \r
739         protected void handleElement(SchemaObject topLevelElement) {\r
740                 LocalComplexType complexType = topLevelElement.getElement().getComplexType();\r
741                 \r
742                 if (complexType != null) {\r
743                         SchemaObject complextTypeObj = complexTypes.get(complexType);\r
744                         handleElementComplexTypeAttributes(complextTypeObj);\r
745                         handleComplexTypeExtension(complextTypeObj);\r
746                 }\r
747                 \r
748                 \r
749         }\r
750         \r
751         \r
752         protected boolean isElementRef(String ref) {\r
753                 return elementName.containsKey(ref);\r
754         }\r
755         \r
756         protected boolean isComplexTypeRef(String ref) {\r
757                 return complexTypeName.containsKey(ref);\r
758         }\r
759         \r
760         protected boolean isSimpleTypeRef(String ref) {\r
761                 return simpleTypeName.containsKey(ref);\r
762         }\r
763         \r
764         protected NamedAttributeGroup getAttributeGroup(String name) {\r
765                 for (OpenAttrs attrs : schema.getSimpleTypeOrComplexTypeOrGroup()) {\r
766                         if (attrs instanceof NamedAttributeGroup) {\r
767                                 NamedAttributeGroup group = (NamedAttributeGroup)attrs;\r
768                                 if (group.getName().equals(name))\r
769                                         return group;\r
770                         }\r
771                 }\r
772                 return null;\r
773         }\r
774         \r
775         protected IDProvider getIDProvider(Element element) {\r
776                 List<IDProvider> idProviders = new ArrayList<IDProvider>(2);\r
777                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
778                         if (e.getValue() instanceof IDProvider) {\r
779                                 IDProvider ref = (IDProvider)e.getValue();\r
780                                 org.simantics.xml.sax.configuration.Element element2 = ref.getElement();\r
781                                 if (element2 != null) {\r
782                                         if (element.getName().equals(element2.getName()))\r
783                                                 idProviders.add(ref);\r
784                                 }\r
785                                 \r
786                         }\r
787                 }\r
788                 if (idProviders.size() == 0)\r
789                         return null;\r
790                 if (idProviders.size() > 1)\r
791                         throw new RuntimeException("Element " + element.getName() + " contains " + idProviders.size() + " id provider rules, only one is allowed.");\r
792                 return idProviders.get(0);\r
793         }\r
794         \r
795         protected IDProvider getIDProvider(ComplexType complexType) {\r
796                 List<IDProvider> idProviders = new ArrayList<IDProvider>(2);\r
797                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
798                         if (e.getValue() instanceof IDProvider) {\r
799                                 IDProvider ref = (IDProvider)e.getValue();\r
800                                 org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType();\r
801                                 if (complexType2 != null) {\r
802                                         if (complexType.getName().equals(complexType2.getName()))\r
803                                                 idProviders.add(ref);\r
804                                 }\r
805 \r
806                         }\r
807                 }\r
808                 if (idProviders.size() == 0)\r
809                         return null;\r
810                 if (idProviders.size() > 1)\r
811                         throw new RuntimeException("Element " + complexType.getName() + " contains " + idProviders.size() + " id provider rules, only one is allowed.");\r
812                 return idProviders.get(0);\r
813         }\r
814         \r
815         protected List<IDReference> getIDReferences(Element element) {\r
816                 List<IDReference> idReferences = new ArrayList<IDReference>(2);\r
817                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
818                         if (e.getValue() instanceof IDReference) {\r
819                                 IDReference ref = (IDReference)e.getValue();\r
820                                 org.simantics.xml.sax.configuration.Element element2 = ref.getElement();\r
821                                 if (element2 != null) {\r
822                                         if (element.getName().equals(element2.getName()))\r
823                                                 idReferences.add(ref);\r
824                                 }\r
825                         }\r
826                 }\r
827                 return idReferences;\r
828         }\r
829         \r
830         protected List<IDReference> getIDReferences(ComplexType complexType) {\r
831                 List<IDReference> idReferences = new ArrayList<IDReference>(2);\r
832                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
833                         if (e.getValue() instanceof IDReference) {\r
834                                 IDReference ref = (IDReference)e.getValue();\r
835                                 org.simantics.xml.sax.configuration.ComplexType complexType2 = ref.getComplexType();\r
836                                 if (complexType2 != null) {\r
837                                         if (complexType.getName().equals(complexType2.getName()))\r
838                                                 idReferences.add(ref);\r
839                                 }\r
840                         }\r
841                 }\r
842                 return idReferences;\r
843         }\r
844         \r
845         public UnrecognizedChildElement getUnknown(ComplexType complexType) {\r
846                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
847                         if (e.getValue() instanceof UnrecognizedChildElement) {\r
848                                 UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue();\r
849                                 org.simantics.xml.sax.configuration.ComplexType complexType2 = rule.getComplexType();\r
850                                 if (complexType2 != null) {\r
851                                         if (complexType.getName().equals(complexType2.getName()))\r
852                                                 return rule;\r
853                                 }\r
854                         }\r
855                 }\r
856                 return null;\r
857         }\r
858         \r
859         public UnrecognizedChildElement getUnknown(Element element) {\r
860                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
861                         if (e.getValue() instanceof UnrecognizedChildElement) {\r
862                                 UnrecognizedChildElement rule = (UnrecognizedChildElement)e.getValue();\r
863                                 org.simantics.xml.sax.configuration.Element element2 = rule.getElement();\r
864                                 if (element2 != null) {\r
865                                         if (element.getName().equals(element2.getName()))\r
866                                                 return rule;\r
867                                 }\r
868                         }\r
869                 }\r
870                 return null;\r
871         }\r
872         \r
873         public Rename getRename(Attribute element) {\r
874                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
875                         if (e.getValue() instanceof Rename) {\r
876                                 Rename rule = (Rename)e.getValue();\r
877                                 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);\r
878                                 if (!(ref instanceof org.simantics.xml.sax.configuration.Attribute))\r
879                                         continue;\r
880                                 org.simantics.xml.sax.configuration.Attribute element2 = (org.simantics.xml.sax.configuration.Attribute)ref;\r
881                                 if (element2.getName().equals(element.getName())) {\r
882                                         return rule;\r
883                                 }\r
884                         }\r
885                 }\r
886                 return null;\r
887         }\r
888         \r
889         public Rename getRename(ComplexType element) {\r
890                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
891                         if (e.getValue() instanceof Rename) {\r
892                                 Rename rule = (Rename)e.getValue();\r
893                                 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);\r
894                                 if (!(ref instanceof org.simantics.xml.sax.configuration.ComplexType))\r
895                                         continue;\r
896                                 org.simantics.xml.sax.configuration.ComplexType element2 = (org.simantics.xml.sax.configuration.ComplexType)ref;\r
897                                 if (element2.getName().equals(element.getName())) {\r
898                                         return rule;\r
899                                 }\r
900                         }\r
901                 }\r
902                 return null;\r
903         }\r
904         \r
905         public Rename getRename(Element element) {\r
906                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
907                         if (e.getValue() instanceof Rename) {\r
908                                 Rename rule = (Rename)e.getValue();\r
909                                 Object ref = rule.getElementOrComplexTypeOrAttribute().get(0);\r
910                                 if (!(ref instanceof org.simantics.xml.sax.configuration.Element))\r
911                                         continue;\r
912                                 org.simantics.xml.sax.configuration.Element element2 = (org.simantics.xml.sax.configuration.Element)ref;\r
913                                 if (element2.getName().equals(element.getName())) {\r
914                                         return rule;\r
915                                 }\r
916                         }\r
917                 }\r
918                 return null;\r
919         }\r
920          \r
921         \r
922         public boolean useOriginalList(SchemaObject parent, SchemaElement indicator, SchemaElement element,  boolean reference, String ref, QName refType) {\r
923                 if (parent.getName() == null)\r
924                         parent = parent.getParent();\r
925                 if (parent.getName().contains("PipingNetworkSegment"))\r
926                         System.out.println();\r
927                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
928                         if (e.getValue() instanceof OrderedChild) {\r
929                                 OrderedChild oc = (OrderedChild)e.getValue();\r
930                                 org.simantics.xml.sax.configuration.Element element2 = oc.getElement();\r
931                                 org.simantics.xml.sax.configuration.ComplexType complexType = oc.getComplexType();\r
932                                 org.simantics.xml.sax.configuration.Element child = oc.getChild();\r
933                                 if (!oc.getType().equals("original"))\r
934                                         continue;\r
935                                 boolean match = false;\r
936                                 if (element2 != null) {\r
937                                         if (parent.getType() == ObjectType.ELEMENT && parent.getName().equals(element2.getName())) {\r
938                                                 match = true;\r
939                                         }\r
940                                 } else if (complexType != null) {\r
941                                         if (parent.getType() == ObjectType.COMPLEX_TYPE && parent.getName() != null && parent.getName().equals(complexType.getName())) {\r
942                                                 match = true;\r
943                                         }\r
944                                         \r
945                                 }\r
946                                 if (match) {\r
947                                         if (child != null) {\r
948                                                 if (matchChild(child, ref, refType)) {\r
949                                                         if (oc.getValue().equals("disable"))\r
950                                                                 return false;\r
951                                                         else return true;\r
952                                                 }\r
953                                         } else { \r
954                                                 if (oc.getValue().equals("disable"))\r
955                                                         return false;\r
956                                                 return true;\r
957                                         }\r
958                                         \r
959                                 }\r
960                         }\r
961                 }\r
962                 return indicator.order();\r
963         }\r
964         \r
965         public boolean useElementList(SchemaObject parent, SchemaElement indicator, SchemaElement element,  boolean reference, String refName, QName refType) {\r
966                 if (parent.getName() == null)\r
967                         parent = parent.getParent();\r
968                 if (parent.getName() == "PipingNetworkSegment")\r
969                         System.out.println();\r
970                 for (JAXBElement<?> e : configuration.getConversionRule()) {\r
971                         if (e.getValue() instanceof OrderedChild) {\r
972                                 OrderedChild oc = (OrderedChild)e.getValue();\r
973                                 org.simantics.xml.sax.configuration.Element element2 = oc.getElement();\r
974                                 org.simantics.xml.sax.configuration.ComplexType complexType = oc.getComplexType();\r
975                                 org.simantics.xml.sax.configuration.Element child = oc.getChild();\r
976                                 if (!oc.getType().equals("child"))\r
977                                         continue;\r
978                                 boolean match = false;\r
979                                 if (element2 != null) {\r
980                                         if (parent.getType() == ObjectType.ELEMENT && parent.getName().equals(element2.getName())) {\r
981                                                 match = true;\r
982                                         }\r
983                                 } else if (complexType != null) {\r
984                                         if (parent.getType() == ObjectType.COMPLEX_TYPE && parent.getName() != null && parent.getName().equals(complexType.getName())) {\r
985                                                 match = true;\r
986                                         }\r
987                                         \r
988                                 }\r
989                                 if (match) {\r
990                                         if (child != null) {\r
991                                                 if (matchChild(child, refName, refType)) {\r
992                                                         if (oc.getValue().equals("disable"))\r
993                                                                 return false;\r
994                                                         else return true;\r
995                                                 }\r
996                                         } else {\r
997                                                 if (oc.getValue().equals("disable"))\r
998                                                         return false;\r
999                                                 return true;\r
1000                                         }\r
1001                                         \r
1002                                 }\r
1003                         }\r
1004                 }\r
1005                 return element.many() && element.order();\r
1006         }\r
1007         \r
1008         private boolean matchChild(org.simantics.xml.sax.configuration.Element child, String refName, QName refType) {\r
1009                 if (refType != null && refType.getLocalPart().equals(child.getName()))\r
1010                         return true;\r
1011                 if (refName != null && refName.equals(child.getName()))\r
1012                         return true;\r
1013                 return false;\r
1014         }\r
1015         \r
1016         public static class TypeEntry {\r
1017                 String l0Type;\r
1018                 String binding;\r
1019                 String javaType;\r
1020                 String defaultValue;\r
1021                 boolean id;\r
1022                 String getterPrefix;\r
1023                 String getterPostfix;\r
1024                 String stringPrefix;\r
1025                 String stringPostfix;\r
1026                 public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix) {\r
1027                         super();\r
1028                         this.l0Type = l0Type;\r
1029                         this.binding = binding;\r
1030                         this.javaType = javaType;\r
1031                         this.defaultValue = defaultValue;\r
1032                         this.id = false;\r
1033                         this.getterPrefix = getterPrefix;\r
1034                         this.getterPostfix = getterPostfix;\r
1035                         this.stringPrefix = stringPrefix;\r
1036                         this.stringPostfix = stringPostfix;\r
1037                 }\r
1038                 \r
1039                 public TypeEntry(String l0Type, String binding, String javaType, String defaultValue, String getterPrefix, String getterPostfix, String stringPrefix, String stringPostfix, boolean id) {\r
1040                         super();\r
1041                         this.l0Type = l0Type;\r
1042                         this.binding = binding;\r
1043                         this.javaType = javaType;\r
1044                         this.defaultValue = defaultValue;\r
1045                         this.id = id;\r
1046                         this.getterPrefix = getterPrefix;\r
1047                         this.getterPostfix = getterPostfix;\r
1048                         this.stringPrefix = stringPrefix;\r
1049                         this.stringPostfix = stringPostfix;\r
1050                 }\r
1051                 \r
1052                 public String getValueGetterMethod(String name) {\r
1053                         return getterPrefix + name + ".getValue()"+getterPostfix;\r
1054                 }\r
1055                 public String getValueGetter(String name) {\r
1056                         return getterPrefix + name+getterPostfix;\r
1057                 }\r
1058                 public String getValueGetter()\r
1059                 {\r
1060                         return getValueGetter("value");\r
1061                 }\r
1062                 \r
1063                 public String getToString(String name) {\r
1064                         return stringPrefix +"("+javaType+")"+name+stringPostfix;\r
1065                 }\r
1066                 \r
1067                 public String getElementToString(String name) {\r
1068                         return stringPrefix + name+stringPostfix;\r
1069                 }\r
1070                 \r
1071         }\r
1072 \r
1073 }\r