| 1 |
package com.thaiopensource.relaxng.output.xsd; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.edit.AbstractPatternVisitor; |
| 4 |
import com.thaiopensource.relaxng.edit.Annotated; |
| 5 |
import com.thaiopensource.relaxng.edit.AnnotationChild; |
| 6 |
import com.thaiopensource.relaxng.edit.AttributePattern; |
| 7 |
import com.thaiopensource.relaxng.edit.ChoicePattern; |
| 8 |
import com.thaiopensource.relaxng.edit.Comment; |
| 9 |
import com.thaiopensource.relaxng.edit.ComponentVisitor; |
| 10 |
import com.thaiopensource.relaxng.edit.CompositePattern; |
| 11 |
import com.thaiopensource.relaxng.edit.DataPattern; |
| 12 |
import com.thaiopensource.relaxng.edit.DefineComponent; |
| 13 |
import com.thaiopensource.relaxng.edit.DivComponent; |
| 14 |
import com.thaiopensource.relaxng.edit.ElementAnnotation; |
| 15 |
import com.thaiopensource.relaxng.edit.ElementPattern; |
| 16 |
import com.thaiopensource.relaxng.edit.EmptyPattern; |
| 17 |
import com.thaiopensource.relaxng.edit.GrammarPattern; |
| 18 |
import com.thaiopensource.relaxng.edit.GroupPattern; |
| 19 |
import com.thaiopensource.relaxng.edit.IncludeComponent; |
| 20 |
import com.thaiopensource.relaxng.edit.InterleavePattern; |
| 21 |
import com.thaiopensource.relaxng.edit.ListPattern; |
| 22 |
import com.thaiopensource.relaxng.edit.MixedPattern; |
| 23 |
import com.thaiopensource.relaxng.edit.NameNameClass; |
| 24 |
import com.thaiopensource.relaxng.edit.OneOrMorePattern; |
| 25 |
import com.thaiopensource.relaxng.edit.OptionalPattern; |
| 26 |
import com.thaiopensource.relaxng.edit.Param; |
| 27 |
import com.thaiopensource.relaxng.edit.Pattern; |
| 28 |
import com.thaiopensource.relaxng.edit.PatternVisitor; |
| 29 |
import com.thaiopensource.relaxng.edit.RefPattern; |
| 30 |
import com.thaiopensource.relaxng.edit.SourceLocation; |
| 31 |
import com.thaiopensource.relaxng.edit.TextAnnotation; |
| 32 |
import com.thaiopensource.relaxng.edit.TextPattern; |
| 33 |
import com.thaiopensource.relaxng.edit.UnaryPattern; |
| 34 |
import com.thaiopensource.relaxng.edit.ValuePattern; |
| 35 |
import com.thaiopensource.util.VoidValue; |
| 36 |
import com.thaiopensource.relaxng.edit.ZeroOrMorePattern; |
| 37 |
import com.thaiopensource.relaxng.output.common.ErrorReporter; |
| 38 |
import com.thaiopensource.relaxng.output.common.NameClassSplitter; |
| 39 |
import com.thaiopensource.relaxng.output.xsd.basic.Annotation; |
| 40 |
import com.thaiopensource.relaxng.output.xsd.basic.Attribute; |
| 41 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeGroup; |
| 42 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeGroupRef; |
| 43 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeUse; |
| 44 |
import com.thaiopensource.relaxng.output.xsd.basic.AttributeUseChoice; |
| 45 |
import com.thaiopensource.relaxng.output.xsd.basic.ComplexType; |
| 46 |
import com.thaiopensource.relaxng.output.xsd.basic.ComplexTypeComplexContent; |
| 47 |
import com.thaiopensource.relaxng.output.xsd.basic.ComplexTypeNotAllowedContent; |
| 48 |
import com.thaiopensource.relaxng.output.xsd.basic.ComplexTypeSimpleContent; |
| 49 |
import com.thaiopensource.relaxng.output.xsd.basic.Element; |
| 50 |
import com.thaiopensource.relaxng.output.xsd.basic.Facet; |
| 51 |
import com.thaiopensource.relaxng.output.xsd.basic.GroupRef; |
| 52 |
import com.thaiopensource.relaxng.output.xsd.basic.Occurs; |
| 53 |
import com.thaiopensource.relaxng.output.xsd.basic.OptionalAttribute; |
| 54 |
import com.thaiopensource.relaxng.output.xsd.basic.Particle; |
| 55 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleAll; |
| 56 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleChoice; |
| 57 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleRepeat; |
| 58 |
import com.thaiopensource.relaxng.output.xsd.basic.ParticleSequence; |
| 59 |
import com.thaiopensource.relaxng.output.xsd.basic.Schema; |
| 60 |
import com.thaiopensource.relaxng.output.xsd.basic.SimpleType; |
| 61 |
import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeList; |
| 62 |
import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeRef; |
| 63 |
import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeRestriction; |
| 64 |
import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeUnion; |
| 65 |
import com.thaiopensource.relaxng.output.xsd.basic.Wildcard; |
| 66 |
import com.thaiopensource.relaxng.output.xsd.basic.WildcardAttribute; |
| 67 |
import com.thaiopensource.relaxng.output.xsd.basic.WildcardElement; |
| 68 |
import com.thaiopensource.xml.util.Name; |
| 69 |
import com.thaiopensource.xml.util.WellKnownNamespaces; |
| 70 |
|
| 71 |
import java.util.Collections; |
| 72 |
import java.util.HashSet; |
| 73 |
import java.util.Iterator; |
| 74 |
import java.util.List; |
| 75 |
import java.util.Map; |
| 76 |
import java.util.Set; |
| 77 |
import java.util.Vector; |
| 78 |
|
| 79 |
public class BasicBuilder { |
| 80 |
private final PatternVisitor<SimpleType> simpleTypeBuilder = new SimpleTypeBuilder(); |
| 81 |
private final PatternVisitor<AttributeUse> attributeUseBuilder = new AttributeUseBuilder(); |
| 82 |
private final PatternVisitor<AttributeUse> optionalAttributeUseBuilder = new OptionalAttributeUseBuilder(); |
| 83 |
private final PatternVisitor<Particle> particleBuilder = new ParticleBuilder(); |
| 84 |
private final PatternVisitor<Occurs> occursCalculator = new OccursCalculator(); |
| 85 |
private final ComponentVisitor<VoidValue> schemaBuilder; |
| 86 |
private final ErrorReporter er; |
| 87 |
private final String inheritedNamespace; |
| 88 |
private final Schema schema; |
| 89 |
private final SchemaInfo si; |
| 90 |
private final Guide guide; |
| 91 |
|
| 92 |
/** |
| 93 |
* Preconditions for calling visit methods in this class are that the child type |
| 94 |
* - contains DATA |
| 95 |
* - does not contains ELEMENT |
| 96 |
* - does not contain TEXT |
| 97 |
*/ |
| 98 |
private class SimpleTypeBuilder extends AbstractPatternVisitor<SimpleType> { |
| 99 |
public SimpleType visitData(DataPattern p) { |
| 100 |
String library = p.getDatatypeLibrary(); |
| 101 |
String type = p.getType(); |
| 102 |
List<Facet> facets = new Vector<Facet>(); |
| 103 |
SourceLocation location = p.getSourceLocation(); |
| 104 |
if (!library.equals("") && !library.equals(WellKnownNamespaces.XML_SCHEMA_DATATYPES)) { |
| 105 |
type = "string"; |
| 106 |
er.warning("unsupported_datatype_library", library, location); |
| 107 |
} |
| 108 |
else { |
| 109 |
if (type.equals("NOTATION")) |
| 110 |
type = "QName"; |
| 111 |
for (Param param : p.getParams()) |
| 112 |
facets.add(new Facet(param.getSourceLocation(), |
| 113 |
makeAnnotation(param), |
| 114 |
param.getName(), |
| 115 |
param.getValue())); |
| 116 |
} |
| 117 |
return new SimpleTypeRestriction(location, makeAnnotation(p), type, facets); |
| 118 |
} |
| 119 |
|
| 120 |
public SimpleType visitValue(ValuePattern p) { |
| 121 |
String library = p.getDatatypeLibrary(); |
| 122 |
String type = p.getType(); |
| 123 |
List<Facet> facets = new Vector<Facet>(); |
| 124 |
SourceLocation location = p.getSourceLocation(); |
| 125 |
if (!library.equals("") && !library.equals(WellKnownNamespaces.XML_SCHEMA_DATATYPES)) { |
| 126 |
type = "string"; |
| 127 |
er.warning("unsupported_datatype_library", library, location); |
| 128 |
} |
| 129 |
else { |
| 130 |
if (type.equals("NOTATION")) |
| 131 |
type = "QName"; |
| 132 |
String prefix = null; |
| 133 |
String namespace = null; |
| 134 |
Iterator<Map.Entry<String,String>> bindings = p.getPrefixMap().entrySet().iterator(); |
| 135 |
if (bindings.hasNext()) { |
| 136 |
Map.Entry<String,String> binding = bindings.next(); |
| 137 |
prefix = binding.getKey(); |
| 138 |
namespace = resolveNamespace(binding.getValue()); |
| 139 |
} |
| 140 |
facets.add(new Facet(location, makeAnnotation(p), "enumeration", p.getValue(), prefix, namespace)); |
| 141 |
} |
| 142 |
return new SimpleTypeRestriction(location, null, type, facets); |
| 143 |
} |
| 144 |
|
| 145 |
public SimpleType visitComposite(CompositePattern p) { |
| 146 |
List<SimpleType> result = new Vector<SimpleType>(); |
| 147 |
for (Pattern child : p.getChildren()) { |
| 148 |
if (si.getChildType(child).contains(ChildType.DATA)) |
| 149 |
result.add(child.accept(this)); |
| 150 |
} |
| 151 |
if (result.size() == 1) |
| 152 |
return result.get(0); |
| 153 |
else |
| 154 |
return new SimpleTypeUnion(p.getSourceLocation(), makeAnnotation(p), result); |
| 155 |
} |
| 156 |
|
| 157 |
public SimpleType visitUnary(UnaryPattern p) { |
| 158 |
return p.getChild().accept(this); |
| 159 |
} |
| 160 |
|
| 161 |
public SimpleType visitList(ListPattern p) { |
| 162 |
SourceLocation location = p.getSourceLocation(); |
| 163 |
Pattern child = p.getChild(); |
| 164 |
ChildType childType = si.getChildType(child); |
| 165 |
if (childType.equals(ChildType.EMPTY)) |
| 166 |
return makeEmptySimpleType(location); |
| 167 |
boolean bad = false; |
| 168 |
if (childType.contains(ChildType.ELEMENT)) { |
| 169 |
er.warning("list_contains_element", location); |
| 170 |
bad = true; |
| 171 |
} |
| 172 |
if (childType.contains(ChildType.ATTRIBUTE)) { |
| 173 |
er.warning("list_contains_attribute", location); |
| 174 |
bad = true; |
| 175 |
} |
| 176 |
if (childType.contains(ChildType.TEXT)) { |
| 177 |
er.warning("list_contains_text", location); |
| 178 |
bad = true; |
| 179 |
} |
| 180 |
if (bad) |
| 181 |
return makeStringType(location); |
| 182 |
// the type isn't NOT_ALLOWED, because the list would have type NOT_ALLOWED if it was |
| 183 |
// the type isn't EMPTY (checked above) |
| 184 |
// the type does not contain TEXT, ELEMENT or ATTRIBUTE (checked above) |
| 185 |
// therefore the type must contain DATA |
| 186 |
// so the preconditions for calling accept(this) are met |
| 187 |
return new SimpleTypeList(location, |
| 188 |
makeAnnotation(p), |
| 189 |
child.accept(this), |
| 190 |
child.accept(occursCalculator)); |
| 191 |
} |
| 192 |
|
| 193 |
public SimpleType visitRef(RefPattern p) { |
| 194 |
return new SimpleTypeRef(p.getSourceLocation(), makeAnnotation(p), p.getName()); |
| 195 |
} |
| 196 |
|
| 197 |
public SimpleType visitPattern(Pattern p) { |
| 198 |
// TODO throw an error |
| 199 |
return null; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
class OccursCalculator extends AbstractPatternVisitor<Occurs> { |
| 204 |
public Occurs visitOptional(OptionalPattern p) { |
| 205 |
return new Occurs(0, p.getChild().accept(this).getMax()); |
| 206 |
} |
| 207 |
|
| 208 |
public Occurs visitZeroOrMore(ZeroOrMorePattern p) { |
| 209 |
return new Occurs(0, Occurs.UNBOUNDED); |
| 210 |
} |
| 211 |
|
| 212 |
public Occurs visitOneOrMore(OneOrMorePattern p) { |
| 213 |
return new Occurs(p.getChild().accept(this).getMin(), Occurs.UNBOUNDED); |
| 214 |
} |
| 215 |
|
| 216 |
public Occurs visitData(DataPattern p) { |
| 217 |
return Occurs.EXACTLY_ONE; |
| 218 |
} |
| 219 |
|
| 220 |
public Occurs visitValue(ValuePattern p) { |
| 221 |
return Occurs.EXACTLY_ONE; |
| 222 |
} |
| 223 |
|
| 224 |
public Occurs visitEmpty(EmptyPattern p) { |
| 225 |
return new Occurs(0, 0); |
| 226 |
} |
| 227 |
|
| 228 |
private Occurs sum(CompositePattern p) { |
| 229 |
Occurs occ = new Occurs(0, 0); |
| 230 |
List<Pattern> children = p.getChildren(); |
| 231 |
for (int i = 0, len = children.size(); i < len; i++) |
| 232 |
occ = Occurs.add(occ, children.get(i).accept(this)); |
| 233 |
return occ; |
| 234 |
} |
| 235 |
|
| 236 |
public Occurs visitInterleave(InterleavePattern p) { |
| 237 |
return sum(p); |
| 238 |
} |
| 239 |
|
| 240 |
public Occurs visitGroup(GroupPattern p) { |
| 241 |
return sum(p); |
| 242 |
} |
| 243 |
|
| 244 |
public Occurs visitChoice(ChoicePattern p) { |
| 245 |
List<Pattern> children = p.getChildren(); |
| 246 |
Occurs occ = children.get(0).accept(this); |
| 247 |
for (int i = 1, len = children.size(); i < len; i++) { |
| 248 |
Occurs tem = children.get(i).accept(this); |
| 249 |
occ = new Occurs(Math.min(occ.getMin(), tem.getMin()), |
| 250 |
Math.max(occ.getMax(), tem.getMax())); |
| 251 |
} |
| 252 |
return occ; |
| 253 |
} |
| 254 |
|
| 255 |
public Occurs visitRef(RefPattern p) { |
| 256 |
return si.getBody(p).accept(this); |
| 257 |
} |
| 258 |
|
| 259 |
public Occurs visitPattern(Pattern p) { |
| 260 |
return null; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Precondition for calling visit methods in this class is that the child type |
| 266 |
* contains ELEMENT. |
| 267 |
*/ |
| 268 |
class ParticleBuilder extends AbstractPatternVisitor<Particle> { |
| 269 |
public Particle visitElement(ElementPattern p) { |
| 270 |
ComplexType type; |
| 271 |
Pattern child = p.getChild(); |
| 272 |
ChildType ct = si.getChildType(child); |
| 273 |
AttributeUse attributeUses; |
| 274 |
if (ct.contains(ChildType.ATTRIBUTE)) |
| 275 |
attributeUses = child.accept(attributeUseBuilder); |
| 276 |
else |
| 277 |
attributeUses = AttributeGroup.EMPTY; |
| 278 |
Particle particle = null; |
| 279 |
boolean mixed = false; |
| 280 |
if (ct.contains(ChildType.ELEMENT)) { |
| 281 |
if (ct.contains(ChildType.DATA)) |
| 282 |
mixed = true; // TODO give an error |
| 283 |
particle = child.accept(particleBuilder); |
| 284 |
} |
| 285 |
if (ct.contains(ChildType.TEXT)) |
| 286 |
mixed = true; |
| 287 |
if (particle == null && mixed && attributeUses.equals(AttributeGroup.EMPTY)) |
| 288 |
type = new ComplexTypeSimpleContent(attributeUses, |
| 289 |
makeStringType(p.getSourceLocation())); |
| 290 |
else if (ct.contains(ChildType.DATA) && !mixed && particle == null) { |
| 291 |
SimpleType simpleType = child.accept(simpleTypeBuilder); |
| 292 |
if (ct.contains(ChildType.EMPTY)) |
| 293 |
simpleType = makeUnionWithEmptySimpleType(simpleType, p.getSourceLocation()); |
| 294 |
type = new ComplexTypeSimpleContent(attributeUses, simpleType); |
| 295 |
} |
| 296 |
else if (ct.equals(ChildType.NOT_ALLOWED)) |
| 297 |
type = new ComplexTypeNotAllowedContent(); |
| 298 |
else |
| 299 |
type = new ComplexTypeComplexContent(attributeUses, particle, mixed); |
| 300 |
List<NameNameClass> names = NameClassSplitter.split(p.getNameClass()); |
| 301 |
Wildcard[] wc = splitElementWildcard(WildcardBuilder.createWildcard(p.getNameClass(), inheritedNamespace)); |
| 302 |
Annotation annotation = makeAnnotation(p); |
| 303 |
Annotation elementAnnotation = names.size() + wc.length == 1 ? annotation : null; |
| 304 |
List<Particle> result = new Vector<Particle>(); |
| 305 |
for (NameNameClass name : names) |
| 306 |
result.add(new Element(p.getSourceLocation(), elementAnnotation, makeName(name), type)); |
| 307 |
for (int i = 0; i < wc.length; i++) |
| 308 |
result.add(new WildcardElement(p.getSourceLocation(), elementAnnotation, wc[i])); |
| 309 |
if (result.size() == 1) |
| 310 |
return result.get(0); |
| 311 |
return new ParticleChoice(p.getSourceLocation(), annotation, result); |
| 312 |
} |
| 313 |
|
| 314 |
public Particle visitOneOrMore(OneOrMorePattern p) { |
| 315 |
return new ParticleRepeat(p.getSourceLocation(), |
| 316 |
makeAnnotation(p), |
| 317 |
p.getChild().accept(this), |
| 318 |
Occurs.ONE_OR_MORE); |
| 319 |
} |
| 320 |
|
| 321 |
public Particle visitZeroOrMore(ZeroOrMorePattern p) { |
| 322 |
return new ParticleRepeat(p.getSourceLocation(), |
| 323 |
makeAnnotation(p), |
| 324 |
p.getChild().accept(this), |
| 325 |
Occurs.ZERO_OR_MORE); |
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
public Particle visitOptional(OptionalPattern p) { |
| 330 |
return new ParticleRepeat(p.getSourceLocation(), |
| 331 |
makeAnnotation(p), |
| 332 |
p.getChild().accept(this), |
| 333 |
Occurs.OPTIONAL); |
| 334 |
} |
| 335 |
|
| 336 |
public Particle visitChoice(ChoicePattern p) { |
| 337 |
List<Particle> children = new Vector<Particle>(); |
| 338 |
boolean optional = false; |
| 339 |
for (Pattern pattern : p.getChildren()) { |
| 340 |
ChildType ct = si.getChildType(pattern); |
| 341 |
if (ct.contains(ChildType.ELEMENT)) |
| 342 |
children.add(pattern.accept(this)); |
| 343 |
else if (!ct.equals(ChildType.NOT_ALLOWED)) |
| 344 |
optional = true; |
| 345 |
} |
| 346 |
Annotation annotation = makeAnnotation(p); |
| 347 |
Particle result; |
| 348 |
if (children.size() == 1 && annotation == null) |
| 349 |
result = children.get(0); |
| 350 |
else |
| 351 |
result = new ParticleChoice(p.getSourceLocation(), annotation, children); |
| 352 |
if (optional) |
| 353 |
return new ParticleRepeat(p.getSourceLocation(), null, result, Occurs.OPTIONAL); |
| 354 |
return result; |
| 355 |
} |
| 356 |
|
| 357 |
public Particle visitGroup(GroupPattern p) { |
| 358 |
Annotation annotation = makeAnnotation(p); |
| 359 |
List<Particle> children = buildChildren(p); |
| 360 |
if (children.size() == 1 && annotation == null) |
| 361 |
return children.get(0); |
| 362 |
else |
| 363 |
return new ParticleSequence(p.getSourceLocation(), annotation, children); |
| 364 |
} |
| 365 |
|
| 366 |
public Particle visitInterleave(InterleavePattern p) { |
| 367 |
Annotation annotation = makeAnnotation(p); |
| 368 |
List<Particle> children = buildChildren(p); |
| 369 |
if (children.size() == 1 && annotation == null) |
| 370 |
return children.get(0); |
| 371 |
else |
| 372 |
return new ParticleAll(p.getSourceLocation(), annotation, children); |
| 373 |
} |
| 374 |
|
| 375 |
private List<Particle> buildChildren(CompositePattern p) { |
| 376 |
List<Particle> result = new Vector<Particle>(); |
| 377 |
for (Pattern pattern : p.getChildren()) { |
| 378 |
if (si.getChildType(pattern).contains(ChildType.ELEMENT)) |
| 379 |
result.add(pattern.accept(this)); |
| 380 |
} |
| 381 |
return result; |
| 382 |
} |
| 383 |
|
| 384 |
public Particle visitMixed(MixedPattern p) { |
| 385 |
return p.getChild().accept(this); |
| 386 |
} |
| 387 |
|
| 388 |
public Particle visitRef(RefPattern p) { |
| 389 |
return new GroupRef(p.getSourceLocation(), makeAnnotation(p), p.getName()); |
| 390 |
} |
| 391 |
|
| 392 |
public Particle visitPattern(Pattern p) { |
| 393 |
return null; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
/** |
| 399 |
* Precondition for visitMethods is that the childType contains ATTRIBUTE |
| 400 |
*/ |
| 401 |
class OptionalAttributeUseBuilder extends AbstractPatternVisitor<AttributeUse> { |
| 402 |
public AttributeUse visitAttribute(AttributePattern p) { |
| 403 |
SourceLocation location = p.getSourceLocation(); |
| 404 |
Pattern child = p.getChild(); |
| 405 |
ChildType ct = si.getChildType(child); |
| 406 |
SimpleType value; |
| 407 |
if (ct.contains(ChildType.DATA) && !ct.contains(ChildType.TEXT)) { |
| 408 |
value = child.accept(simpleTypeBuilder); |
| 409 |
if (ct.contains(ChildType.EMPTY)) |
| 410 |
value = makeUnionWithEmptySimpleType(value, location); |
| 411 |
} |
| 412 |
else if (ct.contains(ChildType.EMPTY) && !ct.contains(ChildType.TEXT)) |
| 413 |
value = makeEmptySimpleType(location); |
| 414 |
else |
| 415 |
value = null; |
| 416 |
List<NameNameClass> names = NameClassSplitter.split(p.getNameClass()); |
| 417 |
Wildcard wc = WildcardBuilder.createWildcard(p.getNameClass(), inheritedNamespace); |
| 418 |
List<AttributeUse> choices = new Vector<AttributeUse>(); |
| 419 |
Annotation annotation = makeAnnotation(p); |
| 420 |
boolean singleChoice = names.size() + (wc != null ? 1 : 0) == 1; |
| 421 |
Annotation attributeAnnotation = singleChoice ? annotation : null; |
| 422 |
for (NameNameClass name : names) { |
| 423 |
Attribute att = new Attribute(location, |
| 424 |
attributeAnnotation, |
| 425 |
makeName((name)), |
| 426 |
value); |
| 427 |
if (!singleChoice || isOptional()) |
| 428 |
choices.add(new OptionalAttribute(att.getLocation(), null, att, |
| 429 |
p.getAttributeAnnotation(WellKnownNamespaces.RELAX_NG_COMPATIBILITY_ANNOTATIONS, |
| 430 |
"defaultValue"))); |
| 431 |
else |
| 432 |
choices.add(att); |
| 433 |
} |
| 434 |
|
| 435 |
if (wc != null) { |
| 436 |
if (!allowsAnyString(child)) |
| 437 |
er.warning("wildcard_attribute_value", p.getSourceLocation()); |
| 438 |
if (!isOptional()) |
| 439 |
er.warning("wildcard_attribute_optional", p.getSourceLocation()); |
| 440 |
choices.add(new WildcardAttribute(p.getSourceLocation(), attributeAnnotation, wc)); |
| 441 |
} |
| 442 |
if (choices.size() == 1) |
| 443 |
return choices.get(0); |
| 444 |
return new AttributeGroup(p.getSourceLocation(), annotation, choices); |
| 445 |
} |
| 446 |
|
| 447 |
boolean isOptional() { |
| 448 |
return true; |
| 449 |
} |
| 450 |
|
| 451 |
public AttributeUse visitOneOrMore(OneOrMorePattern p) { |
| 452 |
return p.getChild().accept(this); |
| 453 |
} |
| 454 |
|
| 455 |
public AttributeUse visitMixed(MixedPattern p) { |
| 456 |
return p.getChild().accept(this); |
| 457 |
} |
| 458 |
|
| 459 |
public AttributeUse visitZeroOrMore(ZeroOrMorePattern p) { |
| 460 |
return p.getChild().accept(optionalAttributeUseBuilder); |
| 461 |
} |
| 462 |
|
| 463 |
public AttributeUse visitOptional(OptionalPattern p) { |
| 464 |
return p.getChild().accept(optionalAttributeUseBuilder); |
| 465 |
} |
| 466 |
|
| 467 |
public AttributeUse visitRef(RefPattern p) { |
| 468 |
AttributeUse ref = new AttributeGroupRef(p.getSourceLocation(), makeAnnotation(p), p.getName()); |
| 469 |
if (!isOptional()) |
| 470 |
return ref; |
| 471 |
List<AttributeUse> choices = new Vector<AttributeUse>(); |
| 472 |
choices.add(ref); |
| 473 |
choices.add(AttributeGroup.EMPTY); |
| 474 |
return new AttributeUseChoice(p.getSourceLocation(), null, choices); |
| 475 |
} |
| 476 |
|
| 477 |
public AttributeUse visitComposite(CompositePattern p) { |
| 478 |
List<AttributeUse> uses = new Vector<AttributeUse>(); |
| 479 |
for (Pattern child : p.getChildren()) { |
| 480 |
if (si.getChildType(child).contains(ChildType.ATTRIBUTE)) |
| 481 |
uses.add(child.accept(this)); |
| 482 |
} |
| 483 |
if (uses.size() == 0) |
| 484 |
return AttributeGroup.EMPTY; |
| 485 |
if (uses.size() == 1) |
| 486 |
return uses.get(0); |
| 487 |
if (isOptional()) |
| 488 |
er.warning("optional_attribute_group", p.getSourceLocation()); |
| 489 |
return new AttributeGroup(p.getSourceLocation(), null, uses); |
| 490 |
} |
| 491 |
|
| 492 |
public AttributeUse visitChoice(ChoicePattern p) { |
| 493 |
PatternVisitor<AttributeUse> childVisitor = this; |
| 494 |
for (Pattern child : p.getChildren()) { |
| 495 |
if (!si.getChildType(child).contains(ChildType.ATTRIBUTE)) { |
| 496 |
childVisitor = optionalAttributeUseBuilder; |
| 497 |
break; |
| 498 |
} |
| 499 |
} |
| 500 |
boolean hasChildren = false; |
| 501 |
List<AttributeUse> uses = new Vector<AttributeUse>(); |
| 502 |
for (Pattern child : p.getChildren()) { |
| 503 |
ChildType ct = si.getChildType(child); |
| 504 |
if (ct.contains(ChildType.ATTRIBUTE)) { |
| 505 |
AttributeUse use = child.accept(childVisitor); |
| 506 |
if (uses.size() != 1 || !use.equals(uses.get(0))) |
| 507 |
uses.add(use); |
| 508 |
} |
| 509 |
if (ct.contains(ChildType.ELEMENT) |
| 510 |
|| ct.contains(ChildType.DATA) |
| 511 |
|| ct.contains(ChildType.TEXT)) |
| 512 |
hasChildren = true; |
| 513 |
} |
| 514 |
if (hasChildren) |
| 515 |
er.warning("attribute_child_choice", p.getSourceLocation()); |
| 516 |
if (uses.size() == 1) |
| 517 |
return uses.get(0); |
| 518 |
return new AttributeUseChoice(p.getSourceLocation(), null, uses); |
| 519 |
} |
| 520 |
|
| 521 |
public AttributeUse visitPattern(Pattern p) { |
| 522 |
return null; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
class AttributeUseBuilder extends OptionalAttributeUseBuilder { |
| 527 |
boolean isOptional() { |
| 528 |
return false; |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
class SchemaBuilder implements ComponentVisitor<VoidValue> { |
| 533 |
boolean groupEnableAbstractElements; |
| 534 |
|
| 535 |
SchemaBuilder(boolean groupEnableAbstractElements) { |
| 536 |
this.groupEnableAbstractElements = groupEnableAbstractElements; |
| 537 |
} |
| 538 |
|
| 539 |
public VoidValue visitDefine(DefineComponent c) { |
| 540 |
addLeadingComments(c); |
| 541 |
String name = c.getName(); |
| 542 |
SourceLocation location = c.getSourceLocation(); |
| 543 |
Annotation annotation = makeAnnotation(c); |
| 544 |
if (name == DefineComponent.START) { |
| 545 |
if (!si.isIgnored(c)) { |
| 546 |
Pattern body = c.getBody(); |
| 547 |
ChildType ct = si.getChildType(body); |
| 548 |
if (ct.contains(ChildType.ELEMENT)) |
| 549 |
schema.addRoot(body.accept(particleBuilder), |
| 550 |
location, |
| 551 |
annotation); |
| 552 |
} |
| 553 |
} |
| 554 |
else { |
| 555 |
Pattern body = si.getBody(c); |
| 556 |
if (body != null) { |
| 557 |
ChildType ct = si.getChildType(body); |
| 558 |
if (ct.contains(ChildType.ELEMENT)) { |
| 559 |
guide.setGroupEnableAbstractElement(name, |
| 560 |
getGroupEnableAbstractElements(c, groupEnableAbstractElements)); |
| 561 |
schema.defineGroup(name, |
| 562 |
body.accept(particleBuilder), |
| 563 |
location, |
| 564 |
annotation); |
| 565 |
} |
| 566 |
else if (ct.contains(ChildType.DATA) && !ct.contains(ChildType.TEXT)) |
| 567 |
schema.defineSimpleType(name, |
| 568 |
body.accept(simpleTypeBuilder), |
| 569 |
location, |
| 570 |
annotation); |
| 571 |
if (ct.contains(ChildType.ATTRIBUTE)) |
| 572 |
schema.defineAttributeGroup(name, |
| 573 |
body.accept(attributeUseBuilder), |
| 574 |
location, |
| 575 |
annotation); |
| 576 |
} |
| 577 |
} |
| 578 |
addTrailingComments(c); |
| 579 |
return VoidValue.VOID; |
| 580 |
} |
| 581 |
|
| 582 |
public VoidValue visitDiv(DivComponent c) { |
| 583 |
addLeadingComments(c); |
| 584 |
addInitialChildComments(c); |
| 585 |
boolean saveGroupEnableAbstractElements = groupEnableAbstractElements; |
| 586 |
groupEnableAbstractElements = getGroupEnableAbstractElements(c, groupEnableAbstractElements); |
| 587 |
c.componentsAccept(this); |
| 588 |
groupEnableAbstractElements = saveGroupEnableAbstractElements; |
| 589 |
addTrailingComments(c); |
| 590 |
return VoidValue.VOID; |
| 591 |
} |
| 592 |
|
| 593 |
public VoidValue visitInclude(IncludeComponent c) { |
| 594 |
addLeadingComments(c); |
| 595 |
addInitialChildComments(c); |
| 596 |
boolean saveGroupEnableAbstractElements = groupEnableAbstractElements; |
| 597 |
groupEnableAbstractElements = getGroupEnableAbstractElements(c, groupEnableAbstractElements); |
| 598 |
c.componentsAccept(this); |
| 599 |
String uri = c.getUri(); |
| 600 |
Schema sub = schema.addInclude(uri, si.getEncoding(uri), c.getSourceLocation(), makeAnnotation(c)); |
| 601 |
GrammarPattern includedGrammar = si.getSchema(uri); |
| 602 |
new BasicBuilder(er, |
| 603 |
si, |
| 604 |
guide, |
| 605 |
sub, |
| 606 |
resolveNamespace(c.getNs()), |
| 607 |
includedGrammar, |
| 608 |
groupEnableAbstractElements).processGrammar(includedGrammar); |
| 609 |
groupEnableAbstractElements = saveGroupEnableAbstractElements; |
| 610 |
addTrailingComments(c); |
| 611 |
return VoidValue.VOID; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
private BasicBuilder(ErrorReporter er, SchemaInfo si, Guide guide, Schema schema, String inheritedNamespace, Annotated annotated, boolean groupEnableAbstractElements) { |
| 616 |
this.er = er; |
| 617 |
this.si = si; |
| 618 |
this.guide = guide; |
| 619 |
this.schema = schema; |
| 620 |
this.inheritedNamespace = inheritedNamespace; |
| 621 |
this.schemaBuilder = new SchemaBuilder(getGroupEnableAbstractElements(annotated, groupEnableAbstractElements)); |
| 622 |
} |
| 623 |
|
| 624 |
static Schema buildBasicSchema(SchemaInfo si, Guide guide, ErrorReporter er) { |
| 625 |
GrammarPattern grammar = si.getGrammar(); |
| 626 |
Schema schema = new Schema(grammar.getSourceLocation(), makeAnnotation(grammar), si.getMainUri(), si.getEncoding(si.getMainUri())); |
| 627 |
new BasicBuilder(er, si, guide, schema, "", grammar, |
| 628 |
guide.getDefaultGroupEnableAbstractElements()).processGrammar(grammar); |
| 629 |
return schema; |
| 630 |
} |
| 631 |
|
| 632 |
private void processGrammar(GrammarPattern grammar) { |
| 633 |
copyComments(grammar.getLeadingComments(), schema.getLeadingComments()); |
| 634 |
addInitialChildComments(grammar); |
| 635 |
grammar.componentsAccept(schemaBuilder); |
| 636 |
copyComments(grammar.getFollowingElementAnnotations(), schema.getTrailingComments()); |
| 637 |
} |
| 638 |
|
| 639 |
private static SimpleType makeUnionWithEmptySimpleType(SimpleType type, SourceLocation location) { |
| 640 |
List<SimpleType> list = new Vector<SimpleType>(); |
| 641 |
list.add(type); |
| 642 |
list.add(makeEmptySimpleType(location)); |
| 643 |
return new SimpleTypeUnion(location, null, list); |
| 644 |
} |
| 645 |
|
| 646 |
private static SimpleType makeEmptySimpleType(SourceLocation location) { |
| 647 |
List<Facet> facets = new Vector<Facet>(); |
| 648 |
facets.add(new Facet(location, null, "length", "0")); |
| 649 |
return new SimpleTypeRestriction(location, null, "token", facets); |
| 650 |
} |
| 651 |
|
| 652 |
private static SimpleType makeStringType(SourceLocation sourceLocation) { |
| 653 |
List<Facet> facets = Collections.emptyList(); |
| 654 |
return new SimpleTypeRestriction(sourceLocation, |
| 655 |
null, |
| 656 |
"string", |
| 657 |
facets); |
| 658 |
} |
| 659 |
|
| 660 |
private Name makeName(NameNameClass nc) { |
| 661 |
return new Name(resolveNamespace(nc.getNamespaceUri()), nc.getLocalName()); |
| 662 |
} |
| 663 |
|
| 664 |
private String resolveNamespace(String ns) { |
| 665 |
return resolveNamespace(ns, inheritedNamespace); |
| 666 |
} |
| 667 |
|
| 668 |
private static String resolveNamespace(String ns, String inheritedNamespace) { |
| 669 |
if (ns == NameNameClass.INHERIT_NS) |
| 670 |
return inheritedNamespace; |
| 671 |
return ns; |
| 672 |
} |
| 673 |
|
| 674 |
private static Wildcard[] splitElementWildcard(Wildcard wc) { |
| 675 |
if (wc == null) |
| 676 |
return new Wildcard[0]; |
| 677 |
if (wc.isPositive() || wc.getNamespaces().contains("") || wc.getNamespaces().size() != 1) |
| 678 |
return new Wildcard[] { wc }; |
| 679 |
Set<String> positiveNamespaces = new HashSet<String>(); |
| 680 |
positiveNamespaces.add(""); |
| 681 |
Set<String> negativeNamespaces = new HashSet<String>(); |
| 682 |
negativeNamespaces.add(wc.getNamespaces().iterator().next()); |
| 683 |
negativeNamespaces.add(""); |
| 684 |
Set<Name> positiveExcludeNames = new HashSet<Name>(); |
| 685 |
Set<Name> negativeExcludeNames = new HashSet<Name>(); |
| 686 |
for (Name name : wc.getExcludedNames()) |
| 687 |
(name.getNamespaceUri().equals("") ? positiveExcludeNames : negativeExcludeNames).add(name); |
| 688 |
return new Wildcard[] { |
| 689 |
new Wildcard(false, negativeNamespaces, negativeExcludeNames), |
| 690 |
new Wildcard(true, positiveNamespaces, positiveExcludeNames) |
| 691 |
}; |
| 692 |
} |
| 693 |
|
| 694 |
private boolean allowsAnyString(Pattern p) { |
| 695 |
while (p instanceof RefPattern) |
| 696 |
p = si.getBody((RefPattern)p); |
| 697 |
if (p instanceof TextPattern) |
| 698 |
return true; |
| 699 |
if (!(p instanceof DataPattern)) |
| 700 |
return false; |
| 701 |
DataPattern dp = (DataPattern)p; |
| 702 |
if (dp.getParams().size() != 0) |
| 703 |
return false; |
| 704 |
String lib = dp.getDatatypeLibrary(); |
| 705 |
if (lib.equals("")) |
| 706 |
return true; |
| 707 |
if (!lib.equals(WellKnownNamespaces.XML_SCHEMA_DATATYPES)) |
| 708 |
return false; |
| 709 |
String type = dp.getType(); |
| 710 |
return type.equals("string") || type.equals("token") || type.equals("normalizedString"); |
| 711 |
} |
| 712 |
|
| 713 |
private static Annotation makeAnnotation(Annotated annotated) { |
| 714 |
List<AnnotationChild> elements = (annotated.mayContainText() |
| 715 |
? annotated.getFollowingElementAnnotations() |
| 716 |
: annotated.getChildElementAnnotations()); |
| 717 |
for (AnnotationChild child : elements) { |
| 718 |
// child might be a Comment |
| 719 |
if (child instanceof ElementAnnotation) { |
| 720 |
ElementAnnotation element = (ElementAnnotation)child; |
| 721 |
if (element.getNamespaceUri().equals(WellKnownNamespaces.RELAX_NG_COMPATIBILITY_ANNOTATIONS) |
| 722 |
&& element.getLocalName().equals("documentation")) { |
| 723 |
String value = getAtomicValue(element); |
| 724 |
if (value == null) |
| 725 |
break; |
| 726 |
return new Annotation(value); |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
return null; |
| 731 |
} |
| 732 |
|
| 733 |
private static String getAtomicValue(ElementAnnotation elem) { |
| 734 |
String value = null; |
| 735 |
StringBuffer buf = null; |
| 736 |
List<AnnotationChild> children = elem.getChildren(); |
| 737 |
for (int i = 0, len = children.size(); i < len; i++) { |
| 738 |
Object obj = children.get(i); |
| 739 |
if (obj instanceof TextAnnotation) { |
| 740 |
String tem = ((TextAnnotation)obj).getValue(); |
| 741 |
if (buf != null) |
| 742 |
buf.append(tem); |
| 743 |
else if (value == null) |
| 744 |
value = tem; |
| 745 |
else { |
| 746 |
buf = new StringBuffer(value); |
| 747 |
buf.append(tem); |
| 748 |
value = null; |
| 749 |
} |
| 750 |
} |
| 751 |
else if (obj instanceof ElementAnnotation) |
| 752 |
return null; |
| 753 |
} |
| 754 |
if (buf != null) |
| 755 |
return buf.toString(); |
| 756 |
if (value != null) |
| 757 |
return value; |
| 758 |
return ""; |
| 759 |
} |
| 760 |
|
| 761 |
static private final String GUIDE_NAMESPACE = "http://www.thaiopensource.com/ns/relaxng/xsd"; |
| 762 |
|
| 763 |
private static boolean getGroupEnableAbstractElements(Annotated annotated, boolean current) { |
| 764 |
String value = annotated.getAttributeAnnotation(GUIDE_NAMESPACE, "enableAbstractElements"); |
| 765 |
if (value != null) { |
| 766 |
value = value.trim(); |
| 767 |
if (value.equals("true")) |
| 768 |
current = true; |
| 769 |
else if (value.equals("false")) |
| 770 |
current = false; |
| 771 |
} |
| 772 |
return current; |
| 773 |
} |
| 774 |
|
| 775 |
private void addLeadingComments(Annotated annotated) { |
| 776 |
addComments(annotated.getLeadingComments()); |
| 777 |
} |
| 778 |
|
| 779 |
private void addInitialChildComments(Annotated annotated) { |
| 780 |
addComments(annotated.getChildElementAnnotations()); |
| 781 |
} |
| 782 |
|
| 783 |
private void addTrailingComments(Annotated annotated) { |
| 784 |
addComments(annotated.getFollowingElementAnnotations()); |
| 785 |
} |
| 786 |
|
| 787 |
private void addComments(List<? extends AnnotationChild> list) { |
| 788 |
for (AnnotationChild child : list) { |
| 789 |
if (child instanceof Comment) { |
| 790 |
Comment comment = (Comment)child; |
| 791 |
schema.addComment(comment.getValue(), comment.getSourceLocation()); |
| 792 |
} |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
private static void copyComments(List<? extends AnnotationChild> fromList, |
| 797 |
List<com.thaiopensource.relaxng.output.xsd.basic.Comment> toList) { |
| 798 |
for (AnnotationChild child : fromList) { |
| 799 |
if (child instanceof Comment) { |
| 800 |
Comment comment = (Comment)child; |
| 801 |
toList.add(new com.thaiopensource.relaxng.output.xsd.basic.Comment(comment.getSourceLocation(), |
| 802 |
comment.getValue())); |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
} |