KickJava   Java API By Example, From Geeks To Geeks.

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


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.relaxng.program.ElementItem;
33 import com.caucho.relaxng.program.Item;
34
35 /**
36  * Relax element pattern
37  */

38 public class ElementPattern extends Pattern {
39   private String JavaDoc _defName;
40   
41   private NameClassPattern _name;
42   private GroupPattern _children = new GroupPattern();
43
44   private Item _item;
45
46   /**
47    * Creates a new element pattern.
48    */

49   public ElementPattern(String JavaDoc defName)
50   {
51     _defName = defName;
52   }
53
54   /**
55    * Returns the Relax schema name.
56    */

57   public String JavaDoc getTagName()
58   {
59     return "element";
60   }
61
62   /**
63    * Returns the definition name.
64    */

65   public String JavaDoc getDefName()
66   {
67     return _defName;
68   }
69
70   /**
71    * Returns the children pattern.
72    */

73   public GroupPattern getChildren()
74   {
75     return _children;
76   }
77
78   /**
79    * Returns true if it contains an element.
80    */

81   public boolean hasElement()
82   {
83     return true;
84   }
85
86   /**
87    * Adds an element.
88    */

89   public void addNameChild(NameClassPattern child)
90     throws RelaxException
91   {
92     _name = child;
93     setElementName(_name.toProduction());
94   }
95
96   /**
97    * Adds an element.
98    */

99   public NameClassPattern getNameChild()
100     throws RelaxException
101   {
102     return _name;
103   }
104
105   /**
106    * Adds an element.
107    */

108   public void addChild(Pattern child)
109     throws RelaxException
110   {
111     if (_name == null)
112       throw new RelaxException(L.l("<element> must have <name> definitions before other children."));
113     
114     child.setParent(_children);
115     child.setElementName(_children.getElementName());
116
117     _children.addChild(child);
118   }
119
120   /**
121    * Ends the element.
122    */

123   public void endElement()
124     throws RelaxException
125   {
126     if (_name == null)
127       throw new RelaxException(L.l("<element> must have a <name> definition."));
128     
129     if (_children.getSize() == 0)
130       throw new RelaxException(L.l("<element> tag `{0}' must have a child grammar production.",
131                                    _name.toProduction()));
132   }
133
134   /**
135    * Creates the item, i.e. program
136    */

137   public Item createItem(GrammarPattern grammar)
138     throws RelaxException
139   {
140     if (_item == null) {
141       ElementItem item = new ElementItem(this, _name.createNameItem());
142       _item = item;
143       item.setChildrenItem(_children.createItem(grammar));
144     }
145     
146     return _item;
147   }
148
149   /**
150    * Returns a string for the production.
151    */

152   public String JavaDoc toProduction()
153   {
154     return _name.toProduction();
155   }
156
157   public boolean equals(Object JavaDoc o)
158   {
159     if (this == o)
160       return true;
161
162     if (! (o instanceof ElementPattern))
163       return false;
164
165     ElementPattern elt = (ElementPattern) o;
166
167     return _defName.equals(elt._defName);
168   }
169
170   /**
171    * Debugging.
172    */

173   public String JavaDoc toString()
174   {
175     return "ElementPattern[" + _name + "]";
176   }
177 }
178
179
Popular Tags