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 com.sun.xml.internal.rngom.nc.SimpleNameClass;
032: import org.xml.sax.Locator;
033: import org.xml.sax.SAXException;
034:
035: public final class AttributePattern extends Pattern {
036: private NameClass nameClass;
037: private Pattern p;
038: private Locator loc;
039:
040: AttributePattern(NameClass nameClass, Pattern value, Locator loc) {
041: super (false, EMPTY_CONTENT_TYPE, combineHashCode(
042: ATTRIBUTE_HASH_CODE, nameClass.hashCode(), value
043: .hashCode()));
044: this .nameClass = nameClass;
045: this .p = value;
046: this .loc = loc;
047: }
048:
049: Pattern expand(SchemaPatternBuilder b) {
050: Pattern ep = p.expand(b);
051: if (ep != p)
052: return b.makeAttribute(nameClass, ep, loc);
053: else
054: return this ;
055: }
056:
057: void checkRestrictions(int context, DuplicateAttributeDetector dad,
058: Alphabet alpha) throws RestrictionViolationException {
059: switch (context) {
060: case START_CONTEXT:
061: throw new RestrictionViolationException(
062: "start_contains_attribute");
063: case ELEMENT_CONTEXT:
064: if (nameClass.isOpen())
065: throw new RestrictionViolationException(
066: "open_name_class_not_repeated");
067: break;
068: case ELEMENT_REPEAT_GROUP_CONTEXT:
069: throw new RestrictionViolationException(
070: "one_or_more_contains_group_contains_attribute");
071: case ELEMENT_REPEAT_INTERLEAVE_CONTEXT:
072: throw new RestrictionViolationException(
073: "one_or_more_contains_interleave_contains_attribute");
074: case LIST_CONTEXT:
075: throw new RestrictionViolationException(
076: "list_contains_attribute");
077: case ATTRIBUTE_CONTEXT:
078: throw new RestrictionViolationException(
079: "attribute_contains_attribute");
080: case DATA_EXCEPT_CONTEXT:
081: throw new RestrictionViolationException(
082: "data_except_contains_attribute");
083: }
084: if (!dad.addAttribute(nameClass)) {
085: if (nameClass instanceof SimpleNameClass)
086: throw new RestrictionViolationException(
087: "duplicate_attribute_detail",
088: ((SimpleNameClass) nameClass).name);
089: else
090: throw new RestrictionViolationException(
091: "duplicate_attribute");
092: }
093: try {
094: p.checkRestrictions(ATTRIBUTE_CONTEXT, null, null);
095: } catch (RestrictionViolationException e) {
096: e.maybeSetLocator(loc);
097: throw e;
098: }
099: }
100:
101: boolean samePattern(Pattern other) {
102: if (!(other instanceof AttributePattern))
103: return false;
104: AttributePattern ap = (AttributePattern) other;
105: return nameClass.equals(ap.nameClass) && p == ap.p;
106: }
107:
108: void checkRecursion(int depth) throws SAXException {
109: p.checkRecursion(depth);
110: }
111:
112: public void accept(PatternVisitor visitor) {
113: visitor.visitAttribute(nameClass, p);
114: }
115:
116: public Object apply(PatternFunction f) {
117: return f.caseAttribute(this );
118: }
119:
120: public Pattern getContent() {
121: return p;
122: }
123:
124: public NameClass getNameClass() {
125: return nameClass;
126: }
127:
128: public Locator getLocator() {
129: return loc;
130: }
131: }
|