KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > events > StartElementEvent


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39 package com.sun.xml.fastinfoset.stax.events ;
40
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Collection JavaDoc;
47
48 import javax.xml.stream.XMLStreamConstants;
49 import javax.xml.namespace.QName JavaDoc;
50 import javax.xml.namespace.NamespaceContext JavaDoc;
51 import javax.xml.stream.events.StartElement;
52 import javax.xml.stream.events.Attribute;
53 import javax.xml.stream.events.Namespace;
54 import java.io.Writer JavaDoc;
55
56 import com.sun.xml.fastinfoset.util.ReadIterator;
57 import com.sun.xml.fastinfoset.util.EmptyIterator;
58
59 public class StartElementEvent extends EventBase implements StartElement {
60     
61     private Map JavaDoc _attributes;
62     private List JavaDoc _namespaces;
63     private NamespaceContext JavaDoc _context = null;
64     private QName JavaDoc _qname;
65     
66     public void reset() {
67         if (_attributes != null) _attributes.clear();
68         if (_namespaces != null) _namespaces.clear();
69         if (_context != null) _context = null;
70     }
71     
72     public StartElementEvent() {
73         init();
74     }
75     
76     public StartElementEvent(String JavaDoc prefix, String JavaDoc uri, String JavaDoc localpart) {
77         init();
78         if (uri == null) uri = "";
79         if (prefix == null) prefix ="";
80         _qname = new QName JavaDoc(uri, localpart, prefix);
81         setEventType(START_ELEMENT);
82     }
83     
84     public StartElementEvent(QName JavaDoc qname) {
85         init();
86         _qname = qname;
87     }
88     
89     public StartElementEvent(StartElement startelement) {
90         this(startelement.getName());
91         addAttributes(startelement.getAttributes());
92         addNamespaces(startelement.getNamespaces());
93     }
94     
95     protected void init() {
96         setEventType(XMLStreamConstants.START_ELEMENT);
97         _attributes = new HashMap JavaDoc();
98         _namespaces = new ArrayList JavaDoc();
99     }
100     
101     // ---------------------methods defined by StartElement-----------------//
102
/**
103     * Get the name of this event
104     * @return the qualified name of this event
105     */

106     public QName JavaDoc getName() {
107         return _qname;
108     }
109     /**
110     * Returns an Iterator of non-namespace declared attributes
111     * returns an empty iterator if there are no attributes. The
112     * iterator must contain only implementations of the javax.xml.stream.Attribute
113     * interface. Attributes are fundamentally unordered and may not be reported
114     * in any order.
115     *
116     * @return a readonly Iterator over Attribute interfaces, or an
117     * empty iterator
118     */

119     public Iterator JavaDoc getAttributes() {
120         if(_attributes != null){
121             Collection JavaDoc coll = _attributes.values();
122             return new ReadIterator(coll.iterator());
123         }
124         return EmptyIterator.getInstance();
125     }
126      
127   /**
128    * Returns an Iterator of namespaces declared on this element.
129    * This Iterator does not contain previously declared namespaces
130    * unless they appear on the current START_ELEMENT.
131    * Therefore this list may contain redeclared namespaces and duplicate namespace
132    * declarations. Use the getNamespaceContext() method to get the
133    * current context of namespace declarations.
134    *
135    * <p>The iterator must contain only implementations of the
136    * javax.xml.stream.Namespace interface.
137    *
138    * <p>A Namespace is an Attribute. One
139    * can iterate over a list of namespaces as a list of attributes.
140    * However this method returns only the list of namespaces
141    * declared on this START_ELEMENT and does not
142    * include the attributes declared on this START_ELEMENT.
143    *
144    * @return a readonly Iterator over Namespace interfaces, or an
145    * empty iterator if there are no namespaces.
146    *
147    */

148     public Iterator JavaDoc getNamespaces() {
149         if(_namespaces != null){
150             return new ReadIterator(_namespaces.iterator());
151         }
152         return EmptyIterator.getInstance();
153     }
154  
155   /**
156    * Returns the attribute referred to by this name
157    * @param name the qname of the desired name
158    * @return the attribute corresponding to the name value or null
159    */

160     public Attribute getAttributeByName(QName JavaDoc qname) {
161         if(qname == null)
162             return null;
163         return (Attribute)_attributes.get(qname);
164     }
165     
166     /** Gets a read-only namespace context. If no context is
167      * available this method will return an empty namespace context.
168      * The NamespaceContext contains information about all namespaces
169      * in scope for this StartElement.
170      *
171      * @return the current namespace context
172      */

173     public NamespaceContext JavaDoc getNamespaceContext() {
174         return _context;
175     }
176 // ---------------------end of methods defined by StartElement-----------------//
177

178     public void setName(QName JavaDoc qname) {
179         this._qname = qname;
180     }
181     
182     
183     public String JavaDoc getNamespace(){
184         return _qname.getNamespaceURI();
185     }
186     
187     /**
188     * Gets the value that the prefix is bound to in the
189     * context of this element. Returns null if
190     * the prefix is not bound in this context
191     * @param prefix the prefix to lookup
192     * @return the uri bound to the prefix or null
193     */

194     public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
195         //first check if the URI was supplied when creating this startElement event
196
if( getNamespace() != null ) return getNamespace();
197         //else check the namespace context
198
if(_context != null)
199             return _context.getNamespaceURI(prefix);
200         return null;
201     }
202     
203     public String JavaDoc toString() {
204         String JavaDoc s = "<" + nameAsString();
205         
206         if(_attributes != null){
207             Iterator JavaDoc it = this.getAttributes();
208             Attribute attr = null;
209             while(it.hasNext()){
210                 attr = (Attribute)it.next();
211                 s = s + " " + attr.toString();
212             }
213         }
214         
215         if(_namespaces != null){
216             Iterator JavaDoc it = _namespaces.iterator();
217             Namespace attr = null;
218             while(it.hasNext()){
219                 attr = (Namespace)it.next();
220                 s = s + " " + attr.toString();
221             }
222         }
223         s = s + ">";
224         return s;
225     }
226     
227     /** Return this event as String
228      * @return String Event returned as string.
229      */

230     public String JavaDoc nameAsString() {
231         if("".equals(_qname.getNamespaceURI()))
232             return _qname.getLocalPart();
233         if(_qname.getPrefix() != null)
234             return "['" + _qname.getNamespaceURI() + "']:" + _qname.getPrefix() + ":" + _qname.getLocalPart();
235         else
236             return "['" + _qname.getNamespaceURI() + "']:" + _qname.getLocalPart();
237     }
238     
239     
240     public void setNamespaceContext(NamespaceContext JavaDoc context) {
241         _context = context;
242     }
243     
244     public void addAttribute(Attribute attr){
245         _attributes.put(attr.getName(),attr);
246     }
247     
248     public void addAttributes(Iterator JavaDoc attrs){
249         if(attrs != null) {
250             while(attrs.hasNext()){
251                 Attribute attr = (Attribute)attrs.next();
252                 _attributes.put(attr.getName(),attr);
253             }
254         }
255     }
256     
257     public void addNamespace(Namespace namespace){
258         if(namespace != null) {
259             _namespaces.add(namespace);
260         }
261     }
262     
263     public void addNamespaces(Iterator JavaDoc namespaces){
264         if(namespaces != null) {
265             while(namespaces.hasNext()){
266                 Namespace namespace = (Namespace)namespaces.next();
267                 _namespaces.add(namespace);
268             }
269         }
270     }
271     
272 }
273
Popular Tags