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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.relaxng.pattern;
030:
031: import com.caucho.relaxng.RelaxException;
032: import com.caucho.relaxng.program.ElementItem;
033: import com.caucho.relaxng.program.Item;
034:
035: /**
036: * Relax element pattern
037: */
038: public class ElementPattern extends Pattern {
039: private String _defName;
040:
041: private NameClassPattern _name;
042: private GroupPattern _children = new GroupPattern();
043:
044: private Item _item;
045:
046: /**
047: * Creates a new element pattern.
048: */
049: public ElementPattern(String defName) {
050: _defName = defName;
051: }
052:
053: /**
054: * Returns the Relax schema name.
055: */
056: public String getTagName() {
057: return "element";
058: }
059:
060: /**
061: * Returns the definition name.
062: */
063: public String getDefName() {
064: return _defName;
065: }
066:
067: /**
068: * Returns the children pattern.
069: */
070: public GroupPattern getChildren() {
071: return _children;
072: }
073:
074: /**
075: * Returns true if it contains an element.
076: */
077: public boolean hasElement() {
078: return true;
079: }
080:
081: /**
082: * Adds an element.
083: */
084: public void addNameChild(NameClassPattern child)
085: throws RelaxException {
086: _name = child;
087: setElementName(_name.toProduction());
088: }
089:
090: /**
091: * Adds an element.
092: */
093: public NameClassPattern getNameChild() throws RelaxException {
094: return _name;
095: }
096:
097: /**
098: * Adds an element.
099: */
100: public void addChild(Pattern child) throws RelaxException {
101: if (_name == null)
102: throw new RelaxException(
103: L
104: .l("<element> must have <name> definitions before other children."));
105:
106: child.setParent(_children);
107: child.setElementName(_children.getElementName());
108:
109: _children.addChild(child);
110: }
111:
112: /**
113: * Ends the element.
114: */
115: public void endElement() throws RelaxException {
116: if (_name == null)
117: throw new RelaxException(L
118: .l("<element> must have a <name> definition."));
119:
120: if (_children.getSize() == 0)
121: throw new RelaxException(
122: L
123: .l(
124: "<element> tag `{0}' must have a child grammar production.",
125: _name.toProduction()));
126: }
127:
128: /**
129: * Creates the item, i.e. program
130: */
131: public Item createItem(GrammarPattern grammar)
132: throws RelaxException {
133: if (_item == null) {
134: ElementItem item = new ElementItem(this , _name
135: .createNameItem());
136: _item = item;
137: item.setChildrenItem(_children.createItem(grammar));
138: }
139:
140: return _item;
141: }
142:
143: /**
144: * Returns a string for the production.
145: */
146: public String toProduction() {
147: return _name.toProduction();
148: }
149:
150: public boolean equals(Object o) {
151: if (this == o)
152: return true;
153:
154: if (!(o instanceof ElementPattern))
155: return false;
156:
157: ElementPattern elt = (ElementPattern) o;
158:
159: return _defName.equals(elt._defName);
160: }
161:
162: /**
163: * Debugging.
164: */
165: public String toString() {
166: return "ElementPattern[" + _name + "]";
167: }
168: }
|