1/*
2 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26 */
27
28package com.sun.xml.internal.fastinfoset.stax.events ;
29
30import java.util.ArrayList;
31import java.util.Iterator;
32import java.util.List;
33
34import javax.xml.namespace.QName;
35import javax.xml.stream.events.EndElement;
36import javax.xml.stream.events.Namespace;
37
38import com.sun.xml.internal.fastinfoset.stax.events.EmptyIterator;
39
40
41public class EndElementEvent extends EventBase implements EndElement {
42
43    List _namespaces = null;
44    QName _qname ;
45
46    public void reset() {
47        if (_namespaces != null) _namespaces.clear();
48    }
49
50    public EndElementEvent() {
51        setEventType(END_ELEMENT);
52    }
53
54    public EndElementEvent(String prefix, String namespaceURI, String localpart) {
55        _qname = getQName(namespaceURI,localpart,prefix);
56        setEventType(END_ELEMENT);
57    }
58
59    public EndElementEvent(QName qname) {
60        _qname = qname;
61        setEventType(END_ELEMENT);
62    }
63
64  /**
65   * Get the name of this event
66   * @return the qualified name of this event
67   */
68    public QName getName() {
69        return _qname;
70    }
71
72    public void setName(QName qname) {
73        _qname = qname;
74    }
75
76
77    /** Returns an Iterator of namespaces that have gone out
78     * of scope.  Returns an empty iterator if no namespaces have gone
79     * out of scope.
80     * @return an Iterator over Namespace interfaces, or an
81     * empty iterator
82     */
83    public Iterator getNamespaces() {
84        if(_namespaces != null)
85            return _namespaces.iterator();
86        return EmptyIterator.getInstance();
87    }
88
89    public void addNamespace(Namespace namespace){
90        if (_namespaces == null) {
91            _namespaces = new ArrayList();
92        }
93        _namespaces.add(namespace);
94    }
95
96    public String toString() {
97        StringBuffer sb = new StringBuffer();
98        sb.append("</").append(nameAsString());
99        Iterator namespaces = getNamespaces();
100        while(namespaces.hasNext()) {
101            sb.append(" ").append(namespaces.next().toString());
102        }
103        sb.append(">");
104        return sb.toString();
105    }
106
107
108    private String nameAsString() {
109        if("".equals(_qname.getNamespaceURI()))
110            return _qname.getLocalPart();
111        if(_qname.getPrefix() != null)
112            return "['" + _qname.getNamespaceURI() + "']:" + _qname.getPrefix() + ":" + _qname.getLocalPart();
113        else
114            return "['" + _qname.getNamespaceURI() + "']:" + _qname.getLocalPart();
115    }
116    private QName getQName(String uri, String localPart, String prefix){
117        QName qn = null;
118        if(prefix != null && uri != null)
119            qn = new QName(uri, localPart, prefix);
120        else if(prefix == null && uri != null)
121            qn = new QName(uri, localPart);
122        else if(prefix == null && uri == null)
123            qn = new QName(localPart);
124        return qn;
125    }
126}
127