KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.relaxng.pattern;
31
32 import com.caucho.relaxng.RelaxException;
33 import com.caucho.relaxng.program.AttributeItem;
34 import com.caucho.relaxng.program.Item;
35
36 /**
37  * Relax attribute pattern
38  */

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

49   public AttributePattern()
50   {
51   }
52
53   /**
54    * Returns the Relax schema name.
55    */

56   public String JavaDoc getTagName()
57   {
58     return "attribute";
59   }
60
61   /**
62    * Returns the children pattern.
63    */

64   public GroupPattern getChildren()
65   {
66     return _children;
67   }
68
69   /**
70    * Adds an element.
71    */

72   public void addNameChild(NameClassPattern child)
73     throws RelaxException
74   {
75     _name = child;
76     setElementName(_name.toProduction());
77   }
78
79   /**
80    * get the name child
81    */

82   public NameClassPattern getNameChild()
83     throws RelaxException
84   {
85     return _name;
86   }
87
88   /**
89    * Adds an attribute.
90    */

91   public void addChild(Pattern child)
92     throws RelaxException
93   {
94     if (_name == null)
95       throw new RelaxException(L.l("<attribute> must have a <name> definition before any children."));
96     
97     child.setParent(_children);
98     child.setElementName(_children.getElementName());
99
100     _children.addChild(child);
101   }
102   
103   /**
104    * Ends the element.
105    */

106   public void endElement()
107     throws RelaxException
108   {
109     if (_name == null)
110       throw new RelaxException(L.l("<attribute> must have a <name> definition."));
111   }
112
113   /**
114    * Creates the program (somewhat bogus)
115    */

116   public Item createItem(GrammarPattern grammar)
117     throws RelaxException
118   {
119     if (_item == null)
120       _item = new AttributeItem(_name.createNameItem());
121     
122     return _item;
123   }
124
125   /**
126    * Returns a string for the production.
127    */

128   public String JavaDoc toProduction()
129   {
130     return "@" + _name.toProduction();
131   }
132
133   public boolean equals(Object JavaDoc o)
134   {
135     if (this == o)
136       return true;
137
138     if (! (o instanceof AttributePattern))
139       return false;
140
141     AttributePattern elt = (AttributePattern) o;
142
143     if (! _name.equals(elt._name))
144       return false;
145     else
146       return _children.equals(elt._children);
147   }
148
149   /**
150    * Debugging.
151    */

152   public String JavaDoc toString()
153   {
154     return "AttributePattern[" + _name.toProduction() + "]";
155   }
156 }
157
158
Popular Tags