package org.simantics.xml.sax; import org.simantics.xml.sax.configuration.Rename; import org.w3._2001.xmlschema.AttributeGroup; import org.w3._2001.xmlschema.AttributeGroupRef; import org.w3._2001.xmlschema.ComplexType; import org.w3._2001.xmlschema.Element; import org.w3._2001.xmlschema.NamedAttributeGroup; import org.w3._2001.xmlschema.NamedGroup; import org.w3._2001.xmlschema.OpenAttrs; import org.w3._2001.xmlschema.SimpleType; public class SchemaObject { enum ObjectType{ELEMENT,COMPLEX_TYPE,SIMPLE_TYPE,ATTRIBUTE_GROUP,MODEL_GROUP}; private SchemaConversionBase sc; private String name; private SchemaObject parent; private ObjectType type; private OpenAttrs obj; private Rename rename; public SchemaObject(SchemaConversionBase sc, Element element) { this(sc,null,element); } public void setRename(Rename rename) { this.rename = rename; _init(); } public SchemaObject(SchemaConversionBase sc,ComplexType complexType) { this(sc,null, complexType); } public SchemaObject(SchemaConversionBase sc,SimpleType simpleType) { this(sc,null, simpleType); } public SchemaObject(SchemaConversionBase sc,NamedGroup namedGroup) { this(sc,null, namedGroup); } public SchemaObject(SchemaConversionBase sc,SchemaObject parent, Element element) { this.parent = parent; this.obj = element; this.type = ObjectType.ELEMENT; this.sc = sc; _init(); } public SchemaObject(SchemaConversionBase sc,SchemaObject parent, ComplexType complexType) { this.parent = parent; this.obj = complexType; this.type = ObjectType.COMPLEX_TYPE; this.sc = sc; _init(); } public SchemaObject(SchemaConversionBase sc,SchemaObject parent, AttributeGroup attributeGroup) { this.parent = parent; this.obj = attributeGroup; this.type = ObjectType.ATTRIBUTE_GROUP; this.sc = sc; _init(); } public SchemaObject(SchemaConversionBase sc,SchemaObject parent, NamedGroup namedGroup) { this.parent = parent; this.obj = namedGroup; this.type = ObjectType.MODEL_GROUP; this.sc = sc; _init(); } public SchemaObject(SchemaConversionBase sc,SchemaObject parent, SimpleType simpleType) { this.parent = parent; this.obj = simpleType; this.type = ObjectType.SIMPLE_TYPE; this.sc = sc; _init(); } private void _init() { name = _getName(); if (name != null) name = name.replaceAll("\\.", "_"); } public Element getElement() { if (type != ObjectType.ELEMENT) return null; return (Element)obj; } public ComplexType getComplexType() { if (type != ObjectType.COMPLEX_TYPE) return null; return (ComplexType)obj; } public SimpleType getSimpleType() { if (type != ObjectType.SIMPLE_TYPE) return null; return (SimpleType)obj; } public AttributeGroup getAttributeGroup() { if (type != ObjectType.ATTRIBUTE_GROUP) return null; return (AttributeGroup)obj; } public NamedGroup getModelGroup() { if (type != ObjectType.MODEL_GROUP) return null; return (NamedGroup)obj; } public SchemaObject getParent() { return parent; } public OpenAttrs getObj() { return obj; } public String getName() { return name; } public String getLibShortName() { return sc.converter.shortName; } private String _getName() { switch (type) { case ATTRIBUTE_GROUP: if (obj instanceof NamedAttributeGroup) return ((NamedAttributeGroup)obj).getName(); else return ((AttributeGroupRef)obj).getRef().getLocalPart(); case COMPLEX_TYPE: if (rename != null) return rename.getName(); return ((ComplexType)obj).getName(); case ELEMENT: if (rename != null) return rename.getName(); return ((Element)obj).getName(); case SIMPLE_TYPE: return ((SimpleType)obj).getName(); } throw new RuntimeException("Unknown object type " + type); } public ObjectType getType() { return type; } public SchemaConversionBase getSc() { return sc; } }