Author: scheu
Date: Sat Aug  9 11:34:22 2008
New Revision: 684312

URL: http://svn.apache.org/viewvc?rev=684312&view=rev
Log:
WSCOMMONS-370
Added validator.  The validator filters the OMStaXWrapper events if debug is 
enabled.  
If the validator detects assertion failures (i.e. the end element event does 
not match the start element event), then
debug messages are logged. These assertion messages will help identify problems.

I also added other miscellaneous debug trace.

All of these changes are only enabled if debug is enabled. 

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/OMXMLStreamReaderValidator.java
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MIMEOutputUtils.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MIMEOutputUtils.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MIMEOutputUtils.java?rev=684312&r1=684311&r2=684312&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MIMEOutputUtils.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MIMEOutputUtils.java
 Sat Aug  9 11:34:22 2008
@@ -38,6 +38,8 @@
 import org.apache.axiom.om.OMText;
 import org.apache.axiom.soap.SOAP11Constants;
 import org.apache.axiom.soap.SOAP12Constants;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * Utility class used to write out XML with Attachments
@@ -45,6 +47,9 @@
  *
  */
 public class MIMEOutputUtils {
+    
+    private static Log log = LogFactory.getLog(MIMEOutputUtils.class);
+    private static boolean isDebugEnabled = log.isDebugEnabled();
 
     private static byte[] CRLF = { 13, 10 };
 
@@ -128,6 +133,9 @@
                                 String charSetEncoding, 
                                 String SOAPContentType) {
         try {
+            if (isDebugEnabled) {
+                log.debug("Start: write the SOAPPart and the attachments");
+            }
             // TODO: Instead of buffering the SOAPPart contents, it makes more
             // sense to split this method in two.  Write out the SOAPPart 
headers
             // and later write out the attachments.  This will avoid the cost 
and
@@ -162,6 +170,9 @@
             }
             finishWritingMime(outStream);
             outStream.flush();
+            if (isDebugEnabled) {
+                log.debug("End: write the SOAPPart and the attachments");
+            }
         } catch (IOException e) {
             throw new OMException("Error while writing to the OutputStream.", 
e);
         } catch (MessagingException e) {
@@ -196,6 +207,9 @@
     public static MimeBodyPart createMimeBodyPart(String contentID,
                                                   DataHandler dataHandler)
             throws MessagingException {
+        if (isDebugEnabled) {
+            log.debug("Create MimeBodyPart for " + contentID);
+        }
         String encoding = null;
         MimeBodyPart mimeBodyPart = new MimeBodyPart();
         mimeBodyPart.setDataHandler(dataHandler);
@@ -242,16 +256,25 @@
                                      MimeBodyPart part,
                                      String boundary) throws IOException,
             MessagingException {
+        if (isDebugEnabled) {
+            log.debug("Start writeMimeBodyPart for " + part.getContentID());
+        }
         outStream.write(CRLF);
         part.writeTo(outStream);
         outStream.write(CRLF);
         writeMimeBoundary(outStream, boundary);
         outStream.flush();
+        if (isDebugEnabled) {
+            log.debug("End writeMimeBodyPart");
+        }
     }
 
     /** @throws IOException This will write "--" to the end of last boundary */
     public static void finishWritingMime(OutputStream outStream)
             throws IOException {
+        if (isDebugEnabled) {
+            log.debug("Write --, which indicates the end of the last 
boundary");
+        }
         outStream.write(new byte[] { 45, 45 });
     }
 

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java?rev=684312&r1=684311&r2=684312&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/MTOMXMLStreamWriter.java
 Sat Aug  9 11:34:22 2008
@@ -1,421 +1,444 @@
-/*
- * 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.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
-import java.util.LinkedList;
-
-import javax.activation.DataHandler;
-import javax.xml.namespace.NamespaceContext;
-import javax.xml.stream.FactoryConfigurationError;
-import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
-
-import org.apache.axiom.attachments.impl.BufferUtils;
-import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.OMNode;
-import org.apache.axiom.om.OMOutputFormat;
-import org.apache.axiom.om.OMText;
-import org.apache.axiom.om.util.CommonUtils;
-import org.apache.axiom.om.util.StAXUtils;
-import org.apache.axiom.soap.SOAP11Constants;
-import org.apache.axiom.soap.SOAP12Constants;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-
-/**
- * MTOMXMLStreamWriter is an XML + Attachments stream writer.
- * 
- * For the moment this assumes that transport takes the decision of whether to 
optimize or not by
- * looking at whether the MTOM optimize is enabled & also looking at the OM 
tree whether it has any
- * optimizable content.
- */
-public class MTOMXMLStreamWriter implements XMLStreamWriter {
-    private static Log log = LogFactory.getLog(MTOMXMLStreamWriter.class);
-    private final static int UNSUPPORTED = -1;
-    private final static int EXCEED_LIMIT = 1;
-    private XMLStreamWriter xmlWriter;
-    private OutputStream outStream;
-    private LinkedList binaryNodeList = new LinkedList();
-    private ByteArrayOutputStream bufferedXML;  // XML for the SOAPPart
-    private OMOutputFormat format = new OMOutputFormat();
-    
-    // State variables
-    private boolean isEndDocument = false; // has endElement been called
-    private boolean isComplete = false;    // have the attachments been written
-    private int depth = 0;                 // current eleement depth
-
-    public MTOMXMLStreamWriter(XMLStreamWriter xmlWriter) {
-        this.xmlWriter = xmlWriter;
-    }
-
-    /**
-     * Creates a new MTOMXMLStreamWriter with specified encoding.
-     *
-     * @param outStream
-     * @param format
-     * @throws XMLStreamException
-     * @throws FactoryConfigurationError
-     * @see OMOutputFormat#DEFAULT_CHAR_SET_ENCODING
-     */
-    public MTOMXMLStreamWriter(OutputStream outStream, OMOutputFormat format)
-            throws XMLStreamException, FactoryConfigurationError {
-        if (log.isDebugEnabled()) {
-            log.debug("OutputStream =" + outStream.getClass());
-            log.debug("OMFormat = " + format.toString());
-            log.debug("Call Stack =" + CommonUtils.callStackToString());
-        }
-        this.format = format;
-        this.outStream = outStream;
-
-        if (format.getCharSetEncoding() == null) //Default encoding is UTF-8
-            
format.setCharSetEncoding(OMOutputFormat.DEFAULT_CHAR_SET_ENCODING);
-
-        if (format.isOptimized()) {
-            // REVIEW If the buffered XML gets too big, should it be written 
out to a file 
-            bufferedXML = new ByteArrayOutputStream();
-            xmlWriter = 
StAXUtils.createXMLStreamWriter(bufferedXML,format.getCharSetEncoding());
-        } else {
-            xmlWriter = StAXUtils.createXMLStreamWriter(outStream,
-                                                        
format.getCharSetEncoding());
-        }
-    }
-
-    public void writeStartElement(String string) throws XMLStreamException {
-        xmlWriter.writeStartElement(string);
-        depth++;
-    }
-
-    public void writeStartElement(String string, String string1) throws 
XMLStreamException {
-        xmlWriter.writeStartElement(string, string1);
-        depth++;
-    }
-
-    public void writeStartElement(String string, String string1, String 
string2)
-            throws XMLStreamException {
-        xmlWriter.writeStartElement(string, string1, string2);
-        depth++;
-    }
-
-    public void writeEmptyElement(String string, String string1) throws 
XMLStreamException {
-        xmlWriter.writeStartElement(string, string1);
-    }
-
-    public void writeEmptyElement(String string, String string1, String 
string2)
-            throws XMLStreamException {
-        xmlWriter.writeStartElement(string, string1, string2);
-    }
-
-    public void writeEmptyElement(String string) throws XMLStreamException {
-        xmlWriter.writeStartElement(string);
-    }
-
-    public void writeEndElement() throws XMLStreamException {
-        xmlWriter.writeEndElement();
-        depth--;
-    }
-
-    public void writeEndDocument() throws XMLStreamException {
-        xmlWriter.writeEndDocument();
-        isEndDocument = true; 
-    }
-
-    public void close() throws XMLStreamException {
-        xmlWriter.close();
-    }
-
-    /**
-     * Flush is overridden to trigger the attachment serialization
-     */
-    public void flush() throws XMLStreamException {
-        xmlWriter.flush();
-        String SOAPContentType;
-        // flush() triggers the optimized attachment writing.
-        // If the optimized attachments are specified, and the xml
-        // document is completed, then write out the attachments.
-        if (format.isOptimized() && !isComplete & (isEndDocument || depth == 
0)) {
-            isComplete = true;
-            if (format.isSOAP11()) {
-                SOAPContentType = SOAP11Constants.SOAP_11_CONTENT_TYPE;
-            } else {
-                SOAPContentType = SOAP12Constants.SOAP_12_CONTENT_TYPE;
-            }
-            try {
-                MIMEOutputUtils.complete(outStream,
-                                         bufferedXML.toByteArray(),
-                                         binaryNodeList,
-                                         format.getMimeBoundary(),
-                                         format.getRootContentId(),
-                                         format.getCharSetEncoding(),
-                                         SOAPContentType);
-                bufferedXML.close();
-                bufferedXML = null;
-            } catch (UnsupportedEncodingException e) {
-                throw new OMException(e);
-            } catch (IOException e) {
-                throw new OMException(e);
-            }
-        }
-    }
-    
-
-    public void writeAttribute(String string, String string1) throws 
XMLStreamException {
-        xmlWriter.writeAttribute(string, string1);
-    }
-
-    public void writeAttribute(String string, String string1, String string2, 
String string3)
-            throws XMLStreamException {
-        xmlWriter.writeAttribute(string, string1, string2, string3);
-    }
-
-    public void writeAttribute(String string, String string1, String string2)
-            throws XMLStreamException {
-        xmlWriter.writeAttribute(string, string1, string2);
-    }
-
-    public void writeNamespace(String string, String string1) throws 
XMLStreamException {
-        xmlWriter.writeNamespace(string, string1);
-    }
-
-    public void writeDefaultNamespace(String string) throws XMLStreamException 
{
-        xmlWriter.writeDefaultNamespace(string);
-    }
-
-    public void writeComment(String string) throws XMLStreamException {
-        xmlWriter.writeComment(string);
-    }
-
-    public void writeProcessingInstruction(String string) throws 
XMLStreamException {
-        xmlWriter.writeProcessingInstruction(string);
-    }
-
-    public void writeProcessingInstruction(String string, String string1)
-            throws XMLStreamException {
-        xmlWriter.writeProcessingInstruction(string, string1);
-    }
-
-    public void writeCData(String string) throws XMLStreamException {
-        xmlWriter.writeCData(string);
-    }
-
-    public void writeDTD(String string) throws XMLStreamException {
-        xmlWriter.writeDTD(string);
-    }
-
-    public void writeEntityRef(String string) throws XMLStreamException {
-        xmlWriter.writeEntityRef(string);
-    }
-
-    public void writeStartDocument() throws XMLStreamException {
-        xmlWriter.writeStartDocument();
-    }
-
-    public void writeStartDocument(String string) throws XMLStreamException {
-        xmlWriter.writeStartDocument(string);
-    }
-
-    public void writeStartDocument(String string, String string1) throws 
XMLStreamException {
-        xmlWriter.writeStartDocument(string, string1);
-    }
-
-    public void writeCharacters(String string) throws XMLStreamException {
-        xmlWriter.writeCharacters(string);
-    }
-
-    public void writeCharacters(char[] chars, int i, int i1) throws 
XMLStreamException {
-        xmlWriter.writeCharacters(chars, i, i1);
-    }
-
-    public String getPrefix(String string) throws XMLStreamException {
-        return xmlWriter.getPrefix(string);
-    }
-
-    public void setPrefix(String string, String string1) throws 
XMLStreamException {
-        xmlWriter.setPrefix(string, string1);
-    }
-
-    public void setDefaultNamespace(String string) throws XMLStreamException {
-        xmlWriter.setDefaultNamespace(string);
-    }
-
-    public void setNamespaceContext(NamespaceContext namespaceContext) throws 
XMLStreamException {
-        xmlWriter.setNamespaceContext(namespaceContext);
-    }
-
-    public NamespaceContext getNamespaceContext() {
-        return xmlWriter.getNamespaceContext();
-    }
-
-    public Object getProperty(String string) throws IllegalArgumentException {
-        return xmlWriter.getProperty(string);
-    }
-
-    public boolean isOptimized() {
-        return format.isOptimized();
-    }
-
-    public String getContentType() {
-        return format.getContentType();
-    }
-
-    public void writeOptimized(OMText node) {
-        if(log.isDebugEnabled()){
-            log.debug("Start MTOMXMLStreamWriter.writeOptimized()");
-        }
-        binaryNodeList.add(node);    
-        if(log.isDebugEnabled()){
-            log.debug("Exit MTOMXMLStreamWriter.writeOptimized()");
-        }
-    }
-    /*
-     * This method check if size of dataHandler exceeds the optimization 
Threshold
-     * set on OMOutputFormat. 
-     * return true is size exceeds the threshold limit.
-     * return false otherwise.
-     */
-    public boolean isOptimizedThreshold(OMText node){
-       if(log.isDebugEnabled()){
-            log.debug("Start MTOMXMLStreamWriter.isOptimizedThreshold()");
-        }
-        DataHandler dh = (DataHandler)node.getDataHandler();
-        int optimized = UNSUPPORTED;
-        if(dh!=null){
-            if(log.isDebugEnabled()){
-                log.debug("DataHandler fetched, starting optimized Threshold 
processing");
-            }
-            optimized= BufferUtils.doesDataHandlerExceedLimit(dh, 
format.getOptimizedThreshold());
-        }
-        if(optimized == UNSUPPORTED || optimized == EXCEED_LIMIT){
-            if(log.isDebugEnabled()){
-                log.debug("node should be added to binart NodeList for 
optimization");
-            }
-            return true;
-        }
-        return false;
-    }
-    
-    public void setXmlStreamWriter(XMLStreamWriter xmlWriter) {
-        this.xmlWriter = xmlWriter;
-    }
-
-    public XMLStreamWriter getXmlStreamWriter() {
-        return xmlWriter;
-    }
-
-    public String getMimeBoundary() {
-        return format.getMimeBoundary();
-    }
-
-    public String getRootContentId() {
-        return format.getRootContentId();
-    }
-
-    public String getNextContentId() {
-        return format.getNextContentId();
-    }
-
-    /**
-     * Returns the character set encoding scheme. If the value of the 
charSetEncoding is not set
-     * then the default will be returned.
-     *
-     * @return Returns encoding.
-     */
-    public String getCharSetEncoding() {
-        return format.getCharSetEncoding();
-    }
-
-    public void setCharSetEncoding(String charSetEncoding) {
-        format.setCharSetEncoding(charSetEncoding);
-    }
-
-    public String getXmlVersion() {
-        return format.getXmlVersion();
-    }
-
-    public void setXmlVersion(String xmlVersion) {
-        format.setXmlVersion(xmlVersion);
-    }
-
-    public void setSoap11(boolean b) {
-        format.setSOAP11(b);
-    }
-
-    public boolean isIgnoreXMLDeclaration() {
-        return format.isIgnoreXMLDeclaration();
-    }
-
-    public void setIgnoreXMLDeclaration(boolean ignoreXMLDeclaration) {
-        format.setIgnoreXMLDeclaration(ignoreXMLDeclaration);
-    }
-
-    public void setDoOptimize(boolean b) {
-        format.setDoOptimize(b);
-    }
-
-    public void setOutputFormat(OMOutputFormat format) {
-        this.format = format;
-    }
-    
-    /**
-     * If this XMLStreamWriter is connected to an OutputStream
-     * then the OutputStream is returned.  This allows a node
-     * (perhaps an OMSourcedElement) to write its content
-     * directly to the OutputStream.
-     * @return OutputStream or null
-     */
-    public OutputStream getOutputStream() throws XMLStreamException {  
-        OutputStream os = null;
-        if (bufferedXML != null) {
-            os = bufferedXML;
-        } else {
-            os = outStream;
-        }
-       
-        if (os != null) {
-            // Flush the state of the writer..Many times the 
-            // write defers the writing of tag characters (>)
-            // until the next write.  Flush out this character
-            this.writeCharacters(""); 
-            this.flush();
-        }
-        return os;
-    }
-    
-    /**
-     * Writes the relevant output.
-     *
-     * @param writer
-     * @throws XMLStreamException
-     */
-    private void writeOutput(OMText textNode) throws XMLStreamException {
-        int type = textNode.getType();
-        if (type == OMNode.TEXT_NODE || type == OMNode.SPACE_NODE) {
-            writeCharacters(textNode.getText());
-        } else if (type == OMNode.CDATA_SECTION_NODE) {
-            writeCData(textNode.getText());
-        } else if (type == OMNode.ENTITY_REFERENCE_NODE) {
-            writeEntityRef(textNode.getText());
-        }
-    }
-}
+/*
+ * 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.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.LinkedList;
+
+import javax.activation.DataHandler;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.attachments.impl.BufferUtils;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.util.CommonUtils;
+import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.soap.SOAP11Constants;
+import org.apache.axiom.soap.SOAP12Constants;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * MTOMXMLStreamWriter is an XML + Attachments stream writer.
+ * 
+ * For the moment this assumes that transport takes the decision of whether to 
optimize or not by
+ * looking at whether the MTOM optimize is enabled & also looking at the OM 
tree whether it has any
+ * optimizable content.
+ */
+public class MTOMXMLStreamWriter implements XMLStreamWriter {
+    private static Log log = LogFactory.getLog(MTOMXMLStreamWriter.class);
+    private static boolean isDebugEnabled = log.isDebugEnabled();
+    private final static int UNSUPPORTED = -1;
+    private final static int EXCEED_LIMIT = 1;
+    private XMLStreamWriter xmlWriter;
+    private OutputStream outStream;
+    private LinkedList binaryNodeList = new LinkedList();
+    private ByteArrayOutputStream bufferedXML;  // XML for the SOAPPart
+    private OMOutputFormat format = new OMOutputFormat();
+    
+    // State variables
+    private boolean isEndDocument = false; // has endElement been called
+    private boolean isComplete = false;    // have the attachments been written
+    private int depth = 0;                 // current eleement depth
+
+    public MTOMXMLStreamWriter(XMLStreamWriter xmlWriter) {
+        this.xmlWriter = xmlWriter;
+    }
+
+    /**
+     * Creates a new MTOMXMLStreamWriter with specified encoding.
+     *
+     * @param outStream
+     * @param format
+     * @throws XMLStreamException
+     * @throws FactoryConfigurationError
+     * @see OMOutputFormat#DEFAULT_CHAR_SET_ENCODING
+     */
+    public MTOMXMLStreamWriter(OutputStream outStream, OMOutputFormat format)
+            throws XMLStreamException, FactoryConfigurationError {
+        if (isDebugEnabled) {
+            log.debug("OutputStream =" + outStream.getClass());
+            log.debug("OMFormat = " + format.toString());
+            log.debug("Call Stack =" + CommonUtils.callStackToString());
+        }
+        this.format = format;
+        this.outStream = outStream;
+
+        if (format.getCharSetEncoding() == null) //Default encoding is UTF-8
+            
format.setCharSetEncoding(OMOutputFormat.DEFAULT_CHAR_SET_ENCODING);
+
+        if (format.isOptimized()) {
+            // REVIEW If the buffered XML gets too big, should it be written 
out to a file 
+            bufferedXML = new ByteArrayOutputStream();
+            xmlWriter = 
StAXUtils.createXMLStreamWriter(bufferedXML,format.getCharSetEncoding());
+        } else {
+            xmlWriter = StAXUtils.createXMLStreamWriter(outStream,
+                                                        
format.getCharSetEncoding());
+        }
+    }
+
+    public void writeStartElement(String string) throws XMLStreamException {
+        xmlWriter.writeStartElement(string);
+        depth++;
+    }
+
+    public void writeStartElement(String string, String string1) throws 
XMLStreamException {
+        xmlWriter.writeStartElement(string, string1);
+        depth++;
+    }
+
+    public void writeStartElement(String string, String string1, String 
string2)
+            throws XMLStreamException {
+        xmlWriter.writeStartElement(string, string1, string2);
+        depth++;
+    }
+
+    public void writeEmptyElement(String string, String string1) throws 
XMLStreamException {
+        xmlWriter.writeStartElement(string, string1);
+    }
+
+    public void writeEmptyElement(String string, String string1, String 
string2)
+            throws XMLStreamException {
+        xmlWriter.writeStartElement(string, string1, string2);
+    }
+
+    public void writeEmptyElement(String string) throws XMLStreamException {
+        xmlWriter.writeStartElement(string);
+    }
+
+    public void writeEndElement() throws XMLStreamException {
+        xmlWriter.writeEndElement();
+        depth--;
+    }
+
+    public void writeEndDocument() throws XMLStreamException {
+        if (isDebugEnabled) {
+            log.debug("writeEndDocument");
+        }
+        xmlWriter.writeEndDocument();
+        isEndDocument = true; 
+    }
+
+    public void close() throws XMLStreamException {
+        if (isDebugEnabled) {
+            log.debug("close");
+        }
+        xmlWriter.close();
+    }
+
+    /**
+     * Flush is overridden to trigger the attachment serialization
+     */
+    public void flush() throws XMLStreamException {
+        if (isDebugEnabled) {
+            log.debug("Calling MTOMXMLStreamWriter.flush");
+        }
+        xmlWriter.flush();
+        String SOAPContentType;
+        // flush() triggers the optimized attachment writing.
+        // If the optimized attachments are specified, and the xml
+        // document is completed, then write out the attachments.
+        if (format.isOptimized() && !isComplete & (isEndDocument || depth == 
0)) {
+            if (isDebugEnabled) {
+                log.debug("The XML writing is completed.  Now the attachments 
are written");
+            }
+            isComplete = true;
+            if (format.isSOAP11()) {
+                SOAPContentType = SOAP11Constants.SOAP_11_CONTENT_TYPE;
+            } else {
+                SOAPContentType = SOAP12Constants.SOAP_12_CONTENT_TYPE;
+            }
+            try {
+                MIMEOutputUtils.complete(outStream,
+                                         bufferedXML.toByteArray(),
+                                         binaryNodeList,
+                                         format.getMimeBoundary(),
+                                         format.getRootContentId(),
+                                         format.getCharSetEncoding(),
+                                         SOAPContentType);
+                bufferedXML.close();
+                bufferedXML = null;
+            } catch (UnsupportedEncodingException e) {
+                throw new OMException(e);
+            } catch (IOException e) {
+                throw new OMException(e);
+            }
+        }
+    }
+    
+
+    public void writeAttribute(String string, String string1) throws 
XMLStreamException {
+        xmlWriter.writeAttribute(string, string1);
+    }
+
+    public void writeAttribute(String string, String string1, String string2, 
String string3)
+            throws XMLStreamException {
+        xmlWriter.writeAttribute(string, string1, string2, string3);
+    }
+
+    public void writeAttribute(String string, String string1, String string2)
+            throws XMLStreamException {
+        xmlWriter.writeAttribute(string, string1, string2);
+    }
+
+    public void writeNamespace(String string, String string1) throws 
XMLStreamException {
+        xmlWriter.writeNamespace(string, string1);
+    }
+
+    public void writeDefaultNamespace(String string) throws XMLStreamException 
{
+        xmlWriter.writeDefaultNamespace(string);
+    }
+
+    public void writeComment(String string) throws XMLStreamException {
+        xmlWriter.writeComment(string);
+    }
+
+    public void writeProcessingInstruction(String string) throws 
XMLStreamException {
+        xmlWriter.writeProcessingInstruction(string);
+    }
+
+    public void writeProcessingInstruction(String string, String string1)
+            throws XMLStreamException {
+        xmlWriter.writeProcessingInstruction(string, string1);
+    }
+
+    public void writeCData(String string) throws XMLStreamException {
+        xmlWriter.writeCData(string);
+    }
+
+    public void writeDTD(String string) throws XMLStreamException {
+        xmlWriter.writeDTD(string);
+    }
+
+    public void writeEntityRef(String string) throws XMLStreamException {
+        xmlWriter.writeEntityRef(string);
+    }
+
+    public void writeStartDocument() throws XMLStreamException {
+        xmlWriter.writeStartDocument();
+    }
+
+    public void writeStartDocument(String string) throws XMLStreamException {
+        xmlWriter.writeStartDocument(string);
+    }
+
+    public void writeStartDocument(String string, String string1) throws 
XMLStreamException {
+        xmlWriter.writeStartDocument(string, string1);
+    }
+
+    public void writeCharacters(String string) throws XMLStreamException {
+        xmlWriter.writeCharacters(string);
+    }
+
+    public void writeCharacters(char[] chars, int i, int i1) throws 
XMLStreamException {
+        xmlWriter.writeCharacters(chars, i, i1);
+    }
+
+    public String getPrefix(String string) throws XMLStreamException {
+        return xmlWriter.getPrefix(string);
+    }
+
+    public void setPrefix(String string, String string1) throws 
XMLStreamException {
+        xmlWriter.setPrefix(string, string1);
+    }
+
+    public void setDefaultNamespace(String string) throws XMLStreamException {
+        xmlWriter.setDefaultNamespace(string);
+    }
+
+    public void setNamespaceContext(NamespaceContext namespaceContext) throws 
XMLStreamException {
+        xmlWriter.setNamespaceContext(namespaceContext);
+    }
+
+    public NamespaceContext getNamespaceContext() {
+        return xmlWriter.getNamespaceContext();
+    }
+
+    public Object getProperty(String string) throws IllegalArgumentException {
+        return xmlWriter.getProperty(string);
+    }
+
+    public boolean isOptimized() {
+        return format.isOptimized();
+    }
+
+    public String getContentType() {
+        return format.getContentType();
+    }
+
+    public void writeOptimized(OMText node) {
+        if(isDebugEnabled){
+            log.debug("Start MTOMXMLStreamWriter.writeOptimized()");
+        }
+        binaryNodeList.add(node);    
+        if(isDebugEnabled){
+            log.debug("Exit MTOMXMLStreamWriter.writeOptimized()");
+        }
+    }
+    /*
+     * This method check if size of dataHandler exceeds the optimization 
Threshold
+     * set on OMOutputFormat. 
+     * return true is size exceeds the threshold limit.
+     * return false otherwise.
+     */
+    public boolean isOptimizedThreshold(OMText node){
+       if(isDebugEnabled){
+            log.debug("Start MTOMXMLStreamWriter.isOptimizedThreshold()");
+        }
+        DataHandler dh = (DataHandler)node.getDataHandler();
+        int optimized = UNSUPPORTED;
+        if(dh!=null){
+            if(isDebugEnabled){
+                log.debug("DataHandler fetched, starting optimized Threshold 
processing");
+            }
+            optimized= BufferUtils.doesDataHandlerExceedLimit(dh, 
format.getOptimizedThreshold());
+        }
+        if(optimized == UNSUPPORTED || optimized == EXCEED_LIMIT){
+            if(log.isDebugEnabled()){
+                log.debug("node should be added to binart NodeList for 
optimization");
+            }
+            return true;
+        }
+        return false;
+    }
+    
+    public void setXmlStreamWriter(XMLStreamWriter xmlWriter) {
+        this.xmlWriter = xmlWriter;
+    }
+
+    public XMLStreamWriter getXmlStreamWriter() {
+        return xmlWriter;
+    }
+
+    public String getMimeBoundary() {
+        return format.getMimeBoundary();
+    }
+
+    public String getRootContentId() {
+        return format.getRootContentId();
+    }
+
+    public String getNextContentId() {
+        return format.getNextContentId();
+    }
+
+    /**
+     * Returns the character set encoding scheme. If the value of the 
charSetEncoding is not set
+     * then the default will be returned.
+     *
+     * @return Returns encoding.
+     */
+    public String getCharSetEncoding() {
+        return format.getCharSetEncoding();
+    }
+
+    public void setCharSetEncoding(String charSetEncoding) {
+        format.setCharSetEncoding(charSetEncoding);
+    }
+
+    public String getXmlVersion() {
+        return format.getXmlVersion();
+    }
+
+    public void setXmlVersion(String xmlVersion) {
+        format.setXmlVersion(xmlVersion);
+    }
+
+    public void setSoap11(boolean b) {
+        format.setSOAP11(b);
+    }
+
+    public boolean isIgnoreXMLDeclaration() {
+        return format.isIgnoreXMLDeclaration();
+    }
+
+    public void setIgnoreXMLDeclaration(boolean ignoreXMLDeclaration) {
+        format.setIgnoreXMLDeclaration(ignoreXMLDeclaration);
+    }
+
+    public void setDoOptimize(boolean b) {
+        format.setDoOptimize(b);
+    }
+
+    public void setOutputFormat(OMOutputFormat format) {
+        this.format = format;
+    }
+    
+    /**
+     * If this XMLStreamWriter is connected to an OutputStream
+     * then the OutputStream is returned.  This allows a node
+     * (perhaps an OMSourcedElement) to write its content
+     * directly to the OutputStream.
+     * @return OutputStream or null
+     */
+    public OutputStream getOutputStream() throws XMLStreamException {  
+        OutputStream os = null;
+        if (bufferedXML != null) {
+            os = bufferedXML;
+        } else {
+            os = outStream;
+        }
+        
+        if (isDebugEnabled) {
+            if (os == null) {
+                log.debug("Direct access to the output stream is not 
available.");
+            } else if (bufferedXML != null) {
+                log.debug("Returning access to the buffered xml stream: " + 
bufferedXML);
+            } else {
+                log.debug("Returning access to the original output stream: " + 
os);
+            }
+        }
+       
+        if (os != null) {
+            // Flush the state of the writer..Many times the 
+            // write defers the writing of tag characters (>)
+            // until the next write.  Flush out this character
+            this.writeCharacters(""); 
+            this.flush();
+        }
+        return os;
+    }
+    
+    /**
+     * Writes the relevant output.
+     *
+     * @param writer
+     * @throws XMLStreamException
+     */
+    private void writeOutput(OMText textNode) throws XMLStreamException {
+        int type = textNode.getType();
+        if (type == OMNode.TEXT_NODE || type == OMNode.SPACE_NODE) {
+            writeCharacters(textNode.getText());
+        } else if (type == OMNode.CDATA_SECTION_NODE) {
+            writeCData(textNode.getText());
+        } else if (type == OMNode.ENTITY_REFERENCE_NODE) {
+            writeEntityRef(textNode.getText());
+        }
+    }
+}

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/OMXMLStreamReaderValidator.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/OMXMLStreamReaderValidator.java?rev=684312&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/OMXMLStreamReaderValidator.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/OMXMLStreamReaderValidator.java
 Sat Aug  9 11:34:22 2008
@@ -0,0 +1,383 @@
+/*
+ * 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.util;
+
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMXMLStreamReader;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+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 java.util.Stack;
+
+/**
+ * There are several places in the code where events are passed from 
+ * a source to a consumer using XMLStreamReader events. 
+ * 
+ *     OMXMLStreamReader (impl)--> consumer of XMLStreamReader events
+ * 
+ * This simple class can be interjected as a filter and used to do some simple 
validation.
+ * Validating the events coming from source (impl) can help find and correct 
errors 
+ * when they occur.  Otherwise the errors may be caught much further 
downstream and hard to fix.
+ * 
+ *    OMXMLStreamReader (impl)--> OMXMLStreamReaderValiator-> consumer of 
XMLStreamReader events
+ * 
+ * 
+ * In the initial version, the XMStreamValidator ensures that the start 
element events match the 
+ * end element events.
+ *
+ */
+public class OMXMLStreamReaderValidator implements OMXMLStreamReader {
+
+    private static Log log = 
LogFactory.getLog(OMXMLStreamReaderValidator.class);
+    private static boolean IS_DEBUG_ENABLED = log.isDebugEnabled();
+    private static boolean IS_ADV_DEBUG_ENABLED = false;  // Turn this on to 
trace every event
+    
+    
+    private final OMXMLStreamReader delegate;  // Actual XMLStreamReader 
implementation 
+    private boolean throwExceptions = false;   // Indicates whether 
OMException should be thrown if errors are disovered
+    private Stack stack = new Stack();         // Stack keeps track of the 
nested element QName
+    
+
+    /**
+     * @param delegate XMLStreamReader to validate
+     * @param throwExceptions (true if exceptions should be thrown when errors 
are encountered)
+     */
+    public OMXMLStreamReaderValidator(OMXMLStreamReader delegate, boolean 
throwExceptions) {
+        super();
+        this.delegate = delegate;
+        this.throwExceptions = throwExceptions;
+    }
+
+ 
+    public int next() throws XMLStreamException {
+        int event = delegate.next();
+        logParserState();
+        
+        // Make sure that the start element and end element events match.
+        // Mismatched events are a key indication that the delegate stream 
reader is 
+        // broken or corrupted.
+        switch (event) {
+        case XMLStreamConstants.START_ELEMENT:
+            stack.push(delegate.getName());
+            break;
+        case XMLStreamConstants.END_ELEMENT:
+            QName delegateQName = delegate.getName();
+            if (stack.isEmpty()) {
+                reportMismatchedEndElement(null, delegateQName);
+            } else {
+                QName expectedQName = (QName) stack.pop();
+                
+                if (!expectedQName.equals(delegateQName)) {
+                    reportMismatchedEndElement(expectedQName, delegateQName);
+                }
+            }
+            break;
+            
+        default :
+        
+        }
+        
+        return event;
+    }
+    
+    
+    /**
+     * Report a mismatched end element.
+     * @param expectedQName
+     * @param delegateQName
+     */
+    private void reportMismatchedEndElement(QName expectedQName, QName 
delegateQName) {
+        String text = null;
+        if (expectedQName == null) {
+            text = "An END_ELEMENT event for " + delegateQName + 
+                " was encountered, but the START_ELEMENT stack is empty.";
+        } else {
+            text = "An END_ELEMENT event for " + delegateQName + 
+                " was encountered.  But this does match the corresponding 
START_ELEMENT " + 
+                expectedQName + " event.";
+        }
+        if (IS_DEBUG_ENABLED) {
+            log.debug(text);
+        }       
+        if (throwExceptions) {
+            throw new OMException(text);
+        }
+    }
+    
+    public void close() throws XMLStreamException {
+        delegate.close();
+    }
+
+    public int getAttributeCount() {
+        return delegate.getAttributeCount();
+    }
+
+    public String getAttributeLocalName(int arg0) {
+        return delegate.getAttributeLocalName(arg0);
+    }
+
+    public QName getAttributeName(int arg0) {
+        return delegate.getAttributeName(arg0);
+    }
+
+    public String getAttributeNamespace(int arg0) {
+        return delegate.getAttributeNamespace(arg0);
+    }
+
+    public String getAttributePrefix(int arg0) {
+        return delegate.getAttributePrefix(arg0);
+    }
+
+    public String getAttributeType(int arg0) {
+        return delegate.getAttributeType(arg0);
+    }
+
+    public String getAttributeValue(int arg0) {
+        return delegate.getAttributeValue(arg0);
+    }
+
+    public String getAttributeValue(String arg0, String arg1) {
+        return delegate.getAttributeValue(arg0, arg1);
+    }
+
+    public String getCharacterEncodingScheme() {
+        return delegate.getCharacterEncodingScheme();
+    }
+
+    public String getElementText() throws XMLStreamException {
+        return delegate.getElementText();
+    }
+
+    public String getEncoding() {
+        return delegate.getEncoding();
+    }
+
+    public int getEventType() {
+        return delegate.getEventType();
+    }
+
+    public String getLocalName() {
+        return delegate.getLocalName();
+    }
+
+    public Location getLocation() {
+        return delegate.getLocation();
+    }
+
+    public QName getName() {
+        return delegate.getName();
+    }
+
+    public NamespaceContext getNamespaceContext() {
+        return delegate.getNamespaceContext();
+    }
+
+    public int getNamespaceCount() {
+        return delegate.getNamespaceCount();
+    }
+
+    public String getNamespacePrefix(int arg0) {
+        return delegate.getNamespacePrefix(arg0);
+    }
+
+    public String getNamespaceURI() {
+        return delegate.getNamespaceURI();
+    }
+
+    public String getNamespaceURI(int arg0) {
+        return delegate.getNamespaceURI(arg0);
+    }
+
+    public String getNamespaceURI(String arg0) {
+        return delegate.getNamespaceURI(arg0);
+    }
+
+    public String getPIData() {
+        return delegate.getPIData();
+    }
+
+    public String getPITarget() {
+        return delegate.getPITarget();
+    }
+
+    public String getPrefix() {
+        return delegate.getPrefix();
+    }
+
+    public Object getProperty(String arg0) throws IllegalArgumentException {
+        return delegate.getProperty(arg0);
+    }
+
+    public String getText() {
+        return delegate.getText();
+    }
+
+    public char[] getTextCharacters() {
+        return delegate.getTextCharacters();
+    }
+
+    public int getTextCharacters(int arg0, char[] arg1, int arg2, int arg3) 
throws XMLStreamException {
+        return delegate.getTextCharacters(arg0, arg1, arg2, arg3);
+    }
+
+    public int getTextLength() {
+        return delegate.getTextLength();
+    }
+
+    public int getTextStart() {
+        return delegate.getTextStart();
+    }
+
+    public String getVersion() {
+        return delegate.getVersion();
+    }
+
+    public boolean hasName() {
+        return delegate.hasName();
+    }
+
+    public boolean hasNext() throws XMLStreamException {
+        return delegate.hasNext();
+    }
+
+    public boolean hasText() {
+        return delegate.hasText();
+    }
+
+    public boolean isAttributeSpecified(int arg0) {
+        return delegate.isAttributeSpecified(arg0);
+    }
+
+    public boolean isCharacters() {
+        return delegate.isCharacters();
+    }
+
+    public boolean isEndElement() {
+        return delegate.isEndElement();
+    }
+
+    public boolean isStandalone() {
+        return delegate.isStandalone();
+    }
+
+    public boolean isStartElement() {
+        return delegate.isStartElement();
+    }
+
+    public boolean isWhiteSpace() {
+        return delegate.isWhiteSpace();
+    }
+
+   
+
+    public int nextTag() throws XMLStreamException {
+        return delegate.nextTag();
+    }
+
+    public void require(int arg0, String arg1, String arg2) throws 
XMLStreamException {
+        delegate.require(arg0, arg1, arg2);
+    }
+
+    public boolean standaloneSet() {
+        return delegate.standaloneSet();
+    }
+    
+    
+    /**
+     * Dump the current event of the delegate.
+     */
+    protected void logParserState() {
+        if (IS_ADV_DEBUG_ENABLED) {
+            int currentEvent = delegate.getEventType();
+            
+            switch (currentEvent) {
+            case XMLStreamConstants.START_ELEMENT:
+                log.trace("START_ELEMENT: ");
+                log.trace("  QName: " + delegate.getName());
+                break;
+            case XMLStreamConstants.START_DOCUMENT:
+                log.trace("START_DOCUMENT: ");
+                break;
+            case XMLStreamConstants.CHARACTERS:
+                log.trace("CHARACTERS: ");
+                log.trace(   "[" + delegate.getText() + "]");
+                break;
+            case XMLStreamConstants.CDATA:
+                log.trace("CDATA: ");
+                log.trace(   "[" + delegate.getText() + "]");
+                break;
+            case XMLStreamConstants.END_ELEMENT:
+                log.trace("END_ELEMENT: ");
+                log.trace("  QName: " + delegate.getName());
+                break;
+            case XMLStreamConstants.END_DOCUMENT:
+                log.trace("END_DOCUMENT: ");
+                break;
+            case XMLStreamConstants.SPACE:
+                log.trace("SPACE: ");
+                log.trace(   "[" + delegate.getText() + "]");
+                break;
+            case XMLStreamConstants.COMMENT:
+                log.trace("COMMENT: ");
+                log.trace(   "[" + delegate.getText() + "]");
+                break;
+            case XMLStreamConstants.DTD:
+                log.trace("DTD: ");
+                log.trace(   "[" + delegate.getText() + "]");
+                break;
+            case XMLStreamConstants.PROCESSING_INSTRUCTION:
+                log.trace("PROCESSING_INSTRUCTION: ");
+                log.trace("   [" + delegate.getPITarget() + "][" +
+                            delegate.getPIData() + "]");
+                break;
+            case XMLStreamConstants.ENTITY_REFERENCE:
+                log.trace("ENTITY_REFERENCE: ");
+                log.trace("    " + delegate.getLocalName() + "[" +
+                            delegate.getText() + "]");
+                break;
+            default :
+                log.trace("UNKNOWN_STATE: " + currentEvent);
+            
+            }
+        }
+    }
+
+    public DataHandler getDataHandler(String blobcid) {
+        return delegate.getDataHandler(blobcid);
+    }
+
+
+    public boolean isInlineMTOM() {
+        return delegate.isInlineMTOM();
+    }
+
+
+    public void setInlineMTOM(boolean value) {
+        delegate.setInlineMTOM(value);
+    }
+    
+
+}

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java?rev=684312&r1=684311&r2=684312&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
 Sat Aug  9 11:34:22 2008
@@ -31,6 +31,7 @@
 import org.apache.axiom.om.OMSourcedElement;
 import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.OMXMLStreamReader;
 import org.apache.axiom.om.impl.OMContainerEx;
 import org.apache.axiom.om.impl.OMNamespaceImpl;
 import org.apache.axiom.om.impl.OMNodeEx;
@@ -45,6 +46,7 @@
 import org.apache.axiom.om.impl.util.EmptyIterator;
 import org.apache.axiom.om.impl.util.OMSerializerUtil;
 import org.apache.axiom.om.util.ElementHelper;
+import org.apache.axiom.om.util.OMXMLStreamReaderValidator;
 import org.apache.axiom.om.util.StAXUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -763,19 +765,34 @@
                 this.buildNext();
             }
         }
+        
         // The om tree was built by hand and is already complete
+        OMXMLStreamReader reader = null;
         if ((builder == null) && done) {
-            return new OMStAXWrapper(null, this, false);
-        }
-        if ((builder == null) && !cache) {
-            throw new UnsupportedOperationException(
-                    "This element was not created in a manner to be switched");
+            reader =  new OMStAXWrapper(null, this, false);
+        } else {
+            if ((builder == null) && !cache) {
+                throw new UnsupportedOperationException(
+                "This element was not created in a manner to be switched");
+            }
+            if (builder != null && builder.isCompleted() && !cache && !done) {
+                throw new UnsupportedOperationException(
+                "The parser is already consumed!");
+            }
+            reader = new OMStAXWrapper(builder, this, cache);
         }
-        if (builder != null && builder.isCompleted() && !cache && !done) {
-            throw new UnsupportedOperationException(
-                    "The parser is already consumed!");
+        
+        // If debug is enabled, wrap the OMXMLStreamReader in a validator.
+        // The validator will check for mismatched events to help determine if 
the OMStAXWrapper
+        // is functioning correctly.  All problems are reported as debug.log 
messages
+        
+        if (DEBUG_ENABLED) {
+            reader = 
+                new OMXMLStreamReaderValidator(reader, // delegate to actual 
reader
+                     false); // log problems (true will cause exceptions to be 
thrown)
         }
-        return new OMStAXWrapper(builder, this, cache);
+        
+        return reader;
     }
 
     /**


Reply via email to