feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,131 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import javax.xml.namespace.QName;
/**
* <p> The Attributes interface is essentially a version of the
* org.xml.sax.Attributes interface modified to use the JAX-WS QName class.</p>
*
* <p> Although namespace declarations can appear in the attribute list, the
* actual values of the local name and URI properties are
* implementation-specific. </p>
*
* <p> Applications that need to iterate through all the attributes can use the
* {@link #isNamespaceDeclaration} method to identify namespace declarations
* and skip them. </p>
*
* <p> Also, the URI property of an attribute will never be null. The value
* "" (empty string) is used for the URI of non-qualified attributes. </p>
*
* @author WS Development Team
*/
public interface Attributes {
/**
* Return the number of attributes in the list.
*
*/
public int getLength();
/**
* Return true if the attribute at the given index is a namespace
* declaration.
*
* <p> Implementations are encouraged to optimize this method by taking into
* account their internal representations of attributes. </p>
*
*/
public boolean isNamespaceDeclaration(int index);
/**
* Look up an attribute's QName by index.
*
*/
public QName getName(int index);
/**
* Look up an attribute's URI by index.
*
*/
public String getURI(int index);
/**
* Look up an attribute's local name by index.
* If attribute is a namespace declaration, result
* is expected including "xmlns:".
*/
public String getLocalName(int index);
/**
* Look up an attribute's prefix by index.
*
*/
public String getPrefix(int index);
/**
* Look up an attribute's value by index.
*
*/
public String getValue(int index);
/**
* Look up the index of an attribute by QName.
*
*/
public int getIndex(QName name);
/**
* Look up the index of an attribute by URI and local name.
*
*/
public int getIndex(String uri, String localName);
/**
* Look up the index of an attribute by local name.
*
*/
public int getIndex(String localName);
/**
* Look up the value of an attribute by QName.
*
*/
public String getValue(QName name);
/**
* Look up the value of an attribute by URI and local name.
*
*/
public String getValue(String uri, String localName);
/**
* Look up the value of an attribute by local name.
*
*/
public String getValue(String localName);
}

View File

