001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xml.stream.events;
031:
032: import javax.xml.namespace.NamespaceContext;
033: import javax.xml.namespace.QName;
034: import javax.xml.stream.XMLStreamException;
035: import javax.xml.stream.events.Attribute;
036: import javax.xml.stream.events.Namespace;
037: import javax.xml.stream.events.StartElement;
038: import java.io.IOException;
039: import java.io.Writer;
040: import java.util.HashMap;
041: import java.util.Iterator;
042:
043: public class StartElementImpl extends XMLEventImpl implements
044: StartElement {
045: private final QName _name;
046: private final HashMap<QName, Attribute> _attributes;
047: private final HashMap<String, Namespace> _namespaces;
048: private final NamespaceContext _namespaceContext;
049:
050: public StartElementImpl(QName name,
051: HashMap<QName, Attribute> attributes,
052: HashMap<String, Namespace> namespaces,
053: NamespaceContext namespaceContext) {
054: _name = name;
055: _attributes = attributes;
056: _namespaces = namespaces;
057: _namespaceContext = namespaceContext;
058: }
059:
060: public Attribute getAttributeByName(QName name) {
061: return _attributes.get(name);
062: }
063:
064: public Iterator getAttributes() {
065: return _attributes.values().iterator();
066: }
067:
068: public QName getName() {
069: return _name;
070: }
071:
072: public NamespaceContext getNamespaceContext() {
073: return _namespaceContext;
074: }
075:
076: public Iterator getNamespaces() {
077: return _namespaces.values().iterator();
078: }
079:
080: public String getNamespaceURI(String prefix) {
081: return _namespaces.get(prefix).getNamespaceURI();
082: }
083:
084: public int getEventType() {
085: return START_ELEMENT;
086: }
087:
088: public void writeAsEncodedUnicode(Writer writer)
089: throws XMLStreamException {
090: try {
091: writer.write("<" + _name + ">");
092:
093: for (Attribute attribute : _attributes.values())
094: attribute.writeAsEncodedUnicode(writer);
095:
096: for (Namespace namespace : _namespaces.values())
097: namespace.writeAsEncodedUnicode(writer);
098: } catch (IOException e) {
099: throw new XMLStreamException(e);
100: }
101: }
102:
103: public String toString() {
104: StringBuilder sb = new StringBuilder();
105:
106: sb.append("StartElement[" + _name);
107:
108: for (Attribute attribute : _attributes.values()) {
109: sb.append(" ");
110: sb.append(attribute.toString());
111: }
112:
113: for (Namespace namespace : _namespaces.values()) {
114: sb.append(" ");
115: sb.append(namespace.toString());
116: }
117:
118: sb.append("]");
119:
120: return sb.toString();
121: }
122:
123: public boolean equals(Object o) {
124: if (!(o instanceof StartElement))
125: return false;
126: if (o == null)
127: return false;
128: if (this == o)
129: return true;
130:
131: StartElement start = (StartElement) o;
132:
133: // Namespaces
134:
135: int namespaceCount = 0;
136: Iterator namespaces = start.getNamespaces();
137:
138: while (namespaces.hasNext()) {
139: Namespace ns2 = (Namespace) namespaces.next();
140: namespaceCount++;
141:
142: Namespace ns1 = _namespaces.get(ns2.getPrefix());
143:
144: if (!ns2.equals(ns1))
145: return false;
146: }
147:
148: if (namespaceCount < _namespaces.size())
149: return false;
150:
151: // Attributes
152:
153: int attributeCount = 0;
154: Iterator attributes = start.getAttributes();
155:
156: while (attributes.hasNext()) {
157: Attribute a2 = (Attribute) attributes.next();
158: attributeCount++;
159:
160: Attribute a1 = _attributes.get(a2.getName());
161:
162: if (!a2.equals(a1))
163: return false;
164: }
165:
166: if (attributeCount < _attributes.size())
167: return false;
168:
169: return getName().equals(start.getName());
170: }
171: }
|