| 1 |
package com.thaiopensource.relaxng.output.xsd.basic; |
| 2 |
|
| 3 |
import java.util.List; |
| 4 |
import java.util.Vector; |
| 5 |
|
| 6 |
public class SchemaTransformer implements SchemaVisitor, ParticleVisitor<Particle>, |
| 7 |
ComplexTypeVisitor<ComplexType>, AttributeUseVisitor<AttributeUse>, SimpleTypeVisitor<SimpleType> { |
| 8 |
private final Schema schema; |
| 9 |
|
| 10 |
public SchemaTransformer(Schema schema) { |
| 11 |
this.schema = schema; |
| 12 |
} |
| 13 |
|
| 14 |
public Schema getSchema() { |
| 15 |
return schema; |
| 16 |
} |
| 17 |
|
| 18 |
public void transform() { |
| 19 |
schema.accept(this); |
| 20 |
} |
| 21 |
|
| 22 |
public void visitGroup(GroupDefinition def) { |
| 23 |
def.setParticle(def.getParticle().accept(this)); |
| 24 |
} |
| 25 |
|
| 26 |
public void visitAttributeGroup(AttributeGroupDefinition def) { |
| 27 |
def.setAttributeUses(def.getAttributeUses().accept(this)); |
| 28 |
} |
| 29 |
|
| 30 |
public void visitSimpleType(SimpleTypeDefinition def) { |
| 31 |
def.setSimpleType(def.getSimpleType().accept(this)); |
| 32 |
} |
| 33 |
|
| 34 |
public void visitRoot(RootDeclaration decl) { |
| 35 |
decl.setParticle(decl.getParticle().accept(this)); |
| 36 |
} |
| 37 |
|
| 38 |
public void visitInclude(Include include) { |
| 39 |
include.getIncludedSchema().accept(this); |
| 40 |
} |
| 41 |
|
| 42 |
public void visitComment(Comment comment) { |
| 43 |
} |
| 44 |
|
| 45 |
public Particle visitRepeat(ParticleRepeat p) { |
| 46 |
Particle child = p.getChild().accept(this); |
| 47 |
if (child == p.getChild()) |
| 48 |
return p; |
| 49 |
return new ParticleRepeat(p.getLocation(), p.getAnnotation(), child, p.getOccurs()); |
| 50 |
} |
| 51 |
|
| 52 |
public Particle visitGroupRef(GroupRef p) { |
| 53 |
return p; |
| 54 |
} |
| 55 |
|
| 56 |
public Particle visitElement(Element p) { |
| 57 |
ComplexType ct = p.getComplexType().accept(this); |
| 58 |
if (ct == p.getComplexType()) |
| 59 |
return p; |
| 60 |
return new Element(p.getLocation(), p.getAnnotation(), p.getName(), ct); |
| 61 |
} |
| 62 |
|
| 63 |
public Particle visitWildcardElement(WildcardElement p) { |
| 64 |
return p; |
| 65 |
} |
| 66 |
|
| 67 |
public Particle visitSequence(ParticleSequence p) { |
| 68 |
List<Particle> children = transformParticleList(p.getChildren()); |
| 69 |
if (children == p.getChildren()) |
| 70 |
return p; |
| 71 |
if (children.size() == 1) |
| 72 |
return children.get(0); |
| 73 |
if (children.size() == 0) |
| 74 |
return null; |
| 75 |
return new ParticleSequence(p.getLocation(), p.getAnnotation(), children); |
| 76 |
} |
| 77 |
|
| 78 |
public Particle visitChoice(ParticleChoice p) { |
| 79 |
List<Particle> children = transformParticleList(p.getChildren()); |
| 80 |
if (children == p.getChildren()) |
| 81 |
return p; |
| 82 |
return new ParticleChoice(p.getLocation(), p.getAnnotation(), children); |
| 83 |
} |
| 84 |
|
| 85 |
public Particle visitAll(ParticleAll p) { |
| 86 |
List<Particle> children = transformParticleList(p.getChildren()); |
| 87 |
if (children == p.getChildren()) |
| 88 |
return p; |
| 89 |
return new ParticleAll(p.getLocation(), p.getAnnotation(), children); |
| 90 |
} |
| 91 |
|
| 92 |
public ComplexType visitComplexContent(ComplexTypeComplexContent t) { |
| 93 |
Particle particle = t.getParticle(); |
| 94 |
AttributeUse attributeUses = t.getAttributeUses().accept(this); |
| 95 |
if (particle != null) |
| 96 |
particle = particle.accept(this); |
| 97 |
if (particle == t.getParticle() && attributeUses == t.getAttributeUses()) |
| 98 |
return t; |
| 99 |
return new ComplexTypeComplexContent(attributeUses, particle, t.isMixed()); |
| 100 |
} |
| 101 |
|
| 102 |
public ComplexType visitSimpleContent(ComplexTypeSimpleContent t) { |
| 103 |
SimpleType simpleType = t.getSimpleType().accept(this); |
| 104 |
AttributeUse attributeUses = t.getAttributeUses().accept(this); |
| 105 |
if (simpleType == t.getSimpleType() && attributeUses == t.getAttributeUses()) |
| 106 |
return t; |
| 107 |
return new ComplexTypeSimpleContent(attributeUses, simpleType); |
| 108 |
} |
| 109 |
|
| 110 |
public ComplexType visitNotAllowedContent(ComplexTypeNotAllowedContent t) { |
| 111 |
return t; |
| 112 |
} |
| 113 |
|
| 114 |
public AttributeUse visitAttribute(Attribute a) { |
| 115 |
SimpleType type = a.getType(); |
| 116 |
if (type != null) { |
| 117 |
type = type.accept(this); |
| 118 |
if (type == null || type != a.getType()) |
| 119 |
return new Attribute(a.getLocation(), a.getAnnotation(), a.getName(), type); |
| 120 |
} |
| 121 |
return a; |
| 122 |
} |
| 123 |
|
| 124 |
public AttributeUse visitWildcardAttribute(WildcardAttribute a) { |
| 125 |
return a; |
| 126 |
} |
| 127 |
|
| 128 |
public AttributeUse visitAttributeGroupRef(AttributeGroupRef a) { |
| 129 |
return a; |
| 130 |
} |
| 131 |
|
| 132 |
public AttributeUse visitOptionalAttribute(OptionalAttribute a) { |
| 133 |
Attribute attribute = (Attribute)a.getAttribute().accept(this); |
| 134 |
if (attribute == a.getAttribute()) |
| 135 |
return a; |
| 136 |
return new OptionalAttribute(a.getLocation(), a.getAnnotation(), attribute, a.getDefaultValue()); |
| 137 |
} |
| 138 |
|
| 139 |
public AttributeUse visitAttributeGroup(AttributeGroup a) { |
| 140 |
List<AttributeUse> children = transformAttributeUseList(a.getChildren()); |
| 141 |
if (children == a.getChildren()) |
| 142 |
return a; |
| 143 |
return new AttributeGroup(a.getLocation(), a.getAnnotation(), children); |
| 144 |
} |
| 145 |
|
| 146 |
public AttributeUse visitAttributeUseChoice(AttributeUseChoice a) { |
| 147 |
List<AttributeUse> children = transformAttributeUseList(a.getChildren()); |
| 148 |
if (children == a.getChildren()) |
| 149 |
return a; |
| 150 |
return new AttributeUseChoice(a.getLocation(), a.getAnnotation(), children); |
| 151 |
} |
| 152 |
|
| 153 |
public SimpleType visitRestriction(SimpleTypeRestriction t) { |
| 154 |
return t; |
| 155 |
} |
| 156 |
|
| 157 |
public SimpleType visitUnion(SimpleTypeUnion t) { |
| 158 |
List<SimpleType> children = transformSimpleTypeList(t.getChildren()); |
| 159 |
if (children == t.getChildren()) |
| 160 |
return t; |
| 161 |
return new SimpleTypeUnion(t.getLocation(), t.getAnnotation(), children); |
| 162 |
} |
| 163 |
|
| 164 |
public SimpleType visitList(SimpleTypeList t) { |
| 165 |
SimpleType itemType = t.getItemType().accept(this); |
| 166 |
if (itemType == t.getItemType()) |
| 167 |
return t; |
| 168 |
return new SimpleTypeList(t.getLocation(), t.getAnnotation(), itemType, t.getOccurs()); |
| 169 |
} |
| 170 |
|
| 171 |
public SimpleType visitRef(SimpleTypeRef t) { |
| 172 |
return t; |
| 173 |
} |
| 174 |
|
| 175 |
public List<AttributeUse> transformAttributeUseList(List<AttributeUse> list) { |
| 176 |
List<AttributeUse> transformed = null; |
| 177 |
for (int i = 0, len = list.size(); i < len; i++) { |
| 178 |
AttributeUse use = list.get(i).accept(this); |
| 179 |
if (transformed != null) |
| 180 |
transformed.add(use); |
| 181 |
else if (use != list.get(i)) { |
| 182 |
transformed = new Vector<AttributeUse>(); |
| 183 |
for (int j = 0; j < i; j++) |
| 184 |
transformed.add(list.get(j)); |
| 185 |
if (!use.equals(AttributeGroup.EMPTY)) |
| 186 |
transformed.add(use); |
| 187 |
} |
| 188 |
} |
| 189 |
if (transformed == null) |
| 190 |
return list; |
| 191 |
return transformed; |
| 192 |
} |
| 193 |
|
| 194 |
public List<Particle> transformParticleList(List<Particle> list) { |
| 195 |
List<Particle> transformed = null; |
| 196 |
for (int i = 0, len = list.size(); i < len; i++) { |
| 197 |
Particle p = list.get(i).accept(this); |
| 198 |
if (transformed != null) { |
| 199 |
if (p != null) |
| 200 |
transformed.add(p); |
| 201 |
} |
| 202 |
else if (p != list.get(i)) { |
| 203 |
transformed = new Vector<Particle>(); |
| 204 |
for (int j = 0; j < i; j++) |
| 205 |
transformed.add(list.get(j)); |
| 206 |
if (p != null) |
| 207 |
transformed.add(p); |
| 208 |
} |
| 209 |
} |
| 210 |
if (transformed == null) |
| 211 |
return list; |
| 212 |
return transformed; |
| 213 |
} |
| 214 |
|
| 215 |
public List<SimpleType> transformSimpleTypeList(List<SimpleType> list) { |
| 216 |
List<SimpleType> transformed = null; |
| 217 |
for (int i = 0, len = list.size(); i < len; i++) { |
| 218 |
SimpleType st = list.get(i).accept(this); |
| 219 |
if (transformed != null) |
| 220 |
transformed.add(st); |
| 221 |
else if (st != list.get(i)) { |
| 222 |
transformed = new Vector<SimpleType>(); |
| 223 |
for (int j = 0; j < i; j++) |
| 224 |
transformed.add(list.get(j)); |
| 225 |
transformed.add(st); |
| 226 |
} |
| 227 |
} |
| 228 |
if (transformed == null) |
| 229 |
return list; |
| 230 |
return transformed; |
| 231 |
} |
| 232 |
} |