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: public class PatternBuilder {
029: private final EmptyPattern empty;
030: protected final NotAllowedPattern notAllowed;
031: protected final PatternInterner interner;
032:
033: public PatternBuilder() {
034: empty = new EmptyPattern();
035: notAllowed = new NotAllowedPattern();
036: interner = new PatternInterner();
037: }
038:
039: public PatternBuilder(PatternBuilder parent) {
040: empty = parent.empty;
041: notAllowed = parent.notAllowed;
042: interner = new PatternInterner(parent.interner);
043: }
044:
045: Pattern makeEmpty() {
046: return empty;
047: }
048:
049: Pattern makeNotAllowed() {
050: return notAllowed;
051: }
052:
053: Pattern makeGroup(Pattern p1, Pattern p2) {
054: if (p1 == empty)
055: return p2;
056: if (p2 == empty)
057: return p1;
058: if (p1 == notAllowed || p2 == notAllowed)
059: return notAllowed;
060: if (false && p1 instanceof GroupPattern) {
061: GroupPattern sp = (GroupPattern) p1;
062: return makeGroup(sp.p1, makeGroup(sp.p2, p2));
063: }
064: Pattern p = new GroupPattern(p1, p2);
065: return interner.intern(p);
066: }
067:
068: Pattern makeInterleave(Pattern p1, Pattern p2) {
069: if (p1 == empty)
070: return p2;
071: if (p2 == empty)
072: return p1;
073: if (p1 == notAllowed || p2 == notAllowed)
074: return notAllowed;
075: if (false && p1 instanceof InterleavePattern) {
076: InterleavePattern ip = (InterleavePattern) p1;
077: return makeInterleave(ip.p1, makeInterleave(ip.p2, p2));
078: }
079: if (false) {
080: if (p2 instanceof InterleavePattern) {
081: InterleavePattern ip = (InterleavePattern) p2;
082: if (p1.hashCode() > ip.p1.hashCode())
083: return makeInterleave(ip.p1, makeInterleave(p1,
084: ip.p2));
085: } else if (p1.hashCode() > p2.hashCode())
086: return makeInterleave(p2, p1);
087: }
088: Pattern p = new InterleavePattern(p1, p2);
089: return interner.intern(p);
090: }
091:
092: Pattern makeChoice(Pattern p1, Pattern p2) {
093: if (p1 == empty && p2.isNullable())
094: return p2;
095: if (p2 == empty && p1.isNullable())
096: return p1;
097: Pattern p = new ChoicePattern(p1, p2);
098: return interner.intern(p);
099: }
100:
101: Pattern makeOneOrMore(Pattern p) {
102: if (p == empty || p == notAllowed
103: || p instanceof OneOrMorePattern)
104: return p;
105: Pattern p1 = new OneOrMorePattern(p);
106: return interner.intern(p1);
107: }
108:
109: Pattern makeOptional(Pattern p) {
110: return makeChoice(p, empty);
111: }
112:
113: Pattern makeZeroOrMore(Pattern p) {
114: return makeOptional(makeOneOrMore(p));
115: }
116: }
|