1 /*
   2  * Copyright (c) 1997, 2010, 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 
  26 package com.sun.xml.internal.messaging.saaj.soap.impl;
  27 
  28 import java.util.Iterator;
  29 import java.util.NoSuchElementException;
  30 
  31 import javax.xml.namespace.QName;
  32 import javax.xml.soap.*;
  33 
  34 import org.w3c.dom.Element;
  35 
  36 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
  37 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
  38 
  39 public abstract class DetailImpl extends FaultElementImpl implements Detail {
  40     public DetailImpl(SOAPDocumentImpl ownerDoc, NameImpl detailName) {
  41         super(ownerDoc, detailName);
  42     }
  43 
  44     protected abstract DetailEntry createDetailEntry(Name name);
  45     protected abstract DetailEntry createDetailEntry(QName name);
  46 
  47     public DetailEntry addDetailEntry(Name name) throws SOAPException {
  48         DetailEntry entry = createDetailEntry(name);
  49         addNode(entry);
  50         return (DetailEntry) circumventBug5034339(entry);
  51     }
  52 
  53     public DetailEntry addDetailEntry(QName qname) throws SOAPException {
  54         DetailEntry entry = createDetailEntry(qname);
  55         addNode(entry);
  56         return (DetailEntry) circumventBug5034339(entry);
  57     }
  58 
  59     protected SOAPElement addElement(Name name) throws SOAPException {
  60         return addDetailEntry(name);
  61     }
  62 
  63     protected SOAPElement addElement(QName name) throws SOAPException {
  64         return addDetailEntry(name);
  65     }
  66 
  67     protected SOAPElement convertToSoapElement(Element element) {
  68         if (element instanceof DetailEntry) {
  69             return (SOAPElement) element;
  70         } else {
  71             DetailEntry detailEntry =
  72                 createDetailEntry(NameImpl.copyElementName(element));
  73             return replaceElementWithSOAPElement(
  74                 element,
  75                 (ElementImpl) detailEntry);
  76         }
  77     }
  78 
  79     public Iterator getDetailEntries() {
  80         return new Iterator() {
  81             Iterator eachNode = getChildElementNodes();
  82             SOAPElement next = null;
  83             SOAPElement last = null;
  84 
  85             public boolean hasNext() {
  86                 if (next == null) {
  87                     while (eachNode.hasNext()) {
  88                         next = (SOAPElement) eachNode.next();
  89                         if (next instanceof DetailEntry) {
  90                             break;
  91                         }
  92                         next = null;
  93                     }
  94                 }
  95                 return next != null;
  96             }
  97 
  98             public Object next() {
  99                 if (!hasNext()) {
 100                     throw new NoSuchElementException();
 101                 }
 102                 last = next;
 103                 next = null;
 104                 return last;
 105             }
 106 
 107             public void remove() {
 108                 if (last == null) {
 109                     throw new IllegalStateException();
 110                 }
 111                 SOAPElement target = last;
 112                 removeChild(target);
 113                 last = null;
 114             }
 115         };
 116     }
 117 
 118    protected  boolean isStandardFaultElement() {
 119        return true;
 120    }
 121 
 122     //overriding this method since the only two uses of this method
 123     // are in ElementImpl and DetailImpl
 124     //whereas the original base impl does the correct job for calls to it inside ElementImpl
 125     // But it would not work for DetailImpl.
 126     protected SOAPElement circumventBug5034339(SOAPElement element) {
 127 
 128         Name elementName = element.getElementName();
 129         if (!isNamespaceQualified(elementName)) {
 130             String prefix = elementName.getPrefix();
 131             String defaultNamespace = getNamespaceURI(prefix);
 132             if (defaultNamespace != null) {
 133                 Name newElementName =
 134                     NameImpl.create(
 135                         elementName.getLocalName(),
 136                         elementName.getPrefix(),
 137                         defaultNamespace);
 138                 SOAPElement newElement = createDetailEntry(newElementName);
 139                 replaceChild(newElement, element);
 140                 return newElement;
 141             }
 142         }
 143         return element;
 144     }
 145 
 146 }