001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.rngom.binary;
027:
028: import com.sun.xml.internal.rngom.nc.NameClass;
029: import org.relaxng.datatype.Datatype;
030: import org.xml.sax.Locator;
031:
032: public class SchemaPatternBuilder extends PatternBuilder {
033: private boolean idTypes;
034: private final Pattern unexpandedNotAllowed = new NotAllowedPattern() {
035: boolean isNotAllowed() {
036: return false;
037: }
038:
039: Pattern expand(SchemaPatternBuilder b) {
040: return b.makeNotAllowed();
041: }
042: };
043:
044: private final TextPattern text = new TextPattern();
045: private final PatternInterner schemaInterner = new PatternInterner();
046:
047: public SchemaPatternBuilder() {
048: }
049:
050: public boolean hasIdTypes() {
051: return idTypes;
052: }
053:
054: Pattern makeElement(NameClass nameClass, Pattern content,
055: Locator loc) {
056: Pattern p = new ElementPattern(nameClass, content, loc);
057: return schemaInterner.intern(p);
058: }
059:
060: Pattern makeAttribute(NameClass nameClass, Pattern value,
061: Locator loc) {
062: if (value == notAllowed)
063: return value;
064: Pattern p = new AttributePattern(nameClass, value, loc);
065: return schemaInterner.intern(p);
066: }
067:
068: Pattern makeData(Datatype dt) {
069: noteDatatype(dt);
070: Pattern p = new DataPattern(dt);
071: return schemaInterner.intern(p);
072: }
073:
074: Pattern makeDataExcept(Datatype dt, Pattern except, Locator loc) {
075: noteDatatype(dt);
076: Pattern p = new DataExceptPattern(dt, except, loc);
077: return schemaInterner.intern(p);
078: }
079:
080: Pattern makeValue(Datatype dt, Object obj) {
081: noteDatatype(dt);
082: Pattern p = new ValuePattern(dt, obj);
083: return schemaInterner.intern(p);
084: }
085:
086: Pattern makeText() {
087: return text;
088: }
089:
090: Pattern makeOneOrMore(Pattern p) {
091: if (p == text)
092: return p;
093: return super .makeOneOrMore(p);
094: }
095:
096: Pattern makeUnexpandedNotAllowed() {
097: return unexpandedNotAllowed;
098: }
099:
100: Pattern makeError() {
101: Pattern p = new ErrorPattern();
102: return schemaInterner.intern(p);
103: }
104:
105: Pattern makeChoice(Pattern p1, Pattern p2) {
106: if (p1 == notAllowed || p1 == p2)
107: return p2;
108: if (p2 == notAllowed)
109: return p1;
110: return super .makeChoice(p1, p2);
111: }
112:
113: Pattern makeList(Pattern p, Locator loc) {
114: if (p == notAllowed)
115: return p;
116: Pattern p1 = new ListPattern(p, loc);
117: return schemaInterner.intern(p1);
118: }
119:
120: Pattern makeMixed(Pattern p) {
121: return makeInterleave(text, p);
122: }
123:
124: private void noteDatatype(Datatype dt) {
125: if (dt.getIdType() != Datatype.ID_TYPE_NULL)
126: idTypes = true;
127: }
128: }
|