View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  package com.bea.xml.stream.events;
16  
17  import com.bea.xml.stream.util.EmptyIterator;
18  
19  import javax.xml.namespace.NamespaceContext;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.xml.stream.events.Attribute;
27  import javax.xml.stream.events.Namespace;
28  import javax.xml.stream.events.XMLEvent;
29  import javax.xml.stream.events.StartElement;
30  import javax.xml.namespace.QName;
31  
32  public class StartElementEvent 
33    extends NamedEvent
34    implements StartElement 
35  {
36    private List attributes;
37    private List namespaces;
38    private NamespaceContext context;
39  
40    public StartElementEvent() { super();}
41  
42    public StartElementEvent(QName name) {
43      super(name);
44      init();
45    }
46  
47    public void reset() {
48      if (attributes != null) attributes.clear();
49      if (namespaces != null) namespaces.clear();
50      if (context != null) context = null;
51    }
52    public StartElementEvent(StartElement element) {
53      super(element.getName());
54      init();
55      setName(element.getName());
56  
57      Iterator ai = element.getAttributes();
58      while(ai.hasNext()) 
59        addAttribute((Attribute) ai.next());
60  
61      Iterator ni = element.getNamespaces();
62      ni = element.getNamespaces();
63      while(ni.hasNext())
64        addNamespace((Namespace) ni.next());
65    }
66    protected void init() {setEventType(XMLEvent.START_ELEMENT); }
67    public Iterator getAttributes() { 
68      if (attributes == null) return EmptyIterator.emptyIterator;
69      return attributes.iterator(); 
70    }
71    public Iterator getNamespaces() { 
72      if (namespaces == null) return EmptyIterator.emptyIterator;
73      return namespaces.iterator(); 
74    }
75  
76    public Attribute getAttributeByName(QName name) {
77      if (name == null) return null;
78      Iterator i = getAttributes();
79      while (i.hasNext()) {
80        Attribute a = (Attribute) i.next();
81        if (a.getName().equals(name))
82          return a;
83      }
84      return null;
85    }
86    public void setAttributes(List attributes) {
87      this.attributes = attributes;
88    }
89    public void addAttribute(Attribute attribute) {
90      if (attributes == null)
91        attributes = new ArrayList();
92      attributes.add(attribute);
93    }
94    public void addNamespace(Namespace attribute) {
95      if (namespaces == null)
96        namespaces = new ArrayList();
97      namespaces.add(attribute);
98    }
99    public String getNamespaceURI(String prefix) {
100     if (context == null) return null;
101     return (String) context.getNamespaceURI(prefix);
102   }
103 
104   public void setNamespaceContext(NamespaceContext c) {
105     this.context = c;
106   }
107 
108   public NamespaceContext getNamespaceContext() {
109     return context;
110   }
111   public String toString() {
112     String value = "<"+nameAsString();
113     Iterator ai = getAttributes();
114     while (ai.hasNext()) 
115       value = value +" "+ ai.next().toString();
116     Iterator ni = getNamespaces();
117     while (ni.hasNext()) 
118       value = value +" "+ ni.next().toString();
119 
120     value = value + ">";
121     return value;
122   }
123 }