ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1332573 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/ axiom-api/src/main/java/org/apache/axiom/om/ds/ axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/ axiom-impl/src/main/java/or...
Date Tue, 01 May 2012 08:45:27 GMT
Author: veithen
Date: Tue May  1 08:45:27 2012
New Revision: 1332573

URL: http://svn.apache.org/viewvc?rev=1332573&view=rev
Log:
Added a method to OMSourcedElement that enables safe access to the Java object backing the
OMDataSource.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetObject.java
  (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMSourcedElement.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromDataSource.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromReader.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPHeaderBlockImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java?rev=1332573&r1=1332572&r2=1332573&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMDataSourceExt.java
Tue May  1 08:45:27 2012
@@ -96,8 +96,15 @@ public interface OMDataSourceExt extends
     public XMLStreamReader getReader() throws XMLStreamException;
     
     /**
-     * Returns the backing Object.
-     * @return Object
+     * Get the object that backs this data source. Application code should in general not
call this
+     * method directly, but use {@link OMSourcedElement#getObject(Class)} instead.
+     * <p>
+     * Data sources that support non destructive read/write should return the object from
which the
+     * XML is produced. Data sources with destructive read/write should return a non null
value only
+     * if the backing object has not been consumed yet (even partially).
+     * 
+     * @return the backing object, or <code>null</code> if the data source has
no backing object or
+     *         if the backing object can't be accessed in a safe way
      */
     public Object getObject();
     

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMSourcedElement.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMSourcedElement.java?rev=1332573&r1=1332572&r2=1332573&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMSourcedElement.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMSourcedElement.java
Tue May  1 08:45:27 2012
@@ -58,4 +58,28 @@ public interface OMSourcedElement extend
      */
     OMDataSource setDataSource(OMDataSource dataSource);
     
+    /**
+     * Get the object that backs the data source set on this element. This method provides
a safe
+     * way to access that object. It will return a non null value if all of the following
conditions
+     * are satisfied:
+     * <ol>
+     * <li>The element is configured with an {@link OMDataSource} of the type specified
by the
+     * <code>dataSourceClass</code> parameter (in the sense of {@link Class#isInstance(Object)}).
+     * <li>The {@link OMDataSourceExt#getObject()} method returns a non null value
on the configured
+     * data source.
+     * <li>The instance can ensure that the content of the element has not been modified.
This is
+     * always the case if the element has not been expanded (i.e. if {@link #isExpanded()}
returns
+     * <code>false</code>), but the implementation may use additional mechanisms
to detect changes
+     * after expansion of the element.
+     * </ol>
+     * These are exactly the conditions that must be satisfied if the application code wants
to
+     * implement optimized processing of the sourced element by accessing the backing object.
+     * 
+     * @param dataSourceClass
+     *            the expected data source class; must be assignment compatible with
+     *            {@link OMDataSourceExt}
+     * @return the backing Java object or <code>null</code> if the conditions
specified above are
+     *         not satisfied
+     */
+    Object getObject(Class dataSourceClass);
 } 

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromDataSource.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromDataSource.java?rev=1332573&r1=1332572&r2=1332573&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromDataSource.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromDataSource.java
Tue May  1 08:45:27 2012
@@ -33,6 +33,7 @@ import org.apache.axiom.util.stax.Wrappe
 
 /**
  * {@link WrappedTextNodeOMDataSource} that pulls the text data from a {@link DataSource}
object.
+ * The {@link #getObject()} method returns the {@link DataSource} instance.
  */
 public class WrappedTextNodeOMDataSourceFromDataSource extends WrappedTextNodeOMDataSource
{
     private final DataSource binaryData;

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromReader.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromReader.java?rev=1332573&r1=1332572&r2=1332573&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromReader.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/WrappedTextNodeOMDataSourceFromReader.java
Tue May  1 08:45:27 2012
@@ -29,10 +29,12 @@ import org.apache.axiom.util.stax.Wrappe
 
