You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ke...@apache.org on 2007/06/21 16:33:18 UTC

svn commit: r549495 - in /incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo: ExecuteSamples2.java specCodeSnippets/AccessDataObjectUsingValidXPath.java

Author: kelvingoodson
Date: Thu Jun 21 07:33:17 2007
New Revision: 549495

URL: http://svn.apache.org/viewvc?view=rev&rev=549495
Log:
Fixed up the XPath sample

Modified:
    incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java
    incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java

Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java?view=diff&rev=549495&r1=549494&r2=549495
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java Thu Jun 21 07:33:17 2007
@@ -28,6 +28,7 @@
 import org.apache.tuscany.samples.sdo.otherSources.CreatePurchaseOrder;
 import org.apache.tuscany.samples.sdo.otherSources.ReadPurchaseOrder;
 import org.apache.tuscany.samples.sdo.specCodeSnippets.AccessDataObjectPropertiesByName;
+import org.apache.tuscany.samples.sdo.specCodeSnippets.AccessDataObjectUsingValidXPath;
 import org.apache.tuscany.samples.sdo.specCodeSnippets.DynamicCustomerTypeSample;
 import org.apache.tuscany.samples.sdo.specCodeSnippets.ObtainingDataGraphFromXml;
 
@@ -38,8 +39,9 @@
     Class[] sampleClasses = {
       org.apache.tuscany.samples.sdo.otherSources.CreateCompany.class,
       ReadPurchaseOrder.class,
-      AccessDataObjectPropertiesByName.class,
       CreatePurchaseOrder.class,
+      AccessDataObjectPropertiesByName.class,
+      AccessDataObjectUsingValidXPath.class,
       DynamicCustomerTypeSample.class,
       ObtainingDataGraphFromXml.class,
       org.apache.tuscany.samples.sdo.tuscanyapi.CreateCompany.class 

Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java?view=diff&rev=549495&r1=549494&r2=549495
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java Thu Jun 21 07:33:17 2007
@@ -20,7 +20,10 @@
 
 package org.apache.tuscany.samples.sdo.specCodeSnippets;
 
+import java.util.List;
+
 import org.apache.tuscany.samples.sdo.SampleBase;
+import org.apache.tuscany.samples.sdo.SampleInfrastructure;
 import org.apache.tuscany.samples.sdo.SdoSampleConstants;
 
 import commonj.sdo.DataObject;
@@ -72,32 +75,11 @@
     HelperContext scope;
   
     public AccessDataObjectUsingValidXPath(Integer userLevel) {
-      super(userLevel);
+      super(userLevel, SampleInfrastructure.SAMPLE_LEVEL_NOVICE);
     }
 
 
     /**
-     * Prints a subset of item properties to System.out where the individual item was
-     * accessed using an xPath expression
-     * 
-     * @param purchaseOrder.
-     *            DataObject defined by Types in
-     *            {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE}
-     */
-    public static void accessDataObjectUsingXPath(DataObject purchaseOrder) {
-
-        System.out.println("Accessing individual item from list using xpath");
-        // TODO: use variety of xpath expressions such as items/item[1]
-        // TODO: add to junit test cases for test cases above
-        DataObject item = purchaseOrder.getDataObject("items/item[1]");
-        System.out.println("Item toString : " + item.toString());
-
-        System.out.println("Item name:" + item.get("productName"));
-        System.out.println("Part num: " + item.get("partNum"));
-
-    }
-
-    /**
      * Accesses and modifies properties of a purchase order DataObject using xPath(
      * properties are defined in the xsd
      * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} and
@@ -108,50 +90,68 @@
      *            No parameters required.
      */
     public static void main(String[] args) {
-      // TODO make the default level COMMENTARY_FOR_NOVICE, once the rest of the sample has been
-      // converted to using commentary()
-      AccessDataObjectPropertiesByName sample = new AccessDataObjectPropertiesByName(COMMENTARY_FOR_INTERMEDIATE);
-
-      try {
-        sample.run();
-      }
-      catch (Exception e) {
-        sample.somethingUnexpectedHasHappened(e);
-      }
+      AccessDataObjectUsingValidXPath sample = new AccessDataObjectUsingValidXPath(COMMENTARY_FOR_NOVICE);
+      sample.run();
+
     }
 
     public void runSample () throws Exception {
 
-        // information
-        System.out.println("***************************************");
-        System.out.println("SDO Sample AccessDataObjectUsingValidXPath");
-        System.out.println("***************************************");
-        System.out.println("Demonstrats accessing a created DataObject's properties using xPath.");
-        System.out.println("***************************************");
-
-        // create a DataObejct
-        DataObject purchaseOrder = null;
-        scope = createScopeForTypes();
-        try {
-            scope.getXSDHelper().define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
-            purchaseOrder = scope.getXMLHelper().load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE)).getRootObject();
-            System.out.println("DataObject created");
-        } catch (Exception e) {
-            System.out.println("Error creating DataObject " + e.toString());
-            e.printStackTrace();
-            return;
-        }
+        commentary("Demonstrates accessing a DataObject's properties using the XPath style getter/setter methods");
+
 
