001: package org.kohsuke.rngom.binary;
002:
003: import org.kohsuke.rngom.nc.NameClass;
004: import org.relaxng.datatype.Datatype;
005: import org.xml.sax.Locator;
006:
007: public class SchemaPatternBuilder extends PatternBuilder {
008: private boolean idTypes;
009: private final Pattern unexpandedNotAllowed = new NotAllowedPattern() {
010: boolean isNotAllowed() {
011: return false;
012: }
013:
014: Pattern expand(SchemaPatternBuilder b) {
015: return b.makeNotAllowed();
016: }
017: };
018:
019: private final TextPattern text = new TextPattern();
020: private final PatternInterner schemaInterner = new PatternInterner();
021:
022: public SchemaPatternBuilder() {
023: }
024:
025: public boolean hasIdTypes() {
026: return idTypes;
027: }
028:
029: Pattern makeElement(NameClass nameClass, Pattern content,
030: Locator loc) {
031: Pattern p = new ElementPattern(nameClass, content, loc);
032: return schemaInterner.intern(p);
033: }
034:
035: Pattern makeAttribute(NameClass nameClass, Pattern value,
036: Locator loc) {
037: if (value == notAllowed)
038: return value;
039: Pattern p = new AttributePattern(nameClass, value, loc);
040: return schemaInterner.intern(p);
041: }
042:
043: Pattern makeData(Datatype dt) {
044: noteDatatype(dt);
045: Pattern p = new DataPattern(dt);
046: return schemaInterner.intern(p);
047: }
048:
049: Pattern makeDataExcept(Datatype dt, Pattern except, Locator loc) {
050: noteDatatype(dt);
051: Pattern p = new DataExceptPattern(dt, except, loc);
052: return schemaInterner.intern(p);
053: }
054:
055: Pattern makeValue(Datatype dt, Object obj) {
056: noteDatatype(dt);
057: Pattern p = new ValuePattern(dt, obj);
058: return schemaInterner.intern(p);
059: }
060:
061: Pattern makeText() {
062: return text;
063: }
064:
065: Pattern makeOneOrMore(Pattern p) {
066: if (p == text)
067: return p;
068: return super .makeOneOrMore(p);
069: }
070:
071: Pattern makeUnexpandedNotAllowed() {
072: return unexpandedNotAllowed;
073: }
074:
075: Pattern makeError() {
076: Pattern p = new ErrorPattern();
077: return schemaInterner.intern(p);
078: }
079:
080: Pattern makeChoice(Pattern p1, Pattern p2) {
081: if (p1 == notAllowed || p1 == p2)
082: return p2;
083: if (p2 == notAllowed)
084: return p1;
085: return super .makeChoice(p1, p2);
086: }
087:
088: Pattern makeList(Pattern p, Locator loc) {
089: if (p == notAllowed)
090: return p;
091: Pattern p1 = new ListPattern(p, loc);
092: return schemaInterner.intern(p1);
093: }
094:
095: Pattern makeMixed(Pattern p) {
096: return makeInterleave(text, p);
097: }
098:
099: private void noteDatatype(Datatype dt) {
100: if (dt.getIdType() != Datatype.ID_TYPE_NULL)
101: idTypes = true;
102: }
103: }
|