1 /*
   2  * Copyright (c) 1997, 2014, 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.org.jvnet.staxex.util;
  27 
  28 import java.util.Iterator;
  29 
  30 import javax.xml.soap.SOAPElement;
  31 import javax.xml.soap.SOAPException;
  32 import javax.xml.stream.XMLStreamException;
  33 
  34 import com.sun.xml.internal.org.jvnet.staxex.Base64Data;
  35 import com.sun.xml.internal.org.jvnet.staxex.XMLStreamReaderEx;
  36 import com.sun.xml.internal.org.jvnet.staxex.BinaryText;
  37 
  38 import org.w3c.dom.Node;
  39 import org.w3c.dom.Text;
  40 
  41 /**
  42  * SaajStaxReaderEx
  43  *
  44  * @author shih-chang.chen@oracle.com
  45  */
  46 public class SaajStaxReaderEx extends DOMStreamReader implements XMLStreamReaderEx {
  47     //TODO extends com.sun.xml.internal.ws.streaming.DOMStreamReader
  48     private BinaryText binaryText = null;
  49     private Base64Data base64AttData = null;
  50 
  51     public SaajStaxReaderEx(SOAPElement se) {
  52         super(se);
  53     }
  54 
  55     @Override
  56     public int next() throws XMLStreamException {
  57         binaryText = null;
  58         base64AttData = null;
  59         while(true) {
  60             int r = _next();
  61             switch (r) {
  62             case CHARACTERS:
  63                 if (_current instanceof BinaryText) {
  64                     binaryText = (BinaryText) _current;
  65                     base64AttData = new Base64Data();
  66                     try {
  67                         base64AttData.set(binaryText.getDataHandler());
  68 //System.out.println("--------------- debug SaajStaxReaderEx binaryText " + binaryText);
  69                     } catch (SOAPException e) {
  70                         throw new XMLStreamException(e);
  71                     }
  72                 } else {
  73                     // if we are currently at text node, make sure that this is a meaningful text node.
  74                     Node prev = _current.getPreviousSibling();
  75                     if(prev!=null && prev.getNodeType()==Node.TEXT_NODE)
  76                         continue;   // nope. this is just a continuation of previous text that should be invisible
  77 
  78                     Text t = (Text)_current;
  79                     wholeText = t.getWholeText();
  80                     if(wholeText.length()==0)
  81                         continue;   // nope. this is empty text.
  82                 }
  83                 return CHARACTERS;
  84             case START_ELEMENT:
  85                 splitAttributes();
  86                 return START_ELEMENT;
  87             default:
  88                 return r;
  89             }
  90         }
  91     }
  92 
  93     @Override
  94     public String getElementTextTrim() throws XMLStreamException {
  95         // TODO Auto-generated method stub
  96         return null;
  97     }
  98 
  99     @Override
 100     public CharSequence getPCDATA() throws XMLStreamException {
 101         return (binaryText != null) ? base64AttData : getText();
 102     }
 103 
 104     @Override
 105     public com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx getNamespaceContext() {
 106         return new com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx() {
 107 
 108             @Override
 109             public String getNamespaceURI(String prefix) {
 110                 return _current.lookupNamespaceURI(prefix);
 111             }
 112 
 113             @Override
 114             public String getPrefix(String uri) {
 115                 return _current.lookupPrefix(uri);
 116             }
 117 
 118             @Override
 119             public Iterator getPrefixes(String arg0) {
 120                 // TODO Auto-generated method stub
 121                 return null;
 122             }
 123 
 124             @Override
 125             public Iterator<Binding> iterator() {
 126                 // TODO Auto-generated method stub
 127                 return null;
 128             }
 129 
 130         };
 131     }
 132 
 133 
 134     @Override
 135     public int getTextLength() {
 136         return (binaryText != null) ? base64AttData.length() : super.getTextLength();
 137     }
 138 
 139     @Override
 140     public int getTextStart() {
 141         return (binaryText != null) ? 0: super.getTextStart();
 142     }
 143 
 144     @Override
 145     public char[] getTextCharacters() {
 146         if (binaryText != null) {
 147             char[] chars = new char[base64AttData.length()];
 148             base64AttData.writeTo(chars, 0);
 149             return chars;
 150         }
 151         return super.getTextCharacters();
 152     }
 153 }