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.pattern;
031:
032: import com.caucho.relaxng.RelaxException;
033: import com.caucho.relaxng.program.AttributeItem;
034: import com.caucho.relaxng.program.Item;
035:
036: /**
037: * Relax attribute pattern
038: */
039: public class AttributePattern extends Pattern {
040:
041: private NameClassPattern _name;
042: private GroupPattern _children = new GroupPattern();
043:
044: private Item _item;
045:
046: /**
047: * Creates a new attribute pattern.
048: */
049: public AttributePattern() {
050: }
051:
052: /**
053: * Returns the Relax schema name.
054: */
055: public String getTagName() {
056: return "attribute";
057: }
058:
059: /**
060: * Returns the children pattern.
061: */
062: public GroupPattern getChildren() {
063: return _children;
064: }
065:
066: /**
067: * Adds an element.
068: */
069: public void addNameChild(NameClassPattern child)
070: throws RelaxException {
071: _name = child;
072: setElementName(_name.toProduction());
073: }
074:
075: /**
076: * get the name child
077: */
078: public NameClassPattern getNameChild() throws RelaxException {
079: return _name;
080: }
081:
082: /**
083: * Adds an attribute.
084: */
085: public void addChild(Pattern child) throws RelaxException {
086: if (_name == null)
087: throw new RelaxException(
088: L
089: .l("<attribute> must have a <name> definition before any children."));
090:
091: child.setParent(_children);
092: child.setElementName(_children.getElementName());
093:
094: _children.addChild(child);
095: }
096:
097: /**
098: * Ends the element.
099: */
100: public void endElement() throws RelaxException {
101: if (_name == null)
102: throw new RelaxException(L
103: .l("<attribute> must have a <name> definition."));
104: }
105:
106: /**
107: * Creates the program (somewhat bogus)
108: */
109: public Item createItem(GrammarPattern grammar)
110: throws RelaxException {
111: if (_item == null)
112: _item = new AttributeItem(_name.createNameItem());
113:
114: return _item;
115: }
116:
117: /**
118: * Returns a string for the production.
119: */
120: public String toProduction() {
121: return "@" + _name.toProduction();
122: }
123:
124: public boolean equals(Object o) {
125: if (this == o)
126: return true;
127:
128: if (!(o instanceof AttributePattern))
129: return false;
130:
131: AttributePattern elt = (AttributePattern) o;
132:
133: if (!_name.equals(elt._name))
134: return false;
135: else
136: return _children.equals(elt._children);
137: }
138:
139: /**
140: * Debugging.
141: */
142: public String toString() {
143: return "AttributePattern[" + _name.toProduction() + "]";
144: }
145: }
|