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