KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > relaxng > pattern > GrammarPattern


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.relaxng.pattern;
30
31 import com.caucho.relaxng.RelaxException;
32 import com.caucho.util.L10N;
33
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 /**
38  * Relax grammar pattern
39  */

40 public class GrammarPattern extends Pattern {
41   protected static final L10N L = new L10N(GrammarPattern.class);
42   
43   private Pattern _start;
44   private int _id;
45
46   private HashMap JavaDoc<String JavaDoc,Pattern> _definitions = new HashMap JavaDoc<String JavaDoc,Pattern>();
47
48   /**
49    * Creates a new grammar pattern.
50    */

51   public GrammarPattern()
52   {
53   }
54
55   /**
56    * Returns the Relax schema name.
57    */

58   public String JavaDoc getTagName()
59   {
60     return "grammar";
61   }
62
63   /**
64    * Returns the start pattern.
65    */

66   public Pattern getStart()
67   {
68     return _start;
69   }
70
71   /**
72    * Sets the start element
73    */

74   public void setStart(Pattern start)
75     throws RelaxException
76   {
77     if (_start != null)
78       throw new RelaxException(L.l("Duplicate <start> in <grammar>. The <grammar> element can only have one <start>."));
79
80     _start = start;
81   }
82
83   /**
84    * Generates a name.
85    */

86   public String JavaDoc generateId()
87   {
88     return "__caucho_" + _id++;
89   }
90
91   /**
92    * Start definition.
93    */

94   public void setDefinition(String JavaDoc name, Pattern pattern)
95   {
96     _definitions.put(name, pattern);
97   }
98
99   /**
100    * Gets a definition.
101    */

102   public Pattern getDefinition(String JavaDoc name)
103   {
104     return _definitions.get(name);
105   }
106
107   /**
108    * Merges an include.
109    */

110   public void mergeInclude(GrammarPattern grammar)
111   {
112     Iterator JavaDoc<String JavaDoc> names = grammar._definitions.keySet().iterator();
113
114     while (names.hasNext()) {
115       String JavaDoc name = names.next();
116
117       Pattern includePattern = grammar._definitions.get(name);
118
119       _definitions.put(name, includePattern);
120     }
121   }
122
123   /**
124    * Returns equals.
125    */

126   public boolean equals(Object JavaDoc o)
127   {
128     return this == o;
129   }
130
131   /**
132    * Debugging.
133    */

134   public String JavaDoc toString()
135   {
136     return "GrammarPattern[" + _start + "]";
137   }
138 }
139
140
Popular Tags