Author: dsosnoski
Date: Wed Apr 19 16:39:40 2006
New Revision: 395441

URL: http://svn.apache.org/viewcvs?rev=395441&view=rev
Log:
Added OMElement subclass with general data source.

Added:
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMDataSource.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
    
webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java
Modified:
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMFactory.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/factory/OMLinkedListImplFactory.java

Added: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMDataSource.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMDataSource.java?rev=395441&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMDataSource.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMDataSource.java
 Wed Apr 19 16:39:40 2006
@@ -0,0 +1,59 @@
+/**
+ * 
+ */
+package org.apache.axiom.om;
+
+import java.io.OutputStream;
+import java.io.Writer;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+/**
+ * Interface to arbitrary source of XML element data. This provides the hook 
for
+ * using a general data source (such as data binding frameworks) as the backing
+ * source of data for an element.
+ */
+public interface OMDataSource
+{
+    /**
+     * Serializes element data directly to stream.
+     *
+     * @param output destination stream for element XML text
+     * @param format output format information (<code>null</code> if none; may
+     * be ignored if not supported by data binding even if supplied)
+     * @throws XMLStreamException
+     */
+    public void serialize(OutputStream output, OMOutputFormat format)
+            throws XMLStreamException;
+    
+    /**
+     * Serializes element data directly to writer.
+     *
+     * @param writer destination writer for element XML text
+     * @param format output format information (<code>null</code> if none; may
+     * be ignored if not supported by data binding even if supplied)
+     * @throws XMLStreamException
+     */
+    public void serialize(Writer writer, OMOutputFormat format)
+            throws XMLStreamException;
+    
+    /**
+     * Serializes element data directly to StAX writer.
+     *
+     * @param xmlWriter destination writer
+     * @throws XMLStreamException
+     */
+    public void serialize(XMLStreamWriter xmlWriter)
+            throws XMLStreamException;
+    
+    /**
+     * Get parser for element data. In the general case this may require the
+     * data source to serialize data as XML text and then parse that text.
+     *
+     * @return element parser
+     * @throws XMLStreamException
+     */
+    public XMLStreamReader getReader() throws XMLStreamException;
+}

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMFactory.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMFactory.java?rev=395441&r1=395440&r2=395441&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMFactory.java 
(original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/OMFactory.java 
Wed Apr 19 16:39:40 2006
@@ -50,6 +50,17 @@
                                      OMXMLParserWrapper builder);
 
     /**
+     * Construct element with arbitrary data source. This is an optional
+     * operation which may not be supported by all factories.
+     * 
+     * @param source
+     * @param localName
+     * @param ns
+     */
+    public OMElement createOMElement(OMDataSource source, String localName,
+                                     OMNamespace ns);
+
+    /**
      * This is almost the same as as createOMElement(localName,OMNamespace) 
method above.
      * But some people may, for some reason, need to use the conventional 
method of putting a namespace.
      * Or in other words people might not want to use the new OMNamespace.

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=395441&r1=395440&r2=395441&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
 Wed Apr 19 16:39:40 2006
@@ -65,6 +65,21 @@
     }
 
     /**
+     * Constructor linked to existing element.
+     *
+     * @param factory
+     * @param parser
+     * @param element
+     */
+    public StAXOMBuilder(OMFactory factory, XMLStreamReader parser, OMElement 
element) {
+        this(factory, parser);
+        lastNode = element;
+        document.setOMDocumentElement(element);
+        doDebug = log.isDebugEnabled();
+        populateOMElement(element);
+    }
+
+    /**
      * @param filePath - Path to the XML file
      * @throws XMLStreamException
      * @throws FileNotFoundException
@@ -193,6 +208,20 @@
         }
     }
     
+    /**
+     * Populate element with data from parser START_ELEMENT event. This is used
+     * when the source of data for an element needs to be parsed on demand. The
+     * supplied element must already be set to the proper name and namespace.
+     * 
+     * @param node element to be populated
+     */
+    private void populateOMElement(OMElement node) {
+        // create the namespaces
+        processNamespaceData(node);
+        // fill in the attributes
+        processAttributes(node);
+        node.setLineNumber(parser.getLocation().getLineNumber());
+    }
 
     /**
      * Method createOMElement.
@@ -216,11 +245,7 @@
                     (OMElement) lastNode, this);
             e.setFirstChild(node);
         }
-        // create the namespaces
-        processNamespaceData(node);
-        // fill in the attributes
-        processAttributes(node);
-        node.setLineNumber(parser.getLocation().getLineNumber());
+        populateOMElement(node);
         return node;
     }
 

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java?rev=395441&r1=395440&r2=395441&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/dom/factory/OMDOMFactory.java
 Wed Apr 19 16:39:40 2006
@@ -18,6 +18,7 @@
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMComment;
 import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMDataSource;
 import org.apache.axiom.om.OMDocType;
 import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
@@ -132,6 +133,20 @@
                     "The parent container can only be an ELEMENT, DOCUMENT " +
                     "or a DOCUMENT FRAGMENT");
         }
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMFactory#createOMElement(org.apache.axiom.om.OMDataSource, 
java.lang.String, org.apache.axiom.om.OMNamespace, 
org.apache.axiom.om.OMContainer)
+     */
+    public OMElement createOMElement(OMDataSource source, String localName, 
OMNamespace ns, OMContainer parent) {
+        throw new UnsupportedOperationException("Not supported for DOM");
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMFactory#createOMElement(org.apache.axiom.om.OMDataSource, 
java.lang.String, org.apache.axiom.om.OMNamespace)
+     */
+    public OMElement createOMElement(OMDataSource source, String localName, 
OMNamespace ns) {
+        throw new UnsupportedOperationException("Not supported for DOM");
     }
 
     /**

Added: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=395441&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 Wed Apr 19 16:39:40 2006
@@ -0,0 +1,648 @@
+/*
+ * Copyright 2006 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.axiom.om.impl.llom;
+
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * <p>Element backed by an arbitrary data source. When necessary, this element
+ * will be expanded by creating a parser from the data source.</p>
+ * 
+ * <p>Whenever methods are added to the base [EMAIL PROTECTED]
+ * org.apache.axiom.om.impl.llom.OMElementImpl} class the corresponding methods
+ * must be added to this class (there's a unit test to verify that this has 
been
+ * done, just to make sure nothing gets accidentally broken). If the method
+ * only requires the element name and/or namespace information, the base class
+ * method can be called directly. Otherwise, the element must be expanded into 
a
+ * full OM tree (by calling the [EMAIL PROTECTED] #forceExpand()} method) 
before the base
+ * class method is called. This will typically involve a heavy overhead 
penalty,
+ * so should be avoided if possible.</p>
+ */
+public class OMSourcedElementImpl extends OMElementImpl
+{
+    /** Data source for element data. */
+    private final OMDataSource dataSource;
+    
+    /** Namespace for element, needed in order to bypass base class handling. 
*/
+    private OMNamespace definedNamespace;
+    
+    /** Flag for parser provided to base element class. */
+    private boolean isParserSet;
+    
+    private static Log log = LogFactory.getLog(OMSourcedElementImpl.class);
+    
+    /**
+     * Constructor.
+     * 
+     * @param localName
+     * @param ns
+     * @param factory
+     * @param source
+     */
+    public OMSourcedElementImpl(String localName, OMNamespace ns, OMFactory 
factory, OMDataSource source) {
+        super(localName, null, factory);
+        dataSource = source;
+        definedNamespace = ns;
+    }
+
+    /**
+     * Get parser from data source.
+     */
+    private XMLStreamReader getDirectReader() {
+        try {
+            return dataSource.getReader();
+        } catch (XMLStreamException e) {
+            log.error("OMSourcedElementImpl.getDirectReader: could not get 
parser from data source for element " +
+                getLocalName(), e);
+            throw new RuntimeException("Error obtaining parser from data 
source:" +
+                e.getMessage());
+        }
+    }
+
+    /**
+     * Set parser for OM, if not previously set. Since the builder is what
+     * actually constructs the tree on demand, this first creates a builder
+     */
+    private void forceExpand() {
+        if (!isParserSet) {
+            
+            log.debug("OMSourcedElementImpl.forceExpand: expanding element " +
+                getLocalName());
+            
+            // position reader to start tag
+            XMLStreamReader reader = getDirectReader();
+            try {
+                while (reader.next() != XMLStreamConstants.START_ELEMENT);
+            } catch (XMLStreamException e) {
+                log.error("OMSourcedElementImpl.forceExpand: error parsing 
data soruce document for element " +
+                    getLocalName(), e);
+                throw new RuntimeException("Error parsing data source 
document:" +
+                    e.getMessage());
+            }
+            
+            // make sure element name matches what was expected
+            if (!reader.getLocalName().equals(getLocalName())) {
+                log.error("OMSourcedElementImpl.forceExpand: expected element 
name " +
+                    getLocalName() + ", found " + reader.getLocalName());
+                throw new RuntimeException("Element name from data source is " 
+
+                    reader.getLocalName() + ", not the expected " + 
getLocalName());
+            }
+            if (!reader.getNamespaceURI().equals(getNamespace().getName())) {
+                String uri = getNamespace().getName();
+                log.error("OMSourcedElementImpl.forceExpand: expected element 
namespace " +
+                    getLocalName() + ", found " + uri);
+                throw new RuntimeException("Element namespace from data source 
is " +
+                    reader.getNamespaceURI() + ", not the expected " + uri);
+            }
+            
+            // set the builder for this element
+            isParserSet = true;
+            super.setBuilder(new StAXOMBuilder(getOMFactory(), reader, this));
+            setComplete(false);
+        }
+    }
+    
+    /**
+     * Check if element has been expanded into tree.
+     * 
+     * @return <code>true</code> if expanded, <code>false</code> if not
+     */
+    public boolean isExpanded() {
+        return isParserSet;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getChildElements()
+     */
+    public Iterator getChildElements() {
+        forceExpand();
+        return super.getChildElements();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#declareNamespace(java.lang.String, 
java.lang.String)
+     */
+    public OMNamespace declareNamespace(String uri, String prefix) {
+        forceExpand();
+        return super.declareNamespace(uri, prefix);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#declareDefaultNamespace(java.lang.String)
+     */
+    public OMNamespace declareDefaultNamespace(String uri) {
+        forceExpand();
+        return super.declareDefaultNamespace(uri);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getDefaultNamespace()
+     */
+    public OMNamespace getDefaultNamespace() {
+        forceExpand();
+        return super.getDefaultNamespace();
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#declareNamespace(org.apache.axiom.om.OMNamespace)
+     */
+    public OMNamespace declareNamespace(OMNamespace namespace) {
+        forceExpand();
+        return super.declareNamespace(namespace);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#findNamespace(java.lang.String, 
java.lang.String)
+     */
+    public OMNamespace findNamespace(String uri, String prefix) {
+        forceExpand();
+        return super.findNamespace(uri, prefix);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#findNamespaceURI(java.lang.String)
+     */
+    public OMNamespace findNamespaceURI(String prefix) {
+        forceExpand();
+        return super.findNamespaceURI(prefix);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getAllDeclaredNamespaces()
+     */
+    public Iterator getAllDeclaredNamespaces() throws OMException {
+        forceExpand();
+        return super.getAllDeclaredNamespaces();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getAllAttributes()
+     */
+    public Iterator getAllAttributes() {
+        forceExpand();
+        return super.getAllAttributes();
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#getAttribute(javax.xml.namespace.QName)
+     */
+    public OMAttribute getAttribute(QName qname) {
+        forceExpand();
+        return super.getAttribute(qname);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#getAttributeValue(javax.xml.namespace.QName)
+     */
+    public String getAttributeValue(QName qname) {
+        forceExpand();
+        return super.getAttributeValue(qname);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#addAttribute(org.apache.axiom.om.OMAttribute)
+     */
+    public OMAttribute addAttribute(OMAttribute attr) {
+        forceExpand();
+        return super.addAttribute(attr);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#addAttribute(java.lang.String, 
java.lang.String, org.apache.axiom.om.OMNamespace)
+     */
+    public OMAttribute addAttribute(String attributeName, String value, 
OMNamespace namespace) {
+        forceExpand();
+        return super.addAttribute(attributeName, value, namespace);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#removeAttribute(org.apache.axiom.om.OMAttribute)
+     */
+    public void removeAttribute(OMAttribute attr) {
+        forceExpand();
+        super.removeAttribute(attr);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#setBuilder(org.apache.axiom.om.OMXMLParserWrapper)
+     */
+    public void setBuilder(OMXMLParserWrapper wrapper) {
+        throw new UnsupportedOperationException("Builder cannot be set for 
element backed by data source");
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getBuilder()
+     */
+    public OMXMLParserWrapper getBuilder() {
+        forceExpand();
+        return super.getBuilder();
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#setFirstChild(org.apache.axiom.om.OMNode)
+     */
+    public void setFirstChild(OMNode node) {
+        forceExpand();
+        super.setFirstChild(node);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getFirstElement()
+     */
+    public OMElement getFirstElement() {
+        forceExpand();
+        return super.getFirstElement();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getXMLStreamReader()
+     */
+    public XMLStreamReader getXMLStreamReader() {
+        if (isParserSet) {
+            return super.getXMLStreamReader();
+        } else {
+            return getDirectReader();
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getXMLStreamReaderWithoutCaching()
+     */
+    public XMLStreamReader getXMLStreamReaderWithoutCaching() {
+        return getXMLStreamReader();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#setText(java.lang.String)
+     */
+    public void setText(String text) {
+        forceExpand();
+        super.setText(text);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#setText(javax.xml.namespace.QName)
+     */
+    public void setText(QName text) {
+        forceExpand();
+        super.setText(text);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getText()
+     */
+    public String getText() {
+        forceExpand();
+        return super.getText();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getTextAsQName()
+     */
+    public QName getTextAsQName() {
+        forceExpand();
+        return super.getTextAsQName();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getLocalName()
+     */
+    public String getLocalName() {
+        // no need to set the parser, just call base method directly
+        return super.getLocalName();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#setLocalName(java.lang.String)
+     */
+    public void setLocalName(String localName) {
+        // no need to expand the tree, just call base method directly
+        super.setLocalName(localName);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getNamespace()
+     */
+    public OMNamespace getNamespace() throws OMException {
+        return definedNamespace;
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#setNamespace(org.apache.axiom.om.OMNamespace)
+     */
+    public void setNamespace(OMNamespace namespace) {
+        forceExpand();
+        super.setNamespace(namespace);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMElement#setNamespaceWithNoFindInCurrentScope(org.apache.axiom.om.OMNamespace)
+     */
+    public void setNamespaceWithNoFindInCurrentScope(OMNamespace namespace) {
+        forceExpand();
+        super.setNamespaceWithNoFindInCurrentScope(namespace);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getQName()
+     */
+    public QName getQName() {
+        if (isParserSet) {
+            return super.getQName();
+        } else if (definedNamespace != null) {
+            // always ignore prefix on name from sourced element
+            return new QName(definedNamespace.getName(), getLocalName());
+            
+        } else {
+            return new QName(getLocalName());
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#toStringWithConsume()
+     */
+    public String toStringWithConsume() throws XMLStreamException {
+        StringWriter writer = new StringWriter();
+        dataSource.serialize(writer, null);
+        return writer.toString();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#resolveQName(java.lang.String)
+     */
+    public QName resolveQName(String qname) {
+        forceExpand();
+        return super.resolveQName(qname);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#cloneOMElement()
+     */
+    public OMElement cloneOMElement() {
+        forceExpand();
+        return super.cloneOMElement();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#setLineNumber(int)
+     */
+    public void setLineNumber(int lineNumber) {
+        // no need to expand the tree, just call base method directly
+        super.setLineNumber(lineNumber);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMElement#getLineNumber()
+     */
+    public int getLineNumber() {
+        // no need to expand the tree, just call base method directly
+        return super.getLineNumber();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMNode#discard()
+     */
+    public void discard() throws OMException {
+        // discard without expanding the tree
+        setComplete(true);
+        super.detach();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMNode#getType()
+     */
+    public int getType() {
+        // no need to expand the tree, just call base method directly
+        return super.getType();
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMNode#internalSerialize(javax.xml.stream.XMLStreamWriter)
+     */
+    public void internalSerialize(javax.xml.stream.XMLStreamWriter writer) 
throws XMLStreamException {
+        dataSource.serialize(writer);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.impl.llom.OMElementImpl#internalSerialize(javax.xml.stream.XMLStreamWriter,
 boolean)
+     */
+    protected void internalSerialize(XMLStreamWriter writer, boolean cache) 
throws XMLStreamException {
+        dataSource.serialize(writer);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMNode#internalSerializeAndConsume(javax.xml.stream.XMLStreamWriter)
+     */
+    public void internalSerializeAndConsume(XMLStreamWriter writer) throws 
XMLStreamException {
+        dataSource.serialize(writer);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMNode#serialize(javax.xml.stream.XMLStreamWriter)
+     */
+    public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException 
{
+        dataSource.serialize(xmlWriter);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMNode#serialize(java.io.OutputStream)
+     */
+    public void serialize(OutputStream output) throws XMLStreamException {
+        dataSource.serialize(output, null);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMNode#serialize(java.io.Writer)
+     */
+    public void serialize(Writer writer) throws XMLStreamException {
+        dataSource.serialize(writer, null);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMNode#serialize(java.io.OutputStream, 
org.apache.axiom.om.OMOutputFormat)
+     */
+    public void serialize(OutputStream output, OMOutputFormat format) throws 
XMLStreamException {
+        dataSource.serialize(output, format);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMNode#serialize(java.io.Writer, 
org.apache.axiom.om.OMOutputFormat)
+     */
+    public void serialize(Writer writer, OMOutputFormat format) throws 
XMLStreamException {
+        dataSource.serialize(writer, format);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMNode#serializeAndConsume(javax.xml.stream.XMLStreamWriter)
+     */
+    public void serializeAndConsume(javax.xml.stream.XMLStreamWriter 
xmlWriter) throws XMLStreamException {
+        dataSource.serialize(xmlWriter);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMNode#serializeAndConsume(java.io.OutputStream)
+     */
+    public void serializeAndConsume(OutputStream output) throws 
XMLStreamException {
+        dataSource.serialize(output, null);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMNode#serializeAndConsume(java.io.Writer)
+     */
+    public void serializeAndConsume(Writer writer) throws XMLStreamException {
+        dataSource.serialize(writer, null);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMNode#serializeAndConsume(java.io.OutputStream, 
org.apache.axiom.om.OMOutputFormat)
+     */
+    public void serializeAndConsume(OutputStream output, OMOutputFormat 
format) throws XMLStreamException {
+        dataSource.serialize(output, format);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMNode#serializeAndConsume(java.io.Writer, 
org.apache.axiom.om.OMOutputFormat)
+     */
+    public void serializeAndConsume(Writer writer, OMOutputFormat format) 
throws XMLStreamException {
+        dataSource.serialize(writer, format);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMContainer#addChild(org.apache.axiom.om.OMNode)
+     */
+    public void addChild(OMNode omNode) {
+        forceExpand();
+        super.addChild(omNode);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMContainer#getChildrenWithName(javax.xml.namespace.QName)
+     */
+    public Iterator getChildrenWithName(QName elementQName) {
+        forceExpand();
+        return super.getChildrenWithName(elementQName);
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.OMContainer#getFirstChildWithName(javax.xml.namespace.QName)
+     */
+    public OMElement getFirstChildWithName(QName elementQName) throws 
OMException {
+        forceExpand();
+        return super.getFirstChildWithName(elementQName);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMContainer#getChildren()
+     */
+    public Iterator getChildren() {
+        forceExpand();
+        return super.getChildren();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMContainer#getFirstOMChild()
+     */
+    public OMNode getFirstOMChild() {
+        forceExpand();
+        return super.getFirstOMChild();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.OMContainer#buildNext()
+     */
+    public void buildNext() {
+        forceExpand();
+        super.buildNext();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.impl.llom.OMElementImpl#detach()
+     */
+    public OMNode detach() throws OMException {
+        // detach without expanding the tree
+        boolean complete = isComplete();
+        setComplete(true);
+        OMNode result = super.detach();
+        setComplete(complete);
+        return result;
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.impl.llom.OMElementImpl#getNextOMSibling()
+     */
+    public OMNode getNextOMSibling() throws OMException {
+        // no need to expand the tree, just call base method directly
+        return super.getNextOMSibling();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.impl.llom.OMElementImpl#getTrimmedText()
+     */
+    public String getTrimmedText() {
+        forceExpand();
+        return super.getTrimmedText();
+    }
+
+    /* (non-Javadoc)
+     * @see 
org.apache.axiom.om.impl.llom.OMElementImpl#handleNamespace(javax.xml.namespace.QName)
+     */
+    OMNamespace handleNamespace(QName qname) {
+        forceExpand();
+        return super.handleNamespace(qname);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.impl.llom.OMElementImpl#isComplete()
+     */
+    public boolean isComplete() {
+        if (isParserSet) {
+            return super.isComplete();
+        } else {
+            return false;
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.axiom.om.impl.llom.OMElementImpl#toString()
+     */
+    public String toString() {
+        forceExpand();
+        return super.toString();
+    }
+}
\ No newline at end of file

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/factory/OMLinkedListImplFactory.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/factory/OMLinkedListImplFactory.java?rev=395441&r1=395440&r2=395441&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/factory/OMLinkedListImplFactory.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/factory/OMLinkedListImplFactory.java
 Wed Apr 19 16:39:40 2006
@@ -19,6 +19,7 @@
 import org.apache.axiom.om.OMAttribute;
 import org.apache.axiom.om.OMComment;
 import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMDataSource;
 import org.apache.axiom.om.OMDocType;
 import org.apache.axiom.om.OMDocument;
 import org.apache.axiom.om.OMElement;
@@ -35,6 +36,7 @@
 import org.apache.axiom.om.impl.llom.OMElementImpl;
 import org.apache.axiom.om.impl.llom.OMNamespaceImpl;
 import org.apache.axiom.om.impl.llom.OMProcessingInstructionImpl;
+import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
 import org.apache.axiom.om.impl.llom.OMTextImpl;
 
 import javax.xml.namespace.QName;
@@ -110,6 +112,17 @@
     public OMElement createOMElement(QName qname, OMContainer parent)
             throws OMException {
         return new OMElementImpl(qname, parent, this);
+    }
+
+    /**
+     * Construct element with arbitrary data source.
+     * 
+     * @param source
+     * @param localName
+     * @param ns
+     */
+    public OMElement createOMElement(OMDataSource source, String localName, 
OMNamespace ns) {
+        return new OMSourcedElementImpl(localName, ns, this, source);
     }
 
     /**

Added: 
webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java?rev=395441&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java
 Wed Apr 19 16:39:40 2006
@@ -0,0 +1,193 @@
+/*
+ * 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.axiom.om.impl.llom;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.om.AbstractTestCase;
+import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory;
+import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
+
+public class OMSourcedElementTest extends AbstractTestCase {
+    private static String testDocument =
+        "<library xmlns='http://www.sosnoski.com/uwjws/library' books='1'>" +
+        "<type id='java' category='professional' deductable='true'>"+
+        "<name>Java Reference</name></type><type id='xml' "+
+        "category='professional' deductable='true'><name>XML Reference</name>" 
+
+        "</type><book isbn='1930110111' type='xml'><title>XSLT 
Quickly</title>" +
+        "<author>DuCharme, Bob</author><publisher>Manning</publisher>" +
+        "<price>29.95</price></book></library>";
+    
+    private OMSourcedElementImpl element;
+
+    /**
+     * @param testName
+     */
+    public OMSourcedElementTest(String testName) {
+        super(testName);
+    }
+
+    protected void setUp() throws Exception {
+        element = new OMSourcedElementImpl("library",
+            new OMNamespaceImpl("http://www.sosnoski.com/uwjws/library";, ""),
+                new OMLinkedListImplFactory(), new 
TestDataSource(testDocument));
+    }
+    
+    public void testMethodOverrides() {
+        Method[] submeths = OMSourcedElementImpl.class.getDeclaredMethods();
+        Method[] supmeths = OMElementImpl.class.getDeclaredMethods();
+        outer: for (int i = 0; i < supmeths.length; i++) {
+            Method supmeth = supmeths[i];
+            Class[] params = supmeth.getParameterTypes();
+            if (!Modifier.isPrivate(supmeth.getModifiers())) {
+                for (int j = 0; j < submeths.length; j++) {
+                    Method submeth = submeths[j];
+                    if (supmeth.getName().equals(submeth.getName())) {
+                        if (Arrays.equals(params, 
submeth.getParameterTypes())) {
+                            continue outer;
+                        }
+                    }
+                }
+                fail("OMSourcedElementImpl must override method " + supmeth +
+                    "\nSee class JavaDocs for details");
+            }
+        }
+    }
+    
+    private int countItems(Iterator iter) {
+        int count = 0;
+        while (iter.hasNext()) {
+            count++;
+            iter.next();
+        }
+        return count;
+    }
+    
+    public void testSerializeToStream() throws Exception {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        element.serialize(bos);
+        assertEquals("Serialized text error", testDocument,
+            new String(bos.toByteArray()));
+        assertFalse("Element expansion when serializing", 
element.isExpanded());
+    }
+    
+    public void testSerializeToWriter() throws Exception {
+        StringWriter writer = new StringWriter();
+        element.serialize(writer);
+        assertEquals("Serialized text error", testDocument, writer.toString());
+        assertFalse("Element expansion when serializing", 
element.isExpanded());
+    }
+    
+    public void testSerializeToXMLWriter() throws Exception {
+        StringWriter writer = new StringWriter();
+        XMLStreamWriter xmlwriter = 
XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
+        element.serialize(writer);
+        xmlwriter.flush();
+        assertEquals("Serialized text error", testDocument, writer.toString());
+        assertFalse("Element expansion when serializing", 
element.isExpanded());
+    }
+
+    public void testExpand() throws Exception {
+        element.getAllDeclaredNamespaces();
+        assertEquals("Expanded namespace count error", 1,
+            countItems(element.getAllDeclaredNamespaces()));
+        assertEquals("Expanded attribute count error", 1,
+            countItems(element.getAllAttributes()));
+        assertEquals("Expanded attribute value error", "1",
+            element.getAttributeValue(new QName("books")));
+        OMElement child = element.getFirstElement();
+        assertEquals("Child element name", "type", child.getLocalName());
+        assertEquals("Child element namespace",
+            "http://www.sosnoski.com/uwjws/library";, 
child.getNamespace().getName());
+        OMNode next = child.getNextOMSibling();
+        assertTrue("Expected child element", next instanceof OMElement);
+        next = next.getNextOMSibling();
+        assertTrue("Expected child element", next instanceof OMElement);
+        child = (OMElement)next;
+        assertEquals("Child element name", "book", child.getLocalName());
+        assertEquals("Child element namespace",
+            "http://www.sosnoski.com/uwjws/library";, 
child.getNamespace().getName());
+        assertEquals("Attribute value error", "xml",
+            child.getAttributeValue(new QName("type")));
+    }
+    
+    private static class TestDataSource implements OMDataSource {
+        private final String data;
+        
+        private TestDataSource(String data) {
+            this.data = data;
+        }
+
+        /* (non-Javadoc)
+         * @see 
org.apache.axiom.om.OMDataSource#serialize(java.io.OutputStream, 
org.apache.axiom.om.OMOutputFormat)
+         */
+        public void serialize(OutputStream output, OMOutputFormat format) 
throws XMLStreamException {
+            try {
+                output.write(data.getBytes());
+            } catch (IOException e) {
+                throw new XMLStreamException(e);
+            }
+        }
+
+        /* (non-Javadoc)
+         * @see org.apache.axiom.om.OMDataSource#serialize(java.io.Writer, 
org.apache.axiom.om.OMOutputFormat)
+         */
+        public void serialize(Writer writer, OMOutputFormat format) throws 
XMLStreamException {
+            try {
+                writer.write(data);
+            } catch (IOException e) {
+                throw new XMLStreamException(e);
+            }
+        }
+
+        /* (non-Javadoc)
+         * @see 
org.apache.axiom.om.OMDataSource#serialize(javax.xml.stream.XMLStreamWriter)
+         */
+        public void serialize(XMLStreamWriter xmlWriter) throws 
XMLStreamException {
+            StreamingOMSerializer serializer = new StreamingOMSerializer();
+            serializer.serialize(getReader(), xmlWriter);
+        }
+
+        /* (non-Javadoc)
+         * @see org.apache.axiom.om.OMDataSource#getReader()
+         */
+        public XMLStreamReader getReader() throws XMLStreamException {
+            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+            return inputFactory.createXMLStreamReader(new StringReader(data));
+        }
+    }
+}
\ No newline at end of file


Reply via email to