1 /*
2  * Copyright (c) 2005, 2016, 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 package com.sun.xml.internal.stream.events;
26 
27 import java.util.List;
28 import java.util.ArrayList;
29 
30 import javax.xml.namespace.QName;
31 import javax.xml.stream.events.EndElement;
32 import javax.xml.stream.events.Namespace;
33 import java.util.Iterator;
34 import javax.xml.stream.events.XMLEvent;
35 import com.sun.xml.internal.stream.util.ReadOnlyIterator;
36 
37 /**
38  * Implementation of EndElement event.
39  *
40  * @author Neeraj Bajaj Sun Microsystems,Inc.
41  * @author K.Venugopal Sun Microsystems,Inc.
42  */
43 public class EndElementEvent extends DummyEvent
44         implements EndElement {
45 
46     List<Namespace> fNamespaces = null;
47     QName fQName;
48 
EndElementEvent()49     public EndElementEvent() {
50         init();
51     }
52 
init()53     protected final void init() {
54         setEventType(XMLEvent.END_ELEMENT);
55         fNamespaces = new ArrayList<>();
56     }
57 
EndElementEvent(String prefix, String uri, String localpart)58     public EndElementEvent(String prefix, String uri, String localpart) {
59         this(new QName(uri, localpart, prefix));
60     }
61 
EndElementEvent(QName qname)62     public EndElementEvent(QName qname) {
63         this.fQName = qname;
64         init();
65     }
66 
67     @Override
getName()68     public QName getName() {
69         return fQName;
70     }
71 
setName(QName qname)72     public void setName(QName qname) {
73         this.fQName = qname;
74     }
75 
76     @Override
writeAsEncodedUnicodeEx(java.io.Writer writer)77     protected void writeAsEncodedUnicodeEx(java.io.Writer writer)
78             throws java.io.IOException {
79         writer.write("</");
80         String prefix = fQName.getPrefix();
81         if (prefix != null && prefix.length() > 0) {
82             writer.write(prefix);
83             writer.write(':');
84         }
85         writer.write(fQName.getLocalPart());
86         writer.write('>');
87     }
88 
89     /**
90      * Returns an Iterator of namespaces that have gone out of scope. Returns an
91      * empty iterator if no namespaces have gone out of scope.
92      *
93      * @return an Iterator over Namespace interfaces, or an empty iterator
94      */
95     @Override
getNamespaces()96     public Iterator<Namespace> getNamespaces() {
97         if (fNamespaces != null) {
98             fNamespaces.iterator();
99         }
100         return new ReadOnlyIterator<>();
101     }
102 
addNamespace(Namespace ns)103     void addNamespace(Namespace ns) {
104         if (ns != null) {
105             fNamespaces.add(ns);
106         }
107     }
108 
toString()109     public String toString() {
110         String s = "</" + nameAsString();
111         s = s + ">";
112         return s;
113     }
114 
nameAsString()115     public String nameAsString() {
116         if ("".equals(fQName.getNamespaceURI())) {
117             return fQName.getLocalPart();
118         }
119 
120         if (fQName.getPrefix() != null) {
121             return "['" + fQName.getNamespaceURI() + "']:" + fQName.getPrefix() + ":" + fQName.getLocalPart();
122         } else {
123             return "['" + fQName.getNamespaceURI() + "']:" + fQName.getLocalPart();
124         }
125     }
126 
127 }
128