| 1 |
package com.thaiopensource.relaxng.parse.sax; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.parse.Annotations; |
| 4 |
import com.thaiopensource.relaxng.parse.CommentList; |
| 5 |
import com.thaiopensource.relaxng.parse.Context; |
| 6 |
import com.thaiopensource.relaxng.parse.DataPatternBuilder; |
| 7 |
import com.thaiopensource.relaxng.parse.Div; |
| 8 |
import com.thaiopensource.relaxng.parse.ElementAnnotationBuilder; |
| 9 |
import com.thaiopensource.relaxng.parse.Grammar; |
| 10 |
import com.thaiopensource.relaxng.parse.GrammarSection; |
| 11 |
import com.thaiopensource.relaxng.parse.IllegalSchemaException; |
| 12 |
import com.thaiopensource.relaxng.parse.Include; |
| 13 |
import com.thaiopensource.relaxng.parse.IncludedGrammar; |
| 14 |
import com.thaiopensource.relaxng.parse.ParsedPatternFuture; |
| 15 |
import com.thaiopensource.relaxng.parse.SchemaBuilder; |
| 16 |
import com.thaiopensource.relaxng.parse.Scope; |
| 17 |
import com.thaiopensource.util.Localizer; |
| 18 |
import com.thaiopensource.util.Uri; |
| 19 |
import com.thaiopensource.xml.sax.AbstractLexicalHandler; |
| 20 |
import com.thaiopensource.xml.sax.XmlBaseHandler; |
| 21 |
import com.thaiopensource.xml.util.Naming; |
| 22 |
import com.thaiopensource.xml.util.WellKnownNamespaces; |
| 23 |
import org.xml.sax.Attributes; |
| 24 |
import org.xml.sax.ContentHandler; |
| 25 |
import org.xml.sax.ErrorHandler; |
| 26 |
import org.xml.sax.Locator; |
| 27 |
import org.xml.sax.SAXException; |
| 28 |
import org.xml.sax.SAXNotRecognizedException; |
| 29 |
import org.xml.sax.SAXNotSupportedException; |
| 30 |
import org.xml.sax.SAXParseException; |
| 31 |
import org.xml.sax.XMLReader; |
| 32 |
import org.xml.sax.helpers.DefaultHandler; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.HashMap; |
| 36 |
import java.util.HashSet; |
| 37 |
import java.util.List; |
| 38 |
import java.util.Map; |
| 39 |
import java.util.Set; |
| 40 |
import java.util.Stack; |
| 41 |
|
| 42 |
class SchemaParser<Pattern, NameClass, Location, ElementAnnotation, |
| 43 |
CommentListImpl extends CommentList<Location>, |
| 44 |
AnnotationsImpl extends Annotations<Location, ElementAnnotation, CommentListImpl>> |
| 45 |
implements ParsedPatternFuture<Pattern> { |
| 46 |
private static final String relaxngURIPrefix = |
| 47 |
WellKnownNamespaces.RELAX_NG.substring(0, WellKnownNamespaces.RELAX_NG.lastIndexOf('/') + 1); |
| 48 |
static final String relaxng10URI = WellKnownNamespaces.RELAX_NG; |
| 49 |
private static final Localizer localizer = new Localizer(SchemaParser.class); |
| 50 |
|
| 51 |
private String relaxngURI; |
| 52 |
private final XMLReader xr; |
| 53 |
private final ErrorHandler eh; |
| 54 |
private final SchemaBuilder<Pattern, NameClass, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> schemaBuilder; |
| 55 |
private Pattern startPattern; |
| 56 |
private Locator locator; |
| 57 |
private final XmlBaseHandler xmlBaseHandler = new XmlBaseHandler(); |
| 58 |
private final ContextImpl context = new ContextImpl(); |
| 59 |
|
| 60 |
private boolean hadError = false; |
| 61 |
|
| 62 |
private Map<String, State> patternMap; |
| 63 |
private Map<String, State> nameClassMap; |
| 64 |
|
| 65 |
static class PrefixMapping { |
| 66 |
final String prefix; |
| 67 |
final String uri; |
| 68 |
final PrefixMapping next; |
| 69 |
|
| 70 |
PrefixMapping(String prefix, String uri, PrefixMapping next) { |
| 71 |
this.prefix = prefix; |
| 72 |
this.uri = uri; |
| 73 |
this.next = next; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static abstract class AbstractContext extends DtdContext implements Context { |
| 78 |
PrefixMapping prefixMapping; |
| 79 |
|
| 80 |
AbstractContext() { |
| 81 |
prefixMapping = new PrefixMapping("xml", WellKnownNamespaces.XML, null); |
| 82 |
} |
| 83 |
|
| 84 |
AbstractContext(AbstractContext context) { |
| 85 |
super(context); |
| 86 |
prefixMapping = context.prefixMapping; |
| 87 |
} |
| 88 |
|
| 89 |
public String resolveNamespacePrefix(String prefix) { |
| 90 |
for (PrefixMapping p = prefixMapping; p != null; p = p.next) |
| 91 |
if (p.prefix.equals(prefix)) |
| 92 |
return p.uri; |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
public Set<String> prefixes() { |
| 97 |
Set<String> set = new HashSet<String>(); |
| 98 |
for (PrefixMapping p = prefixMapping; p != null; p = p.next) |
| 99 |
set.add(p.prefix); |
| 100 |
return set; |
| 101 |
} |
| 102 |
|
| 103 |
public Context copy() { |
| 104 |
return new SavedContext(this); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
static class SavedContext extends AbstractContext { |
| 109 |
private final String baseUri; |
| 110 |
SavedContext(AbstractContext context) { |
| 111 |
super(context); |
| 112 |
this.baseUri = context.getBaseUri(); |
| 113 |
} |
| 114 |
|
| 115 |
public String getBaseUri() { |
| 116 |
return baseUri; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
class ContextImpl extends AbstractContext { |
| 121 |
public String getBaseUri() { |
| 122 |
return xmlBaseHandler.getBaseUri(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
static interface CommentHandler { |
| 127 |
void comment(String value); |
| 128 |
} |
| 129 |
|
| 130 |
abstract class Handler implements ContentHandler, CommentHandler { |
| 131 |
CommentListImpl comments; |
| 132 |
|
| 133 |
CommentListImpl getComments() { |
| 134 |
CommentListImpl tem = comments; |
| 135 |
comments = null; |
| 136 |
return tem; |
| 137 |
} |
| 138 |
|
| 139 |
public void comment(String value) { |
| 140 |
if (comments == null) |
| 141 |
comments = schemaBuilder.makeCommentList(); |
| 142 |
comments.addComment(value, makeLocation()); |
| 143 |
} |
| 144 |
public void processingInstruction(String target, String date) { } |
| 145 |
public void skippedEntity(String name) { } |
| 146 |
public void ignorableWhitespace(char[] ch, int start, int len) { } |
| 147 |
public void startDocument() { } |
| 148 |
public void endDocument() { } |
| 149 |
public void startPrefixMapping(String prefix, String uri) { |
| 150 |
context.prefixMapping = new PrefixMapping(prefix, uri, context.prefixMapping); |
| 151 |
} |
| 152 |
|
| 153 |
public void endPrefixMapping(String prefix) { |
| 154 |
context.prefixMapping = context.prefixMapping.next; |
| 155 |
} |
| 156 |
|
| 157 |
public void setDocumentLocator(Locator loc) { |
| 158 |
locator = loc; |
| 159 |
xmlBaseHandler.setLocator(loc); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
abstract class State extends Handler { |
| 164 |
State parent; |
| 165 |
String nsInherit; |
| 166 |
String ns; |
| 167 |
String datatypeLibrary; |
| 168 |
Scope<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> scope; |
| 169 |
Location startLocation; |
| 170 |
AnnotationsImpl annotations; |
| 171 |
|
| 172 |
void set() { |
| 173 |
xr.setContentHandler(this); |
| 174 |
} |
| 175 |
|
| 176 |
abstract State create(); |
| 177 |
abstract State createChildState(String localName) throws SAXException; |
| 178 |
|
| 179 |
RootState toRootState() { |
| 180 |
return null; |
| 181 |
} |
| 182 |
|
| 183 |
NameClassChoiceState toNameClassChoiceState() { |
| 184 |
return null; |
| 185 |
} |
| 186 |
|
| 187 |
void setParent(State parent) { |
| 188 |
this.parent = parent; |
| 189 |
this.nsInherit = parent.getNs(); |
| 190 |
this.datatypeLibrary = parent.datatypeLibrary; |
| 191 |
this.scope = parent.scope; |
| 192 |
this.startLocation = makeLocation(); |
| 193 |
if (parent.comments != null) { |
| 194 |
annotations = schemaBuilder.makeAnnotations(parent.comments, getContext()); |
| 195 |
parent.comments = null; |
| 196 |
} |
| 197 |
else if (parent.toRootState() != null) |
| 198 |
annotations = schemaBuilder.makeAnnotations(null, getContext()); |
| 199 |
} |
| 200 |
|
| 201 |
String getNs() { |
| 202 |
return ns == null ? nsInherit : ns; |
| 203 |
} |
| 204 |
|
| 205 |
boolean isRelaxNGElement(String uri) throws SAXException { |
| 206 |
return uri.equals(relaxngURI); |
| 207 |
} |
| 208 |
|
| 209 |
public void startElement(String namespaceURI, |
| 210 |
String localName, |
| 211 |
String qName, |
| 212 |
Attributes atts) throws SAXException { |
| 213 |
xmlBaseHandler.startElement(); |
| 214 |
if (isRelaxNGElement(namespaceURI)) { |
| 215 |
State state = createChildState(localName); |
| 216 |
if (state == null) { |
| 217 |
xr.setContentHandler(new Skipper(this)); |
| 218 |
return; |
| 219 |
} |
| 220 |
state.setParent(this); |
| 221 |
state.set(); |
| 222 |
state.attributes(atts); |
| 223 |
} |
| 224 |
else { |
| 225 |
checkForeignElement(); |
| 226 |
ForeignElementHandler feh = new ForeignElementHandler(this, getComments()); |
| 227 |
feh.startElement(namespaceURI, localName, qName, atts); |
| 228 |
xr.setContentHandler(feh); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
public void endElement(String namespaceURI, |
| 233 |
String localName, |
| 234 |
String qName) throws SAXException { |
| 235 |
xmlBaseHandler.endElement(); |
| 236 |
parent.set(); |
| 237 |
end(); |
| 238 |
} |
| 239 |
|
| 240 |
void setName(String name) throws SAXException { |
| 241 |
error("illegal_name_attribute"); |
| 242 |
} |
| 243 |
|
| 244 |
void setOtherAttribute(String name, String value) throws SAXException { |
| 245 |
error("illegal_attribute_ignored", name); |
| 246 |
} |
| 247 |
|
| 248 |
void endAttributes() throws SAXException { |
| 249 |
} |
| 250 |
|
| 251 |
void checkForeignElement() throws SAXException { |
| 252 |
} |
| 253 |
|
| 254 |
void attributes(Attributes atts) throws SAXException { |
| 255 |
int len = atts.getLength(); |
| 256 |
for (int i = 0; i < len; i++) { |
| 257 |
String uri = atts.getURI(i); |
| 258 |
if (uri.length() == 0) { |
| 259 |
String name = atts.getLocalName(i); |
| 260 |
if (name.equals("name")) |
| 261 |
setName(atts.getValue(i).trim()); |
| 262 |
else if (name.equals("ns")) |
| 263 |
ns = atts.getValue(i); |
| 264 |
else if (name.equals("datatypeLibrary")) { |
| 265 |
datatypeLibrary = atts.getValue(i); |
| 266 |
checkUri(datatypeLibrary); |
| 267 |
if (!datatypeLibrary.equals("") |
| 268 |
&& !Uri.isAbsolute(datatypeLibrary)) |
| 269 |
error("relative_datatype_library"); |
| 270 |
if (Uri.hasFragmentId(datatypeLibrary)) |
| 271 |
error("fragment_identifier_datatype_library"); |
| 272 |
datatypeLibrary = Uri.escapeDisallowedChars(datatypeLibrary); |
| 273 |
} |
| 274 |
else |
| 275 |
setOtherAttribute(name, atts.getValue(i)); |
| 276 |
} |
| 277 |
else if (uri.equals(relaxngURI)) |
| 278 |
error("qualified_attribute", atts.getLocalName(i)); |
| 279 |
else if (uri.equals(WellKnownNamespaces.XML) |
| 280 |
&& atts.getLocalName(i).equals("base")) |
| 281 |
xmlBaseHandler.xmlBaseAttribute(atts.getValue(i)); |
| 282 |
else { |
| 283 |
if (annotations == null) |
| 284 |
annotations = schemaBuilder.makeAnnotations(null, getContext()); |
| 285 |
annotations.addAttribute(uri, atts.getLocalName(i), findPrefix(atts.getQName(i), uri), |
| 286 |
atts.getValue(i), startLocation); |
| 287 |
} |
| 288 |
} |
| 289 |
endAttributes(); |
| 290 |
} |
| 291 |
|
| 292 |
abstract void end() throws SAXException; |
| 293 |
|
| 294 |
void endPatternChild(Pattern pattern) { |
| 295 |
// XXX cannot happen; throw exception |
| 296 |
} |
| 297 |
|
| 298 |
void endNameClassChild(NameClass nc) { |
| 299 |
// XXX cannot happen; throw exception |
| 300 |
} |
| 301 |
|
| 302 |
public void startDocument() { } |
| 303 |
public void endDocument() { |
| 304 |
if (comments != null && startPattern != null) { |
| 305 |
startPattern = schemaBuilder.commentAfterPattern(startPattern, comments); |
| 306 |
comments = null; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
public void characters(char[] ch, int start, int len) throws SAXException { |
| 311 |
for (int i = 0; i < len; i++) { |
| 312 |
switch(ch[start + i]) { |
| 313 |
case ' ': |
| 314 |
case '\r': |
| 315 |
case '\n': |
| 316 |
case '\t': |
| 317 |
break; |
| 318 |
default: |
| 319 |
error("illegal_characters_ignored"); |
| 320 |
break; |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
boolean isPatternNamespaceURI(String s) { |
| 326 |
return s.equals(relaxngURI); |
| 327 |
} |
| 328 |
|
| 329 |
void endForeignChild(ElementAnnotation ea) { |
| 330 |
if (annotations == null) |
| 331 |
annotations = schemaBuilder.makeAnnotations(null, getContext()); |
| 332 |
annotations.addElement(ea); |
| 333 |
} |
| 334 |
|
| 335 |
void mergeLeadingComments() { |
| 336 |
if (comments != null) { |
| 337 |
if (annotations == null) |
| 338 |
annotations = schemaBuilder.makeAnnotations(comments, getContext()); |
| 339 |
else |
| 340 |
annotations.addLeadingComment(comments); |
| 341 |
comments = null; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
class ForeignElementHandler extends Handler { |
| 347 |
final State nextState; |
| 348 |
ElementAnnotationBuilder<Location, ElementAnnotation, CommentListImpl> builder; |
| 349 |
final Stack<ElementAnnotationBuilder<Location, ElementAnnotation, CommentListImpl>> builderStack = new Stack<ElementAnnotationBuilder<Location, ElementAnnotation, CommentListImpl>>(); |
| 350 |
StringBuffer textBuf; |
| 351 |
Location textLoc; |
| 352 |
|
| 353 |
ForeignElementHandler(State nextState, CommentListImpl comments) { |
| 354 |
this.nextState = nextState; |
| 355 |
this.comments = comments; |
| 356 |
} |
| 357 |
|
| 358 |
public void startElement(String namespaceURI, String localName, |
| 359 |
String qName, Attributes atts) { |
| 360 |
flushText(); |
| 361 |
if (builder != null) |
| 362 |
builderStack.push(builder); |
| 363 |
Location loc = makeLocation(); |
| 364 |
builder = schemaBuilder.makeElementAnnotationBuilder(namespaceURI, |
| 365 |
localName, |
| 366 |
findPrefix(qName, namespaceURI), |
| 367 |
loc, |
| 368 |
getComments(), |
| 369 |
getContext()); |
| 370 |
int len = atts.getLength(); |
| 371 |
for (int i = 0; i < len; i++) { |
| 372 |
String uri = atts.getURI(i); |
| 373 |
builder.addAttribute(uri, atts.getLocalName(i), findPrefix(atts.getQName(i), uri), |
| 374 |
atts.getValue(i), loc); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
public void endElement(String namespaceURI, String localName, |
| 379 |
String qName) { |
| 380 |
flushText(); |
| 381 |
if (comments != null) |
| 382 |
builder.addComment(getComments()); |
| 383 |
ElementAnnotation ea = builder.makeElementAnnotation(); |
| 384 |
if (builderStack.empty()) { |
| 385 |
nextState.endForeignChild(ea); |
| 386 |
nextState.set(); |
| 387 |
} |
| 388 |
else { |
| 389 |
builder = builderStack.pop(); |
| 390 |
builder.addElement(ea); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
public void characters(char ch[], int start, int length) { |
| 395 |
if (textBuf == null) |
| 396 |
textBuf = new StringBuffer(); |
| 397 |
textBuf.append(ch, start, length); |
| 398 |
if (textLoc == null) |
| 399 |
textLoc = makeLocation(); |
| 400 |
} |
| 401 |
|
| 402 |
public void comment(String value) { |
| 403 |
flushText(); |
| 404 |
super.comment(value); |
| 405 |
} |
| 406 |
|
| 407 |
void flushText() { |
| 408 |
if (textBuf != null && textBuf.length() != 0) { |
| 409 |
builder.addText(textBuf.toString(), textLoc, getComments()); |
| 410 |
textBuf.setLength(0); |
| 411 |
} |
| 412 |
textLoc = null; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
class Skipper extends DefaultHandler implements CommentHandler { |
| 417 |
int level = 1; |
| 418 |
final State nextState; |
| 419 |
|
| 420 |
Skipper(State nextState) { |
| 421 |
this.nextState = nextState; |
| 422 |
} |
| 423 |
|
| 424 |
public void startElement(String namespaceURI, |
| 425 |
String localName, |
| 426 |
String qName, |
| 427 |
Attributes atts) throws SAXException { |
| 428 |
++level; |
| 429 |
} |
| 430 |
|
| 431 |
public void endElement(String namespaceURI, |
| 432 |
String localName, |
| 433 |
String qName) throws SAXException { |
| 434 |
if (--level == 0) |
| 435 |
nextState.set(); |
| 436 |
} |
| 437 |
|
| 438 |
public void comment(String value) { |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
abstract class EmptyContentState extends State { |
| 443 |
|
| 444 |
State createChildState(String localName) throws SAXException { |
| 445 |
error("expected_empty", localName); |
| 446 |
return null; |
| 447 |
} |
| 448 |
|
| 449 |
abstract Pattern makePattern() throws SAXException; |
| 450 |
|
| 451 |
void end() throws SAXException { |
| 452 |
if (comments != null) { |
| 453 |
if (annotations == null) |
| 454 |
annotations = schemaBuilder.makeAnnotations(null, getContext()); |
| 455 |
annotations.addComment(comments); |
| 456 |
comments = null; |
| 457 |
} |
| 458 |
parent.endPatternChild(makePattern()); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
abstract class PatternContainerState extends State { |
| 463 |
List<Pattern> childPatterns = new ArrayList<Pattern>(); |
| 464 |
|
| 465 |
State createChildState(String localName) throws SAXException { |
| 466 |
State state = patternMap.get(localName); |
| 467 |
if (state == null) { |
| 468 |
error("expected_pattern", localName); |
| 469 |
return null; |
| 470 |
} |
| 471 |
return state.create(); |
| 472 |
} |
| 473 |
|
| 474 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 475 |
if (patterns.size() == 1 && anno == null) |
| 476 |
return patterns.get(0); |
| 477 |
return schemaBuilder.makeGroup(patterns, loc, anno); |
| 478 |
} |
| 479 |
|
| 480 |
void endPatternChild(Pattern pattern) { |
| 481 |
childPatterns.add(pattern); |
| 482 |
} |
| 483 |
|
| 484 |
void endForeignChild(ElementAnnotation ea) { |
| 485 |
int nChildPatterns = childPatterns.size(); |
| 486 |
if (nChildPatterns == 0) |
| 487 |
super.endForeignChild(ea); |
| 488 |
else |
| 489 |
childPatterns.set(nChildPatterns - 1, |
| 490 |
schemaBuilder.annotateAfterPattern(childPatterns.get(nChildPatterns - 1), ea)); |
| 491 |
} |
| 492 |
|
| 493 |
void end() throws SAXException { |
| 494 |
if (childPatterns.size() == 0) { |
| 495 |
error("missing_children"); |
| 496 |
endPatternChild(schemaBuilder.makeErrorPattern()); |
| 497 |
} |
| 498 |
if (comments != null) { |
| 499 |
int nChildPatterns = childPatterns.size(); |
| 500 |
childPatterns.set(nChildPatterns - 1, |
| 501 |
schemaBuilder.commentAfterPattern(childPatterns.get(nChildPatterns - 1), comments)); |
| 502 |
comments = null; |
| 503 |
} |
| 504 |
sendPatternToParent(buildPattern(childPatterns, startLocation, annotations)); |
| 505 |
} |
| 506 |
|
| 507 |
void sendPatternToParent(Pattern p) { |
| 508 |
parent.endPatternChild(p); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
class GroupState extends PatternContainerState { |
| 513 |
State create() { |
| 514 |
return new GroupState(); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
class ZeroOrMoreState extends PatternContainerState { |
| 519 |
State create() { |
| 520 |
return new ZeroOrMoreState(); |
| 521 |
} |
| 522 |
|
| 523 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 524 |
return schemaBuilder.makeZeroOrMore(super.buildPattern(patterns, loc, null), loc, anno); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
class OneOrMoreState extends PatternContainerState { |
| 529 |
State create() { |
| 530 |
return new OneOrMoreState(); |
| 531 |
} |
| 532 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 533 |
return schemaBuilder.makeOneOrMore(super.buildPattern(patterns, loc, null), loc, anno); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
class OptionalState extends PatternContainerState { |
| 538 |
State create() { |
| 539 |
return new OptionalState(); |
| 540 |
} |
| 541 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 542 |
return schemaBuilder.makeOptional(super.buildPattern(patterns, loc, null), loc, anno); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
class ListState extends PatternContainerState { |
| 547 |
State create() { |
| 548 |
return new ListState(); |
| 549 |
} |
| 550 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 551 |
return schemaBuilder.makeList(super.buildPattern(patterns, loc, null), loc, anno); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
class ChoiceState extends PatternContainerState { |
| 556 |
State create() { |
| 557 |
return new ChoiceState(); |
| 558 |
} |
| 559 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 560 |
return schemaBuilder.makeChoice(patterns, loc, anno); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
class InterleaveState extends PatternContainerState { |
| 565 |
State create() { |
| 566 |
return new InterleaveState(); |
| 567 |
} |
| 568 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) { |
| 569 |
return schemaBuilder.makeInterleave(patterns, loc, anno); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
class MixedState extends PatternContainerState { |
| 574 |
State create() { |
| 575 |
return new MixedState(); |
| 576 |
} |
| 577 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 578 |
return schemaBuilder.makeMixed(super.buildPattern(patterns, loc, null), loc, anno); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
static interface NameClassRef<NC> { |
| 583 |
void setNameClass(NC nc); |
| 584 |
} |
| 585 |
|
| 586 |
class ElementState extends PatternContainerState implements NameClassRef<NameClass> { |
| 587 |
NameClass nameClass; |
| 588 |
boolean nameClassWasAttribute; |
| 589 |
String name; |
| 590 |
|
| 591 |
void setName(String name) { |
| 592 |
this.name = name; |
| 593 |
} |
| 594 |
|
| 595 |
public void setNameClass(NameClass nc) { |
| 596 |
nameClass = nc; |
| 597 |
} |
| 598 |
|
| 599 |
void endAttributes() throws SAXException { |
| 600 |
if (name != null) { |
| 601 |
nameClass = expandName(name, getNs(), null); |
| 602 |
nameClassWasAttribute = true; |
| 603 |
} |
| 604 |
else |
| 605 |
new NameClassChildState(this, this).set(); |
| 606 |
} |
| 607 |
|
| 608 |
State create() { |
| 609 |
return new ElementState(); |
| 610 |
} |
| 611 |
|
| 612 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 613 |
return schemaBuilder.makeElement(nameClass, super.buildPattern(patterns, loc, null), loc, anno); |
| 614 |
} |
| 615 |
|
| 616 |
void endForeignChild(ElementAnnotation ea) { |
| 617 |
if (nameClassWasAttribute || childPatterns.size() > 0 || nameClass == null) |
| 618 |
super.endForeignChild(ea); |
| 619 |
else |
| 620 |
nameClass = schemaBuilder.annotateAfterNameClass(nameClass, ea); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
class RootState extends PatternContainerState { |
| 625 |
IncludedGrammar<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> grammar; |
| 626 |
|
| 627 |
RootState() { |
| 628 |
} |
| 629 |
|
| 630 |
RootState(IncludedGrammar<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> grammar, Scope<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> scope, String ns) { |
| 631 |
this.grammar = grammar; |
| 632 |
this.scope = scope; |
| 633 |
this.nsInherit = ns; |
| 634 |
this.datatypeLibrary = ""; |
| 635 |
} |
| 636 |
|
| 637 |
RootState toRootState() { |
| 638 |
return this; |
| 639 |
} |
| 640 |
|
| 641 |
State create() { |
| 642 |
return new RootState(); |
| 643 |
} |
| 644 |
|
| 645 |
State createChildState(String localName) throws SAXException { |
| 646 |
if (grammar == null) |
| 647 |
return super.createChildState(localName); |
| 648 |
if (localName.equals("grammar")) |
| 649 |
return new MergeGrammarState(grammar); |
| 650 |
error("expected_grammar", localName); |
| 651 |
return null; |
| 652 |
} |
| 653 |
|
| 654 |
void checkForeignElement() throws SAXException { |
| 655 |
error("root_bad_namespace_uri", WellKnownNamespaces.RELAX_NG); |
| 656 |
} |
| 657 |
|
| 658 |
void endPatternChild(Pattern pattern) { |
| 659 |
startPattern = pattern; |
| 660 |
} |
| 661 |
|
| 662 |
boolean isRelaxNGElement(String uri) throws SAXException { |
| 663 |
if (!uri.startsWith(relaxngURIPrefix)) |
| 664 |
return false; |
| 665 |
if (!uri.equals(WellKnownNamespaces.RELAX_NG)) |
| 666 |
warning("wrong_uri_version", |
| 667 |
WellKnownNamespaces.RELAX_NG.substring(relaxngURIPrefix.length()), |
| 668 |
uri.substring(relaxngURIPrefix.length())); |
| 669 |
relaxngURI = uri; |
| 670 |
return true; |
| 671 |
} |
| 672 |
|
| 673 |
} |
| 674 |
|
| 675 |
class NotAllowedState extends EmptyContentState { |
| 676 |
State create() { |
| 677 |
return new NotAllowedState(); |
| 678 |
} |
| 679 |
|
| 680 |
Pattern makePattern() { |
| 681 |
return schemaBuilder.makeNotAllowed(startLocation, annotations); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
class EmptyState extends EmptyContentState { |
| 686 |
State create() { |
| 687 |
return new EmptyState(); |
| 688 |
} |
| 689 |
|
| 690 |
Pattern makePattern() { |
| 691 |
return schemaBuilder.makeEmpty(startLocation, annotations); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
class TextState extends EmptyContentState { |
| 696 |
State create() { |
| 697 |
return new TextState(); |
| 698 |
} |
| 699 |
|
| 700 |
Pattern makePattern() { |
| 701 |
return schemaBuilder.makeText(startLocation, annotations); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
class ValueState extends EmptyContentState { |
| 706 |
final StringBuffer buf = new StringBuffer(); |
| 707 |
String type; |
| 708 |
|
| 709 |
State create() { |
| 710 |
return new ValueState(); |
| 711 |
} |
| 712 |
|
| 713 |
void setOtherAttribute(String name, String value) throws SAXException { |
| 714 |
if (name.equals("type")) |
| 715 |
type = checkNCName(value.trim()); |
| 716 |
else |
| 717 |
super.setOtherAttribute(name, value); |
| 718 |
} |
| 719 |
|
| 720 |
public void characters(char[] ch, int start, int len) { |
| 721 |
buf.append(ch, start, len); |
| 722 |
} |
| 723 |
|
| 724 |
void checkForeignElement() throws SAXException { |
| 725 |
error("value_contains_foreign_element"); |
| 726 |
} |
| 727 |
|
| 728 |
Pattern makePattern() throws SAXException { |
| 729 |
if (type == null) |
| 730 |
return makePattern("", "token"); |
| 731 |
else |
| 732 |
return makePattern(datatypeLibrary, type); |
| 733 |
} |
| 734 |
|
| 735 |
void end() throws SAXException { |
| 736 |
mergeLeadingComments(); |
| 737 |
super.end(); |
| 738 |
} |
| 739 |
|
| 740 |
Pattern makePattern(String datatypeLibrary, String type) { |
| 741 |
return schemaBuilder.makeValue(datatypeLibrary, |
| 742 |
type, |
| 743 |
buf.toString(), |
| 744 |
getContext(), |
| 745 |
getNs(), |
| 746 |
startLocation, |
| 747 |
annotations); |
| 748 |
} |
| 749 |
|
| 750 |
} |
| 751 |
|
| 752 |
class DataState extends State { |
| 753 |
String type; |
| 754 |
Pattern except = null; |
| 755 |
DataPatternBuilder<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> dpb = null; |
| 756 |
|
| 757 |
State create() { |
| 758 |
return new DataState(); |
| 759 |
} |
| 760 |
|
| 761 |
State createChildState(String localName) throws SAXException { |
| 762 |
if (localName.equals("param")) { |
| 763 |
if (except != null) |
| 764 |
error("param_after_except"); |
| 765 |
return new ParamState(dpb); |
| 766 |
} |
| 767 |
if (localName.equals("except")) { |
| 768 |
if (except != null) |
| 769 |
error("multiple_except"); |
| 770 |
return new ChoiceState(); |
| 771 |
} |
| 772 |
error("expected_param_except", localName); |
| 773 |
return null; |
| 774 |
} |
| 775 |
|
| 776 |
void setOtherAttribute(String name, String value) throws SAXException { |
| 777 |
if (name.equals("type")) |
| 778 |
type = checkNCName(value.trim()); |
| 779 |
else |
| 780 |
super.setOtherAttribute(name, value); |
| 781 |
} |
| 782 |
|
| 783 |
void endAttributes() throws SAXException { |
| 784 |
if (type == null) |
| 785 |
error("missing_type_attribute"); |
| 786 |
else |
| 787 |
dpb = schemaBuilder.makeDataPatternBuilder(datatypeLibrary, type, startLocation); |
| 788 |
} |
| 789 |
|
| 790 |
void endForeignChild(ElementAnnotation ea) { |
| 791 |
dpb.annotation(ea); |
| 792 |
} |
| 793 |
|
| 794 |
void end() throws SAXException { |
| 795 |
Pattern p; |
| 796 |
if (dpb != null) { |
| 797 |
if (except != null) |
| 798 |
p = dpb.makePattern(except, startLocation, annotations); |
| 799 |
else |
| 800 |
p = dpb.makePattern(startLocation, annotations); |
| 801 |
} |
| 802 |
else |
| 803 |
p = schemaBuilder.makeErrorPattern(); |
| 804 |
// XXX need to capture comments |
| 805 |
parent.endPatternChild(p); |
| 806 |
} |
| 807 |
|
| 808 |
void endPatternChild(Pattern pattern) { |
| 809 |
except = pattern; |
| 810 |
} |
| 811 |
|
| 812 |
} |
| 813 |
|
| 814 |
class ParamState extends State { |
| 815 |
private final StringBuffer buf = new StringBuffer(); |
| 816 |
private final DataPatternBuilder<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> dpb; |
| 817 |
private String name; |
| 818 |
|
| 819 |
ParamState(DataPatternBuilder<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> dpb) { |
| 820 |
this.dpb = dpb; |
| 821 |
} |
| 822 |
|
| 823 |
State create() { |
| 824 |
return new ParamState(null); |
| 825 |
} |
| 826 |
|
| 827 |
void setName(String name) throws SAXException { |
| 828 |
this.name = checkNCName(name); |
| 829 |
} |
| 830 |
|
| 831 |
void endAttributes() throws SAXException { |
| 832 |
if (name == null) |
| 833 |
error("missing_name_attribute"); |
| 834 |
} |
| 835 |
|
| 836 |
State createChildState(String localName) throws SAXException { |
| 837 |
error("expected_empty", localName); |
| 838 |
return null; |
| 839 |
} |
| 840 |
|
| 841 |
public void characters(char[] ch, int start, int len) { |
| 842 |
buf.append(ch, start, len); |
| 843 |
} |
| 844 |
|
| 845 |
void checkForeignElement() throws SAXException { |
| 846 |
error("param_contains_foreign_element"); |
| 847 |
} |
| 848 |
|
| 849 |
void end() throws SAXException { |
| 850 |
if (name == null) |
| 851 |
return; |
| 852 |
if (dpb == null) |
| 853 |
return; |
| 854 |
mergeLeadingComments(); |
| 855 |
dpb.addParam(name, buf.toString(), getContext(), getNs(), startLocation, annotations); |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
class AttributeState extends PatternContainerState implements NameClassRef<NameClass> { |
| 860 |
NameClass nameClass; |
| 861 |
boolean nameClassWasAttribute; |
| 862 |
String name; |
| 863 |
|
| 864 |
State create() { |
| 865 |
return new AttributeState(); |
| 866 |
} |
| 867 |
|
| 868 |
void setName(String name) { |
| 869 |
this.name = name; |
| 870 |
} |
| 871 |
|
| 872 |
public void setNameClass(NameClass nc) { |
| 873 |
nameClass = nc; |
| 874 |
} |
| 875 |
|
| 876 |
void endAttributes() throws SAXException { |
| 877 |
if (name != null) { |
| 878 |
String nsUse; |
| 879 |
if (ns != null) |
| 880 |
nsUse = ns; |
| 881 |
else |
| 882 |
nsUse = ""; |
| 883 |
nameClass = expandName(name, nsUse, null); |
| 884 |
nameClassWasAttribute = true; |
| 885 |
} |
| 886 |
else |
| 887 |
new NameClassChildState(this, this).set(); |
| 888 |
} |
| 889 |
|
| 890 |
void endForeignChild(ElementAnnotation ea) { |
| 891 |
if (nameClassWasAttribute || childPatterns.size() > 0 || nameClass == null) |
| 892 |
super.endForeignChild(ea); |
| 893 |
else |
| 894 |
nameClass = schemaBuilder.annotateAfterNameClass(nameClass, ea); |
| 895 |
} |
| 896 |
|
| 897 |
void end() throws SAXException { |
| 898 |
if (childPatterns.size() == 0) |
| 899 |
endPatternChild(schemaBuilder.makeText(startLocation, null)); |
| 900 |
super.end(); |
| 901 |
} |
| 902 |
|
| 903 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 904 |
return schemaBuilder.makeAttribute(nameClass, super.buildPattern(patterns, loc, null), loc, anno); |
| 905 |
} |
| 906 |
|
| 907 |
State createChildState(String localName) throws SAXException { |
| 908 |
State tem = super.createChildState(localName); |
| 909 |
if (tem != null && childPatterns.size() != 0) |
| 910 |
error("attribute_multi_pattern"); |
| 911 |
return tem; |
| 912 |
} |
| 913 |
|
| 914 |
} |
| 915 |
|
| 916 |
abstract class SinglePatternContainerState extends PatternContainerState { |
| 917 |
State createChildState(String localName) throws SAXException { |
| 918 |
if (childPatterns.size() == 0) |
| 919 |
return super.createChildState(localName); |
| 920 |
error("too_many_children"); |
| 921 |
return null; |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
class GrammarSectionState extends State { |
| 926 |
GrammarSection<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> section; |
| 927 |
|
| 928 |
GrammarSectionState() { } |
| 929 |
|
| 930 |
GrammarSectionState(GrammarSection<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> section) { |
| 931 |
this.section = section; |
| 932 |
} |
| 933 |
|
| 934 |
State create() { |
| 935 |
return new GrammarSectionState(null); |
| 936 |
} |
| 937 |
|
| 938 |
State createChildState(String localName) throws SAXException { |
| 939 |
if (localName.equals("define")) |
| 940 |
return new DefineState(section); |
| 941 |
if (localName.equals("start")) |
| 942 |
return new StartState(section); |
| 943 |
if (localName.equals("include")) { |
| 944 |
Include<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> include = section.makeInclude(); |
| 945 |
if (include != null) |
| 946 |
return new IncludeState(include); |
| 947 |
} |
| 948 |
if (localName.equals("div")) |
| 949 |
return new DivState(section.makeDiv()); |
| 950 |
error("expected_define", localName); |
| 951 |
// XXX better errors |
| 952 |
return null; |
| 953 |
} |
| 954 |
|
| 955 |
void end() throws SAXException { |
| 956 |
if (comments != null) { |
| 957 |
section.topLevelComment(comments); |
| 958 |
comments = null; |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
void endForeignChild(ElementAnnotation ea) { |
| 963 |
section.topLevelAnnotation(ea); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
class DivState extends GrammarSectionState { |
| 968 |
final Div<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> div; |
| 969 |
DivState(Div<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> div) { |
| 970 |
super(div); |
| 971 |
this.div = div; |
| 972 |
} |
| 973 |
|
| 974 |
void end() throws SAXException { |
| 975 |
super.end(); |
| 976 |
div.endDiv(startLocation, annotations); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
class IncludeState extends GrammarSectionState { |
| 981 |
String href; |
| 982 |
String base; |
| 983 |
final Include<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> include; |
| 984 |
|
| 985 |
IncludeState(Include<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> include) { |
| 986 |
super(include); |
| 987 |
this.include = include; |
| 988 |
} |
| 989 |
|
| 990 |
void setOtherAttribute(String name, String value) throws SAXException { |
| 991 |
if (name.equals("href")) { |
| 992 |
href = value; |
| 993 |
checkUriNoFragmentId(href); |
| 994 |
} |
| 995 |
else |
| 996 |
super.setOtherAttribute(name, value); |
| 997 |
} |
| 998 |
|
| 999 |
void endAttributes() throws SAXException { |
| 1000 |
if (href == null) |
| 1001 |
error("missing_href_attribute"); |
| 1002 |
else |
| 1003 |
base = xmlBaseHandler.getBaseUri(); |
| 1004 |
} |
| 1005 |
|
| 1006 |
void end() throws SAXException { |
| 1007 |
super.end(); |
| 1008 |
if (href != null) { |
| 1009 |
try { |
| 1010 |
include.endInclude(href, base, getNs(), startLocation, annotations); |
| 1011 |
} |
| 1012 |
catch (IllegalSchemaException e) { |
| 1013 |
} |
| 1014 |
} |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
class MergeGrammarState extends GrammarSectionState { |
| 1019 |
final IncludedGrammar<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> grammar; |
| 1020 |
MergeGrammarState(IncludedGrammar<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> grammar) { |
| 1021 |
super(grammar); |
| 1022 |
this.grammar = grammar; |
| 1023 |
} |
| 1024 |
|
| 1025 |
void end() throws SAXException { |
| 1026 |
super.end(); |
| 1027 |
parent.endPatternChild(grammar.endIncludedGrammar(startLocation, annotations)); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
class GrammarState extends GrammarSectionState { |
| 1032 |
Grammar<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> grammar; |
| 1033 |
|
| 1034 |
void setParent(State parent) { |
| 1035 |
super.setParent(parent); |
| 1036 |
grammar = schemaBuilder.makeGrammar(scope); |
| 1037 |
section = grammar; |
| 1038 |
scope = grammar; |
| 1039 |
} |
| 1040 |
|
| 1041 |
State create() { |
| 1042 |
return new GrammarState(); |
| 1043 |
} |
| 1044 |
|
| 1045 |
void end() throws SAXException { |
| 1046 |
super.end(); |
| 1047 |
parent.endPatternChild(grammar.endGrammar(startLocation, annotations)); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
class RefState extends EmptyContentState { |
| 1052 |
String name; |
| 1053 |
|
| 1054 |
State create() { |
| 1055 |
return new RefState(); |
| 1056 |
} |
| 1057 |
|
| 1058 |
void endAttributes() throws SAXException { |
| 1059 |
if (name == null) |
| 1060 |
error("missing_name_attribute"); |
| 1061 |
} |
| 1062 |
|
| 1063 |
void setName(String name) throws SAXException { |
| 1064 |
this.name = checkNCName(name); |
| 1065 |
} |
| 1066 |
|
| 1067 |
Pattern makePattern() { |
| 1068 |
if (name == null) |
| 1069 |
return schemaBuilder.makeErrorPattern(); |
| 1070 |
return scope.makeRef(name, startLocation, annotations); |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
class ParentRefState extends RefState { |
| 1075 |
State create() { |
| 1076 |
return new ParentRefState(); |
| 1077 |
} |
| 1078 |
|
| 1079 |
Pattern makePattern() { |
| 1080 |
if (name == null) |
| 1081 |
return schemaBuilder.makeErrorPattern(); |
| 1082 |
return scope.makeParentRef(name, startLocation, annotations); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
class ExternalRefState extends EmptyContentState { |
| 1087 |
String href; |
| 1088 |
String base; |
| 1089 |
Pattern includedPattern; |
| 1090 |
|
| 1091 |
State create() { |
| 1092 |
return new ExternalRefState(); |
| 1093 |
} |
| 1094 |
|
| 1095 |
void setOtherAttribute(String name, String value) throws SAXException { |
| 1096 |
if (name.equals("href")) { |
| 1097 |
href = value; |
| 1098 |
checkUriNoFragmentId(href); |
| 1099 |
} |
| 1100 |
else |
| 1101 |
super.setOtherAttribute(name, value); |
| 1102 |
} |
| 1103 |
|
| 1104 |
void endAttributes() throws SAXException { |
| 1105 |
if (href == null) |
| 1106 |
error("missing_href_attribute"); |
| 1107 |
else |
| 1108 |
base = xmlBaseHandler.getBaseUri(); |
| 1109 |
} |
| 1110 |
|
| 1111 |
Pattern makePattern() { |
| 1112 |
if (href != null) { |
| 1113 |
try { |
| 1114 |
return schemaBuilder.makeExternalRef(href, |
| 1115 |
base, |
| 1116 |
getNs(), |
| 1117 |
scope, |
| 1118 |
startLocation, |
| 1119 |
annotations); |
| 1120 |
} |
| 1121 |
catch (IllegalSchemaException e) { } |
| 1122 |
} |
| 1123 |
return schemaBuilder.makeErrorPattern(); |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
abstract class DefinitionState extends PatternContainerState { |
| 1128 |
GrammarSection.Combine combine = null; |
| 1129 |
final GrammarSection<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> section; |
| 1130 |
|
| 1131 |
DefinitionState(GrammarSection<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> section) { |
| 1132 |
this.section = section; |
| 1133 |
} |
| 1134 |
|
| 1135 |
void setOtherAttribute(String name, String value) throws SAXException { |
| 1136 |
if (name.equals("combine")) { |
| 1137 |
value = value.trim(); |
| 1138 |
if (value.equals("choice")) |
| 1139 |
combine = GrammarSection.COMBINE_CHOICE; |
| 1140 |
else if (value.equals("interleave")) |
| 1141 |
combine = GrammarSection.COMBINE_INTERLEAVE; |
| 1142 |
else |
| 1143 |
error("combine_attribute_bad_value", value); |
| 1144 |
} |
| 1145 |
else |
| 1146 |
super.setOtherAttribute(name, value); |
| 1147 |
} |
| 1148 |
|
| 1149 |
Pattern buildPattern(List<Pattern> patterns, Location loc, AnnotationsImpl anno) throws SAXException { |
| 1150 |
return super.buildPattern(patterns, loc, null); |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
class DefineState extends DefinitionState { |
| 1155 |
String name; |
| 1156 |
|
| 1157 |
DefineState(GrammarSection<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> section) { |
| 1158 |
super(section); |
| 1159 |
} |
| 1160 |
|
| 1161 |
State create() { |
| 1162 |
return new DefineState(null); |
| 1163 |
} |
| 1164 |
|
| 1165 |
void setName(String name) throws SAXException { |
| 1166 |
this.name = checkNCName(name); |
| 1167 |
} |
| 1168 |
|
| 1169 |
void endAttributes() throws SAXException { |
| 1170 |
if (name == null) |
| 1171 |
error("missing_name_attribute"); |
| 1172 |
} |
| 1173 |
|
| 1174 |
void sendPatternToParent(Pattern p) { |
| 1175 |
if (name != null) |
| 1176 |
section.define(name, combine, p, startLocation, annotations); |
| 1177 |
} |
| 1178 |
|
| 1179 |
} |
| 1180 |
|
| 1181 |
class StartState extends DefinitionState { |
| 1182 |
|
| 1183 |
StartState(GrammarSection<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> section) { |
| 1184 |
super(section); |
| 1185 |
} |
| 1186 |
|
| 1187 |
State create() { |
| 1188 |
return new StartState(null); |
| 1189 |
} |
| 1190 |
|
| 1191 |
void sendPatternToParent(Pattern p) { |
| 1192 |
section.define(GrammarSection.START, combine, p, startLocation, annotations); |
| 1193 |
} |
| 1194 |
|
| 1195 |
State createChildState(String localName) throws SAXException { |
| 1196 |
State tem = super.createChildState(localName); |
| 1197 |
if (tem != null && childPatterns.size() != 0) |
| 1198 |
error("start_multi_pattern"); |
| 1199 |
return tem; |
| 1200 |
} |
| 1201 |
|
| 1202 |
} |
| 1203 |
|
| 1204 |
abstract class NameClassContainerState extends State { |
| 1205 |
State createChildState(String localName) throws SAXException { |
| 1206 |
State state = nameClassMap.get(localName); |
| 1207 |
if (state == null) { |
| 1208 |
error("expected_name_class", localName); |
| 1209 |
return null; |
| 1210 |
} |
| 1211 |
return state.create(); |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
class NameClassChildState extends NameClassContainerState { |
| 1216 |
final State prevState; |
| 1217 |
final NameClassRef<NameClass> nameClassRef; |
| 1218 |
|
| 1219 |
State create() { |
| 1220 |
return null; |
| 1221 |
} |
| 1222 |
|
| 1223 |
NameClassChildState(State prevState, NameClassRef<NameClass> nameClassRef) { |
| 1224 |
this.prevState = prevState; |
| 1225 |
this.nameClassRef = nameClassRef; |
| 1226 |
setParent(prevState.parent); |
| 1227 |
this.ns = prevState.ns; |
| 1228 |
} |
| 1229 |
|
| 1230 |
void endNameClassChild(NameClass nameClass) { |
| 1231 |
nameClassRef.setNameClass(nameClass); |
| 1232 |
prevState.set(); |
| 1233 |
} |
| 1234 |
|
| 1235 |
void endForeignChild(ElementAnnotation ea) { |
| 1236 |
prevState.endForeignChild(ea); |
| 1237 |
} |
| 1238 |
|
| 1239 |
void end() throws SAXException { |
| 1240 |
nameClassRef.setNameClass(schemaBuilder.makeErrorNameClass()); |
| 1241 |
error("missing_name_class"); |
| 1242 |
prevState.set(); |
| 1243 |
prevState.end(); |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
abstract class NameClassBaseState extends State { |
| 1248 |
|
| 1249 |
abstract NameClass makeNameClass() throws SAXException; |
| 1250 |
|
| 1251 |
void end() throws SAXException { |
| 1252 |
parent.endNameClassChild(makeNameClass()); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
class NameState extends NameClassBaseState { |
| 1257 |
final StringBuffer buf = new StringBuffer(); |
| 1258 |
|
| 1259 |
State createChildState(String localName) throws SAXException { |
| 1260 |
error("expected_name", localName); |
| 1261 |
return null; |
| 1262 |
} |
| 1263 |
|
| 1264 |
State create() { |
| 1265 |
return new NameState(); |
| 1266 |
} |
| 1267 |
|
| 1268 |
public void characters(char[] ch, int start, int len) { |
| 1269 |
buf.append(ch, start, len); |
| 1270 |
} |
| 1271 |
|
| 1272 |
void checkForeignElement() throws SAXException { |
| 1273 |
error("name_contains_foreign_element"); |
| 1274 |
} |
| 1275 |
|
| 1276 |
NameClass makeNameClass() throws SAXException { |
| 1277 |
mergeLeadingComments(); |
| 1278 |
return expandName(buf.toString().trim(), getNs(), annotations); |
| 1279 |
} |
| 1280 |
|
| 1281 |
} |
| 1282 |
|
| 1283 |
private static final int PATTERN_CONTEXT = 0; |
| 1284 |
private static final int ANY_NAME_CONTEXT = 1; |
| 1285 |
private static final int NS_NAME_CONTEXT = 2; |
| 1286 |
|
| 1287 |
class AnyNameState extends NameClassBaseState { |
| 1288 |
NameClass except = null; |
| 1289 |
|
| 1290 |
State create() { |
| 1291 |
return new AnyNameState(); |
| 1292 |
} |
| 1293 |
|
| 1294 |
State createChildState(String localName) throws SAXException { |
| 1295 |
if (localName.equals("except")) { |
| 1296 |
if (except != null) |
| 1297 |
error("multiple_except"); |
| 1298 |
return new NameClassChoiceState(getContext()); |
| 1299 |
} |
| 1300 |
error("expected_except", localName); |
| 1301 |
return null; |
| 1302 |
} |
| 1303 |
|
| 1304 |
int getContext() { |
| 1305 |
return ANY_NAME_CONTEXT; |
| 1306 |
} |
| 1307 |
|
| 1308 |
NameClass makeNameClass() { |
| 1309 |
if (except == null) |
| 1310 |
return makeNameClassNoExcept(); |
| 1311 |
else |
| 1312 |
return makeNameClassExcept(except); |
| 1313 |
} |
| 1314 |
|
| 1315 |
NameClass makeNameClassNoExcept() { |
| 1316 |
return schemaBuilder.makeAnyName(startLocation, annotations); |
| 1317 |
} |
| 1318 |
|
| 1319 |
NameClass makeNameClassExcept(NameClass except) { |
| 1320 |
return schemaBuilder.makeAnyName(except, startLocation, annotations); |
| 1321 |
} |
| 1322 |
|
| 1323 |
void endNameClassChild(NameClass nameClass) { |
| 1324 |
except = nameClass; |
| 1325 |
} |
| 1326 |
|
| 1327 |
} |
| 1328 |
|
| 1329 |
class NsNameState extends AnyNameState { |
| 1330 |
State create() { |
| 1331 |
return new NsNameState(); |
| 1332 |
} |
| 1333 |
|
| 1334 |
NameClass makeNameClassNoExcept() { |
| 1335 |
return schemaBuilder.makeNsName(getNs(), null, null); |
| 1336 |
} |
| 1337 |
|
| 1338 |
NameClass makeNameClassExcept(NameClass except) { |
| 1339 |
return schemaBuilder.makeNsName(getNs(), except, null, null); |
| 1340 |
} |
| 1341 |
|
| 1342 |
int getContext() { |
| 1343 |
return NS_NAME_CONTEXT; |
| 1344 |
} |
| 1345 |
|
| 1346 |
} |
| 1347 |
|
| 1348 |
class NameClassChoiceState extends NameClassContainerState { |
| 1349 |
private List<NameClass> nameClasses = new ArrayList<NameClass>(); |
| 1350 |
private int context; |
| 1351 |
|
| 1352 |
NameClassChoiceState() { |
| 1353 |
this.context = PATTERN_CONTEXT; |
| 1354 |
} |
| 1355 |
|
| 1356 |
NameClassChoiceState(int context) { |
| 1357 |
this.context = context; |
| 1358 |
} |
| 1359 |
|
| 1360 |
NameClassChoiceState toNameClassChoiceState() { |
| 1361 |
return this; |
| 1362 |
} |
| 1363 |
|
| 1364 |
void setParent(State parent) { |
| 1365 |
super.setParent(parent); |
| 1366 |
NameClassChoiceState parentChoice = parent.toNameClassChoiceState(); |
| 1367 |
if (parentChoice != null) |
| 1368 |
this.context = parentChoice.context; |
| 1369 |
} |
| 1370 |
|
| 1371 |
State create() { |
| 1372 |
return new NameClassChoiceState(); |
| 1373 |
} |
| 1374 |
|
| 1375 |
State createChildState(String localName) throws SAXException { |
| 1376 |
if (localName.equals("anyName")) { |
| 1377 |
if (context >= ANY_NAME_CONTEXT) { |
| 1378 |
error(context == ANY_NAME_CONTEXT |
| 1379 |
? "any_name_except_contains_any_name" |
| 1380 |
: "ns_name_except_contains_any_name"); |
| 1381 |
return null; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
else if (localName.equals("nsName")) { |
| 1385 |
if (context == NS_NAME_CONTEXT) { |
| 1386 |
error("ns_name_except_contains_ns_name"); |
| 1387 |
return null; |
| 1388 |
} |
| 1389 |
} |
| 1390 |
return super.createChildState(localName); |
| 1391 |
} |
| 1392 |
|
| 1393 |
void endNameClassChild(NameClass nc) { |
| 1394 |
nameClasses.add(nc); |
| 1395 |
} |
| 1396 |
|
| 1397 |
void endForeignChild(ElementAnnotation ea) { |
| 1398 |
int nNameClasses = nameClasses.size(); |
| 1399 |
if (nNameClasses == 0) |
| 1400 |
super.endForeignChild(ea); |
| 1401 |
else |
| 1402 |
nameClasses.set(nNameClasses - 1, |
| 1403 |
schemaBuilder.annotateAfterNameClass(nameClasses.get(nNameClasses - 1), ea)); |
| 1404 |
} |
| 1405 |
|
| 1406 |
void end() throws SAXException { |
| 1407 |
if (nameClasses.size() == 0) { |
| 1408 |
error("missing_name_class"); |
| 1409 |
parent.endNameClassChild(schemaBuilder.makeErrorNameClass()); |
| 1410 |
return; |
| 1411 |
} |
| 1412 |
if (comments != null) { |
| 1413 |
int nNameClasses = nameClasses.size(); |
| 1414 |
nameClasses.set(nNameClasses - 1, |
| 1415 |
schemaBuilder.commentAfterNameClass(nameClasses.get(nNameClasses - 1), comments)); |
| 1416 |
comments = null; |
| 1417 |
} |
| 1418 |
parent.endNameClassChild(schemaBuilder.makeNameClassChoice(nameClasses, startLocation, annotations)); |
| 1419 |
} |
| 1420 |
} |
| 1421 |
|
| 1422 |
private void initPatternTable() { |
| 1423 |
patternMap = new HashMap<String, State>(); |
| 1424 |
patternMap.put("zeroOrMore", new ZeroOrMoreState()); |
| 1425 |
patternMap.put("oneOrMore", new OneOrMoreState()); |
| 1426 |
patternMap.put("optional", new OptionalState()); |
| 1427 |
patternMap.put("list", new ListState()); |
| 1428 |
patternMap.put("choice", new ChoiceState()); |
| 1429 |
patternMap.put("interleave", new InterleaveState()); |
| 1430 |
patternMap.put("group", new GroupState()); |
| 1431 |
patternMap.put("mixed", new MixedState()); |
| 1432 |
patternMap.put("element", new ElementState()); |
| 1433 |
patternMap.put("attribute", new AttributeState()); |
| 1434 |
patternMap.put("empty", new EmptyState()); |
| 1435 |
patternMap.put("text", new TextState()); |
| 1436 |
patternMap.put("value", new ValueState()); |
| 1437 |
patternMap.put("data", new DataState()); |
| 1438 |
patternMap.put("notAllowed", new NotAllowedState()); |
| 1439 |
patternMap.put("grammar", new GrammarState()); |
| 1440 |
patternMap.put("ref", new RefState()); |
| 1441 |
patternMap.put("parentRef", new ParentRefState()); |
| 1442 |
patternMap.put("externalRef", new ExternalRefState()); |
| 1443 |
} |
| 1444 |
|
| 1445 |
private void initNameClassTable() { |
| 1446 |
nameClassMap = new HashMap<String, State>(); |
| 1447 |
nameClassMap.put("name", new NameState()); |
| 1448 |
nameClassMap.put("anyName", new AnyNameState()); |
| 1449 |
nameClassMap.put("nsName", new NsNameState()); |
| 1450 |
nameClassMap.put("choice", new NameClassChoiceState()); |
| 1451 |
} |
| 1452 |
|
| 1453 |
public Pattern getParsedPattern() throws IllegalSchemaException { |
| 1454 |
if (hadError) |
| 1455 |
throw new IllegalSchemaException(); |
| 1456 |
return startPattern; |
| 1457 |
} |
| 1458 |
|
| 1459 |
private void error(String key) throws SAXException { |
| 1460 |
error(key, locator); |
| 1461 |
} |
| 1462 |
|
| 1463 |
private void error(String key, String arg) throws SAXException { |
| 1464 |
error(key, arg, locator); |
| 1465 |
} |
| 1466 |
|
| 1467 |
void error(String key, String arg1, String arg2) throws SAXException { |
| 1468 |
error(key, arg1, arg2, locator); |
| 1469 |
} |
| 1470 |
|
| 1471 |
private void error(String key, Locator loc) throws SAXException { |
| 1472 |
error(new SAXParseException(localizer.message(key), loc)); |
| 1473 |
} |
| 1474 |
|
| 1475 |
private void error(String key, String arg, Locator loc) throws SAXException { |
| 1476 |
error(new SAXParseException(localizer.message(key, arg), loc)); |
| 1477 |
} |
| 1478 |
|
| 1479 |
private void error(String key, String arg1, String arg2, Locator loc) |
| 1480 |
throws SAXException { |
| 1481 |
error(new SAXParseException(localizer.message(key, arg1, arg2), loc)); |
| 1482 |
} |
| 1483 |
|
| 1484 |
private void error(SAXParseException e) throws SAXException { |
| 1485 |
hadError = true; |
| 1486 |
if (eh != null) |
| 1487 |
eh.error(e); |
| 1488 |
} |
| 1489 |
|
| 1490 |
void warning(String key) throws SAXException { |
| 1491 |
warning(key, locator); |
| 1492 |
} |
| 1493 |
|
| 1494 |
private void warning(String key, String arg) throws SAXException { |
| 1495 |
warning(key, arg, locator); |
| 1496 |
} |
| 1497 |
|
| 1498 |
private void warning(String key, String arg1, String arg2) throws SAXException { |
| 1499 |
warning(key, arg1, arg2, locator); |
| 1500 |
} |
| 1501 |
|
| 1502 |
private void warning(String key, Locator loc) throws SAXException { |
| 1503 |
warning(new SAXParseException(localizer.message(key), loc)); |
| 1504 |
} |
| 1505 |
|
| 1506 |
private void warning(String key, String arg, Locator loc) throws SAXException { |
| 1507 |
warning(new SAXParseException(localizer.message(key, arg), loc)); |
| 1508 |
} |
| 1509 |
|
| 1510 |
private void warning(String key, String arg1, String arg2, Locator loc) |
| 1511 |
throws SAXException { |
| 1512 |
warning(new SAXParseException(localizer.message(key, arg1, arg2), loc)); |
| 1513 |
} |
| 1514 |
|
| 1515 |
private void warning(SAXParseException e) throws SAXException { |
| 1516 |
if (eh != null) |
| 1517 |
eh.warning(e); |
| 1518 |
} |
| 1519 |
|
| 1520 |
SchemaParser(XMLReader xr, |
| 1521 |
ErrorHandler eh, |
| 1522 |
SchemaBuilder<Pattern, NameClass, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> schemaBuilder, |
| 1523 |
IncludedGrammar<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> grammar, |
| 1524 |
Scope<Pattern, Location, ElementAnnotation, CommentListImpl, AnnotationsImpl> scope) throws SAXException { |
| 1525 |
this.xr = xr; |
| 1526 |
this.eh = eh; |
| 1527 |
this.schemaBuilder = schemaBuilder; |
| 1528 |
if (eh != null) |
| 1529 |
xr.setErrorHandler(eh); |
| 1530 |
xr.setDTDHandler(context); |
| 1531 |
if (schemaBuilder.usesComments()) { |
| 1532 |
try { |
| 1533 |
xr.setProperty("http://xml.org/sax/properties/lexical-handler", new LexicalHandlerImpl()); |
| 1534 |
} |
| 1535 |
catch (SAXNotRecognizedException e) { |
| 1536 |
warning("no_comment_support", xr.getClass().getName()); |
| 1537 |
} |
| 1538 |
catch (SAXNotSupportedException e) { |
| 1539 |
warning("no_comment_support", xr.getClass().getName()); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
initPatternTable(); |
| 1543 |
initNameClassTable(); |
| 1544 |
new RootState(grammar, scope, SchemaBuilder.INHERIT_NS).set(); |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
private Context getContext() { |
| 1549 |
return context; |
| 1550 |
} |
| 1551 |
|
| 1552 |
class LexicalHandlerImpl extends AbstractLexicalHandler { |
| 1553 |
private boolean inDtd = false; |
| 1554 |
|
| 1555 |
public void startDTD(String s, String s1, String s2) throws SAXException { |
| 1556 |
inDtd = true; |
| 1557 |
} |
| 1558 |
|
| 1559 |
public void endDTD() throws SAXException { |
| 1560 |
inDtd = false; |
| 1561 |
} |
| 1562 |
|
| 1563 |
public void comment(char[] chars, int start, int length) throws SAXException { |
| 1564 |
if (!inDtd) |
| 1565 |
((CommentHandler)xr.getContentHandler()).comment(new String(chars, start, length)); |
| 1566 |
} |
| 1567 |
} |
| 1568 |
|
| 1569 |
private NameClass expandName(String name, String ns, AnnotationsImpl anno) throws SAXException { |
| 1570 |
int ic = name.indexOf(':'); |
| 1571 |
if (ic == -1) |
| 1572 |
return schemaBuilder.makeName(ns, checkNCName(name), null, null, anno); |
| 1573 |
String prefix = checkNCName(name.substring(0, ic)); |
| 1574 |
String localName = checkNCName(name.substring(ic + 1)); |
| 1575 |
for (PrefixMapping tem = context.prefixMapping; tem != null; tem = tem.next) |
| 1576 |
if (tem.prefix.equals(prefix)) |
| 1577 |
return schemaBuilder.makeName(tem.uri, localName, prefix, null, anno); |
| 1578 |
error("undefined_prefix", prefix); |
| 1579 |
return schemaBuilder.makeName("", localName, null, null, anno); |
| 1580 |
} |
| 1581 |
|
| 1582 |
private String findPrefix(String qName, String uri) { |
| 1583 |
String prefix = null; |
| 1584 |
if (qName == null || qName.equals("")) { |
| 1585 |
for (PrefixMapping p = context.prefixMapping; p != null; p = p.next) |
| 1586 |
if (p.uri.equals(uri)) { |
| 1587 |
prefix = p.prefix; |
| 1588 |
break; |
| 1589 |
} |
| 1590 |
} |
| 1591 |
else { |
| 1592 |
int off = qName.indexOf(':'); |
| 1593 |
if (off > 0) |
| 1594 |
prefix = qName.substring(0, off); |
| 1595 |
} |
| 1596 |
return prefix; |
| 1597 |
} |
| 1598 |
private String checkNCName(String str) throws SAXException { |
| 1599 |
if (!Naming.isNcname(str)) |
| 1600 |
error("invalid_ncname", str); |
| 1601 |
return str; |
| 1602 |
} |
| 1603 |
|
| 1604 |
private Location makeLocation() { |
| 1605 |
if (locator == null) |
| 1606 |
return null; |
| 1607 |
return schemaBuilder.makeLocation(locator.getSystemId(), |
| 1608 |
locator.getLineNumber(), |
| 1609 |
locator.getColumnNumber()); |
| 1610 |
} |
| 1611 |
|
| 1612 |
private void checkUriNoFragmentId(String s) throws SAXException { |
| 1613 |
checkUri(s); |
| 1614 |
if (Uri.hasFragmentId(s)) |
| 1615 |
error("href_fragment_id"); |
| 1616 |
} |
| 1617 |
|
| 1618 |
private void checkUri(String s) throws SAXException { |
| 1619 |
if (!Uri.isValid(s)) |
| 1620 |
error("invalid_uri", s); |
| 1621 |
} |
| 1622 |
} |