@@ -0,0 +1,904 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import com.sun.istack.internal.FinalArrayList;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.XMLStreamException2;
import com.sun.xml.internal.ws.util.xml.DummyLocation;
import com.sun.xml.internal.ws.util.xml.XmlUtil;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import static org.w3c.dom.Node.*;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.util.Collections;
import java.util.Iterator;
/**
* Create an {@link XMLStreamReader} on top of a DOM tree.
*
* <p>
* Since various libraries as well as users often create "incorrect" DOM node,
* this class spends a lot of efforts making sure that broken DOM trees are
* nevertheless interpreted correctly.
*
* <p>
* For example, if a DOM level
* 1 tree is passed, each method will attempt to return the correct value
* by using {@link Node#getNodeName()}.
*
* <p>
* Similarly, if DOM is missing explicit namespace declarations,
* this class attempts to emulate necessary declarations.
*
*
* @author Santiago.PericasGeertsen@sun.com
* @author Kohsuke Kawaguchi
*/
public class DOMStreamReader implements XMLStreamReader, NamespaceContext {
/**
* Current DOM node being traversed.
*/
protected Node _current;
/**
* Starting node of the subtree being traversed.
*/
private Node _start;
/**
* Named mapping for attributes and NS decls for the current node.
*/
private NamedNodeMap _namedNodeMap;
/**
* If the reader points at {@link #CHARACTERS the text node},
* its whole value.
*
* <p>
* This is simply a cache of {@link Text#getWholeText()} of {@link #_current},
* but when a large binary data sent as base64 text, this could get very much
* non-trivial.
*/
protected String wholeText;
/**
* List of attributes extracted from <code>_namedNodeMap</code>.
*/
private final FinalArrayList<Attr> _currentAttributes = new FinalArrayList<Attr>();
/**
* {@link Scope} buffer.
*/
protected Scope[] scopes = new Scope[8];
/**
* Depth of the current element. The first element gets depth==0.
* Also used as the index to {@link #scopes}.
*/
protected int depth = 0;
/**
* State of this reader. Any of the valid states defined in StAX'
* XMLStreamConstants class.
*/
protected int _state;
/**
* Namespace declarations on one element.
*
* Instances are reused.
*/
protected static final class Scope {
/**
* Scope for the parent element.
*/
final Scope parent;
/**
* List of namespace declarations extracted from <code>_namedNodeMap</code>
*/
final FinalArrayList<Attr> currentNamespaces = new FinalArrayList<Attr>();
/**
* Additional namespace declarations obtained as a result of "fixing" DOM tree,
* which were not part of the original DOM tree.
*
* One entry occupies two spaces (prefix followed by URI.)
*/
final FinalArrayList<String> additionalNamespaces = new FinalArrayList<String>();
Scope(Scope parent) {
this.parent = parent;
}
void reset() {
currentNamespaces.clear();
additionalNamespaces.clear();
}
int getNamespaceCount() {
return currentNamespaces.size()+additionalNamespaces.size()/2;
}
String getNamespacePrefix(int index) {
int sz = currentNamespaces.size();
if(index< sz) {
Attr attr = currentNamespaces.get(index);
String result = attr.getLocalName();
if (result == null) {
result = QName.valueOf(attr.getNodeName()).getLocalPart();
}
return result.equals("xmlns") ? null : result;
} else {
return additionalNamespaces.get((index-sz)*2);
}
}
String getNamespaceURI(int index) {
int sz = currentNamespaces.size();
if(index< sz) {
return currentNamespaces.get(index).getValue();
} else {
return additionalNamespaces.get((index-sz)*2+1);
}
}
/**
* Returns the prefix bound to the given URI, or null.
* This method recurses to the parent.
*/
String getPrefix(String nsUri) {
for( Scope sp=this; sp!=null; sp=sp.parent ) {
for( int i=sp.currentNamespaces.size()-1; i>=0; i--) {
String result = getPrefixForAttr(sp.currentNamespaces.get(i),nsUri);
if(result!=null)
return result;
}
for( int i=sp.additionalNamespaces.size()-2; i>=0; i-=2 )
if(sp.additionalNamespaces.get(i+1).equals(nsUri))
return sp.additionalNamespaces.get(i);
}
return null;
}
/**
* Returns the namespace URI bound by the given prefix.
*
* @param prefix
* Prefix to look up.
*/
String getNamespaceURI(@NotNull String prefix) {
String nsDeclName = prefix.length()==0 ? "xmlns" : "xmlns:"+prefix;
for( Scope sp=this; sp!=null; sp=sp.parent ) {
for( int i=sp.currentNamespaces.size()-1; i>=0; i--) {
Attr a = sp.currentNamespaces.get(i);
if(a.getNodeName().equals(nsDeclName))
return a.getValue();
}
for( int i=sp.additionalNamespaces.size()-2; i>=0; i-=2 )
if(sp.additionalNamespaces.get(i).equals(prefix))
return sp.additionalNamespaces.get(i+1);
}
return null;
}
}
public DOMStreamReader() {
}
public DOMStreamReader(Node node) {
setCurrentNode(node);
}
public void setCurrentNode(Node node) {
scopes[0] = new Scope(null);
depth=0;
_start = _current = node;
_state = START_DOCUMENT;
// verifyDOMIntegrity(node);
// displayDOM(node, System.out);
}
public void close() throws XMLStreamException {
}
/**
* Called when the current node is {@link Element} to look at attribute list
* (which contains both ns decl and attributes in DOM) and split them
* to attributes-proper and namespace decls.
*/
protected void splitAttributes() {
// Clear attribute and namespace lists
_currentAttributes.clear();
Scope scope = allocateScope();
_namedNodeMap = _current.getAttributes();
if (_namedNodeMap != null) {
final int n = _namedNodeMap.getLength();
for (int i = 0; i < n; i++) {
final Attr attr = (Attr) _namedNodeMap.item(i);
final String attrName = attr.getNodeName();
if (attrName.startsWith("xmlns:") || attrName.equals("xmlns")) { // NS decl?
scope.currentNamespaces.add(attr);
}
else {
_currentAttributes.add(attr);
}
}
}
// verify that all the namespaces used in element and attributes are indeed available
ensureNs(_current);
for( int i=_currentAttributes.size()-1; i>=0; i-- ) {
Attr a = _currentAttributes.get(i);
if(fixNull(a.getNamespaceURI()).length()>0)
ensureNs(a); // no need to declare "" for attributes in the default namespace
}
}
/**
* Sub-routine of {@link #splitAttributes()}.
*
* <p>
* Makes sure that the namespace URI/prefix used in the given node is available,
* and if not, declare it on the current scope to "fix" it.
*
* It's often common to create DOM trees without putting namespace declarations,
* and this makes sure that such DOM tree will be properly marshalled.
*/
private void ensureNs(Node n) {
String prefix = fixNull(n.getPrefix());
String uri = fixNull(n.getNamespaceURI());
Scope scope = scopes[depth];
String currentUri = scope.getNamespaceURI(prefix);
if(prefix.length()==0) {
currentUri = fixNull(currentUri);
if(currentUri.equals(uri))
return; // declared correctly
} else {
if(currentUri!=null && currentUri.equals(uri))
return; // declared correctly
}
if(prefix.equals("xml") || prefix.equals("xmlns"))
return; // implicitly declared namespaces
// needs to be declared
scope.additionalNamespaces.add(prefix);
scope.additionalNamespaces.add(uri);
}
/**
* Allocate new {@link Scope} for {@link #splitAttributes()}.
*/
private Scope allocateScope() {
if(scopes.length==++depth) {
Scope[] newBuf = new Scope[scopes.length*2];
System.arraycopy(scopes,0,newBuf,0,scopes.length);
scopes = newBuf;
}
Scope scope = scopes[depth];
if(scope==null) {
scope = scopes[depth] = new Scope(scopes[depth-1]);
} else {
scope.reset();
}
return scope;
}
public int getAttributeCount() {
if (_state == START_ELEMENT)
return _currentAttributes.size();
throw new IllegalStateException("DOMStreamReader: getAttributeCount() called in illegal state");
}
/**
* Return an attribute's local name. Handle the case of DOM level 1 nodes.
*/
public String getAttributeLocalName(int index) {
if (_state == START_ELEMENT) {
String localName = _currentAttributes.get(index).getLocalName();
return (localName != null) ? localName :
QName.valueOf(_currentAttributes.get(index).getNodeName()).getLocalPart();
}
throw new IllegalStateException("DOMStreamReader: getAttributeLocalName() called in illegal state");
}
/**
* Return an attribute's qname. Handle the case of DOM level 1 nodes.
*/
public QName getAttributeName(int index) {
if (_state == START_ELEMENT) {
Node attr = _currentAttributes.get(index);
String localName = attr.getLocalName();
if (localName != null) {
String prefix = attr.getPrefix();
String uri = attr.getNamespaceURI();
return new QName(fixNull(uri), localName, fixNull(prefix));
}
else {
return QName.valueOf(attr.getNodeName());
}
}
throw new IllegalStateException("DOMStreamReader: getAttributeName() called in illegal state");
}
public String getAttributeNamespace(int index) {
if (_state == START_ELEMENT) {
String uri = _currentAttributes.get(index).getNamespaceURI();
return fixNull(uri);
}
throw new IllegalStateException("DOMStreamReader: getAttributeNamespace() called in illegal state");
}
public String getAttributePrefix(int index) {
if (_state == START_ELEMENT) {
String prefix = _currentAttributes.get(index).getPrefix();
return fixNull(prefix);
}
throw new IllegalStateException("DOMStreamReader: getAttributePrefix() called in illegal state");
}
public String getAttributeType(int index) {
if (_state == START_ELEMENT) {
return "CDATA";
}
throw new IllegalStateException("DOMStreamReader: getAttributeType() called in illegal state");
}
public String getAttributeValue(int index) {
if (_state == START_ELEMENT) {
return _currentAttributes.get(index).getNodeValue();
}
throw new IllegalStateException("DOMStreamReader: getAttributeValue() called in illegal state");
}
public String getAttributeValue(String namespaceURI, String localName) {
if (_state == START_ELEMENT) {
if (_namedNodeMap != null) {
Node attr = _namedNodeMap.getNamedItemNS(namespaceURI, localName);
return attr != null ? attr.getNodeValue() : null;
}
return null;
}
throw new IllegalStateException("DOMStreamReader: getAttributeValue() called in illegal state");
}
public String getCharacterEncodingScheme() {
return null;
}
public String getElementText() throws javax.xml.stream.XMLStreamException {
throw new RuntimeException("DOMStreamReader: getElementText() not implemented");
}
public String getEncoding() {
return null;
}
public int getEventType() {
return _state;
}
/**
* Return an element's local name. Handle the case of DOM level 1 nodes.
*/
public String getLocalName() {
if (_state == START_ELEMENT || _state == END_ELEMENT) {
String localName = _current.getLocalName();
return localName != null ? localName :
QName.valueOf(_current.getNodeName()).getLocalPart();
}
else if (_state == ENTITY_REFERENCE) {
return _current.getNodeName();
}
throw new IllegalStateException("DOMStreamReader: getAttributeValue() called in illegal state");
}
public Location getLocation() {
return DummyLocation.INSTANCE;
}
/**
* Return an element's qname. Handle the case of DOM level 1 nodes.
*/
public javax.xml.namespace.QName getName() {
if (_state == START_ELEMENT || _state == END_ELEMENT) {
String localName = _current.getLocalName();
if (localName != null) {
String prefix = _current.getPrefix();
String uri = _current.getNamespaceURI();
return new QName(fixNull(uri), localName, fixNull(prefix));
}
else {
return QName.valueOf(_current.getNodeName());
}
}
throw new IllegalStateException("DOMStreamReader: getName() called in illegal state");
}
public NamespaceContext getNamespaceContext() {
return this;
}
/**
* Verifies the current state to see if we can return the scope, and do so
* if appropriate.
*
* Used to implement a bunch of StAX API methods that have the same usage restriction.
*/
private Scope getCheckedScope() {
if (_state == START_ELEMENT || _state == END_ELEMENT) {
return scopes[depth];
}
throw new IllegalStateException("DOMStreamReader: neither on START_ELEMENT nor END_ELEMENT");
}
public int getNamespaceCount() {
return getCheckedScope().getNamespaceCount();
}
public String getNamespacePrefix(int index) {
return getCheckedScope().getNamespacePrefix(index);
}
public String getNamespaceURI(int index) {
return getCheckedScope().getNamespaceURI(index);
}
public String getNamespaceURI() {
if (_state == START_ELEMENT || _state == END_ELEMENT) {
String uri = _current.getNamespaceURI();
return fixNull(uri);
}
return null;
}
/**
* This method is not particularly fast, but shouldn't be called very
* often. If we start to use it more, we should keep track of the
* NS declarations using a NamespaceContext implementation instead.
*/
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("DOMStreamReader: getNamespaceURI(String) call with a null prefix");
}
else if (prefix.equals("xml")) {
return "http://www.w3.org/XML/1998/namespace";
}
else if (prefix.equals("xmlns")) {
return "http://www.w3.org/2000/xmlns/";
}
// check scopes
String nsUri = scopes[depth].getNamespaceURI(prefix);
if(nsUri!=null) return nsUri;
// then ancestors above start node
Node node = findRootElement();
String nsDeclName = prefix.length()==0 ? "xmlns" : "xmlns:"+prefix;
while (node.getNodeType() != DOCUMENT_NODE) {
// Is ns declaration on this element?
NamedNodeMap namedNodeMap = node.getAttributes();
Attr attr = (Attr) namedNodeMap.getNamedItem(nsDeclName);
if (attr != null)
return attr.getValue();
node = node.getParentNode();
}
return null;
}
public String getPrefix(String nsUri) {
if (nsUri == null) {
throw new IllegalArgumentException("DOMStreamReader: getPrefix(String) call with a null namespace URI");
}
else if (nsUri.equals("http://www.w3.org/XML/1998/namespace")) {
return "xml";
}
else if (nsUri.equals("http://www.w3.org/2000/xmlns/")) {
return "xmlns";
}
// check scopes
String prefix = scopes[depth].getPrefix(nsUri);
if(prefix!=null) return prefix;
// then ancestors above start node
Node node = findRootElement();
while (node.getNodeType() != DOCUMENT_NODE) {
// Is ns declaration on this element?
NamedNodeMap namedNodeMap = node.getAttributes();
for( int i=namedNodeMap.getLength()-1; i>=0; i-- ) {
Attr attr = (Attr)namedNodeMap.item(i);
prefix = getPrefixForAttr(attr,nsUri);
if(prefix!=null)
return prefix;
}
node = node.getParentNode();
}
return null;
}
/**
* Finds the root element node of the traversal.
*/
private Node findRootElement() {
int type;
Node node = _start;
while ((type = node.getNodeType()) != DOCUMENT_NODE
&& type != ELEMENT_NODE) {
node = node.getParentNode();
}
return node;
}
/**
* If the given attribute is a namespace declaration for the given namespace URI,
* return its prefix. Otherwise null.
*/
private static String getPrefixForAttr(Attr attr, String nsUri) {
String attrName = attr.getNodeName();
if (!attrName.startsWith("xmlns:") && !attrName.equals("xmlns"))
return null; // not nsdecl
if(attr.getValue().equals(nsUri)) {
if(attrName.equals("xmlns"))
return "";
String localName = attr.getLocalName();
return (localName != null) ? localName :
QName.valueOf(attrName).getLocalPart();
}
return null;
}
public Iterator getPrefixes(String nsUri) {
// This is an incorrect implementation,
// but AFAIK it's not used in the JAX-WS runtime
String prefix = getPrefix(nsUri);
if(prefix==null) return Collections.emptyList().iterator();
else return Collections.singletonList(prefix).iterator();
}
public String getPIData() {
if (_state == PROCESSING_INSTRUCTION) {
return ((ProcessingInstruction) _current).getData();
}
return null;
}
public String getPITarget() {
if (_state == PROCESSING_INSTRUCTION) {
return ((ProcessingInstruction) _current).getTarget();
}
return null;
}
public String getPrefix() {
if (_state == START_ELEMENT || _state == END_ELEMENT) {
String prefix = _current.getPrefix();
return fixNull(prefix);
}
return null;
}
public Object getProperty(String str) throws IllegalArgumentException {
return null;
}
public String getText() {
if (_state == CHARACTERS)
return wholeText;
if(_state == CDATA || _state == COMMENT || _state == ENTITY_REFERENCE)
return _current.getNodeValue();
throw new IllegalStateException("DOMStreamReader: getTextLength() called in illegal state");
}
public char[] getTextCharacters() {
return getText().toCharArray();
}
public int getTextCharacters(int sourceStart, char[] target, int targetStart,
int targetLength) throws XMLStreamException {
String text = getText();
int copiedSize = Math.min(targetLength, text.length() - sourceStart);
text.getChars(sourceStart, sourceStart + copiedSize, target, targetStart);
return copiedSize;
}
public int getTextLength() {
return getText().length();
}
public int getTextStart() {
if (_state == CHARACTERS || _state == CDATA || _state == COMMENT || _state == ENTITY_REFERENCE) {
return 0;
}
throw new IllegalStateException("DOMStreamReader: getTextStart() called in illegal state");
}
public String getVersion() {
return null;
}
public boolean hasName() {
return (_state == START_ELEMENT || _state == END_ELEMENT);
}
public boolean hasNext() throws javax.xml.stream.XMLStreamException {
return (_state != END_DOCUMENT);
}
public boolean hasText() {
if (_state == CHARACTERS || _state == CDATA || _state == COMMENT || _state == ENTITY_REFERENCE) {
return getText().trim().length() > 0;
}
return false;
}
public boolean isAttributeSpecified(int param) {
return false;
}
public boolean isCharacters() {
return (_state == CHARACTERS);
}
public boolean isEndElement() {
return (_state == END_ELEMENT);
}
public boolean isStandalone() {
return true;
}
public boolean isStartElement() {
return (_state == START_ELEMENT);
}
public boolean isWhiteSpace() {
if (_state == CHARACTERS || _state == CDATA)
return getText().trim().length()==0;
return false;
}
private static int mapNodeTypeToState(int nodetype) {
switch (nodetype) {
case CDATA_SECTION_NODE:
return CDATA;
case COMMENT_NODE:
return COMMENT;
case ELEMENT_NODE:
return START_ELEMENT;
case ENTITY_NODE:
return ENTITY_DECLARATION;
case ENTITY_REFERENCE_NODE:
return ENTITY_REFERENCE;
case NOTATION_NODE:
return NOTATION_DECLARATION;
case PROCESSING_INSTRUCTION_NODE:
return PROCESSING_INSTRUCTION;
case TEXT_NODE:
return CHARACTERS;
default:
throw new RuntimeException("DOMStreamReader: Unexpected node type");
}
}
public int next() throws XMLStreamException {
while(true) {
int r = _next();
switch (r) {
case CHARACTERS:
// if we are currently at text node, make sure that this is a meaningful text node.
Node prev = _current.getPreviousSibling();
if(prev!=null && prev.getNodeType()==Node.TEXT_NODE)
continue; // nope. this is just a continuation of previous text that should be invisible
Text t = (Text)_current;
wholeText = t.getWholeText();
if(wholeText.length()==0)
continue; // nope. this is empty text.
return CHARACTERS;
case START_ELEMENT:
splitAttributes();
return START_ELEMENT;
default:
return r;
}
}
}
protected int _next() throws XMLStreamException {
Node child;
switch (_state) {
case END_DOCUMENT:
throw new IllegalStateException("DOMStreamReader: Calling next() at END_DOCUMENT");
case START_DOCUMENT:
// Don't skip document element if this is a fragment
if (_current.getNodeType() == ELEMENT_NODE) {
return (_state = START_ELEMENT);
}
child = _current.getFirstChild();
if (child == null) {
return (_state = END_DOCUMENT);
}
else {
_current = child;
return (_state = mapNodeTypeToState(_current.getNodeType()));
}
case START_ELEMENT:
child = _current.getFirstChild();
if (child == null) {
return (_state = END_ELEMENT);
}
else {
_current = child;
return (_state = mapNodeTypeToState(_current.getNodeType()));
}
case END_ELEMENT:
depth--;
// fall through next
case CHARACTERS:
case COMMENT:
case CDATA:
case ENTITY_REFERENCE:
case PROCESSING_INSTRUCTION:
// If at the end of this fragment, then terminate traversal
if (_current == _start) {
return (_state = END_DOCUMENT);
}
Node sibling = _current.getNextSibling();
if (sibling == null) {
_current = _current.getParentNode();
// getParentNode() returns null for fragments
_state = (_current == null || _current.getNodeType() == DOCUMENT_NODE) ?
END_DOCUMENT : END_ELEMENT;
return _state;
}
else {
_current = sibling;
return (_state = mapNodeTypeToState(_current.getNodeType()));
}
case DTD:
case ATTRIBUTE:
case NAMESPACE:
default:
throw new RuntimeException("DOMStreamReader: Unexpected internal state");
}
}
public int nextTag() throws javax.xml.stream.XMLStreamException {
int eventType = next();
while (eventType == CHARACTERS && isWhiteSpace()
|| eventType == CDATA && isWhiteSpace()
|| eventType == SPACE
|| eventType == PROCESSING_INSTRUCTION
|| eventType == COMMENT)
{
eventType = next();
}
if (eventType != START_ELEMENT && eventType != END_ELEMENT) {
throw new XMLStreamException2("DOMStreamReader: Expected start or end tag");
}
return eventType;
}
public void require(int type, String namespaceURI, String localName)
throws javax.xml.stream.XMLStreamException
{
if (type != _state) {
throw new XMLStreamException2("DOMStreamReader: Required event type not found");
}
if (namespaceURI != null && !namespaceURI.equals(getNamespaceURI())) {
throw new XMLStreamException2("DOMStreamReader: Required namespaceURI not found");
}
if (localName != null && !localName.equals(getLocalName())) {
throw new XMLStreamException2("DOMStreamReader: Required localName not found");
}
}
public boolean standaloneSet() {
return true;
}
// -- Debugging ------------------------------------------------------
private static void displayDOM(Node node, java.io.OutputStream ostream) {
try {
System.out.println("\n====\n");
XmlUtil.newTransformer().transform(
new DOMSource(node), new StreamResult(ostream));
System.out.println("\n====\n");
}
catch (Exception e) {
e.printStackTrace();
}
}
private static void verifyDOMIntegrity(Node node) {
switch (node.getNodeType()) {
case ELEMENT_NODE:
case ATTRIBUTE_NODE:
// DOM level 1?
if (node.getLocalName() == null) {
System.out.println("WARNING: DOM level 1 node found");
System.out.println(" -> node.getNodeName() = " + node.getNodeName());
System.out.println(" -> node.getNamespaceURI() = " + node.getNamespaceURI());
System.out.println(" -> node.getLocalName() = " + node.getLocalName());
System.out.println(" -> node.getPrefix() = " + node.getPrefix());
}
if (node.getNodeType() == ATTRIBUTE_NODE) return;
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
verifyDOMIntegrity(attrs.item(i));
}
case DOCUMENT_NODE:
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
verifyDOMIntegrity(children.item(i));
}
}
}
private static String fixNull(String s) {
if(s==null) return "";
else return s;
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import com.sun.xml.internal.ws.message.jaxb.JAXBMessage;
import com.sun.xml.internal.ws.encoding.MtomCodec;
import javax.xml.bind.attachment.AttachmentMarshaller;
import javax.xml.stream.XMLStreamWriter;
/**
* A {@link XMLStreamWriter} that used for MTOM encoding may provide its own
* {@link AttachmentMarshaller}. The marshaller could do processing based on
* MTOM threshold, and make decisions about inlining the attachment data or not.
*
* @author Jitendra Kotamraju
* @see JAXBMessage
* @see MtomCodec
*/
public interface MtomStreamWriter {
AttachmentMarshaller getAttachmentMarshaller();
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
/**
* <p> Interface for prefix factories. </p>
*
* <p> A prefix factory is able to create a new prefix for a URI that
* was encountered for the first time when writing a document
* using an XMLWriter. </p>
*
* @author WS Development Team
*/
public interface PrefixFactory {
/**
* Return a brand new prefix for the given URI.
*/
public String getPrefix(String uri);
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import java.util.HashMap;
import java.util.Map;
/**
* <p> A prefix factory that caches the prefixes it creates. </p>
*
* @author WS Development Team
*/
public class PrefixFactoryImpl implements PrefixFactory {
public PrefixFactoryImpl(String base) {
_base = base;
_next = 1;
}
public String getPrefix(String uri) {
String prefix = null;
if (_cachedUriToPrefixMap == null) {
_cachedUriToPrefixMap = new HashMap();
} else {
prefix = (String) _cachedUriToPrefixMap.get(uri);
}
if (prefix == null) {
prefix = _base + Integer.toString(_next++);
_cachedUriToPrefixMap.put(uri, prefix);
}
return prefix;
}
private String _base;
private int _next;
private Map _cachedUriToPrefixMap;
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
import com.sun.xml.internal.ws.util.FastInfosetUtil;
import com.sun.xml.internal.ws.util.xml.XmlUtil;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Method;
import java.net.URL;
/**
* @author Santiago.PericasGeertsen@sun.com
*/
public class SourceReaderFactory {
/**
* FI FastInfosetSource class.
*/
static Class fastInfosetSourceClass;
/**
* FI <code>StAXDocumentSerializer.setEncoding()</code> method via reflection.
*/
static Method fastInfosetSource_getInputStream;
static {
// Use reflection to avoid static dependency with FI jar
try {
fastInfosetSourceClass =
Class.forName("com.sun.xml.internal.org.jvnet.fastinfoset.FastInfosetSource");
fastInfosetSource_getInputStream =
fastInfosetSourceClass.getMethod("getInputStream");
}
catch (Exception e) {
fastInfosetSourceClass = null;
}
}
public static XMLStreamReader createSourceReader(Source source, boolean rejectDTDs) {
return createSourceReader(source, rejectDTDs, null);
}
public static XMLStreamReader createSourceReader(Source source, boolean rejectDTDs, String charsetName) {
try {
if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
InputStream is = streamSource.getInputStream();
if (is != null) {
// Wrap input stream in Reader if charset is specified
if (charsetName != null) {
return XMLStreamReaderFactory.create(
source.getSystemId(), new InputStreamReader(is, charsetName), rejectDTDs);
}
else {
return XMLStreamReaderFactory.create(
source.getSystemId(), is, rejectDTDs);
}
}
else {
Reader reader = streamSource.getReader();
if (reader != null) {
return XMLStreamReaderFactory.create(
source.getSystemId(), reader, rejectDTDs);
}
else {
return XMLStreamReaderFactory.create(
source.getSystemId(), new URL(source.getSystemId()).openStream(), rejectDTDs );
}
}
}
else if (source.getClass() == fastInfosetSourceClass) {
return FastInfosetUtil.createFIStreamReader((InputStream)
fastInfosetSource_getInputStream.invoke(source));
}
else if (source instanceof DOMSource) {
DOMStreamReader dsr = new DOMStreamReader();
dsr.setCurrentNode(((DOMSource) source).getNode());
return dsr;
}
else if (source instanceof SAXSource) {
// TODO: need SAX to StAX adapter here -- Use transformer for now
Transformer tx = XmlUtil.newTransformer();
DOMResult domResult = new DOMResult();
tx.transform(source, domResult);
return createSourceReader(
new DOMSource(domResult.getNode()),
rejectDTDs);
}
else {
throw new XMLReaderException("sourceReader.invalidSource",
source.getClass().getName());
}
}
catch (Exception e) {
throw new XMLReaderException(e);
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import com.sun.xml.internal.ws.util.xml.XMLStreamReaderFilter;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.ws.WebServiceException;
import java.io.Closeable;
import java.io.IOException;
/**
* Wrapper over XMLStreamReader. It will be used primarily to
* clean up the resources such as closure on InputStream/Reader.
*
* @author Vivek Pandey
*/
public class TidyXMLStreamReader extends XMLStreamReaderFilter {
private final Closeable closeableSource;
public TidyXMLStreamReader(@NotNull XMLStreamReader reader, @Nullable Closeable closeableSource) {
super(reader);
this.closeableSource = closeableSource;
}
public void close() throws XMLStreamException {
super.close();
try {
if(closeableSource != null)
closeableSource.close();
} catch (IOException e) {
throw new WebServiceException(e);
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import com.sun.istack.internal.localization.Localizable;
import com.sun.xml.internal.ws.util.exception.JAXWSExceptionBase;
/**
* <p> XMLReaderException represents an exception that occurred while reading an
* XML document. </p>
*
* @see JAXWSExceptionBase
*
* @author WS Development Team
*/
public class XMLReaderException extends JAXWSExceptionBase {
public XMLReaderException(String key, Object... args) {
super(key, args);
}
public XMLReaderException(Throwable throwable) {
super(throwable);
}
public XMLReaderException(Localizable arg) {
super("xmlreader.nestedError", arg);
}
public String getDefaultResourceBundleName() {
return "com.sun.xml.internal.ws.resources.streaming";
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import com.sun.istack.internal.localization.Localizable;
import com.sun.xml.internal.ws.util.exception.JAXWSExceptionBase;
/**
* <p> XMLStream ReaderException represents an exception that occurred while reading an
* XML document. </p>
*
* @see JAXWSExceptionBase
*
* @author WS Development Team
*/
public class XMLStreamReaderException extends JAXWSExceptionBase {
public XMLStreamReaderException(String key, Object... args) {
super(key, args);
}
public XMLStreamReaderException(Throwable throwable) {
super(throwable);
}
public XMLStreamReaderException(Localizable arg) {
super("xmlreader.nestedError", arg);
}
public String getDefaultResourceBundleName() {
return "com.sun.xml.internal.ws.resources.streaming";
}
}

View File

@@ -0,0 +1,507 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import javax.xml.namespace.QName;
import static javax.xml.stream.XMLStreamConstants.*;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamConstants;
/**
* <p> XMLStreamReaderUtil provides some utility methods intended to be used
* in conjunction with a StAX XMLStreamReader. </p>
*
* @author WS Development Team
*/
public class XMLStreamReaderUtil {
private XMLStreamReaderUtil() {
}
public static void close(XMLStreamReader reader) {
try {
reader.close();
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public static void readRest(XMLStreamReader reader) {
try {
while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
reader.next();
}
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public static int next(XMLStreamReader reader) {
try {
int readerEvent = reader.next();
while (readerEvent != END_DOCUMENT) {
switch (readerEvent) {
case START_ELEMENT:
case END_ELEMENT:
case CDATA:
case CHARACTERS:
case PROCESSING_INSTRUCTION:
return readerEvent;
default:
// falls through ignoring event
}
readerEvent = reader.next();
}
return readerEvent;
}
catch (XMLStreamException e) {
throw wrapException(e);
}
}
public static int nextElementContent(XMLStreamReader reader) {
int state = nextContent(reader);
if (state == CHARACTERS) {
throw new XMLStreamReaderException(
"xmlreader.unexpectedCharacterContent", reader.getText());
}
return state;
}
public static void toNextTag(XMLStreamReader reader, QName name) {
// skip any whitespace
if (reader.getEventType() != XMLStreamConstants.START_ELEMENT &&
reader.getEventType() != XMLStreamConstants.END_ELEMENT) {
XMLStreamReaderUtil.nextElementContent(reader);
}
if(reader.getEventType() == XMLStreamConstants.END_ELEMENT && name.equals(reader.getName())) {
XMLStreamReaderUtil.nextElementContent(reader);
}
}
/**
* Moves next and read spaces from the reader as long as to the next element.
* Comments are ignored
* @param reader
* @return
*/
public static String nextWhiteSpaceContent(XMLStreamReader reader) {
next(reader);
return currentWhiteSpaceContent(reader);
}
/**
* Read spaces from the reader as long as to the next element, starting from
* current position. Comments are ignored.
* @param reader
* @return
*/
public static String currentWhiteSpaceContent(XMLStreamReader reader) {
// since the there might be several valid chunks (spaces/comment/spaces)
// StringBuilder must be used; it's initialized lazily, only when needed
StringBuilder whiteSpaces = null;
for (;;) {
switch (reader.getEventType()) {
case START_ELEMENT:
case END_ELEMENT:
case END_DOCUMENT:
return whiteSpaces == null ? null : whiteSpaces.toString();
case CHARACTERS:
if (reader.isWhiteSpace()) {
if (whiteSpaces == null) {
whiteSpaces = new StringBuilder();
}
whiteSpaces.append(reader.getText());
} else {
throw new XMLStreamReaderException(
"xmlreader.unexpectedCharacterContent", reader.getText());
}
}
next(reader);
}
}
public static int nextContent(XMLStreamReader reader) {
for (;;) {
int state = next(reader);
switch (state) {
case START_ELEMENT:
case END_ELEMENT:
case END_DOCUMENT:
return state;
case CHARACTERS:
if (!reader.isWhiteSpace()) {
return CHARACTERS;
}
}
}
}
/**
* Skip current element, leaving the cursor at END_ELEMENT of
* current element.
*/
public static void skipElement(XMLStreamReader reader) {
assert reader.getEventType() == START_ELEMENT;
skipTags(reader, true);
assert reader.getEventType() == END_ELEMENT;
}
/**
* Skip following siblings, leaving cursor at END_ELEMENT of
* parent element.
*/
public static void skipSiblings(XMLStreamReader reader, QName parent) {
skipTags(reader, reader.getName().equals(parent));
assert reader.getEventType() == END_ELEMENT;
}
private static void skipTags(XMLStreamReader reader, boolean exitCondition) {
try {
int state, tags = 0;
while ((state = reader.next()) != END_DOCUMENT) {
if (state == START_ELEMENT) {
tags++;
}
else if (state == END_ELEMENT) {
if (tags == 0 && exitCondition) return;
tags--;
}
}
}
catch (XMLStreamException e) {
throw wrapException(e);
}
}
/*
* Get the text of an element
*/
public static String getElementText(XMLStreamReader reader) {
try {
return reader.getElementText();
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
/*
* Get a QName with 'someUri' and 'localname' from an
* element of qname type:
* <xyz xmlns:ns1="someUri">ns1:localname</xyz>
*/
public static QName getElementQName(XMLStreamReader reader) {
try {
String text = reader.getElementText().trim();
String prefix = text.substring(0,text.indexOf(':'));
String namespaceURI = reader.getNamespaceContext().getNamespaceURI(prefix);
if (namespaceURI == null) {
namespaceURI = "";
}
String localPart = text.substring(
text.indexOf(':') + 1, text.length());
return new QName(namespaceURI, localPart);
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
/**
* Read all attributes into an data structure. Note that this method cannot
* be called multiple times to get the same list of attributes.
*/
public static Attributes getAttributes(XMLStreamReader reader) {
return (reader.getEventType() == START_ELEMENT ||
reader.getEventType() == ATTRIBUTE) ?
new AttributesImpl(reader) : null;
}
public static void verifyReaderState(XMLStreamReader reader, int expectedState) {
int state = reader.getEventType();
if (state != expectedState) {
throw new XMLStreamReaderException(
"xmlreader.unexpectedState",
getStateName(expectedState), getStateName(state));
}
}
public static void verifyTag(XMLStreamReader reader, String namespaceURI, String localName) {
if (!localName.equals(reader.getLocalName()) || !namespaceURI.equals(reader.getNamespaceURI())) {
throw new XMLStreamReaderException(
"xmlreader.unexpectedState.tag",
"{" + namespaceURI + "}" + localName,
"{" + reader.getNamespaceURI() + "}" + reader.getLocalName());
}
}
public static void verifyTag(XMLStreamReader reader, QName name) {
verifyTag(reader, name.getNamespaceURI(), name.getLocalPart());
}
public static String getStateName(XMLStreamReader reader) {
return getStateName(reader.getEventType());
}
public static String getStateName(int state) {
switch (state) {
case ATTRIBUTE:
return "ATTRIBUTE";
case CDATA:
return "CDATA";
case CHARACTERS:
return "CHARACTERS";
case COMMENT:
return "COMMENT";
case DTD:
return "DTD";
case END_DOCUMENT:
return "END_DOCUMENT";
case END_ELEMENT:
return "END_ELEMENT";
case ENTITY_DECLARATION:
return "ENTITY_DECLARATION";
case ENTITY_REFERENCE:
return "ENTITY_REFERENCE";
case NAMESPACE:
return "NAMESPACE";
case NOTATION_DECLARATION:
return "NOTATION_DECLARATION";
case PROCESSING_INSTRUCTION:
return "PROCESSING_INSTRUCTION";
case SPACE:
return "SPACE";
case START_DOCUMENT:
return "START_DOCUMENT";
case START_ELEMENT:
return "START_ELEMENT";
default :
return "UNKNOWN";
}
}
private static XMLStreamReaderException wrapException(XMLStreamException e) {
return new XMLStreamReaderException("xmlreader.ioException",e);
}
// -- Auxiliary classes ----------------------------------------------
/**
* AttributesImpl class copied from old StAXReader. This class is used to implement
* getAttributes() on a StAX Reader.
*/
public static class AttributesImpl implements Attributes {
static final String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";
static class AttributeInfo {
private QName name;
private String value;
public AttributeInfo(QName name, String value) {
this.name = name;
if (value == null) {
// e.g., <return xmlns=""> -- stax returns null
this.value = "";
} else {
this.value = value;
}
}
QName getName() {
return name;
}
String getValue() {
return value;
}
/*
* Return "xmlns:" as part of name if namespace.
*/
String getLocalName() {
if (isNamespaceDeclaration()) {
if (name.getLocalPart().equals("")) {
return "xmlns";
}
return "xmlns:" + name.getLocalPart();
}
return name.getLocalPart();
}
boolean isNamespaceDeclaration() {
return (name.getNamespaceURI() == XMLNS_NAMESPACE_URI);
}
}
// stores qname and value for each attribute
AttributeInfo [] atInfos;
/*
* Will create a list that contains the namespace declarations
* as well as the other attributes.
*/
public AttributesImpl(XMLStreamReader reader) {
if (reader == null) {
// this is the case when we call getAttributes() on the
// reader when it is not on a start tag
atInfos = new AttributeInfo[0];
} else {
// this is the normal case
int index = 0;
int namespaceCount = reader.getNamespaceCount();
int attributeCount = reader.getAttributeCount();
atInfos = new AttributeInfo[namespaceCount + attributeCount];
for (int i=0; i<namespaceCount; i++) {
String namespacePrefix = reader.getNamespacePrefix(i);
// will be null if default prefix. QName can't take null
if (namespacePrefix == null) {
namespacePrefix = "";
}
atInfos[index++] = new AttributeInfo(
new QName(XMLNS_NAMESPACE_URI,
namespacePrefix,
"xmlns"),
reader.getNamespaceURI(i));
}
for (int i=0; i<attributeCount; i++) {
atInfos[index++] = new AttributeInfo(
reader.getAttributeName(i),
reader.getAttributeValue(i));
}
}
}
public int getLength() {
return atInfos.length;
}
public String getLocalName(int index) {
if (index >= 0 && index < atInfos.length) {
return atInfos[index].getLocalName();
}
return null;
}
public QName getName(int index) {
if (index >= 0 && index < atInfos.length) {
return atInfos[index].getName();
}
return null;
}
public String getPrefix(int index) {
if (index >= 0 && index < atInfos.length) {
return atInfos[index].getName().getPrefix();
}
return null;
}
public String getURI(int index) {
if (index >= 0 && index < atInfos.length) {
return atInfos[index].getName().getNamespaceURI();
}
return null;
}
public String getValue(int index) {
if (index >= 0 && index < atInfos.length) {
return atInfos[index].getValue();
}
return null;
}
public String getValue(QName name) {
int index = getIndex(name);
if (index != -1) {
return atInfos[index].getValue();
}
return null;
}
public String getValue(String localName) {
int index = getIndex(localName);
if (index != -1) {
return atInfos[index].getValue();
}
return null;
}
public String getValue(String uri, String localName) {
int index = getIndex(uri, localName);
if (index != -1) {
return atInfos[index].getValue();
}
return null;
}
public boolean isNamespaceDeclaration(int index) {
if (index >= 0 && index < atInfos.length) {
return atInfos[index].isNamespaceDeclaration();
}
return false;
}
public int getIndex(QName name) {
for (int i=0; i<atInfos.length; i++) {
if (atInfos[i].getName().equals(name)) {
return i;
}
}
return -1;
}
public int getIndex(String localName) {
for (int i=0; i<atInfos.length; i++) {
if (atInfos[i].getName().getLocalPart().equals(localName)) {
return i;
}
}
return -1;
}
public int getIndex(String uri, String localName) {
QName qName;
for (int i=0; i<atInfos.length; i++) {
qName = atInfos[i].getName();
if (qName.getNamespaceURI().equals(uri) &&
qName.getLocalPart().equals(localName)) {
return i;
}
}
return -1;
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import com.sun.istack.internal.localization.Localizable;
import com.sun.xml.internal.ws.util.exception.JAXWSExceptionBase;
/**
* <p> XMLWriterException represents an exception that occurred while writing
* an XML document. </p>
*
* @see JAXWSExceptionBase
*
* @author WS Development Team
*/
public class XMLStreamWriterException extends JAXWSExceptionBase {
public XMLStreamWriterException(String key, Object... args) {
super(key, args);
}
public XMLStreamWriterException(Throwable throwable) {
super(throwable);
}
public XMLStreamWriterException(Localizable arg) {
super("xmlwriter.nestedError", arg);
}
public String getDefaultResourceBundleName() {
return "com.sun.xml.internal.ws.resources.streaming";
}
}

View File

@@ -0,0 +1,146 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.xml.internal.ws.streaming;
import com.sun.istack.internal.Nullable;
import com.sun.xml.internal.ws.api.streaming.XMLStreamWriterFactory;
import com.sun.xml.internal.ws.encoding.HasEncoding;
import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.XMLStreamException;
import java.util.Map;
import java.io.OutputStream;
/**
* <p>XMLStreamWriterUtil provides some utility methods intended to be used
* in conjunction with a StAX XMLStreamWriter. </p>
*
* @author Santiago.PericasGeertsen@sun.com
*/
public class XMLStreamWriterUtil {
private XMLStreamWriterUtil() {
}
/**
* Gives the underlying stream for XMLStreamWriter. It closes any start elements, and returns the stream so
* that JAXB can write infoset directly to the stream.
*
* @param writer XMLStreamWriter for which stream is required
* @return underlying OutputStream, null if writer doesn't provide a way to get it
* @throws XMLStreamException if any of writer operations throw the exception
*/
public static @Nullable OutputStream getOutputStream(XMLStreamWriter writer) throws XMLStreamException {
Object obj = null;
XMLStreamWriter xmlStreamWriter =
writer instanceof XMLStreamWriterFactory.HasEncodingWriter ?
((XMLStreamWriterFactory.HasEncodingWriter) writer).getWriter()
: writer;
// Hack for JDK6's SJSXP
if (xmlStreamWriter instanceof Map) {
obj = ((Map) xmlStreamWriter).get("sjsxp-outputstream");
}
// woodstox
if (obj == null) {
try {
obj = writer.getProperty("com.ctc.wstx.outputUnderlyingStream");
} catch(Exception ie) {
// Catch all exceptions. SJSXP in JDK throws NPE
// nothing to do here
}
}
// SJSXP
if (obj == null) {
try {
obj = writer.getProperty("http://java.sun.com/xml/stream/properties/outputstream");
} catch(Exception ie) {
// Catch all exceptions. SJSXP in JDK throws NPE
// nothing to do here
}
}
if (obj != null) {
writer.writeCharacters(""); // Force completion of open elems
writer.flush();
return (OutputStream)obj;
}
return null;
}
/**
* Gives the encoding with which XMLStreamWriter is created.
*
* @param writer XMLStreamWriter for which encoding is required
* @return null if cannot be found, else the encoding
*/
public static @Nullable String getEncoding(XMLStreamWriter writer) {
/*
* TODO Add reflection logic to handle woodstox writer
* as it implements XMLStreamWriter2#getEncoding()
* It's not that important since woodstox writer is typically wrapped
* in a writer with HasEncoding
*/
return (writer instanceof HasEncoding)
? ((HasEncoding)writer).getEncoding()
: null;
}
public static String encodeQName(XMLStreamWriter writer, QName qname,
PrefixFactory prefixFactory)
{
// NOTE: Here it is assumed that we do not serialize using default
// namespace declarations and therefore that writer.getPrefix will
// never return ""
try {
String namespaceURI = qname.getNamespaceURI();
String localPart = qname.getLocalPart();
if (namespaceURI == null || namespaceURI.equals("")) {
return localPart;
}
else {
String prefix = writer.getPrefix(namespaceURI);
if (prefix == null) {
prefix = prefixFactory.getPrefix(namespaceURI);
writer.writeNamespace(prefix, namespaceURI);
}
return prefix + ":" + localPart;
}
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
}