org.apache.axiom.om.impl.SwitchingWrapper Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.axiom.om.impl;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Stack;
import javax.activation.DataHandler;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.apache.axiom.ext.stax.CharacterDataReader;
import org.apache.axiom.ext.stax.datahandler.DataHandlerProvider;
import org.apache.axiom.ext.stax.datahandler.DataHandlerReader;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMComment;
import org.apache.axiom.om.OMContainer;
import org.apache.axiom.om.OMDataSource;
import org.apache.axiom.om.OMDocType;
import org.apache.axiom.om.OMDocument;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMNode;
import org.apache.axiom.om.OMProcessingInstruction;
import org.apache.axiom.om.OMSerializable;
import org.apache.axiom.om.OMSourcedElement;
import org.apache.axiom.om.OMText;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.apache.axiom.om.impl.OMNavigator;
import org.apache.axiom.om.impl.builder.StAXBuilder;
import org.apache.axiom.om.impl.exception.OMStreamingException;
import org.apache.axiom.util.namespace.MapBasedNamespaceContext;
import org.apache.axiom.util.stax.AbstractXMLStreamReader;
import org.apache.axiom.util.stax.DummyLocation;
import org.apache.axiom.util.stax.XMLStreamReaderUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Class used internally by {@link OMStAXWrapper}.
*/
class SwitchingWrapper extends AbstractXMLStreamReader
implements DataHandlerReader, CharacterDataReader, XMLStreamConstants {
private static final Log log = LogFactory.getLog(SwitchingWrapper.class);
private static boolean DEBUG_ENABLED = log.isDebugEnabled();
/** Field navigator */
private OMNavigator navigator;
/** Field builder */
private OMXMLParserWrapper builder;
/** Field parser */
private XMLStreamReader parser;
/**
* The {@link DataHandlerReader} extension of the underlying parser, or null
* if the parser doesn't support this extension.
*/
private DataHandlerReader dataHandlerReader;
private boolean _isClosed = false; // Indicate if parser is closed
private boolean _releaseParserOnClose = false; // Defaults to legacy behavior, which is keep the reference
/** Field rootNode */
private OMContainer rootNode;
/** Field isFirst */
private boolean isFirst = true;
// Navigable means the output should be taken from the navigator.
// As soon as the navigator returns a null navigable will be reset
// to false and the subsequent events will be taken from the builder
// or the parser directly.
/** Field NAVIGABLE */
private static final short NAVIGABLE = 0;
private static final short SWITCH_AT_NEXT = 1;
private static final short COMPLETED = 2;
private static final short SWITCHED = 3;
private static final short DOCUMENT_COMPLETE = 4;
/** Field state */
private short state;
/** Field currentEvent Default set to START_DOCUMENT */
private int currentEvent = START_DOCUMENT;
// SwitchingAllowed is set to false by default.
// This means that unless the user explicitly states
// that he wants things not to be cached, everything will
// be cached.
/** Field switchingAllowed */
boolean switchingAllowed = false;
// namespaceURI interning
// default is false because most XMLStreamReader implementations don't do interning
// due to performance impacts
boolean namespaceURIInterning = false;
/** Field elementStack */
private Stack nodeStack = null;
// keeps the next event. The parser actually keeps one step ahead to
// detect the end of navigation. (at the end of the stream the navigator
// returns a null
/** Field nextNode */
private OMSerializable nextNode = null;
// holder for the current node. Needs this to generate events from the
// current node
/** Field currentNode */
private OMSerializable currentNode = null;
// needs this to refer to the last known node
/** Field lastNode */
private OMSerializable lastNode = null;
/** Track depth to ensure we stop generating events when we are done with the root node. */
int depth = 0;
// Cache attributes and namespaces. This avoids creating a new Iterator for every call
// to getAttributeXXX and getNamespaceXXX. A value of -1 indicates that the
// attributes or namespaces for the current element have not been loaded yet. The
// two arrays are resized on demand.
private int attributeCount = -1;
private OMAttribute[] attributes = new OMAttribute[16];
private int namespaceCount = -1;
private OMNamespace[] namespaces = new OMNamespace[16];
/**
* Method setAllowSwitching.
*
* @param b
*/
public void setAllowSwitching(boolean b) {
this.switchingAllowed = b;
}
/**
* Method isAllowSwitching.
*
* @return Returns boolean.
*/
public boolean isAllowSwitching() {
return switchingAllowed;
}
/**
* Set namespace uri interning
* @param b
*/
public void setNamespaceURIInterning(boolean b) {
this.namespaceURIInterning = b;
}
/**
* @return if namespace uri interning
*/
public boolean isNamespaceURIInterning() {
return this.namespaceURIInterning;
}
/**
* Constructor.
*
* @param builder
* @param startNode
* @param cache
*/
public SwitchingWrapper(OMXMLParserWrapper builder, OMContainer startNode,
boolean cache) {
// create a navigator
this.navigator = new OMNavigator(startNode);
this.builder = builder;
this.rootNode = startNode;
// initiate the next and current nodes
// Note - navigator is written in such a way that it first
// returns the starting node at the first call to it
// Note - for OMSourcedElements, temporarily set caching
// to get the initial navigator nodes
boolean resetCache = false;
try {
if (startNode instanceof OMSourcedElement &&
!cache && builder != null) {
if (!builder.isCache()) {
resetCache = true;
}
builder.setCache(true); // bootstrap the navigator
}
} catch(Throwable t) {}
currentNode = navigator.getNext();
updateNextNode();
if (resetCache) {
builder.setCache(cache);
}
switchingAllowed = !cache;
if (startNode instanceof OMDocument) {
try {
next();
} catch (XMLStreamException ex) {
throw new OMException(ex);
}
}
}
/**
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getPrefix()
*/
public String getPrefix() {
if (parser != null && currentEvent != END_DOCUMENT) {
return parser.getPrefix();
} else {
if ((currentEvent == START_ELEMENT)
|| (currentEvent == END_ELEMENT)) {
OMNamespace ns = ((OMElement) lastNode).getNamespace();
if (ns == null) {
return null;
} else {
String prefix = ns.getPrefix();
return prefix == null || prefix.length() == 0 ? null : prefix;
}
} else {
throw new IllegalStateException();
}
}
}
/**
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getNamespaceURI()
*/
public String getNamespaceURI() {
String returnStr;
if (parser != null && currentEvent != END_DOCUMENT) {
returnStr = parser.getNamespaceURI();
} else {
if ((currentEvent == START_ELEMENT)
|| (currentEvent == END_ELEMENT)
|| (currentEvent == NAMESPACE)) {
OMNamespace ns = ((OMElement) lastNode).getNamespace();
if (ns == null) {
returnStr = null;
} else {
String namespaceURI = ns.getNamespaceURI();
returnStr = namespaceURI.length() == 0 ? null : namespaceURI;
}
} else {
throw new IllegalStateException();
}
}
// By default most parsers don't intern the namespace.
// Unfortunately the property to detect interning on the delegate parsers is hard to detect.
// Woodstox has a proprietary property on the XMLInputFactory.
// IBM has a proprietary property on the XMLStreamReader.
// For now only force the interning if requested.
if (this.isNamespaceURIInterning()) {
returnStr = (returnStr != null) ? returnStr.intern() : null;
}
return returnStr;
}
/**
* @return Returns boolean.
* @see javax.xml.stream.XMLStreamReader#hasName()
*/
public boolean hasName() {
if (parser != null && currentEvent != END_DOCUMENT) {
return parser.hasName();
} else {
return ((currentEvent == START_ELEMENT)
|| (currentEvent == END_ELEMENT));
}
}
/**
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getLocalName()
*/
public String getLocalName() {
if (parser != null && currentEvent != END_DOCUMENT) {
return parser.getLocalName();
} else {
if ((currentEvent == START_ELEMENT)
|| (currentEvent == END_ELEMENT)
|| (currentEvent == ENTITY_REFERENCE)) {
return ((OMElement) lastNode).getLocalName();
} else {
throw new IllegalStateException();
}
}
}
/**
* @return Returns QName.
* @see javax.xml.stream.XMLStreamReader#getName()
*/
public QName getName() {
if (parser != null && currentEvent != END_DOCUMENT) {
return parser.getName();
} else {
if ((currentEvent == START_ELEMENT)
|| (currentEvent == END_ELEMENT)) {
return getQName((OMElement) lastNode);
} else {
throw new IllegalStateException();
}
}
}
/**
* @return Returns int.
* @see javax.xml.stream.XMLStreamReader#getTextLength()
*/
public int getTextLength() {
if (parser != null) {
return parser.getTextLength();
} else if (currentEvent == DTD) {
// Not sure if that conforms to the StAX spec, but it is what Woodstox does
throw new IllegalStateException();
} else {
return getTextFromNode().length();
}
}
/**
* @return Returns int.
* @see javax.xml.stream.XMLStreamReader#getTextStart()
*/
public int getTextStart() {
if (parser != null) {
return parser.getTextStart();
} else {
if (currentEvent == DTD) {
// Not sure if that conforms to the StAX spec, but it is what Woodstox does
throw new IllegalStateException();
} else if (hasText()) {
// getTextCharacters always returns a new char array and the start
// index is therefore always 0
return 0;
} else {
throw new IllegalStateException();
}
}
}
/**
* @param sourceStart
* @param target
* @param targetStart
* @param length
* @return Returns int.
* @throws XMLStreamException
* @see javax.xml.stream.XMLStreamReader#getTextCharacters(int, char[], int, int)
*/
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
throws XMLStreamException {
if (parser != null) {
try {
return parser.getTextCharacters(sourceStart, target, targetStart, length);
} catch (XMLStreamException e) {
throw new OMStreamingException(e);
}
} else {
if (currentEvent == DTD) {
// Not sure if that conforms to the StAX spec, but it is what Woodstox does
throw new IllegalStateException();
} else {
String text = getTextFromNode();
int copied = Math.min(length, text.length()-sourceStart);
text.getChars(sourceStart, sourceStart + copied, target, targetStart);
return copied;
}
}
}
/**
* @return Returns char[].
* @see javax.xml.stream.XMLStreamReader#getTextCharacters()
*/
public char[] getTextCharacters() {
if (parser != null) {
return parser.getTextCharacters();
} else if (currentEvent == DTD) {
// Not sure if that conforms to the StAX spec, but it is what Woodstox does
throw new IllegalStateException();
} else {
return getTextFromNode().toCharArray();
}
}
/**
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getText()
*/
public String getText() {
if (parser != null) {
return parser.getText();
} else {
return getTextFromNode();
}
}
private String getTextFromNode() {
switch (currentEvent) {
case CHARACTERS:
case CDATA:
case SPACE:
return ((OMText)lastNode).getText();
case COMMENT:
return ((OMComment)lastNode).getValue();
case DTD:
return ((OMDocType)lastNode).getValue();
default:
throw new IllegalStateException();
}
}
public void writeTextTo(Writer writer) throws XMLStreamException, IOException {
if (parser != null) {
XMLStreamReaderUtils.writeTextTo(parser, writer);
} else {
switch (currentEvent) {
case CHARACTERS:
case CDATA:
case SPACE:
OMText text = (OMText)lastNode;
if (text.isCharacters()) {
writer.write(text.getTextCharacters());
} else {
// TODO: we should cover the binary case in an optimized way
writer.write(text.getText());
}
break;
case COMMENT:
writer.write(((OMComment)lastNode).getValue());
break;
default:
throw new IllegalStateException();
}
}
}
/**
* @return Returns int.
* @see javax.xml.stream.XMLStreamReader#getEventType()
*/
// todo this should be improved
public int getEventType() {
return currentEvent;
}
private void loadAttributes() {
if (attributeCount == -1) {
attributeCount = 0;
for (Iterator it = ((OMElement)lastNode).getAllAttributes(); it.hasNext(); ) {
OMAttribute attr = (OMAttribute)it.next();
if (attributeCount == attributes.length) {
OMAttribute[] newAttributes = new OMAttribute[attributes.length*2];
System.arraycopy(attributes, 0, newAttributes, 0, attributes.length);
attributes = newAttributes;
}
attributes[attributeCount] = attr;
attributeCount++;
}
}
}
private void loadNamespaces() {
if (namespaceCount == -1) {
namespaceCount = 0;
for (Iterator it = ((OMElement)lastNode).getAllDeclaredNamespaces(); it.hasNext(); ) {
OMNamespace ns = (OMNamespace)it.next();
// Axiom internally creates an OMNamespace instance for the "xml" prefix, even
// if it is not declared explicitly. Filter this instance out.
if (!"xml".equals(ns.getPrefix())) {
if (namespaceCount == namespaces.length) {
OMNamespace[] newNamespaces = new OMNamespace[namespaces.length*2];
System.arraycopy(namespaces, 0, newNamespaces, 0, namespaces.length);
namespaces = newNamespaces;
}
namespaces[namespaceCount] = ns;
namespaceCount++;
}
}
}
}
/**
* @param i
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getNamespaceURI
*/
public String getNamespaceURI(int i) {
String returnString = null;
if (parser != null) {
returnString = parser.getNamespaceURI(i);
} else {
if (isStartElement() || isEndElement()
|| (currentEvent == NAMESPACE)) {
loadNamespaces();
returnString = namespaces[i].getNamespaceURI();
}
}
/*
The following line is necessary to overcome an issue where the empty
namespace URI returning null rather than the empty string. Our resolution
is to return "" if the return is actually null
Note that this is not the case for getNamespaceURI(prefix) method
where the contract clearly specifies that the return may be null
*/
if (returnString == null) returnString = "";
return returnString;
}
/**
* @param i
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getNamespacePrefix
*/
public String getNamespacePrefix(int i) {
String returnString = null;
if (parser != null) {
returnString = parser.getNamespacePrefix(i);
} else {
if (isStartElement() || isEndElement()
|| (currentEvent == NAMESPACE)) {
loadNamespaces();
String prefix = namespaces[i].getPrefix();
returnString = prefix == null || prefix.length() == 0 ? null : prefix;
}
}
return returnString;
}
/**
* @return Returns int.
* @see javax.xml.stream.XMLStreamReader#getNamespaceCount()
*/
public int getNamespaceCount() {
if (parser != null && currentEvent != END_DOCUMENT) {
return parser.getNamespaceCount();
} else {
if (isStartElement() || isEndElement()
|| (currentEvent == NAMESPACE)) {
loadNamespaces();
return namespaceCount;
} else {
throw new IllegalStateException();
}
}
}
/**
* @param i
* @return Returns boolean.
* @see javax.xml.stream.XMLStreamReader#isAttributeSpecified
*/
public boolean isAttributeSpecified(int i) {
if (parser != null) {
return parser.isAttributeSpecified(i);
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
// The Axiom object model doesn't store this information,
// but returning true is a reasonable default.
return true;
} else {
throw new IllegalStateException(
"attribute type accessed in illegal event!");
}
}
}
/**
* @param i
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getAttributeValue
*/
public String getAttributeValue(int i) {
String returnString = null;
if (parser != null) {
returnString = parser.getAttributeValue(i);
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
loadAttributes();
returnString = attributes[i].getAttributeValue();
} else {
throw new IllegalStateException(
"attribute type accessed in illegal event!");
}
}
return returnString;
}
/**
* @param i
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getAttributeType
*/
public String getAttributeType(int i) {
String returnString = null;
if (parser != null) {
returnString = parser.getAttributeType(i);
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
loadAttributes();
returnString = attributes[i].getAttributeType();
} else {
throw new IllegalStateException(
"attribute type accessed in illegal event!");
}
}
return returnString;
}
/**
* @param i
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getAttributePrefix
*/
public String getAttributePrefix(int i) {
String returnString = null;
if (parser != null) {
returnString = parser.getAttributePrefix(i);
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
loadAttributes();
OMAttribute attrib = attributes[i];
if (attrib != null) {
OMNamespace nameSpace = attrib.getNamespace();
if (nameSpace != null) {
returnString = nameSpace.getPrefix();
}
}
} else {
throw new IllegalStateException(
"attribute prefix accessed in illegal event!");
}
}
return returnString;
}
/**
* @param i
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getAttributeLocalName
*/
public String getAttributeLocalName(int i) {
String returnString = null;
if (parser != null) {
returnString = parser.getAttributeLocalName(i);
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
loadAttributes();
returnString = attributes[i].getLocalName();
} else {
throw new IllegalStateException(
"attribute localName accessed in illegal event!");
}
}
return returnString;
}
/**
* @param i
* @return Returns String.
* @see javax.xml.stream.XMLStreamReader#getAttributeNamespace
*/
public String getAttributeNamespace(int i) {
String returnString = null;
if (parser != null) {
returnString = parser.getAttributeNamespace(i);
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
loadAttributes();
OMAttribute attrib = attributes[i];
if (attrib != null) {
OMNamespace nameSpace = attrib.getNamespace();
if (nameSpace != null) {
returnString = nameSpace.getNamespaceURI();
}
}
} else {
throw new IllegalStateException(
"attribute nameSpace accessed in illegal event!");
}
}
return returnString;
}
/**
* @param i
* @return Returns QName.
* @see javax.xml.stream.XMLStreamReader#getAttributeName
*/
public QName getAttributeName(int i) {
QName returnQName = null;
if (parser != null) {
returnQName = parser.getAttributeName(i);
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
loadAttributes();
returnQName = attributes[i].getQName();
} else {
throw new IllegalStateException(
"attribute count accessed in illegal event!");
}
}
return returnQName;
}
/**
* @return Returns int.
* @see javax.xml.stream.XMLStreamReader#getAttributeCount
*/
public int getAttributeCount() {
int returnCount = 0;
if (parser != null) {
returnCount = parser.getAttributeCount();
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
loadAttributes();
returnCount = attributeCount;
} else {
throw new IllegalStateException(
"attribute count accessed in illegal event (" +
currentEvent + ")!");
}
}
return returnCount;
}
// todo
/**
* Method getAttributeValue.
*
* @param s
* @param s1
* @return Returns String.
*/
public String getAttributeValue(String s, String s1) {
String returnString = null;
if (parser != null) {
returnString = parser.getAttributeValue(s, s1);
} else {
if (isStartElement() || (currentEvent == ATTRIBUTE)) {
QName qname = new QName(s, s1);
OMAttribute attrib = ((OMElement) lastNode).getAttribute(qname);
if (attrib != null) {
returnString = attrib.getAttributeValue();
}
} else {
throw new IllegalStateException(
"attribute type accessed in illegal event!");
}
}
return returnString;
}
/**
* Method isWhiteSpace.
*
* @return Returns boolean.
*/
public boolean isWhiteSpace() {
if (parser != null) {
return parser.isWhiteSpace();
} else {
switch (currentEvent) {
case SPACE:
return true;
case CHARACTERS:
// XMLStreamReader Javadoc says that isWhiteSpace "returns true if the cursor
// points to a character data event that consists of all whitespace". This
// means that this method may return true for a CHARACTER event and we need
// to scan the text of the node.
String text = getTextFromNode();
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy