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.util.L10N;
033:
034: import java.util.HashMap;
035: import java.util.Iterator;
036:
037: /**
038: * Relax grammar pattern
039: */
040: public class GrammarPattern extends Pattern {
041: protected static final L10N L = new L10N(GrammarPattern.class);
042:
043: private Pattern _start;
044: private int _id;
045:
046: private HashMap<String, Pattern> _definitions = new HashMap<String, Pattern>();
047:
048: /**
049: * Creates a new grammar pattern.
050: */
051: public GrammarPattern() {
052: }
053:
054: /**
055: * Returns the Relax schema name.
056: */
057: public String getTagName() {
058: return "grammar";
059: }
060:
061: /**
062: * Returns the start pattern.
063: */
064: public Pattern getStart() {
065: return _start;
066: }
067:
068: /**
069: * Sets the start element
070: */
071: public void setStart(Pattern start) throws RelaxException {
072: if (_start != null)
073: throw new RelaxException(
074: L
075: .l("Duplicate <start> in <grammar>. The <grammar> element can only have one <start>."));
076:
077: _start = start;
078: }
079:
080: /**
081: * Generates a name.
082: */
083: public String generateId() {
084: return "__caucho_" + _id++;
085: }
086:
087: /**
088: * Start definition.
089: */
090: public void setDefinition(String name, Pattern pattern) {
091: _definitions.put(name, pattern);
092: }
093:
094: /**
095: * Gets a definition.
096: */
097: public Pattern getDefinition(String name) {
098: return _definitions.get(name);
099: }
100:
101: /**
102: * Merges an include.
103: */
104: public void mergeInclude(GrammarPattern grammar) {
105: _definitions.putAll(grammar._definitions);
106: }
107:
108: /**
109: * Returns equals.
110: */
111: public boolean equals(Object o) {
112: return this == o;
113: }
114:
115: /**
116: * Debugging.
117: */
118: public String toString() {
119: return "GrammarPattern[" + _start + "]";
120: }
121: }
|