001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.stax.events;
040:
041: import java.util.ArrayList;
042: import java.util.Iterator;
043: import java.util.List;
044:
045: import javax.xml.namespace.QName;
046: import javax.xml.stream.events.EndElement;
047: import javax.xml.stream.events.Namespace;
048:
049: import com.sun.xml.fastinfoset.stax.events.EmptyIterator;
050:
051: public class EndElementEvent extends EventBase implements EndElement {
052:
053: List _namespaces = null;
054: QName _qname;
055:
056: public void reset() {
057: if (_namespaces != null)
058: _namespaces.clear();
059: }
060:
061: public EndElementEvent() {
062: setEventType(END_ELEMENT);
063: }
064:
065: public EndElementEvent(String namespaceURI, String localpart,
066: String prefix) {
067: _qname = getQName(namespaceURI, localpart, prefix);
068: setEventType(END_ELEMENT);
069: }
070:
071: public EndElementEvent(QName qname) {
072: _qname = qname;
073: setEventType(END_ELEMENT);
074: }
075:
076: /**
077: * Get the name of this event
078: * @return the qualified name of this event
079: */
080: public QName getName() {
081: return _qname;
082: }
083:
084: public void setName(QName qname) {
085: _qname = qname;
086: }
087:
088: /** Returns an Iterator of namespaces that have gone out
089: * of scope. Returns an empty iterator if no namespaces have gone
090: * out of scope.
091: * @return an Iterator over Namespace interfaces, or an
092: * empty iterator
093: */
094: public Iterator getNamespaces() {
095: if (_namespaces != null)
096: return _namespaces.iterator();
097: return EmptyIterator.getInstance();
098: }
099:
100: public void addNamespace(Namespace namespace) {
101: if (_namespaces == null) {
102: _namespaces = new ArrayList();
103: }
104: _namespaces.add(namespace);
105: }
106:
107: public String toString() {
108: StringBuffer sb = new StringBuffer();
109: sb.append("</").append(nameAsString());
110: Iterator namespaces = getNamespaces();
111: while (namespaces.hasNext()) {
112: sb.append(" ").append(namespaces.next().toString());
113: }
114: sb.append(">");
115: return sb.toString();
116: }
117:
118: private String nameAsString() {
119: if ("".equals(_qname.getNamespaceURI()))
120: return _qname.getLocalPart();
121: if (_qname.getPrefix() != null)
122: return "['" + _qname.getNamespaceURI() + "']:"
123: + _qname.getPrefix() + ":" + _qname.getLocalPart();
124: else
125: return "['" + _qname.getNamespaceURI() + "']:"
126: + _qname.getLocalPart();
127: }
128:
129: private QName getQName(String uri, String localPart, String prefix) {
130: QName qn = null;
131: if (prefix != null && uri != null)
132: qn = new QName(uri, localPart, prefix);
133: else if (prefix == null && uri != null)
134: qn = new QName(uri, localPart);
135: else if (prefix == null && uri == null)
136: qn = new QName(localPart);
137: return qn;
138: }
139: }
|