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.binary.visitor.PatternFunction;
029: import com.sun.xml.internal.rngom.binary.visitor.PatternVisitor;
030: import com.sun.xml.internal.rngom.nc.NameClass;
031: import org.xml.sax.Locator;
032: import org.xml.sax.SAXException;
033:
034: public final class ElementPattern extends Pattern {
035: private Pattern p;
036: private NameClass origNameClass;
037: private NameClass nameClass;
038: private boolean expanded = false;
039: private boolean checkedRestrictions = false;
040: private Locator loc;
041:
042: ElementPattern(NameClass nameClass, Pattern p, Locator loc) {
043: super (false, ELEMENT_CONTENT_TYPE, combineHashCode(
044: ELEMENT_HASH_CODE, nameClass.hashCode(), p.hashCode()));
045: this .nameClass = nameClass;
046: this .origNameClass = nameClass;
047: this .p = p;
048: this .loc = loc;
049: }
050:
051: void checkRestrictions(int context, DuplicateAttributeDetector dad,
052: Alphabet alpha) throws RestrictionViolationException {
053: if (alpha != null)
054: alpha.addElement(origNameClass);
055: if (checkedRestrictions)
056: return;
057: switch (context) {
058: case DATA_EXCEPT_CONTEXT:
059: throw new RestrictionViolationException(
060: "data_except_contains_element");
061: case LIST_CONTEXT:
062: throw new RestrictionViolationException(
063: "list_contains_element");
064: case ATTRIBUTE_CONTEXT:
065: throw new RestrictionViolationException(
066: "attribute_contains_element");
067: }
068: checkedRestrictions = true;
069: try {
070: p.checkRestrictions(ELEMENT_CONTEXT,
071: new DuplicateAttributeDetector(), null);
072: } catch (RestrictionViolationException e) {
073: checkedRestrictions = false;
074: e.maybeSetLocator(loc);
075: throw e;
076: }
077: }
078:
079: Pattern expand(SchemaPatternBuilder b) {
080: if (!expanded) {
081: expanded = true;
082: p = p.expand(b);
083: if (p.isNotAllowed())
084: nameClass = NameClass.NULL;
085: }
086: return this ;
087: }
088:
089: boolean samePattern(Pattern other) {
090: if (!(other instanceof ElementPattern))
091: return false;
092: ElementPattern ep = (ElementPattern) other;
093: return nameClass.equals(ep.nameClass) && p == ep.p;
094: }
095:
096: void checkRecursion(int depth) throws SAXException {
097: p.checkRecursion(depth + 1);
098: }
099:
100: public void accept(PatternVisitor visitor) {
101: visitor.visitElement(nameClass, p);
102: }
103:
104: public Object apply(PatternFunction f) {
105: return f.caseElement(this );
106: }
107:
108: void setContent(Pattern p) {
109: this .p = p;
110: }
111:
112: public Pattern getContent() {
113: return p;
114: }
115:
116: public NameClass getNameClass() {
117: return nameClass;
118: }
119:
120: public Locator getLocator() {
121: return loc;
122: }
123: }
|