001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.relaxng.program;
031:
032: import com.caucho.relaxng.RelaxException;
033: import com.caucho.util.L10N;
034: import com.caucho.xml.QName;
035:
036: import java.util.HashSet;
037:
038: /**
039: * Generates programs from patterns.
040: */
041: public class AttributeItem extends Item {
042: protected final static L10N L = new L10N(AttributeItem.class);
043:
044: private final NameClassItem _name;
045:
046: public AttributeItem(NameClassItem name) {
047: _name = name;
048: }
049:
050: public NameClassItem getNameClassItem() {
051: return _name;
052: }
053:
054: /**
055: * Returns the first set, the set of element names possible.
056: */
057: public void firstSet(HashSet<QName> set) {
058: }
059:
060: /**
061: * The attribute does not allow the empty match.
062: */
063: public boolean allowEmpty() {
064: return false;
065: }
066:
067: /**
068: * Returns the attribute set, the set of attribute names possible.
069: */
070: public void attributeSet(HashSet<QName> set) {
071: _name.firstSet(set);
072: }
073:
074: /**
075: * Returns true if the attribute is allowed.
076: *
077: * @param name the name of the attribute
078: */
079: public boolean allowAttribute(QName name, String value)
080: throws RelaxException {
081: return _name.matches(name);
082: }
083:
084: /**
085: * Returns the next item on the match.
086: *
087: * @param name the name of the attribute
088: * @param value the value of the attribute
089: */
090: public Item setAttribute(QName name, String value)
091: throws RelaxException {
092: if (_name.matches(name))
093: return null;
094: else
095: return this ;
096: }
097:
098: /**
099: * Returns the item after the attribute ends. In this case,
100: * return null since this attribute is still required.
101: */
102: public Item attributeEnd() {
103: return null;
104: }
105:
106: /**
107: * Returns the pretty printed syntax.
108: */
109: public String toSyntaxDescription(int depth) {
110: return _name.toSyntaxDescription("@");
111: }
112:
113: /**
114: * Returns true for an element with simple syntax.
115: */
116: protected boolean isSimpleSyntax() {
117: return true;
118: }
119:
120: /**
121: * Returns the hash code for the empty item.
122: */
123: public int hashCode() {
124: return 27 + _name.hashCode();
125: }
126:
127: /**
128: * Returns true if the object is an empty item.
129: */
130: public boolean equals(Object o) {
131: if (this == o)
132: return true;
133:
134: if (!(o instanceof AttributeItem))
135: return false;
136:
137: AttributeItem attr = (AttributeItem) o;
138:
139: return _name.equals(attr._name);
140: }
141:
142: public String toString() {
143: return "AttributeItem[" + _name + "]";
144: }
145: }
|