-        // start of sample
-        try {
+        HelperContext scope = createScopeForTypes();
+        
+        
+        commentary(
+            "First we create the type system using an XML Schema file and then create\n"+
+            "A DataObject using an XML document for convenience");
+        
+        loadTypesFromXMLSchemaFile(scope, SdoSampleConstants.PO_XSD_RESOURCE);
+        DataObject purchaseOrder = getDataObjectFromFile(scope, SdoSampleConstants.PO_XML_RESOURCE);
 
-            accessDataObjectUsingXPath(purchaseOrder);
-        } catch (Exception e) {
-            System.out.println("Sorry there was an error accessing properties by name " + e.toString());
-            e.printStackTrace();
+
+
+        commentary(
+            "Accessing data from the purchase order using the DataObjects XPath style methods\n");
+        
+
+        System.out.println("First we use the simplest kind of path\n" +
+            "purchaseOrder.getString(\"billTo/name\")\n" +
+            "The purchase is to be paid for by .... " +
+            purchaseOrder.getString("billTo/name"));
+        
+        
+        System.out.println("\nThen we use indexing by integer starting from 1\n" +
+            "purchaseOrder.getString(\"items/item[1]/productName\"\n" +
+            "The first item in the order is a ... " +
+            purchaseOrder.getString("items/item[1]/productName"));
+        
+       
+        System.out.println("\nThe alternative style of indexing uses a . notation and starts from 0\n"+
+            "purchaseOrder.getFloat(\"items/item.0/price\")\n" +
+            "The price of this item is ... " +
+            purchaseOrder.getFloat("items/item.0/price"));
+        
+
+        System.out.println("\nDataObjects can be looked up by supplying the value of one of the contained simple valued Properties\n"+
+            "DataObject babyMonitorItem = purchaseOrder.getDataObject(\"items/item[productName=\\\"Baby Monitor\\\"]");
+                
+        DataObject babyMonitorItem = purchaseOrder.getDataObject("items/item[productName=\"Baby Monitor\"]");
+        System.out.println("The price of the Baby Monitor is .... " +
+        babyMonitorItem.getFloat("price"));
+        
+        
+        System.out.println("\nA parent DataObject can be accessed with the .. notation\n"+
+            "List onlyIfBuyingGrassSeed = purchaseOrder.getList(\"items/item[productName=GrassSeed]/../item\");");
+        List onlyIfBuyingGrassSeed = purchaseOrder.getList("items/item[productName=GrassSeed]/../item");
+        if(onlyIfBuyingGrassSeed != null) {
+          System.out.println("The purchase order included grass seed and " + new Integer(onlyIfBuyingGrassSeed.size()-1) + " other items");
+        } else {
+          System.out.println("The purchase order did not include GrassSeed");
         }
-        System.out.println("GoodBye");
-        // end of sample
+
+
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org