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.Item;
033: import com.caucho.relaxng.program.NameClassItem;
034: import com.caucho.util.L10N;
035:
036: /**
037: * Relax pattern
038: */
039: abstract public class Pattern {
040: protected static final L10N L = new L10N(Pattern.class);
041:
042: private Pattern _parent;
043: private String _elementName;
044:
045: private String _filename;
046: private int _line;
047:
048: /**
049: * Returns the relax config name.
050: */
051: public String getTagName() {
052: return getClass().getName();
053: }
054:
055: /**
056: * Sets the pattern source location.
057: */
058: public void setFilename(String filename) {
059: _filename = filename;
060: }
061:
062: /**
063: * Sets the pattern line
064: */
065: public void setLine(int line) {
066: _line = line;
067: }
068:
069: /**
070: * Gets the location.
071: */
072: public String getLocation() {
073: if (_filename != null)
074: return _filename + ":" + _line;
075: else if (_parent != null)
076: return _parent.getLocation();
077: else
078: return null;
079: }
080:
081: /**
082: * Returns the element-name.
083: */
084: public String getElementName() {
085: return _elementName;
086: }
087:
088: /**
089: * Sets the element-name.
090: */
091: public void setElementName(String elementName) {
092: _elementName = elementName;
093: }
094:
095: /**
096: * Sets the parent.
097: */
098: public void setParent(Pattern parent) throws RelaxException {
099: _parent = parent;
100: }
101:
102: /**
103: * Gets the parent.
104: */
105: public Pattern getParent() {
106: return _parent;
107: }
108:
109: /**
110: * Returns true if it contains a data element.
111: */
112: public boolean hasData() {
113: return false;
114: }
115:
116: /**
117: * Returns true if it contains an element.
118: */
119: public boolean hasElement() {
120: return false;
121: }
122:
123: /**
124: * Adds a name child.
125: */
126: public void addNameChild(NameClassPattern child)
127: throws RelaxException {
128: throw new RelaxException(L.l(
129: "<{0}> is not an allowed child for <{1}>.", child
130: .getTagName(), getTagName()));
131: }
132:
133: /**
134: * Adds an element child.
135: */
136: public void addChild(Pattern child) throws RelaxException {
137: throw new RelaxException(L.l(
138: "<{0}> is not an allowed child for <{1}>.", child
139: .getTagName(), getTagName()));
140: }
141:
142: /**
143: * Ends the element.
144: */
145: public void endElement() throws RelaxException {
146: }
147:
148: /**
149: * Creates the current state
150: */
151: public Item createItem(GrammarPattern grammar)
152: throws RelaxException {
153: throw new RelaxException(L.l("item isn't allowed in `{0}'.",
154: getClass().getName()));
155: }
156:
157: /**
158: * Creates the name program
159: */
160: public NameClassItem createNameItem() throws RelaxException {
161: throw new RelaxException(L.l(
162: "name-item isn't allowed in `{0}'.", getClass()
163: .getName()));
164: }
165:
166: abstract public boolean equals(Object o);
167:
168: /**
169: * Returns a string for the production.
170: */
171: public String toProduction() {
172: return "unknown";
173: }
174:
175: /**
176: * creates an error.
177: */
178: public RelaxException error(String msg) {
179: String location = getLocation();
180:
181: if (location != null)
182: return new RelaxException(location + ": " + msg);
183: else
184: return new RelaxException(msg);
185: }
186: }
|