 /**
  * {@link WrappedTextNodeOMDataSource} that pulls text data from a {@link Reader} object.
Since the
- * stream can only be read once, this data source is destructive.
+ * stream can only be read once, this data source is destructive. The {@link #getObject()}
method
+ * returns the {@link Reader} object if it has not been accessed yet.
  */
 public class WrappedTextNodeOMDataSourceFromReader extends WrappedTextNodeOMDataSource {
     private final Reader reader;
+    private boolean isAccessed;
     
     public WrappedTextNodeOMDataSourceFromReader(QName wrapperElementName, Reader reader)
{
         super(wrapperElementName);
@@ -40,9 +42,14 @@ public class WrappedTextNodeOMDataSource
     }
 
     public XMLStreamReader getReader() throws XMLStreamException {
+        isAccessed = true;
         return new WrappedTextNodeStreamReader(wrapperElementName, reader);
     }
 
+    public Object getObject() {
+        return isAccessed ? null : reader;
+    }
+
     public boolean isDestructiveRead() {
         return true;
     }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPHeaderBlockImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPHeaderBlockImpl.java?rev=1332573&r1=1332572&r2=1332573&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPHeaderBlockImpl.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/soap/impl/dom/SOAPHeaderBlockImpl.java
Tue May  1 08:45:27 2012
@@ -128,4 +128,8 @@ public abstract class SOAPHeaderBlockImp
     public OMDataSource setDataSource(OMDataSource dataSource) {
         throw new UnsupportedOperationException();
     }
+
+    public Object getObject(Class dataSourceClass) {
+        throw new UnsupportedOperationException();
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=1332573&r1=1332572&r2=1332573&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
Tue May  1 08:45:27 2012
@@ -1036,4 +1036,12 @@ public class OMSourcedElementImpl extend
         }
         
     }
+
+    public Object getObject(Class dataSourceClass) {
+        if (dataSource == null || isExpanded || !dataSourceClass.isInstance(dataSource))
{
+            return null;
+        } else {
+            return ((OMDataSourceExt)dataSource).getObject();
+        }
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1332573&r1=1332572&r2=1332573&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
(original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
Tue May  1 08:45:27 2012
@@ -346,6 +346,7 @@ public class OMTestSuiteBuilder extends 
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestExpand(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceNormalized(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetNamespaceNormalized2(metaFactory));
+            addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetObject(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestGetTextAsStreamWithNonDestructiveOMDataSource(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestInputStreamDS(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestName1DefaultPrefix(metaFactory));

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetObject.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetObject.java?rev=1332573&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetObject.java
(added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetObject.java
Tue May  1 08:45:27 2012
@@ -0,0 +1,53 @@
+/*
+ * 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.ts.om.sourcedelement;
+
+import java.nio.charset.Charset;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMSourcedElement;
+import org.apache.axiom.om.ds.CharArrayDataSource;
+import org.apache.axiom.om.ds.WrappedTextNodeOMDataSource;
+import org.apache.axiom.om.ds.WrappedTextNodeOMDataSourceFromDataSource;
+import org.apache.axiom.ts.AxiomTestCase;
+
+public class TestGetObject extends AxiomTestCase {
+    public TestGetObject(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        DataSource ds = new DataHandler("test", "text/plain; charset=utf-8").getDataSource();
+        OMSourcedElement element = factory.createOMElement(new WrappedTextNodeOMDataSourceFromDataSource(new
QName("wrapper"), ds, Charset.forName("utf-8")));
+        // getObject returns null if the data source is not of the expected type
+        assertNull(element.getObject(CharArrayDataSource.class));
+        // Test with the right data source type
+        assertSame(ds, element.getObject(WrappedTextNodeOMDataSourceFromDataSource.class));
+        assertSame(ds, element.getObject(WrappedTextNodeOMDataSource.class));
+        // Now modify the content of the element
+        factory.createOMComment(element, "comment");
+        assertNull(element.getObject(WrappedTextNodeOMDataSourceFromDataSource.class));
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestGetObject.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message