1 38 39 package com.sun.xml.fastinfoset.stax.events ; 40 41 import java.util.List ; 42 import java.util.Map ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.ArrayList ; 46 import java.util.Collection ; 47 48 import javax.xml.stream.XMLStreamConstants; 49 import javax.xml.namespace.QName ; 50 import javax.xml.namespace.NamespaceContext ; 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 ; 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 _attributes; 62 private List _namespaces; 63 private NamespaceContext _context = null; 64 private QName _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 prefix, String uri, String localpart) { 77 init(); 78 if (uri == null) uri = ""; 79 if (prefix == null) prefix =""; 80 _qname = new QName (uri, localpart, prefix); 81 setEventType(START_ELEMENT); 82 } 83 84 public StartElementEvent(QName 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 (); 98 _namespaces = new ArrayList (); 99 } 100 101 106 public QName getName() { 107 return _qname; 108 } 109 119 public Iterator getAttributes() { 120 if(_attributes != null){ 121 Collection coll = _attributes.values(); 122 return new ReadIterator(coll.iterator()); 123 } 124 return EmptyIterator.getInstance(); 125 } 126 127 148 public Iterator getNamespaces() { 149 if(_namespaces != null){ 150 return new ReadIterator(_namespaces.iterator()); 151 } 152 return EmptyIterator.getInstance(); 153 } 154 155 160 public Attribute getAttributeByName(QName qname) { 161 if(qname == null) 162 return null; 163 return (Attribute)_attributes.get(qname); 164 } 165 166 173 public NamespaceContext getNamespaceContext() { 174 return _context; 175 } 176 178 public void setName(QName qname) { 179 this._qname = qname; 180 } 181 182 183 public String getNamespace(){ 184 return _qname.getNamespaceURI(); 185 } 186 187 194 public String getNamespaceURI(String prefix) { 195 if( getNamespace() != null ) return getNamespace(); 197 if(_context != null) 199 return _context.getNamespaceURI(prefix); 200 return null; 201 } 202 203 public String toString() { 204 String s = "<" + nameAsString(); 205 206 if(_attributes != null){ 207 Iterator 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 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 230 public String 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 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 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 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 |