You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2011/11/21 12:21:40 UTC

svn commit: r1204446 - in /webservices/commons/trunk/modules/axiom: modules/axiom-samples/src/test/java/org/apache/axiom/samples/ src/site/ src/site/apt/

Author: veithen
Date: Mon Nov 21 11:21:39 2011
New Revision: 1204446

URL: http://svn.apache.org/viewvc?rev=1204446&view=rev
Log:
More quick start samples.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ParseSample.java   (with props)
    webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt.vm
      - copied, changed from r1204268, webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt
Removed:
    webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ValidateSample.java
    webservices/commons/trunk/modules/axiom/src/site/site.xml

Added: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ParseSample.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ParseSample.java?rev=1204446&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ParseSample.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ParseSample.java Mon Nov 21 11:21:39 2011
@@ -0,0 +1,60 @@
+/*
+ * 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.samples;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMException;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+
+public class ParseSample extends TestCase {
+    // START SNIPPET: main
+    public void processFile(File file) throws IOException, OMException {
+        // Create a builder for the file and get the root element
+        InputStream in = new FileInputStream(file);
+        OMElement root = OMXMLBuilderFactory.createOMBuilder(in).getDocumentElement();
+        
+        // Process the content of the file
+        OMElement urlElement = root.getFirstChildWithName(
+                new QName("http://maven.apache.org/POM/4.0.0", "url"));
+        if (urlElement == null) {
+            System.out.println("No <url> element found");
+        } else {
+            System.out.println("url = " + urlElement.getText());
+        }
+        
+        // Because Axiom uses deferred parsing, the stream must be closed AFTER
+        // processing the document (unless OMElement#build() is called)
+        in.close();
+    }
+    // END SNIPPET: main
+    
+    public void test() throws Exception {
+        String basedir = System.getProperty("basedir");
+        processFile(new File(basedir == null ? "." : basedir, "pom.xml"));
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ParseSample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ValidateSample.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ValidateSample.java?rev=1204446&r1=1204445&r2=1204446&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ValidateSample.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-samples/src/test/java/org/apache/axiom/samples/ValidateSample.java Mon Nov 21 11:21:39 2011
@@ -22,19 +22,23 @@ import java.io.InputStream;
 import java.net.URL;
 
 import javax.xml.XMLConstants;
+import javax.xml.transform.dom.DOMSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
 
 import junit.framework.TestCase;
 
+import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMMetaFactory;
 import org.apache.axiom.om.OMXMLBuilderFactory;
 import org.apache.axiom.soap.SOAPEnvelope;
 import org.apache.axiom.soap.SOAPModelBuilder;
+import org.w3c.dom.Element;
 
 public class ValidateSample extends TestCase {
-    // START SNIPPET: main
+    // START SNIPPET: sax
     public void validate(InputStream in, URL schemaUrl) throws Exception {
         SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(in, "UTF-8");
         SOAPEnvelope envelope = builder.getSOAPEnvelope();
@@ -44,9 +48,26 @@ public class ValidateSample extends Test
         Validator validator = schema.newValidator();
         validator.validate(bodyContent.getSAXSource(true));
     }
-    // END SNIPPET: main
+    // END SNIPPET: sax
     
-    public void test() throws Exception {
+    public void testSAX() throws Exception {
+        validate(getClass().getResourceAsStream("soap-request.xml"), getClass().getResource("schema.xsd"));
+    }
+
+    // START SNIPPET: dom
+    public void validateUsingDOM(InputStream in, URL schemaUrl) throws Exception {
+        OMMetaFactory mf = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM);
+        SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(mf, in, "UTF-8");
+        SOAPEnvelope envelope = builder.getSOAPEnvelope();
+        OMElement bodyContent = envelope.getBody().getFirstElement();
+        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        Schema schema = schemaFactory.newSchema(schemaUrl);
+        Validator validator = schema.newValidator();
+        validator.validate(new DOMSource((Element)bodyContent));
+    }
+    // END SNIPPET: dom
+    
+    public void testDOM() throws Exception {
         validate(getClass().getResourceAsStream("soap-request.xml"), getClass().getResource("schema.xsd"));
     }
 }

Copied: webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt.vm (from r1204268, webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt)
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt.vm?p2=webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt.vm&p1=webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt&r1=1204268&r2=1204446&rev=1204446&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt (original)
+++ webservices/commons/trunk/modules/axiom/src/site/apt/quickstart-samples.apt.vm Mon Nov 21 11:21:39 2011
@@ -19,16 +19,67 @@
       Quick Start Samples
      ---------------------
 
+  Content:
+
+%{toc}
+
+Adding Axiom as a Maven dependency
+
+  To use Axiom in a project built using Maven, add the following dependencies:
+
++------------------------------------------------+
+<dependency>
+    <groupId>org.apache.ws.commons.axiom</groupId>
+    <artifactId>axiom-api</artifactId>
+    <version>${axiom_version}</version>
+</dependency>
+<dependency>
+    <groupId>org.apache.ws.commons.axiom</groupId>
+    <artifactId>axiom-impl</artifactId>
+    <version>${axiom_version}</version>
+    <scope>runtime</scope>
+</dependency>
++------------------------------------------------+
+
+  Note that the <<<axiom-impl>>> dependency is added in scope <<<runtime>>>
+  because application code should not refer to implementation classes directly.
+  All Axiom features are accessible through the public API which is provided
+  by <<<axiom-api>>>.
+  
+  If the application code requires a DOM compliant Axiom implementation,
+  then the following dependency needs to be added too:
+
++------------------------------------------------+
+<dependency>
+    <groupId>org.apache.ws.commons.axiom</groupId>
+    <artifactId>axiom-dom</artifactId>
+    <version>${axiom_version}</version>
+    <scope>runtime</scope>
+</dependency>
++------------------------------------------------+
+
+Parsing and processing an XML document
+
+  The following sample shows how to parse and process an XML document using Axiom.
+  It is pretty much self-explaining:
+
+%{snippet|id=main|file=modules/axiom-samples/src/test/java/org/apache/axiom/samples/ParseSample.java}
+
 Schema validation using javax.xml.validation
 
   This sample demonstrates how to validate a part of an Axiom tree (actually the body of a SOAP message)
   using the <<<javax.xml.validation>>> API:
 
-%{snippet|id=main|file=modules/axiom-samples/src/test/java/org/apache/axiom/samples/ValidateSample.java}
+%{snippet|id=sax|file=modules/axiom-samples/src/test/java/org/apache/axiom/samples/ValidateSample.java}
 
   It leverages the fact that Axiom is capable of constructing a <<<SAXSource>>> from an <<<OMDocument>>>
   or <<<OMElement>>>.
 
+  Alternatively, one can use a DOM compliant Axiom implementation and use a
+  <<<DOMSource>>> to pass the XML fragment to the validator:
+
+%{snippet|id=dom|file=modules/axiom-samples/src/test/java/org/apache/axiom/samples/ValidateSample.java}
+
 Loading local chunks from a large XML document
 
   Here the goal is to process a large XML document "by chunks", i.e.

Modified: webservices/commons/trunk/modules/axiom/src/site/site.xml
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/src/site/site.xml?rev=1204446&r1=1204445&r2=1204446&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/src/site/site.xml (original)
+++ webservices/commons/trunk/modules/axiom/src/site/site.xml Mon Nov 21 11:21:39 2011
@@ -41,6 +41,7 @@
                 <item name="Source Code" href="source-repository.html"/>
             </item>
             <item name="Documentation" href="documentation.html">
+                <item name="Quick start samples" href="quickstart-samples.html"/>
                 <item name="User guide" href="userguide/userguide.html"/>
                 <item name="Developer guide" href="devguide/devguide.html"/>
                 <item name="Articles" href="articles.html"/>