1/*
2 * Copyright (c) 1997, 2017, 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
26package com.sun.xml.internal.messaging.saaj.soap.impl;
27
28import java.util.Iterator;
29import java.util.NoSuchElementException;
30
31import javax.xml.namespace.QName;
32import javax.xml.soap.*;
33
34import org.w3c.dom.Element;
35
36import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
37import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
38
39public abstract class DetailImpl extends FaultElementImpl implements Detail {
40    public DetailImpl(SOAPDocumentImpl ownerDoc, NameImpl detailName) {
41        super(ownerDoc, detailName);
42    }
43
44    public DetailImpl(SOAPDocumentImpl ownerDoc, Element domElement) {
45        super(ownerDoc, domElement);
46    }
47
48    protected abstract DetailEntry createDetailEntry(Name name);
49    protected abstract DetailEntry createDetailEntry(QName name);
50
51    @Override
52    public DetailEntry addDetailEntry(Name name) throws SOAPException {
53        DetailEntry entry = createDetailEntry(name);
54        addNode(entry);
55        return entry;
56    }
57
58    @Override
59    public DetailEntry addDetailEntry(QName qname) throws SOAPException {
60        DetailEntry entry = createDetailEntry(qname);
61        addNode(entry);
62        return entry;
63    }
64
65    @Override
66    protected SOAPElement addElement(Name name) throws SOAPException {
67        return addDetailEntry(name);
68    }
69
70    @Override
71    protected SOAPElement addElement(QName name) throws SOAPException {
72        return addDetailEntry(name);
73    }
74
75    @Override
76    protected SOAPElement convertToSoapElement(Element element) {
77        final javax.xml.soap.Node soapNode = getSoapDocument().find(element);
78        if (soapNode instanceof DetailEntry) {
79            return (SOAPElement) soapNode;
80        } else {
81            DetailEntry detailEntry =
82                createDetailEntry(NameImpl.copyElementName(element));
83            return replaceElementWithSOAPElement(
84                element,
85                (ElementImpl) detailEntry);
86        }
87    }
88
89    @Override
90    public Iterator<DetailEntry> getDetailEntries() {
91        return new Iterator<DetailEntry>() {
92            Iterator<org.w3c.dom.Node> eachNode = getChildElementNodes();
93            SOAPElement next = null;
94            SOAPElement last = null;
95
96            @Override
97            public boolean hasNext() {
98                if (next == null) {
99                    while (eachNode.hasNext()) {
100                        next = (SOAPElement) eachNode.next();
101                        if (next instanceof DetailEntry) {
102                            break;
103                        }
104                        next = null;
105                    }
106                }
107                return next != null;
108            }
109
110            @Override
111            public DetailEntry next() {
112                if (!hasNext()) {
113                    throw new NoSuchElementException();
114                }
115                last = next;
116                next = null;
117                return (DetailEntry) last;
118            }
119
120            @Override
121            public void remove() {
122                if (last == null) {
123                    throw new IllegalStateException();
124                }
125                SOAPElement target = last;
126                removeChild(target);
127                last = null;
128            }
129        };
130    }
131
132    @Override
133   protected  boolean isStandardFaultElement() {
134       return true;
135   }
136
137}
138