You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ch...@apache.org on 2005/03/15 11:56:40 UTC
svn commit: r157534 [3/4] - in
webservices/axis/trunk/archive/java/scratch/Thilina/MTOM: lib/
src/java/org/apache/axis/impl/ src/java/org/apache/axis/om/impl/
src/java/org/apache/axis/om/impl/llom/
src/java/org/apache/axis/om/impl/llom/builder/
src/java/org/apache/axis/om/impl/llom/exception/
src/java/org/apache/axis/om/impl/llom/mtom/
src/java/org/apache/axis/om/impl/llom/serialize/
src/java/org/apache/axis/om/impl/llom/traverse/ src/test-resources/
src/test/org/apache/axis/om/ src/test/org/apache/axis/om/impl/
src/test/org/apache/axis/om/impl/llom/
src/test/org/apache/axis/om/impl/llom/mtom/
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXBuilder.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXBuilder.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXBuilder.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,462 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.axis.om.impl.llom.builder;
+
+import java.io.InputStream;
+
+import org.apache.axis.om.OMConstants;
+import org.apache.axis.om.OMElement;
+import org.apache.axis.om.OMException;
+import org.apache.axis.om.OMFactory;
+import org.apache.axis.om.OMNamespace;
+import org.apache.axis.om.OMNode;
+import org.apache.axis.om.OMXMLParserWrapper;
+import org.apache.axis.om.impl.llom.OMElementImpl;
+import org.apache.axis.om.impl.llom.OMNodeImpl;
+
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+/**
+ * OM should be able to built from any data source. And the model it builds may be a SOAP specific one
+ * or just an XML model. This class will give some common functionality of OM Building from StAX.
+ */
+public abstract class StAXBuilder implements OMXMLParserWrapper {
+ /**
+ * Field ombuilderFactory
+ */
+ protected OMFactory ombuilderFactory;
+
+ /**
+ * Field parser
+ */
+ protected XMLStreamReader parser;
+
+ /**
+ * Field omfactory
+ */
+ protected OMFactory omfactory;
+
+ /**
+ * Field lastNode
+ */
+ protected OMNode lastNode;
+
+ // returns the state of completion
+
+ /**
+ * Field done
+ */
+ protected boolean done = false;
+
+ // keeps the state of the cache
+
+ /**
+ * Field cache
+ */
+ protected boolean cache = true;
+
+ // keeps the state of the parser access. if the parser is
+ // accessed atleast once,this flag will be set
+
+ /**
+ * Field parserAccessed
+ */
+ protected boolean parserAccessed = false;
+
+ /**
+ * Constructor StAXBuilder
+ *
+ * @param ombuilderFactory
+ * @param parser
+ * @throws FactoryConfigurationError
+ * @throws XMLStreamException
+ */
+ protected StAXBuilder(OMFactory ombuilderFactory, InputStream inStream) throws XMLStreamException, FactoryConfigurationError {
+ this.ombuilderFactory = ombuilderFactory;
+ this.parser = XMLInputFactory.newInstance().createXMLStreamReader(inStream);
+ omfactory = OMFactory.newInstance();
+ }
+
+ /**
+ * Constructor StAXBuilder
+ *
+ * @param parser
+ * @throws FactoryConfigurationError
+ * @throws XMLStreamException
+ */
+ protected StAXBuilder(InputStream inStream) throws XMLStreamException, FactoryConfigurationError {
+ this(OMFactory.newInstance(), inStream);
+ omfactory = OMFactory.newInstance();
+ }
+ // want suggestions
+ /*
+ * Edited by Thilina Gunarathne
+ */
+ protected StAXBuilder()
+ {
+ this.ombuilderFactory = OMFactory.newInstance();
+ omfactory = OMFactory.newInstance();
+ }
+ protected StAXBuilder(OMFactory ombuilderFactory) {
+ this.ombuilderFactory = ombuilderFactory;
+ omfactory = OMFactory.newInstance();
+ }
+
+ /*
+ * Edit Over
+ */
+
+ /**
+ * Method setOmbuilderFactory
+ *
+ * @param ombuilderFactory
+ */
+ public void setOmbuilderFactory(OMFactory ombuilderFactory) {
+ this.ombuilderFactory = ombuilderFactory;
+ }
+
+ /*
+ * Edited by Thilina Gunarathne
+
+ // clarify how to enforce to do this
+ protected void setParser(XMLStreamReader parser) {
+ this.parser = parser;
+ }
+
+ * Edit Over
+ */
+ /**
+ * Method processNamespaceData
+ *
+ * @param node
+ * @param isSOAPElement
+ */
+ protected abstract void processNamespaceData(OMElement node,
+ boolean isSOAPElement);
+
+ // since the behaviors are different when it comes to namespaces
+ // this must be implemented differently
+
+ /**
+ * Method processAttributes
+ *
+ * @param node
+ */
+ protected void processAttributes(OMElement node) {
+ int attribCount = parser.getAttributeCount();
+ for (int i = 0; i < attribCount; i++) {
+ OMNamespace ns = null;
+ String uri = parser.getAttributeNamespace(i);
+ if (uri.hashCode() != 0) {
+ ns = node.findInScopeNamespace(uri,
+ parser.getAttributePrefix(i));
+ }
+
+ // todo if the attributes are supposed to namespace qualified all the time
+ // todo then this should throw an exception here
+ node.insertAttribute(parser.getAttributeLocalName(i),
+ parser.getAttributeValue(i), ns);
+ }
+ }
+
+ /**
+ * Method createOMText
+ *
+ * @return
+ * @throws OMException
+ */
+ protected OMNode createOMText() throws OMException {
+ if (lastNode == null) {
+ throw new OMException();
+ }
+ OMNode node;
+ if (lastNode.isComplete()) {
+ node = omfactory.createText(lastNode.getParent(), parser.getText());
+ lastNode.setNextSibling(node);
+ node.setPreviousSibling(lastNode);
+ } else {
+ OMElementImpl e = (OMElementImpl) lastNode;
+ node = omfactory.createText(e, parser.getText());
+ e.setFirstChild(node);
+ }
+ return node;
+ }
+
+ /**
+ * Method reset
+ *
+ * @param node
+ * @throws OMException
+ */
+ public void reset(OMNode node) throws OMException {
+ lastNode = null;
+ }
+
+ /**
+ * Method discard
+ *
+ * @param el
+ * @throws OMException
+ */
+ public void discard(OMElement el) throws OMException {
+ OMElementImpl elementImpl = null;
+ if (el instanceof OMElementImpl) {
+ elementImpl = (OMElementImpl) el;
+ } else {
+ throw new OMException();
+ }
+ if (elementImpl.isComplete() || !cache) {
+ throw new OMException();
+ }
+ try {
+ cache = false;
+ do {
+ while (parser.next() != XMLStreamConstants.END_ELEMENT) ;
+
+ // TODO:
+ } while (!parser.getName().equals(elementImpl.getQName()));
+ lastNode = (OMNodeImpl) elementImpl.getPreviousSibling();
+ if (lastNode != null) {
+ lastNode.setNextSibling(null);
+ } else {
+ OMElement parent = elementImpl.getParent();
+ if (parent == null) {
+ throw new OMException();
+ }
+ parent.setFirstChild(null);
+ lastNode = parent;
+ }
+ cache = true;
+ } catch (OMException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new OMException(e);
+ }
+ }
+
+ /**
+ * Method getText
+ *
+ * @return
+ * @throws OMException
+ */
+ public String getText() throws OMException {
+ return parser.getText();
+ }
+
+ /**
+ * Method getNamespace
+ *
+ * @return
+ * @throws OMException
+ */
+ public String getNamespace() throws OMException {
+ return parser.getNamespaceURI();
+ }
+
+ /**
+ * Method getNamespaceCount
+ *
+ * @return
+ * @throws OMException
+ */
+ public int getNamespaceCount() throws OMException {
+ try {
+ return parser.getNamespaceCount();
+ } catch (Exception e) {
+ throw new OMException(e);
+ }
+ }
+
+ /**
+ * Method getNamespacePrefix
+ *
+ * @param index
+ * @return
+ * @throws OMException
+ */
+ public String getNamespacePrefix(int index) throws OMException {
+ try {
+ return parser.getNamespacePrefix(index);
+ } catch (Exception e) {
+ throw new OMException(e);
+ }
+ }
+
+ /**
+ * Method getNamespaceUri
+ *
+ * @param index
+ * @return
+ * @throws OMException
+ */
+ public String getNamespaceUri(int index) throws OMException {
+ try {
+ return parser.getNamespaceURI(index);
+ } catch (Exception e) {
+ throw new OMException(e);
+ }
+ }
+
+ /**
+ * Method setCache
+ *
+ * @param b
+ */
+ public void setCache(boolean b) {
+ if (parserAccessed && b) {
+ throw new UnsupportedOperationException(
+ "parser accessed. cannot set cache");
+ }
+ cache = b;
+ }
+
+ /**
+ * Method getName
+ *
+ * @return
+ * @throws OMException
+ */
+ public String getName() throws OMException {
+ return parser.getLocalName();
+ }
+
+ /**
+ * Method getPrefix
+ *
+ * @return
+ * @throws OMException
+ */
+ public String getPrefix() throws OMException {
+ return parser.getPrefix();
+ }
+
+ /**
+ * Method getAttributeCount
+ *
+ * @return
+ * @throws OMException
+ */
+ public int getAttributeCount() throws OMException {
+ return parser.getAttributeCount();
+ }
+
+ /**
+ * Method getAttributeNamespace
+ *
+ * @param arg
+ * @return
+ * @throws OMException
+ */
+ public String getAttributeNamespace(int arg) throws OMException {
+ return parser.getAttributeNamespace(arg);
+ }
+
+ /**
+ * Method getAttributeName
+ *
+ * @param arg
+ * @return
+ * @throws OMException
+ */
+ public String getAttributeName(int arg) throws OMException {
+ return parser.getAttributeNamespace(arg);
+ }
+
+ /**
+ * Method getAttributePrefix
+ *
+ * @param arg
+ * @return
+ * @throws OMException
+ */
+ public String getAttributePrefix(int arg) throws OMException {
+ return parser.getAttributeNamespace(arg);
+ }
+
+ /**
+ * Method getParser
+ *
+ * @return
+ */
+ public Object getParser() {
+ if (!cache) {
+ parserAccessed = true;
+ return parser;
+ } else {
+ throw new UnsupportedOperationException(
+ "cache must be switched off to access the parser");
+ }
+ }
+
+ /**
+ * Method isCompleted
+ *
+ * @return
+ */
+ public boolean isCompleted() {
+ return done;
+ }
+
+ /**
+ * This method will be called with the XMLStreamConstants.START_ELEMENT event
+ *
+ * @return
+ * @throws OMException
+ */
+ protected abstract OMNode createOMElement() throws OMException;
+
+ /**
+ * This should proceed the parser one step further, if parser is not completed yet.
+ * If this has been called whist parser is done, then throw an OMException.
+ * If the cache is set to false, then should be return the event, *without* building the OM tree.
+ * If the cache is set to true, then this should handle all the events within this, and should build
+ * the object structure appropriately and return the event.
+ *
+ * @return
+ * @throws OMException
+ */
+ public abstract int next() throws OMException;
+
+ /**
+ * @return
+ */
+ public short getBuilderType() {
+ return OMConstants.PULL_TYPE_BUILDER;
+ }
+
+ /**
+ * Method registerExternalContentHandler
+ *
+ * @param obj
+ */
+ public void registerExternalContentHandler(Object obj) {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Method getRegisteredContentHandler
+ *
+ * @return
+ */
+ public Object getRegisteredContentHandler() {
+ throw new UnsupportedOperationException();
+ }
+}
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXOMBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXOMBuilder.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXOMBuilder.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXOMBuilder.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,199 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.axis.om.impl.llom.builder;
+
+import java.io.InputStream;
+
+import org.apache.axis.om.OMElement;
+import org.apache.axis.om.OMException;
+import org.apache.axis.om.OMFactory;
+import org.apache.axis.om.OMNamespace;
+import org.apache.axis.om.OMNode;
+import org.apache.axis.om.OMXMLParserWrapper;
+import org.apache.axis.om.SOAPEnvelope;
+import org.apache.axis.om.impl.llom.OMDocument;
+
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+/**
+ * This will construct an OM without using SOAP specific classes like SOAPEnvelope, SOAPHeader, SOAPHeaderBlock and SOAPBody.
+ * And this will habe the Document concept also.
+ */
+public class StAXOMBuilder extends StAXBuilder implements OMXMLParserWrapper {
+ /**
+ * Field document
+ */
+ protected OMDocument document;
+
+ /**
+ * Field omFactory
+ */
+ protected OMFactory omFactory;
+
+ /**
+ * Constructor StAXOMBuilder
+ *
+ * @param ombuilderFactory
+ * @param parser
+ * @throws FactoryConfigurationError
+ * @throws XMLStreamException
+ */
+ public StAXOMBuilder(OMFactory ombuilderFactory, InputStream inStream) throws XMLStreamException, FactoryConfigurationError {
+ super(ombuilderFactory, inStream);
+ document = new OMDocument(this);
+ omfactory = OMFactory.newInstance();
+ }
+
+ /**
+ * Constructor StAXOMBuilder
+ *
+ * @param parser
+ * @throws FactoryConfigurationError
+ * @throws XMLStreamException
+ */
+ public StAXOMBuilder(InputStream inStream) throws XMLStreamException, FactoryConfigurationError {
+ super(inStream);
+ document = new OMDocument(this);
+ omfactory = OMFactory.newInstance();
+ }
+
+ /**
+ * Method createOMElement
+ *
+ * @return
+ * @throws OMException
+ */
+ protected OMNode createOMElement() throws OMException {
+ OMElement node;
+ String elementName = parser.getLocalName();
+ if (lastNode == null) {
+ node = omfactory.createOMElement(elementName, null, null, this);
+ document.setRootElement(node);
+ } else if (lastNode.isComplete()) {
+ node = omfactory.createOMElement(elementName, null,
+ lastNode.getParent(), this);
+ lastNode.setNextSibling(node);
+ node.setPreviousSibling(lastNode);
+ } else {
+ OMElement e = (OMElement) lastNode;
+ node = omfactory.createOMElement(elementName, null,
+ (OMElement) lastNode, this);
+ e.setFirstChild(node);
+ }
+
+ // create the namespaces
+ processNamespaceData(node, false);
+
+ // fill in the attributes
+ processAttributes(node);
+ return node;
+ }
+
+ /**
+ * Method getOMEnvelope
+ *
+ * @return
+ * @throws OMException
+ */
+ public SOAPEnvelope getOMEnvelope() throws OMException {
+ throw new UnsupportedOperationException(); // TODO implement this
+ }
+
+ /**
+ * Method next
+ *
+ * @return
+ * @throws OMException
+ */
+ public int next() throws OMException {
+ try {
+ if (done) {
+ throw new OMException();
+ }
+ int token = parser.next();
+ if (!cache) {
+ return token;
+ }
+ switch (token) {
+ case XMLStreamConstants.START_ELEMENT:
+ lastNode = createOMElement();
+ break;
+ case XMLStreamConstants.START_DOCUMENT:
+ document = new OMDocument(this);
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ lastNode = createOMText();
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ if (lastNode.isComplete()) {
+ OMElement parent = lastNode.getParent();
+ parent.setComplete(true);
+ lastNode = parent;
+ } else {
+ OMElement e = (OMElement) lastNode;
+ e.setComplete(true);
+ }
+ break;
+ case XMLStreamConstants.END_DOCUMENT:
+ done = true;
+ break;
+ case XMLStreamConstants.SPACE:
+ next();
+ break;
+ default :
+ throw new OMException();
+ }
+ return token;
+ } catch (OMException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new OMException(e);
+ }
+ }
+
+ /**
+ * Method getDocumentElement
+ *
+ * @return
+ */
+ public OMElement getDocumentElement() {
+ return document.getRootElement();
+ }
+
+ /**
+ * Method processNamespaceData
+ *
+ * @param node
+ * @param isSOAPElement
+ */
+ protected void processNamespaceData(OMElement node, boolean isSOAPElement) {
+ int namespaceCount = parser.getNamespaceCount();
+ for (int i = 0; i < namespaceCount; i++) {
+ node.declareNamespace(parser.getNamespaceURI(i),
+ parser.getNamespacePrefix(i));
+ }
+
+ // set the own namespace
+ OMNamespace namespace =
+ node.findInScopeNamespace(parser.getNamespaceURI(),
+ parser.getPrefix());
+ node.setNamespace(namespace);
+ }
+}
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXSOAPModelBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXSOAPModelBuilder.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXSOAPModelBuilder.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/builder/StAXSOAPModelBuilder.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,326 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.axis.om.impl.llom.builder;
+
+import org.apache.axis.om.*;
+import org.apache.axis.om.impl.llom.OMElementImpl;
+import org.apache.axis.om.impl.llom.SOAPEnvelopeImpl;
+import org.apache.axis.om.impl.llom.exception.OMBuilderException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import java.io.InputStream;
+
+/**
+ * Class StAXSOAPModelBuilder
+ */
+public class StAXSOAPModelBuilder extends StAXBuilder {
+ /**
+ * Field envelope
+ */
+ private SOAPEnvelopeImpl envelope;
+
+ /**
+ * Field headerPresent
+ */
+ private boolean headerPresent = false;
+
+ /**
+ * Field bodyPresent
+ */
+ private boolean bodyPresent = false;
+
+ /**
+ * Field log
+ */
+ private Log log = LogFactory.getLog(getClass());
+
+ /**
+ * element level 1 = envelope level element level 2 = Header or Body level
+ * element level 3 = HeaderElement or BodyElement level
+ */
+ private int elementLevel = 0;
+
+ /**
+ * Constructor StAXSOAPModelBuilder
+ *
+ * @param ombuilderFactory
+ * @param inStream
+ * @throws FactoryConfigurationError
+ * @throws XMLStreamException
+ */
+ public StAXSOAPModelBuilder(OMFactory ombuilderFactory,
+ InputStream inStream) throws XMLStreamException, FactoryConfigurationError{
+ super(inStream);
+ }
+
+
+ /**
+ * Constructor StAXSOAPModelBuilder
+ *
+ * @param parser
+ * @throws FactoryConfigurationError
+ * @throws XMLStreamException
+ */
+ public StAXSOAPModelBuilder(InputStream inStream) throws XMLStreamException, FactoryConfigurationError {
+ super(inStream);
+ }
+
+ /**
+ * Constructor StAXSOAPModelBuilder
+ * to be used by MTOMSOAPModelBuilder
+ * @param ombuilderFactory
+ */
+ protected StAXSOAPModelBuilder(OMFactory ombuilderFactory) {
+ super(ombuilderFactory);
+ }
+ /**
+ * Constructor StAXSOAPModelBuilder
+ * to be used by MTOMSOAPModelBuilder
+ */
+ protected StAXSOAPModelBuilder() {
+ super();
+ }
+
+ /**
+ * Method getSOAPEnvelope
+ *
+ * @return
+ * @throws OMException
+ */
+ public SOAPEnvelope getSOAPEnvelope() throws OMException {
+ while ((envelope == null) && !done) {
+ next();
+ }
+ return envelope;
+ }
+
+ /**
+ * Method createOMElement
+ *
+ * @return
+ * @throws OMException
+ */
+ protected OMNode createOMElement() throws OMException {
+ OMElement node;
+ String elementName = parser.getLocalName();
+ if (lastNode == null) {
+ node = constructNode(null, elementName, true);
+ } else if (lastNode.isComplete()) {
+ node = constructNode(lastNode.getParent(), elementName, false);
+ lastNode.setNextSibling(node);
+ node.setPreviousSibling(lastNode);
+ } else {
+ OMElement e = (OMElement) lastNode;
+ node = constructNode((OMElement) lastNode, elementName, false);
+ e.setFirstChild(node);
+ }
+
+ // fill in the attributes
+ processAttributes(node);
+ log.info("Build the OMElelment {" + node.getNamespaceName() + '}'
+ + node.getLocalName() + "By the StaxSOAPModelBuilder");
+ return node;
+ }
+
+ /**
+ * Method constructNode
+ *
+ * @param parent
+ * @param elementName
+ * @param isEnvelope
+ * @return
+ */
+ protected OMElement constructNode(OMElement parent, String elementName,
+ boolean isEnvelope) {
+ OMElement element = null;
+ if (isEnvelope) {
+ if (!elementName.equalsIgnoreCase(OMConstants.SOAPENVELOPE_LOCAL_NAME)) {
+ throw new OMException("First Element must contain the local name, "
+ + OMConstants.SOAPENVELOPE_LOCAL_NAME);
+ }
+ envelope =
+ (SOAPEnvelopeImpl) ombuilderFactory.createSOAPEnvelope(null,
+ this);
+ element = (OMElementImpl) envelope;
+ processNamespaceData(element, true);
+ } else if (elementLevel == 2) {
+
+ // this is either a header or a body
+ if (elementName.equals(OMConstants.HEADER_LOCAL_NAME)) {
+ if (headerPresent) {
+ throw new OMBuilderException("Multiple headers encountered!");
+ }
+ if (bodyPresent) {
+ throw new OMBuilderException("Header Body wrong order!");
+ }
+ headerPresent = true;
+ element =
+ ombuilderFactory.createSOAPHeader((SOAPEnvelope) parent,
+ this);
+
+ // envelope.setHeader((SOAPHeader)element);
+ processNamespaceData(element, true);
+ } else if (elementName.equals(OMConstants.BODY_LOCAL_NAME)) {
+ if (bodyPresent) {
+ throw new OMBuilderException("Multiple body elements encountered");
+ }
+ bodyPresent = true;
+ element =
+ ombuilderFactory.createSOAPBody((SOAPEnvelope) parent,
+ this);
+
+ // envelope.setBody((SOAPBody)element);
+ processNamespaceData(element, true);
+ } else {
+ throw new OMBuilderException(elementName
+ + " is not supported here. Envelope can not have elements other than Header and Body.");
+ }
+ } else if ((elementLevel == 3)
+ && parent.getLocalName().equalsIgnoreCase(OMConstants.HEADER_LOCAL_NAME)) {
+
+ // this is a headerblock
+ element = ombuilderFactory.createSOAPHeaderBlock(elementName, null,
+ parent, this);
+ processNamespaceData(element, false);
+ } else if ((elementLevel == 3) && parent.getLocalName().equalsIgnoreCase(OMConstants.BODY_LOCAL_NAME) && elementName.equalsIgnoreCase(OMConstants.BODY_FAULT_LOCAL_NAME)) {
+
+ // this is a headerblock
+ element = ombuilderFactory.createSOAPFault(null, (SOAPBody) parent,
+ this);
+ processNamespaceData(element, false);
+ } else {
+
+ // this is neither of above. Just create an element
+ element = ombuilderFactory.createOMElement(elementName, null,
+ parent, this);
+ processNamespaceData(element, false);
+ }
+ return element;
+ }
+
+ /**
+ * Method next
+ *
+ * @return
+ * @throws OMException
+ */
+ public int next() throws OMException {
+ try {
+ if (done) {
+ throw new OMException();
+ }
+ int token = parser.next();
+ if (!cache) {
+ return token;
+ }
+ switch (token) {
+ case XMLStreamConstants.START_ELEMENT:
+ elementLevel++;
+ lastNode = createOMElement();
+ break;
+ case XMLStreamConstants.CHARACTERS:
+ lastNode = createOMText();
+ break;
+ case XMLStreamConstants.END_ELEMENT:
+ if (lastNode.isComplete()) {
+ OMElement parent = lastNode.getParent();
+ parent.setComplete(true);
+ lastNode = parent;
+ } else {
+ OMNode e = (OMNode) lastNode;
+ e.setComplete(true);
+ }
+ elementLevel--;
+ break;
+ case XMLStreamConstants.END_DOCUMENT:
+ done = true;
+ break;
+ case XMLStreamConstants.SPACE:
+ next();
+ break;
+ default :
+ throw new OMException();
+ }
+ return token;
+ } catch (OMException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new OMException(e);
+ }
+ }
+
+ /**
+ * Method getDocumentElement
+ *
+ * @return
+ */
+ public OMElement getDocumentElement() {
+ return getSOAPEnvelope();
+ }
+
+ /**
+ * Method processNamespaceData
+ *
+ * @param node
+ * @param isSOAPElement
+ */
+ protected void processNamespaceData(OMElement node, boolean isSOAPElement) {
+ int namespaceCount = parser.getNamespaceCount();
+ for (int i = 0; i < namespaceCount; i++) {
+ node.declareNamespace(parser.getNamespaceURI(i),
+ parser.getNamespacePrefix(i));
+ }
+
+ // set the own namespace
+ String namespaceURI = parser.getNamespaceURI();
+ String prefix = parser.getPrefix();
+ OMNamespace namespace = null;
+ if (!"".equals(namespaceURI)) {
+ if (prefix == null) {
+ // this means, this elements has a default namespace or it has inherited a default namespace from its parent
+ namespace = node.findDeclaredNamespace(namespaceURI, "");
+ if (namespace == null) {
+ namespace = node.declareNamespace(namespaceURI, "");
+ }
+ } else {
+ namespace = node.findInScopeNamespace(namespaceURI, prefix);
+ }
+ node.setNamespace(namespace);
+ } else {
+
+ }
+
+
+
+ // TODO we got to have this to make sure OM reject mesagess that are not sname space qualified
+ // But got to comment this to interop with Axis.1.x
+ // if (namespace == null) {
+ // throw new OMException("All elements must be namespace qualified!");
+ // }
+ if (isSOAPElement) {
+ if (node.getNamespace() != null && !node.getNamespace().getName().equals(OMConstants.SOAP_ENVELOPE_NAMESPACE_URI)) {
+ throw new OMBuilderException("invalid SOAP namespace URI");
+ }
+ }
+
+ }
+}
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/exception/OMBuilderException.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/exception/OMBuilderException.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/exception/OMBuilderException.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/exception/OMBuilderException.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.axis.om.impl.llom.exception;
+
+import org.apache.axis.om.OMException;
+
+/**
+ * Class OMBuilderException
+ */
+public class OMBuilderException extends OMException {
+ /**
+ * Constructor OMBuilderException
+ *
+ * @param s
+ */
+ public OMBuilderException(String s) {
+ super(s);
+ }
+}
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/exception/OMStreamingException.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/exception/OMStreamingException.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/exception/OMStreamingException.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/exception/OMStreamingException.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.axis.om.impl.llom.exception;
+
+import org.apache.axis.om.OMException;
+
+/**
+ * Class OMStreamingException
+ */
+public class OMStreamingException extends OMException {
+ /**
+ * Constructor OMStreamingException
+ */
+ public OMStreamingException() {
+ }
+
+ /**
+ * Constructor OMStreamingException
+ *
+ * @param message
+ */
+ public OMStreamingException(String message) {
+ super(message);
+ }
+
+ /**
+ * Constructor OMStreamingException
+ *
+ * @param message
+ * @param cause
+ */
+ public OMStreamingException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ /**
+ * Constructor OMStreamingException
+ *
+ * @param cause
+ */
+ public OMStreamingException(Throwable cause) {
+ super(cause);
+ }
+}
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/ByteArrayDataSource.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/ByteArrayDataSource.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/ByteArrayDataSource.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/ByteArrayDataSource.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,71 @@
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.activation.DataSource;
+
+/**
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * <p/>
+ * Licensed 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ * <p/>
+ */
+
+/**
+ * @author Thilina Gunarathne
+ * thilina@opensource.lk
+ */
+public class ByteArrayDataSource implements DataSource {
+
+ private byte[] data;
+
+ private String type;
+
+ public ByteArrayDataSource(byte[] data, String type) {
+ super();
+ this.data = data;
+ this.type = type;
+ }
+ public ByteArrayDataSource(byte[] data) {
+ super();
+ this.data = data;
+ }
+
+ public void setType(String type)
+ {
+ this.type = type;
+ }
+ public String getContentType() {
+ if (type == null)
+ return "application/octet-stream";
+ else
+ return type;
+ }
+
+ public InputStream getInputStream() throws IOException {
+ return new ByteArrayInputStream(data);
+ }
+
+ public String getName() {
+
+ return "ByteArrayDataSource";
+ }
+
+ public OutputStream getOutputStream() throws IOException {
+ throw new IOException("Not Supported");
+ }
+}
+
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MIMEParser.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MIMEParser.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MIMEParser.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MIMEParser.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,131 @@
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.InputStream;
+import java.util.Properties;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.internet.MimePartDataSource;
+
+/**
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * <p/>
+ * Licensed 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ * <p/>
+ */
+
+/**
+ * @author Thilina Gunarathne
+ * thilina@opensource.lk
+ */
+public class MIMEParser {
+
+ int partIndex = 0;
+
+ //String mimeVersion;
+
+ //String contentType;
+
+ boolean mime;
+
+ //String boundry;
+
+ //boolean complete = false;
+
+ //String start;
+
+ MimeMessage mimeMessage;
+
+ public MIMEParser(InputStream inStream) {
+
+ Properties props = new Properties();
+ javax.mail.Session session = javax.mail.Session
+ .getInstance(props, null);
+ try {
+ mimeMessage = new MimeMessage(session, inStream);
+ mime = true;
+ //in = new MIMEInputStream(inStream);
+ } catch (MessagingException e) {
+ mime = false;
+ }
+ }
+
+ public boolean mimeMessage() {
+ return mime;
+ /*
+ * String line = null; char readChar = ' '; boolean found = false; int
+ * startIndex, finishIndex, valueFinishIndex;
+ *
+ * //Start processing the Mime Header try { in.readLine(); line =
+ * in.readLine(); } catch (Exception e) { System.out.println(e); }
+ *
+ * line = line.trim(); String[] versionArray = line.split(":");
+ *
+ * //Extracts and stores MIME version information if
+ * (versionArray[0].equalsIgnoreCase("MIME-Version")) { mimeVersion =
+ * versionArray[1];
+ * /* extract the Mime boundry
+ */
+ /*
+ * do { try { line = in.readLine(); } catch (Exception e) {
+ * System.out.println(e); }
+ *
+ * //extracting the mime boundry string startIndex =
+ * line.indexOf("boundary="); } while (startIndex == -1);
+ *
+ * finishIndex = (startIndex + ("boundary=").length()); valueFinishIndex =
+ * line.indexOf(";", finishIndex);
+ *
+ * if (valueFinishIndex > 0) { boundry = line.substring(finishIndex,
+ * valueFinishIndex); } else { boundry = line.substring(finishIndex); }
+ * boundry = boundry.replace('"', ' '); boundry = boundry.trim();
+ * /* Move the input stream to the begining of the MIME root part
+ */
+ /*
+ * do { try { in.mark(boundry.length() + 5); readChar = (char)
+ * in.read(); } catch (Exception e) { System.out.println(e); } if
+ * (readChar == '-') { char readNextChar = ' '; try { readNextChar =
+ * (char) in.read(); } catch (IOException e3) { e3.printStackTrace(); }
+ *
+ * if (readNextChar == '-') { char[] cBuf = new char[boundry.length()];
+ *
+ * try { line = in.read(boundry.length()); } catch (IOException e1) {
+ * e1.printStackTrace(); }
+ *
+ * if (line.equals(boundry)) { found = true; } else { try { in.reset(); }
+ * catch (IOException e2) { e2.printStackTrace(); } } } } } while
+ * (!found); return true; } else { return false; }
+ */
+ }
+
+ public MimeBodyPart getMimeBodyPart() throws MessagingException {
+ MimeBodyPart mimeBodyPart = null;
+ /*
+ * if (!complete) {
+ *
+ * MIMEBodyPartInputStream partStream = new MIMEBodyPartInputStream( in,
+ * boundry); part = new MimeBodyPart(partStream); if (part.getSize() ==
+ * 0 | part.getSize() < 0) { complete = true; return null; } }
+ */
+ DataHandler dh = mimeMessage.getDataHandler();
+ MimeMultipart multiPart = new MimeMultipart((MimePartDataSource) dh
+ .getDataSource());
+ mimeBodyPart = (MimeBodyPart) multiPart.getBodyPart(partIndex);
+
+ partIndex++;
+ return mimeBodyPart;
+ }
+}
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MTOMBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MTOMBuilder.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MTOMBuilder.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MTOMBuilder.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,122 @@
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.LinkedList;
+import java.util.ListIterator;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeBodyPart;
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+/**
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * <p/>
+ * Licensed 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ * <p/>
+ */
+
+/**
+ * @author Thilina Gunarathne
+ * thilina@opensource.lk
+ */
+public class MTOMBuilder {
+ LinkedList mimeBodyPartsList;
+
+ InputStream inStream;
+
+ MIMEParser mimeParser;
+
+ public MTOMBuilder(InputStream inStream) {
+ this.inStream = inStream;
+ this.mimeParser = new MIMEParser(inStream);
+ mimeBodyPartsList = new LinkedList();
+ }
+
+ public DataHandler getDataHandler(String blobContentID) throws MessagingException{
+ /*
+ * First checks whether the part is already parsed by checking the parts
+ * linked list. If it is not parsed yet then call the getnextPart() till
+ * we find the required part.
+ */
+ MimeBodyPart mimeBodyPart;
+
+ boolean attachmentFound = false;
+ ListIterator partsIterator = mimeBodyPartsList.listIterator();
+ while (partsIterator.hasNext()) {
+ mimeBodyPart = (MimeBodyPart) partsIterator.next();
+ if (blobContentID.equals(mimeBodyPart.getContentID())) {
+ attachmentFound = true;
+ DataHandler dh = mimeBodyPart.getDataHandler();
+ return dh;
+ }
+ }
+ while (!attachmentFound) {
+ mimeBodyPart = this.getNextMimeBodyPart();
+
+ if (mimeBodyPart == null) {
+ break;
+ }
+ String partContentID = mimeBodyPart.getContentID();
+ String delimitedBlobContentID = "<" + blobContentID + ">";
+ if (delimitedBlobContentID.equals(partContentID)) {
+ attachmentFound = true;
+ DataHandler dh = mimeBodyPart.getDataHandler();
+ return dh;
+ }
+ }
+ return null;
+ }
+
+ public XMLStreamReader getParser() throws XMLStreamException, FactoryConfigurationError, IOException, MessagingException {
+
+ if (mimeParser.mimeMessage()) {
+ MimeBodyPart rootMimeBodyPart = getRootMimeBodyPart();
+ return XMLInputFactory.newInstance().createXMLStreamReader(
+ rootMimeBodyPart.getInputStream());
+ } else {
+ return XMLInputFactory.newInstance()
+ .createXMLStreamReader(inStream);
+ }
+ }
+
+ public MimeBodyPart getRootMimeBodyPart() throws MessagingException {
+ MimeBodyPart rootMimeBodyPart;
+ if (mimeBodyPartsList.isEmpty()) {
+ rootMimeBodyPart = mimeParser.getMimeBodyPart();
+ mimeBodyPartsList.add(rootMimeBodyPart);
+ } else {
+ rootMimeBodyPart = (MimeBodyPart) mimeBodyPartsList.getFirst();
+ }
+ System.out.println("MIME root parsed. NO of parts parsed :"
+ + mimeBodyPartsList.size());
+ return rootMimeBodyPart;
+ }
+
+ public MimeBodyPart getNextMimeBodyPart() throws MessagingException {
+ MimeBodyPart nextMimeBodyPart;
+ nextMimeBodyPart = mimeParser.getMimeBodyPart();
+ if (nextMimeBodyPart != null) {
+ mimeBodyPartsList.add(nextMimeBodyPart);
+ System.out.println("Next part parsed. NO of parts parsed :"
+ + mimeBodyPartsList.size());
+ return nextMimeBodyPart;
+ } else
+ return null;
+ }
+
+}
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,442 @@
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Properties;
+import java.util.Random;
+import java.util.*;
+
+import javax.activation.DataHandler;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMultipart;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+/**
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * <p/>
+ * Licensed 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ * <p/>
+ */
+
+/**
+ * @author Thilina Gunarathne
+ * thilina@opensource.lk
+ */
+public class MTOMXMLStreamWriter implements XMLStreamWriter {
+
+ OutputStream outStream;
+
+ XMLStreamWriter writer;
+
+ Random rnd;
+
+ HashMap attachmentsMap;
+
+ ByteArrayOutputStream bufferedSoapOutStream;
+
+ public static String[] filter = new String[] { "Message-ID" }; // to filter the message ID header
+
+ public MTOMXMLStreamWriter(OutputStream outStream)
+ throws XMLStreamException, FactoryConfigurationError {
+ super();
+ this.outStream = outStream;
+
+ bufferedSoapOutStream = new ByteArrayOutputStream();
+
+ writer = XMLOutputFactory.newInstance().createXMLStreamWriter(
+ bufferedSoapOutStream);
+ attachmentsMap = new HashMap();
+ rnd = new Random();
+
+ }
+
+ public String writeAttachment(OMBlob blob) {
+
+ String contentID = "http://example.org/my.hsh" + (System.currentTimeMillis());
+
+ attachmentsMap.put(contentID, blob);
+
+ return contentID;
+ }
+
+ public MimeBodyPart createMimeBodyPart(String contentID, OMBlob blob)
+ throws Exception {
+ MimeBodyPart mimeBodyPart = new MimeBodyPart();
+ mimeBodyPart.setDataHandler(blob.getDataHandler());
+ mimeBodyPart.addHeader("Content-Transfer-Encoding", "binary");
+ mimeBodyPart.addHeader("Content-ID", "<" + contentID + ">");
+ return mimeBodyPart;
+
+ }
+
+ public void complete() throws Exception {
+ DataHandler dh = new DataHandler(bufferedSoapOutStream.toString(),
+ "text/xml");
+ MimeBodyPart mimeBodyPart = new MimeBodyPart();
+ mimeBodyPart.setDataHandler(dh);
+ mimeBodyPart.addHeader("Content-Type", "application/xop+xml");
+ mimeBodyPart.addHeader("Content-Transfer-Encoding", "8bit");
+ String contentID = "<http://example.org/my.hsh>";
+ mimeBodyPart.addHeader("Content-ID", contentID);
+
+ Properties props = new Properties();
+ javax.mail.Session session = javax.mail.Session.getInstance(props, null);
+ javax.mail.internet.MimeMessage mimeMessage = new javax.mail.internet.MimeMessage(
+ session);
+ MimeMultipart multipartMessage = new MimeMultipart("Related");
+ multipartMessage.addBodyPart(mimeBodyPart);
+ Set attachmentKeys = attachmentsMap.keySet();
+
+ Iterator keyIterator = attachmentKeys.iterator();
+ while (keyIterator.hasNext()) {
+ String partContentID = (String) keyIterator.next();
+ OMBlob blob = (OMBlob) attachmentsMap.get(partContentID);
+ multipartMessage.addBodyPart(createMimeBodyPart(partContentID, blob));
+ }
+ mimeMessage.setContent(multipartMessage);
+ mimeMessage.writeTo(outStream, filter);
+ }
+
+ public void writeStartElement(String localName) throws XMLStreamException {
+ writer.writeStartElement(localName);
+ }
+
+ public void writeStartElement(String namespaceURI, String localName)
+ throws XMLStreamException {
+ writer.writeStartElement(namespaceURI, localName);
+
+ }
+
+ public void writeStartElement(String prefix, String localName,
+ String namespaceURI) throws XMLStreamException {
+ writer.writeStartElement(prefix, localName, namespaceURI);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String,
+ * java.lang.String)
+ */
+ public void writeEmptyElement(String namespaceURI, String localName)
+ throws XMLStreamException {
+ writer.writeEmptyElement(namespaceURI, localName);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String,
+ * java.lang.String, java.lang.String)
+ */
+ public void writeEmptyElement(String prefix, String localName,
+ String namespaceURI) throws XMLStreamException {
+ writer.writeEmptyElement(prefix, localName, namespaceURI);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String)
+ */
+ public void writeEmptyElement(String localName) throws XMLStreamException {
+ writer.writeEmptyElement(localName);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeEndElement()
+ */
+ public void writeEndElement() throws XMLStreamException {
+ writer.writeEndElement();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeEndDocument()
+ */
+ public void writeEndDocument() throws XMLStreamException {
+ writer.writeEndDocument();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#close()
+ */
+ public void close() throws XMLStreamException {
+ writer.close();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#flush()
+ */
+ public void flush() throws XMLStreamException {
+ writer.flush();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String,
+ * java.lang.String)
+ */
+ public void writeAttribute(String localName, String value)
+ throws XMLStreamException {
+ writer.writeAttribute(localName, value);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String,
+ * java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void writeAttribute(String prefix, String namespaceURI,
+ String localName, String value) throws XMLStreamException {
+ writer.writeAttribute(prefix, namespaceURI, localName, value);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String,
+ * java.lang.String, java.lang.String)
+ */
+ public void writeAttribute(String namespaceURI, String localName,
+ String value) throws XMLStreamException {
+ writer.writeAttribute(namespaceURI, localName, value);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeNamespace(java.lang.String,
+ * java.lang.String)
+ */
+ public void writeNamespace(String prefix, String namespaceURI)
+ throws XMLStreamException {
+ writer.writeNamespace(prefix, namespaceURI);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeDefaultNamespace(java.lang.String)
+ */
+ public void writeDefaultNamespace(String namespaceURI)
+ throws XMLStreamException {
+ writer.writeDefaultNamespace(namespaceURI);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeComment(java.lang.String)
+ */
+ public void writeComment(String data) throws XMLStreamException {
+ writer.writeComment(data);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeProcessingInstruction(java.lang.String)
+ */
+ public void writeProcessingInstruction(String target)
+ throws XMLStreamException {
+ writer.writeProcessingInstruction(target);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeProcessingInstruction(java.lang.String,
+ * java.lang.String)
+ */
+ public void writeProcessingInstruction(String target, String data)
+ throws XMLStreamException {
+ writer.writeProcessingInstruction(target, data);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeCData(java.lang.String)
+ */
+ public void writeCData(String data) throws XMLStreamException {
+ writer.writeCData(data);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeDTD(java.lang.String)
+ */
+ public void writeDTD(String dtd) throws XMLStreamException {
+ writer.writeDTD(dtd);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeEntityRef(java.lang.String)
+ */
+ public void writeEntityRef(String name) throws XMLStreamException {
+ writer.writeEntityRef(name);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeStartDocument()
+ */
+ public void writeStartDocument() throws XMLStreamException {
+ writer.writeStartDocument();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeStartDocument(java.lang.String)
+ */
+ public void writeStartDocument(String version) throws XMLStreamException {
+ writer.writeStartDocument(version);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeStartDocument(java.lang.String,
+ * java.lang.String)
+ */
+ public void writeStartDocument(String encoding, String version)
+ throws XMLStreamException {
+ writer.writeStartDocument(encoding, version);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeCharacters(java.lang.String)
+ */
+ public void writeCharacters(String text) throws XMLStreamException {
+ writer.writeCharacters(text);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#writeCharacters(char[], int, int)
+ */
+ public void writeCharacters(char[] text, int start, int len)
+ throws XMLStreamException {
+ writer.writeCharacters(text, start, len);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#getPrefix(java.lang.String)
+ */
+ public String getPrefix(String uri) throws XMLStreamException {
+
+ return writer.getPrefix(uri);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#setPrefix(java.lang.String,
+ * java.lang.String)
+ */
+ public void setPrefix(String prefix, String uri) throws XMLStreamException {
+ writer.setPrefix(prefix, uri);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#setDefaultNamespace(java.lang.String)
+ */
+ public void setDefaultNamespace(String uri) throws XMLStreamException {
+ writer.setDefaultNamespace(uri);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#setNamespaceContext(javax.xml.namespace.NamespaceContext)
+ */
+ public void setNamespaceContext(NamespaceContext context)
+ throws XMLStreamException {
+ writer.setNamespaceContext(context);
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#getNamespaceContext()
+ */
+ public NamespaceContext getNamespaceContext() {
+
+ return writer.getNamespaceContext();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.xml.stream.XMLStreamWriter#getProperty(java.lang.String)
+ */
+ public Object getProperty(String name) throws IllegalArgumentException {
+
+ return writer.getProperty(name);
+ }
+}
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/OMBlob.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/OMBlob.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/OMBlob.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/mtom/OMBlob.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,254 @@
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.*;
+import java.io.IOException;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axis.encoding.Base64;
+import org.apache.axis.om.OMAttribute;
+import org.apache.axis.om.OMElement;
+import org.apache.axis.om.OMException;
+import org.apache.axis.om.OMNamespace;
+import org.apache.axis.om.OMNode;
+import org.apache.axis.om.OMXMLParserWrapper;
+import org.apache.axis.om.impl.llom.OMAttributeImpl;
+import org.apache.axis.om.impl.llom.OMNamespaceImpl;
+import org.apache.axis.om.impl.llom.OMNodeImpl;
+import org.apache.axis.om.impl.llom.serialize.StreamWriterToContentHandlerConverter;
+import org.apache.axis.om.impl.llom.serialize.StreamingOMSerializer;
+
+/**
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * <p/>
+ * Licensed 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ * <p/>
+ */
+
+/**
+ * @author Thilina Gunarathne
+ * thilina@opensource.lk
+ */
+
+public class OMBlob extends OMNodeImpl {
+ //thinking of implementing the OMText
+
+ private String cid = null;
+
+ private MTOMBuilder mtomBuilder;
+
+ private OMXMLParserWrapper builder;
+
+ private DataHandler DH = null;
+
+ public OMBlob(DataHandler DH) {
+ this.DH = DH;
+ }
+
+ public OMBlob(String cid, OMElement parent, MTOMBuilder mimeParser,
+ OMXMLParserWrapper builder) {
+ super(parent);
+ this.builder = builder;
+ this.cid = cid;
+ this.mtomBuilder = mimeParser;
+ }
+
+ public java.io.OutputStream getOutputStream() throws IOException, MessagingException {
+ if (DH == null) {
+ getDataHandler();
+ }
+ OutputStream outStream =DH.getOutputStream();
+ if (!(outStream instanceof java.io.OutputStream))
+ {
+ outStream = new ByteArrayOutputStream();
+ Object object = DH.getContent();
+ ObjectOutputStream objectOutStream = new ObjectOutputStream(outStream);
+ objectOutStream.writeObject(object);
+ }
+ return outStream;
+ }
+
+ public String getValue() throws OMException {
+ throw new OMException(
+ "Blob contains Binary data. Returns Stream or Datahandler only");
+ }
+
+ public DataHandler getDataHandler() throws MessagingException {
+ if (DH == null) {
+ DH = mtomBuilder.getDataHandler(cid);
+ }
+ return DH;
+ }
+
+ public short getType() throws OMException {
+ return OMNode.BLOB_NODE;
+ }
+
+ public boolean isComplete() {
+ return true;
+ }
+
+ public void serialize(XMLStreamWriter writer, boolean cache)
+ throws XMLStreamException {
+ boolean firstElement = false;
+
+ // Special case for the pull type building with cache off
+ // The getPullParser method returns the current elements itself.
+
+ if (isComplete()) {
+
+ // serialize own normally
+ if (writer instanceof MTOMXMLStreamWriter) {
+ MTOMXMLStreamWriter mtomWriter = (MTOMXMLStreamWriter) writer;
+ // write the XOP:Include namespace
+ OMNamespace ns = new OMNamespaceImpl(
+ "http://www.w3.org/2004/08/xop/Include", "xop");
+ String prefix = null;
+ String nameSpaceName = null;
+ if (ns != null) {
+ prefix = ns.getPrefix();
+ nameSpaceName = ns.getName();
+ if (prefix != null) {
+ mtomWriter.writeStartElement(prefix, "Include",
+ nameSpaceName);
+ //if (!prefixList.contains(prefix)) {
+ mtomWriter.writeNamespace(prefix, nameSpaceName);
+ // prefixList.add(prefix);
+ //}
+ }
+ }
+
+ //get the Cid from the MTOMwriter
+ String contentID;
+ contentID = mtomWriter.writeAttachment(this);
+ OMAttribute href = new OMAttributeImpl("href",
+ new OMNamespaceImpl("", ""), "cid:" + contentID.trim());
+ serializeAttribute(href, writer);
+ mtomWriter.writeEndElement();
+ } else {
+ ByteArrayOutputStream byteStream;
+
+ try {
+ byteStream = (ByteArrayOutputStream) this.getOutputStream();
+ writer.writeCharacters(Base64.encode(byteStream
+ .toByteArray()));
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (MessagingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ if (nextSibling != null) {
+ // serilize next sibling
+ nextSibling.serialize(writer, cache);
+ } else {
+ if (parent == null) {
+ return;
+ } else if (parent.isComplete()) {
+ return;
+ } else {
+ // do the special serialization
+ // Only the push serializer is left now
+ builder.setCache(cache);
+ builder.next();
+ }
+ }
+ }
+ }
+
+ protected void serializeAttribute(OMAttribute attr, XMLStreamWriter writer)
+ throws XMLStreamException {
+
+ //first check whether the attribute is associated with a namespace
+ OMNamespace ns = attr.getNamespace();
+ String prefix = null;
+ String namespaceName = null;
+ if (ns != null) {
+ //add the prefix if it's availble
+ prefix = ns.getPrefix();
+ namespaceName = ns.getName();
+
+ if (prefix != null)
+ writer.writeAttribute(prefix, namespaceName, attr
+ .getLocalName(), attr.getValue());
+ else
+ writer.writeAttribute(namespaceName, attr.getLocalName(), attr
+ .getValue());
+ } else {
+ writer.writeAttribute(attr.getLocalName(), attr.getValue());
+ }
+
+ }
+
+ // Decided to let user select whether to optimise or not only when he
+ // creates OMBlob
+ /*
+ * public void setMTOMable(boolean MTOMable) { this.MTOMable = MTOMable; }
+ *
+ * public boolean isMTOMable() { return MTOMable; }
+ */
+
+ // decided to provide only the Datahandler
+ /* public/*Serializable *//*
+ * @author TGunarathne
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+ /* Object getObject() throws Exception { if
+ * @author TGunarathne
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ *
+ * ((binaryObject == null) & (cid != null)) { if
+ * (DH == null) { getDataHandler(); } // /*
+ *
+ *
+ * javax.activation.DataSource ds =
+ * datahandler.getDataSource(); InputStream is =
+ * null; try { is = ds.getInputStream();; } catch
+ * (java.io.IOException io) {
+ * log.error(Messages.getMessage("javaIOException00"),
+ * io); throw new SOAPException(io); } if
+ * (ds.getContentType().equals("text/plain")) { try {
+ * byte[] bytes = new byte[is.available()];
+ * IOUtils.readFully(is, bytes); return new
+ * String(bytes); } catch (java.io.IOException io) {
+ * log.error(Messages.getMessage("javaIOException00"),
+ * io); throw new SOAPException(io); } } else if
+ * (ds.getContentType().equals("text/xml")) {
+ * return new StreamSource(is); } else if
+ * (ds.getContentType().equals("image/gif") ||
+ * ds.getContentType().equals("image/jpeg")) { try {
+ * return
+ * ImageIOFactory.getImageIO().loadImage(is); }
+ * catch (Exception ex) {
+ * log.error(Messages.getMessage("javaIOException00"),
+ * ex); throw new SOAPException(ex); } } return is; //
+ */
+ /*
+ * Object in = DH.getContent(); if (in instanceof InputStream) {
+ * ObjectInputStream ois = new ObjectInputStream((InputStream) in);
+ * binaryObject = ois.readObject(); } else binaryObject = in; return
+ * binaryObject; } else if ((binaryObject == null) & (cid == null)) { throw
+ * new MTOMException("OMBlob not initialised Properly"); } return
+ * binaryObject; }
+ */
+
+ }
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/serialize/SimpleOMSerializer.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/serialize/SimpleOMSerializer.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/serialize/SimpleOMSerializer.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/serialize/SimpleOMSerializer.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,236 @@
+package org.apache.axis.om.impl.llom.serialize;
+
+import java.io.ByteArrayOutputStream;
+import java.util.Iterator;
+import java.util.Vector;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axis.encoding.Base64;
+import org.apache.axis.om.OMAttribute;
+import org.apache.axis.om.OMElement;
+import org.apache.axis.om.OMNamespace;
+import org.apache.axis.om.OMNode;
+import org.apache.axis.om.OMText;
+import org.apache.axis.om.impl.llom.OMAttributeImpl;
+import org.apache.axis.om.impl.llom.OMNamespaceImpl;
+import org.apache.axis.om.impl.llom.mtom.MTOMXMLStreamWriter;
+import org.apache.axis.om.impl.llom.mtom.OMBlob;
+
+/**
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * <p/>
+ * Licensed 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ */
+public class SimpleOMSerializer {
+
+ private Vector prefixList = new Vector();
+
+ public void serialize(Object omNode, XMLStreamWriter writer)
+ throws XMLStreamException {
+
+ if (!(omNode instanceof OMNode)) {
+ throw new UnsupportedOperationException("Unsupported input object. Must be of the the type OMNode");
+ }
+
+ OMNode node = (OMNode) omNode;
+ serializeNode(node, writer);
+ }
+
+ protected void serializeNode(OMNode node, XMLStreamWriter writer)
+ throws XMLStreamException {
+ short nodeType = node.getType();
+ if (nodeType == OMNode.ELEMENT_NODE) {
+ serializeElement((OMElement) node, writer);
+ } else if (nodeType == OMNode.ATTRIBUTE_NODE) {
+ serializeAttribute((OMAttribute) node, writer);
+ } else if (nodeType == OMNode.TEXT_NODE) {
+ serializeText((OMText) node, writer);
+ } else if (nodeType == OMNode.COMMENT_NODE) {
+ serializeComment((OMText) node, writer);
+ } else if (nodeType == OMNode.CDATA_SECTION_NODE) {
+ serializeCData((OMText) node, writer);
+ // added by Thilina
+ } else if (nodeType == OMNode.BLOB_NODE) {
+ serializeBlob((OMBlob) node, writer);
+ }
+ writer.flush();
+ }
+
+ /**
+ * @param element
+ */
+ protected void serializeElement(OMElement element, XMLStreamWriter writer)
+ throws XMLStreamException {
+
+ OMNamespace ns = element.getNamespace();
+ String prefix = null;
+ String nameSpaceName = null;
+ if (ns != null) {
+ prefix = ns.getPrefix();
+ nameSpaceName = ns.getName();
+ if (prefix != null) {
+ writer.writeStartElement(prefix, element.getLocalName(),
+ nameSpaceName);
+ if (!prefixList.contains(prefix)) {
+ writer.writeNamespace(prefix, nameSpaceName);
+ prefixList.add(prefix);
+ }
+ } else {
+ writer.writeStartElement(nameSpaceName, element.getLocalName());
+ //add the own namespace
+ writer.writeDefaultNamespace(nameSpaceName);
+
+ }
+ }
+
+ //add the elements attributes
+ Iterator attributes = element.getAttributes();
+ if (attributes!=null)
+ {
+ while (attributes.hasNext()) {
+ serializeAttribute((OMAttribute) attributes.next(), writer);
+ }
+ }
+
+ //add the namespaces
+ Iterator namespaces = element.getAllDeclaredNamespaces();
+ if (namespaces != null) {
+ while (namespaces.hasNext()) {
+ serializeNamespace((OMNamespace) namespaces.next(), writer);
+ }
+ }
+
+ //add the children
+ Iterator children = element.getChildren();
+
+ while (children.hasNext()) {
+ Object node = children.next();
+ if (node != null) {
+ serializeNode((OMNode) node, writer);
+ }
+ }
+
+ writer.writeEndElement();
+
+ }
+
+ /*
+ * Edited by Thilina Gunarathne
+ */
+ protected void serializeBlob(OMBlob blob, XMLStreamWriter xmlWriter)
+ throws XMLStreamException {
+
+ if (xmlWriter instanceof MTOMXMLStreamWriter) {
+ MTOMXMLStreamWriter writer = (MTOMXMLStreamWriter) xmlWriter;
+ OMNamespace ns = new OMNamespaceImpl("http://www.w3.org/2004/08/xop/Include", "xop");
+ String prefix = null;
+ String nameSpaceName = null;
+ if (ns != null) {
+ prefix = ns.getPrefix();
+ nameSpaceName = ns.getName();
+ if (prefix != null) {
+ writer.writeStartElement(prefix, "Include", nameSpaceName);
+ //if (!prefixList.contains(prefix)) {
+ writer.writeNamespace(prefix, nameSpaceName);
+ // prefixList.add(prefix);
+ //}
+ }
+ }
+
+ //get the Cid from the MTOMwriter
+ String cid;
+ try {
+ cid = writer.writeAttachment(blob);
+ OMAttribute href = new OMAttributeImpl("href",
+ new OMNamespaceImpl("", ""), "cid:" + cid.trim());
+ serializeAttribute(href, writer);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ writer.writeEndElement();
+ } else {
+ ByteArrayOutputStream byteStream;
+ try {
+ byteStream = (ByteArrayOutputStream) blob.getOutputStream();
+ xmlWriter.writeCharacters(Base64.encode(byteStream
+ .toByteArray()));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ }
+
+ /*
+ * Edit over
+ */
+
+ protected void serializeText(OMText text, XMLStreamWriter writer)
+ throws XMLStreamException {
+ writer.writeCharacters(text.getValue());
+ }
+
+ protected void serializeCData(OMText text, XMLStreamWriter writer)
+ throws XMLStreamException {
+ writer.writeCData(text.getValue());
+ }
+
+ protected void serializeComment(OMText text, XMLStreamWriter writer)
+ throws XMLStreamException {
+ writer.writeComment(text.getValue());
+ }
+
+ /**
+ * @param attr
+ * @param writer
+ * @throws XMLStreamException
+ */
+
+ protected void serializeAttribute(OMAttribute attr, XMLStreamWriter writer)
+ throws XMLStreamException {
+
+ //first check whether the attribute is associated with a namespace
+ OMNamespace ns = attr.getNamespace();
+ String prefix = null;
+ String namespaceName = null;
+ if (ns != null) {
+ //add the prefix if it's availble
+ prefix = ns.getPrefix();
+ namespaceName = ns.getName();
+
+ if (prefix != null)
+ writer.writeAttribute(prefix, namespaceName, attr
+ .getLocalName(), attr.getValue());
+ else
+ writer.writeAttribute(namespaceName, attr.getLocalName(), attr
+ .getValue());
+ } else {
+ writer.writeAttribute(attr.getLocalName(), attr.getValue());
+ }
+
+ }
+
+ protected void serializeNamespace(OMNamespace namespace,
+ XMLStreamWriter writer) throws XMLStreamException {
+ if (namespace != null) {
+ String prefix = namespace.getPrefix();
+ if (!prefixList.contains(prefix))
+ writer.writeNamespace(prefix, namespace.getName());
+
+ }
+ }
+
+}
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/traverse/OMChildrenIterator.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/traverse/OMChildrenIterator.java?view=auto&rev=157534
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/traverse/OMChildrenIterator.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM/src/java/org/apache/axis/om/impl/llom/traverse/OMChildrenIterator.java Tue Mar 15 02:56:31 2005
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.axis.om.impl.llom.traverse;
+
+import org.apache.axis.om.OMException;
+import org.apache.axis.om.OMNode;
+
+import java.util.Iterator;
+
+/**
+ * Class OMChildrenIterator
+ */
+public class OMChildrenIterator implements Iterator {
+ /**
+ * Field currentChild
+ */
+ protected OMNode currentChild;
+
+ /**
+ * Field lastChild
+ */
+ protected OMNode lastChild;
+
+ /**
+ * Field nextCalled
+ */
+ protected boolean nextCalled = false;
+
+ /**
+ * Field removeCalled
+ */
+ protected boolean removeCalled = false;
+
+ /**
+ * Constructor OMChildrenIterator
+ *
+ * @param currentChild
+ */
+ public OMChildrenIterator(OMNode currentChild) {
+ this.currentChild = currentChild;
+ }
+
+ /**
+ * Removes from the underlying collection the last element returned by the
+ * iterator (optional operation). This method can be called only once per
+ * call to <tt>next</tt>. The behavior of an iterator is unspecified if
+ * the underlying collection is modified while the iteration is in
+ * progress in any way other than by calling this method.
+ *
+ * @throws UnsupportedOperationException if the <tt>remove</tt>
+ * operation is not supported by this Iterator.
+ * @throws IllegalStateException if the <tt>next</tt> method has not
+ * yet been called, or the <tt>remove</tt> method has already
+ * been called after the last call to the <tt>next</tt>
+ * method.
+ */
+ public void remove() {
+ if (!nextCalled) {
+ throw new IllegalStateException(
+ "next method has not yet being called");
+ }
+ if (removeCalled) {
+ throw new IllegalStateException("remove has already being called");
+ }
+ removeCalled = true;
+
+ // since this acts on the last child there is no need to mess with the current child
+ if (lastChild == null) {
+ throw new OMException("cannot remove a child at this stage!");
+ }
+ lastChild.detach();
+ }
+
+ /**
+ * Returns <tt>true</tt> if the iteration has more elements. (In other
+ * words, returns <tt>true</tt> if <tt>next</tt> would return an element
+ * rather than throwing an exception.)
+ *
+ * @return <tt>true</tt> if the iterator has more elements.
+ */
+ public boolean hasNext() {
+ return (currentChild != null);
+ }
+
+ /**
+ * Returns the next element in the iteration.
+ *
+ * @return the next element in the iteration.
+ * @throws java.util.NoSuchElementException
+ * iteration has no more elements.
+ */
+ public Object next() {
+ nextCalled = true;
+ removeCalled = false;
+ if (hasNext()) {
+ lastChild = currentChild;
+ currentChild = currentChild.getNextSibling();
+ return lastChild;
+ }
+ return null;
+ }
+}