diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-10 19:19:03 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-10 19:19:03 +0000 |
commit | dc741421e88c3e4210b00f39877159c5117b99d3 (patch) | |
tree | 68ae372d3c8a7fee11f44a4701585db55d24369d /sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets | |
parent | 9833a542bb75c2008c71809e4fdfebc41e67fb46 (diff) |
moving SDO branches
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@834611 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets')
9 files changed, 1406 insertions, 0 deletions
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectPropertiesByName.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectPropertiesByName.java new file mode 100644 index 0000000000..7aa9909657 --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectPropertiesByName.java @@ -0,0 +1,154 @@ +/** + * + * 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.tuscany.samples.sdo.specCodeSnippets; + +import java.util.List; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; +import org.apache.tuscany.samples.sdo.SdoSampleConstants; + +/** + * Demonstrates accessing a created DataObject's properties by name. + * + * This sample is from the <a href="http://incubator.apache.org/tuscany" + * target="_blank"> Apache Tuscany</a> project. It was written to help users + * understand and experiement with SDO. It is based upon code snippets contained + * within, and is meant for use with, and reference to the <a + * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1" + * target="_bank">SDO Specification</a>. + * + * <br> + * <br> + * <b>Usage:</b> <br> + * This sample can easily be run from within Eclipse as a Java Application if tuscany or + * the sample-sdo project is imported into Eclipse as an existing project. + * <br><br> + * If executing as a standalone application please do the following: + * <br> + * <UL> + * <LI>Include the following jar files on your classpath : + * <UL> + * <LI>SDO API and Tuscany Implementation + * <UL> + * <LI>sdo-api-{version}.jar - SDO API + * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation + * </UL> + * </LI> + * <LI>EMF dependencies. + * <UL> + * <LI>emf-common-{version}.jar - some common framework utility and base classes + * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel) + * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework + * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader + * <LI>xsd-{version}.jar - the XML Schema model + * </UL> + * </LI> + * </UL> + * + * These jar files can be obtained from directly from Tuscany and EMF projects or from <a + * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies" + * target="_bank">SDO Execution Dependancies </a> </LI> + * <LI>Execute: <br> + * java org.apache.tuscany.samples.sdo.specCodeSnippets.AccessDataObjectPropertiesByName</LI> + * </UL> + * + * @author Robbie Minshall + */ +public class AccessDataObjectPropertiesByName { + + /** + * Prints a subset of PurchaseOrder properties to System.out + * + * @param purchaseOrder. + * DataObject defined by Types in + * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} + */ + public static void printPurchaseOrderSummary(DataObject purchaseOrder) { + + // example accessing properties via property names + System.out.println("Purchase Order: "); + System.out.println(" Order date: " + purchaseOrder.get("orderDate")); + System.out.println(" Comment: " + purchaseOrder.get("comment")); + + DataObject shipTo = purchaseOrder.getDataObject("shipTo"); + System.out.println(" Ship to name: " + shipTo.get("name")); + + DataObject billTo = purchaseOrder.getDataObject("billTo"); + System.out.println(" Bill to name: " + billTo.get("name")); + + DataObject items = purchaseOrder.getDataObject("items"); + List itemList = items.getList("item"); + + System.out.println(" Items:"); + for (int i = 0; i < itemList.size(); i++) { + DataObject item = (DataObject) itemList.get(i); + System.out.println(" Item " + i); + System.out.println(" Part num: " + item.get("partNum")); + System.out.println(" Product name: " + item.get("productName")); + } + } + + /** + * Prints properties of a purchase order DataObject( properties are defined in + * the xsd + * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} and + * populated by xml + * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} ) + * + * @param args. + * No parameters required. + */ + public static void main(String[] args) { + + // information + System.out.println("***************************************"); + System.out.println("SDO Sample AccessDataObjectPropertiesByName"); + System.out.println("***************************************"); + System.out.println("This sample will access a DataObject properties by name"); + System.out.println("***************************************"); + + // create a DataObejct + DataObject purchaseOrder = null; + try { + XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null); + purchaseOrder = XMLHelper.INSTANCE.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; + } + + // start of sample + try { + System.out.println("Accessing properties by name"); + printPurchaseOrderSummary(purchaseOrder); + } catch (Exception e) { + System.out.println("Sorry there was an error accessing properties by name " + e.toString()); + e.printStackTrace(); + } + System.out.println("GoodBye"); + // end of sample + } + +} diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java new file mode 100644 index 0000000000..3906ed3c32 --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/AccessDataObjectUsingValidXPath.java @@ -0,0 +1,146 @@ +/** + * + * 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.tuscany.samples.sdo.specCodeSnippets; + +import org.apache.tuscany.samples.sdo.SdoSampleConstants; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; + +/** + * Demonstrates accessing a created DataObject's properties using xPath. + * + * This sample is from the <a href="http://incubator.apache.org/tuscany" + * target="_blank"> Apache Tuscany</a> project. It was written to help users + * understand and experiement with SDO. It is based upon code snipets contained + * within, and is meant for use with, and reference to the <a + * href="http://download.boulder.ibm.com/ibmdl/pub/software/dw/specs/ws-sdo/SDO_Specification_Java_V2.01.pdf" + * target="_bank">SDO Specification</a>. <br> + * <br> + * It is worth noting that the Examples section of the specification, and the + * {@link org.apache.tuscany.samples.sdo.specExampleSection.AccessingDataObjectsUsingXPath} + * use unpure xpath expression. This sample attempts to demonstrate a more appropiate + * subset of xPath operations.<br> + * <br> + * <br> + * <br> + * <b>Usage:</b> <br> + * This sample can easily be run from within Eclipse as a Java Application if tuscany or + * the sample-sdo project is imported into Eclipse as an existing project. + * <br><br> + * If executing as a standalone application please do the following: + * <br> + * <UL> + * <LI>Include the following jar files on your classpath : + * <UL> + * <LI>SDO API and Tuscany Implementation + * <UL> + * <LI>sdo-api-{version}.jar - SDO API + * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation + * </UL> + * </LI> + * <LI>EMF dependencies. + * <UL> + * <LI>emf-common-{version}.jar - some common framework utility and base classes + * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel) + * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework + * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader + * <LI>xsd-{version}.jar - the XML Schema model + * </UL> + * </LI> + * </UL> + * + * These jar files can be obtained from directly from Tuscany and EMF projects or from <a + * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies" + * target="_bank">SDO Execution Dependencies </a> </LI> + * Execute: java + * org.apache.tuscany.samples.sdo.specCodeSnippets.AccessDataObjectUsingValidXPath + * + * @author Robbie Minshall + */ +public class AccessDataObjectUsingValidXPath { + + /** + * 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 + * populated by xml + * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} ) + * + * @param args. + * No parameters required. + */ + public static void main(String[] args) { + + // 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; + try { + XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null); + purchaseOrder = XMLHelper.INSTANCE.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; + } + + // start of sample + try { + + accessDataObjectUsingXPath(purchaseOrder); + } catch (Exception e) { + System.out.println("Sorry there was an error accessing properties by name " + e.toString()); + e.printStackTrace(); + } + System.out.println("GoodBye"); + // end of sample + } + +} diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXmlString.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXmlString.java new file mode 100644 index 0000000000..b58c7070e9 --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXmlString.java @@ -0,0 +1,163 @@ +/** + * + * 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.tuscany.samples.sdo.specCodeSnippets; + +import java.util.List; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.XMLHelper; + +/** + * Demonstrates creating a DataObject from a String of XML. + * + * The following sample is from the <a href="http://incubator.apache.org/tuscany" + * target="_blank"> Apache Tuscany</a> project. It was written to help users + * understand and experiement with SDO. It is based upon code snippets contained + * within, and is meant for use with, and reference to the <a + * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1" + * target="_bank">SDO Specification</a>. Specifically this sample is based upon section titled "Creating DataObjects from XML documents" <br> + * <br> + * <b>Usage:</b> <br> + * This sample can easily be run from within Eclipse as a Java Application if tuscany or + * the sample-sdo project is imported into Eclipse as an existing project. + * <br><br> + * If executing as a standalone application please do the following: + * <br> + * <UL> + * <LI>Include the following jar files on your classpath : + * <UL> + * <LI>SDO API and Tuscany Implementation + * <UL> + * <LI>sdo-api-{version}.jar - SDO API + * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation + * </UL> + * </LI> + * <LI>EMF dependencies. + * <UL> + * <LI>emf-common-{version}.jar - some common framework utility and base classes + * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel) + * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework + * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader + * <LI>xsd-{version}.jar - the XML Schema model + * </UL> + * </LI> + * </UL> + * + * These jar files can be obtained from directly from Tuscany and EMF projects or from <a + * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies" + * target="_bank">SDO Execution Dependancies </a> </LI> + * <LI>Execute: <br> + * java org.apache.tuscany.samples.sdo.specCodeSnippets.CreateDataObjectFromXmlString</LI> + * </UL> + * + * @author Robbie Minshall + */ +public class CreateDataObjectFromXmlString { + + /** + * String representing a PurchaseOrder in XML format + */ + public static final String XML_STRING = "<purchaseOrder orderDate='1999-10-20'>" + " <shipTo country='US'>" + " <name>Alice Smith</name>" + + " <street>123 Maple Street</street>" + " <city>Mill Valley</city>" + " <state>PA</state>" + " <zip>90952</zip>" + "</shipTo>" + + "</purchaseOrder>"; + + /** + * Creates a DataObject from an Xml String + * + * @return + */ + public static DataObject createDataObjectFromXmlString() { + + // TODO: do this with and without defining the schema + DataObject po = XMLHelper.INSTANCE.load(XML_STRING).getRootObject(); + System.out.println("DataObject has been created : " + po.toString()); + return po; + } + + /** + * @param args + * None required + */ + public static void main(String[] args) { + + // information + System.out.println("***************************************"); + System.out.println("SDO Sample CreateDataObjectFromXmlString"); + System.out.println("***************************************"); + System.out.println("Demonstrats creating a DataObject from a String of XML, based upon section titled 'Creating DataObjects from XML documents'"); + System.out.println("***************************************"); + + // sample + try { + + System.out.println("Use the following XML String: " + XML_STRING); + System.out.println("Creating DataObject"); + DataObject po = createDataObjectFromXmlString(); + System.out.println("The following DataObject sucessfully created from XML String"); + + System.out.println(XMLHelper.INSTANCE.save(po, "http://example.com/purchaseOrder", "purchaseOrder")); + + /* + * Without a Type definition accessing a DataObject is slightly more + * difficult. This is because without a Type definition SDO does not know + * the intended multiplicity of properties. Currently the Tuscany + * implementation assumes that elements and attributes are lists. There + * is some discussion (SDO-3, also mentioned in SDO-22) about adding + * annotations to the XML (sdo:many="false") to control the indended + * multiplicity of DataObjects created without a model. + * + * The following code will not work when a model/schema is not defined + * shipTo = purchaseOrder.getDataObject("shipTo"); assertEquals("testing + * ship to name", shipTo.getString("name"), "Alice Smith"); + * assertEquals("testing ship to zip", shipTo.getString("zip"), "90952"); + */ + + System.out.println("Programatically access content of DataObject - getting name of the person this is getting shipped to"); + // access the shipTo DataObject + List shipToList = po.getList("shipTo"); + DataObject shipTo = (DataObject) shipToList.get(0); + // access the name DataObject + + /* + * This is currently quite cumbersome. In future specifications accessing + * the name element will be simplier and the user could simply execute: + * List nameList = shipTo.getList("name"); String actualName = (String) + * nameList.get(0); + * + * Or if name is an attribute ("<shipTo country='US' name='Alice + * Smith'>") simply: String actualName = shipTo.getString("name"); + * + * There are currently more elegant means to perform this task using the xPath support + */ + List nameList = shipTo.getList("name"); + DataObject name = (DataObject) nameList.get(0); + // access the contents of the name DataObject + List textList = name.getList("text"); + String actualName = (String) textList.get(0); + System.out.println("Name being shipped to: " + actualName); + + } catch (Exception e) { + System.out.println("Sorry, exception caught when creating DataObject : " + e.toString()); + e.printStackTrace(); + } + // end of sample + System.out.println("GoodBye"); + } +} diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXsdAndXmlFiles.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXsdAndXmlFiles.java new file mode 100644 index 0000000000..6f1d8c60db --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/CreateDataObjectFromXsdAndXmlFiles.java @@ -0,0 +1,196 @@ +/** + * + * 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.tuscany.samples.sdo.specCodeSnippets; + +import java.io.InputStream; + +import org.apache.tuscany.samples.sdo.SdoSampleConstants; +import commonj.sdo.DataObject; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; + + +/** + * Demonstrates defining Types by using a predefined xsd resource, and populating a DataObject by using a previously created xml file. + * + * The following sample is from the <a href="http://incubator.apache.org/tuscany" + * target="_blank"> Apache Tuscany</a> project. It was written to help users + * understand and experiement with SDO. It is based upon code snippets contained + * within, and is meant for use with, and reference to the <a + * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1" + * target="_bank">SDO Specification</a>. <br> + * <br> + * <br>Uses {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} and {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} in order to define purcahseOrder DataObjects + * <br><br> + * <b>Usage:</b> <br> + * This sample can easily be run from within Eclipse as a Java Application if tuscany or + * the sample-sdo project is imported into Eclipse as an existing project. + * <br><br> + * If executing as a standalone application please do the following: + * <br> + * <UL> + * <LI>Include the following jar files on your classpath : + * <UL> + * <LI>SDO API and Tuscany Implementation + * <UL> + * <LI>sdo-api-{version}.jar - SDO API + * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation + * </UL> + * </LI> + * <LI>EMF dependencies. + * <UL> + * <LI>emf-common-{version}.jar - some common framework utility and base classes + * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel) + * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework + * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader + * <LI>xsd-{version}.jar - the XML Schema model + * </UL> + * </LI> + * </UL> + * + * These jar files can be obtained from directly from Tuscany and EMF projects or from <a + * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies" + * target="_bank">SDO Execution Dependancies </a> </LI> + * <LI>Execute: <br> + * java org.apache.tuscany.samples.sdo.specCodeSnippets.CreateDataObjectFromXsdAndXmlFiles</LI> + * </UL> + * + * @author Robbie Minshall + */ +public class CreateDataObjectFromXsdAndXmlFiles { + + public static boolean typesDefined = false; + + + /** + * Loads the {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} resource in order to define PurchaseOrder + * Types + */ + public static void definePurchaseOrderTypeUsingXsdResource() + throws Exception { + + InputStream is = null; + try { + System.out.println("Attempting to define types using resource " + + SdoSampleConstants.PO_XSD_RESOURCE); + is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE); + if (is == null) { + System.out.println("InputStream is null, you may need to add the resources directory is on the classpath"); + throw new Exception("InputStream for " + SdoSampleConstants.PO_XSD_RESOURCE + " was null, ensure that it is located on the classpath"); + } else { + System.out.println("Obtained Input Stream from resoruce"); + } + XSDHelper.INSTANCE.define(is, null); + + } catch (Exception e) { + System.out + .println("Exception caught defining Types from resoruce stream to " + + SdoSampleConstants.PO_XSD_RESOURCE); + throw e; + } finally { + // try to close the input stream + if (is != null) { + try { + is.close(); + System.out.println("Closed input stream"); + } catch (Exception e) { + System.out.println("could not close input stream " + e.toString()); + } + } + } + } + + /** + * Uses resource {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} to populate DataObject + * @return populated purchase order DataObject + */ + public static DataObject createPurchaseOrderDataObjectUsingXmlResource() + throws Exception { + + if (! typesDefined){ + definePurchaseOrderTypeUsingXsdResource(); + } + DataObject purchaseOrder = null; + InputStream is = null; + + try { + + System.out + .println("Using resource as stream to access xml to populate DataObjects"); + // FileInputStream fis = new FileInputStream(xmlFileName); + is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE); + if (is == null) { + System.out.println("InputStream is null"); + } else { + System.out + .println("Successfully obtained InputStream from resource"); + } + XMLDocument xmlDoc = XMLHelper.INSTANCE.load(is); + purchaseOrder = xmlDoc.getRootObject(); + + } catch (Exception e) { + System.out.println("Could not use resourceAsStream " + + e.toString()); + e.printStackTrace(); + throw e; + }finally { + // try to close the input stream + if (is != null) { + try { + is.close(); + System.out.println("Closed input stream"); + } catch (Exception e) { + System.out.println("Could not close input stream " + e.toString()); + } + } + } + + return purchaseOrder; + } + + /** + * @param args. No arguments required + */ + public static void main(String[] args) { + // information + System.out.println("***************************************"); + System.out.println("SDO Sample CreateDataObjectFromXsdAndXmlFiles"); + System.out.println("***************************************"); + System.out.println("Demonstrates defining Types by using a predefined xsd resource, and populating a DataObject by using a previously created xml file"); + System.out.println("***************************************"); + + + try { + // define the types + definePurchaseOrderTypeUsingXsdResource(); + + // create a dataObject + DataObject purchaseOrder = createPurchaseOrderDataObjectUsingXmlResource(); + System.out.println("DataObject sucessfully created"); + + } catch (Exception e) { + System.out.println("Sorry there was an error encountered " + e.toString()); + } + System.out.println("GoodBye"); + } + +} diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/DynamicCustomerTypeSample.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/DynamicCustomerTypeSample.java new file mode 100644 index 0000000000..7df6e9fc93 --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/DynamicCustomerTypeSample.java @@ -0,0 +1,192 @@ +/** + * + * 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.tuscany.samples.sdo.specCodeSnippets; + +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.TypeHelper; +import java.util.List; + +/** + * Demonstrates creating and using Types dynamically. This sample is from the <a + * href="http://incubator.apache.org/tuscany" target="_blank"> Apache Tuscany</a> + * project. It was written to help users understand and experiement with SDO. It is + * based upon code snippets contained within, and is meant for use with, and reference + * to the <a + * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1" + * target="_bank">SDO Specification</a>. Specifically, this sample is based upon the + * section titled "Defining SDO Types Dynamically" <br> + * <br> * <b>Usage:</b> <br> + * This sample can easily be run from within Eclipse as a Java Application if tuscany or + * the sample-sdo project is imported into Eclipse as an existing project. + * <br><br> + * If executing as a standalone application please do the following: + * <br> + * <UL> + * <LI>Include the following jar files on your classpath : + * <UL> + * <LI>SDO API and Tuscany Implementation + * <UL> + * <LI>sdo-api-{version}.jar - SDO API + * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation + * </UL> + * </LI> + * <LI>EMF dependencies. + * <UL> + * <LI>emf-common-{version}.jar - some common framework utility and base classes + * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel) + * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework + * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader + * <LI>xsd-{version}.jar - the XML Schema model + * </UL> + * </LI> + * </UL> + * + * These jar files can be obtained from directly from Tuscany and EMF projects or from <a + * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies" + * target="_bank">SDO Execution Dependancies </a> </LI> + * <LI>Execute: <br> + * java org.apache.tuscany.samples.sdo.specCodeSnippets.DynamicCustomerTypeSample</LI> + * </UL> + * + * @author Robbie Minshall + */ +public class DynamicCustomerTypeSample { + + /** + * XSD file used to define the model and Types for Customer Dataobject + */ + public static final String CUSTOMER_MODEL = "customer.xsd"; + + /** + * Default file name to use when generated customer XSD files. + * + * @see org.apache.tuscany.samples.sdo.specCodeSnippets.CreateXmlAndXsdFromDataObject + */ + public static final String CUSTOMER_MODEL_GENERATED = "generatedCustomer.xsd"; + + /** + * Fake namespace used for Customer + */ + public static final String CUSTOMER_NAMESPACE = "http://www.example.com/customer"; + + /** + * XML file used to populate Customer DataObjects + */ + public static final String CUSTOMER_XML = "customer.xml"; + + /** + * Default file name to use when generated customer XML files. + * + * @see org.apache.tuscany.samples.sdo.specCodeSnippets.CreateXmlAndXsdFromDataObject + */ + public static final String CUSTOMER_XML_GENERATED = "generatedCustomer.xml"; + + /** + * Method dynamically defines customer types + */ + public static void defineCustomerTypes() { + // get an instance of the type helper + TypeHelper types = TypeHelper.INSTANCE; + Type intType = types.getType("commonj.sdo", "Int"); + Type stringType = types.getType("commonj.sdo", "String"); + + // create a new Type for Customers + DataObject customerType = DataFactory.INSTANCE.create("commonj.sdo", "Type"); + customerType.set("uri", "http://example.com/customer"); + customerType.set("name", "Customer"); + + // create a customer number property + DataObject custNumProperty = customerType.createDataObject("property"); + custNumProperty.set("name", "custNum"); + custNumProperty.set("type", intType); + + // create a last name property + DataObject lastNameProperty = customerType.createDataObject("property"); + lastNameProperty.set("name", "lastName"); + lastNameProperty.set("type", stringType); + + // create a first name property + DataObject firstNameProperty = customerType.createDataObject("property"); + firstNameProperty.set("name", "firstName"); + firstNameProperty.set("type", stringType); + + // now define the Customer type so that customers can be made + types.define(customerType); + + Type testType = TypeHelper.INSTANCE.getType("http://example.com/customer", "Customer"); + List props = testType.getProperties(); + for (int i = 0; i < props.size(); i++) { + System.out.println(props.get(i)); + } + } + + /** + * Creates a new Customer using dynamically defined Customer Type + * + * @param custNum + * @param firstName + * @param lastName + * @return + */ + private static DataObject createCustomer(int custNum, String firstName, String lastName) { + DataFactory factory = DataFactory.INSTANCE; + DataObject customer1 = factory.create("http://example.com/customer", "Customer"); + customer1.setInt("custNum", custNum); + customer1.set("firstName", firstName); + customer1.set("lastName", lastName); + return customer1; + } + + /** + * Drives sample methods + * + * @param args + * no arguments required + */ + public static void main(String args[]) { + System.out.println("***************************************"); + System.out.println("SDO Sample DynamicCustomerTypeSample"); + System.out.println("***************************************"); + System.out.println("Demonstrats creating and using Types dynamically"); + System.out.println("***************************************"); + try { + System.out.println("Dynamically defining Customer Type"); + defineCustomerTypes(); + + System.out.println("Creating new customer DataObject"); + DataObject customer1 = createCustomer(1, "John", "Adams"); + System.out.println("Customer Created"); + + System.out.println("converting to xml"); + String xmlDocString = XMLHelper.INSTANCE.save(customer1, CUSTOMER_NAMESPACE, "customer"); + System.out.println(xmlDocString); + + } catch (Exception e) { + System.out.println("Sorry unexpected exception caught during sample execution : " + e.toString()); + e.printStackTrace(); + } + System.out.println("GoodBye"); + } + +} diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/ObtainingDataGraphFromXml.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/ObtainingDataGraphFromXml.java new file mode 100644 index 0000000000..69c2098798 --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/ObtainingDataGraphFromXml.java @@ -0,0 +1,189 @@ +/** + * + * 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.tuscany.samples.sdo.specCodeSnippets; + +import java.io.InputStream; + +import org.apache.tuscany.samples.sdo.SdoSampleConstants; + +import org.apache.tuscany.sdo.util.SDOUtil; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; +import commonj.sdo.DataObject; +import commonj.sdo.DataGraph; +import commonj.sdo.Sequence; + +/** + * Demonstrates a vareity of methods available to obtain the root DataObject + * contained within an xml representation of a DataGraph. This is currently a grey + * area of the specification and this samples demonstrates spec complant means, as + * well as utility methods that have been added to Tuscany to address issues within + * the specification. + * + * The following sample is from the <a href="http://incubator.apache.org/tuscany" + * target="_blank"> Apache Tuscany</a> project. It was written to help users + * understand and experiement with SDO. It is based upon example code contained + * within, and is meant for use with, and reference to the <a + * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1" + * target="_bank">SDO Specification</a>. This sample addresses confusing aspects of + * the the AccessDataObjectsUsingXPath example from the Examples section of the SDO + * specification.<br> + * <br> + * To define the correct Types for each DataObject ( CompanyType, DepartmentType etc ) + * this sample relies upon + * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_XSD} which is + * provided in the resources directory of these samples. The xml file + * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_DATAGRAPH_XML} is + * used to load the DataGraph and is also located in this resources directory. <br> + * <br> + * <b>Usage:</b> <br> + * This sample can easily be run from within Eclipse as a Java Application if tuscany or + * the sample-sdo project is imported into Eclipse as an existing project. + * <br><br> + * If executing as a standalone application please do the following: + * <br> + * <UL> + * <LI>Include the following jar files on your classpath : + * <UL> + * <LI>SDO API and Tuscany Implementation + * <UL> + * <LI>sdo-api-{version}.jar - SDO API + * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation + * </UL> + * </LI> + * <LI>EMF dependencies. + * <UL> + * <LI>emf-common-{version}.jar - some common framework utility and base classes + * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel) + * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework + * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader + * <LI>xsd-{version}.jar - the XML Schema model + * </UL> + * </LI> + * </UL> + * + * These jar files can be obtained from directly from Tuscany and EMF projects or from <a + * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies" + * target="_bank">SDO Execution Dependancies </a> </LI> + * <LI>Execute: <br> + * java org.apache.tuscany.samples.sdo.specCodeSnippets.ObtainingDataGraphFromXml</LI> + * </UL> + * + * @see org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsUsingXPath + * @author Robbie Minshall + */ + +public class ObtainingDataGraphFromXml { + + /** + * Execute this method in order to run the sample. + * + * @param args + */ + public static void main(String[] args) { + System.out.println("***************************************"); + System.out.println("SDO Sample ObtainingDataGraphFromXml"); + System.out.println("***************************************"); + System.out.println("Demonstrates a vareity of methods available to obtain the root DataObject" + + "contained within an xml representation of a DataGraph."); + System.out.println("***************************************"); + + + // define Types + try { + System.out.println("Defining Types using XSD"); + InputStream is = null; + is = ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD); + XSDHelper.INSTANCE.define(is, null); + is.close(); + System.out.println("Type definition completed"); + } catch (Exception e) { + System.out.println("Exception caught defining types " + e.toString()); + e.printStackTrace(); + } + + try { + + /** + * A DataGraph is an optional envelope for a graph of DataObjects and an + * associated ChangeSummary. + * + * The specification group is currently investigating options regarding + * obtaining a DataGraph from an xml file as well as the role of the + * DataGraph within the SDO specification. + * + * In order to obtain the rootObject contained within the specification + * the specification demonstrates reading in an XML file and obtaining a + * DataObject that represents the datagraph contained within the file. + * Note, that the specification does not obtain a DataGraph - there are + * not mechanisms within the specification for obtaining a DataGraph from + * xml. Obtaining the root DataObject from this datagraph representation + * is quite cumbersome and is demonstrated below. + * + * In order to address this current limitation within the specification + * Tuscany has added a utility method to SDOUtil that allows a user to + * obtain an actual DataGraph from xml. + */ + + DataObject company = null; + + /** + * Use specification mechanism for obtaining a DataObject that represents + */ + // the a datagraph + System.out.println("Using specification methods to obtain DataObject represeenting a datagraph from xml"); + + // When the xml file represents a DataGraph the root Object + // of the XMLDocument is a DataGraph + DataObject dataObjectRepresentingDataGraph = XMLHelper.INSTANCE.load( + ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAGRAPH_XML)).getRootObject(); + + // Obtain the company DataObject from the DataObject representing the datagraph + Sequence mySeq = (Sequence) dataObjectRepresentingDataGraph.getSequence("any"); + company = (DataObject) mySeq.getValue(0); + + System.out.println("Obtained DataObject representing datagraph"); + System.out.println(dataObjectRepresentingDataGraph); + System.out.println("Obtained root DataObject from datagraph"); + System.out.println(company); + System.out.println(); + + + /** + * Use utility method to obtain an actual DataGraph from the xml + */ + DataGraph datagraph = SDOUtil.loadDataGraph(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAGRAPH_XML), null); + // Obtain the company DataObject from the actual DataGraph + company = datagraph.getRootObject(); + System.out.println("Obtained actual DataGraph:"); + System.out.println(datagraph); + System.out.println("Obtained root DataObject from DataGraph:"); + System.out.println(company); + System.out.println(); + + } catch (Exception e) { + System.out.println("Sorry there was an error encountered " + e.toString()); + e.printStackTrace(); + } + System.out.println("GoodBye"); + + } +} diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/PrintPropertiesOfDataObject.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/PrintPropertiesOfDataObject.java new file mode 100644 index 0000000000..453cec211a --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/PrintPropertiesOfDataObject.java @@ -0,0 +1,177 @@ +/** + * + * 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.tuscany.samples.sdo.specCodeSnippets; + +import java.util.List; + +import org.apache.tuscany.samples.sdo.SdoSampleConstants; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Type; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; + +/** + * Demonstrates iterating through, and inspecting properites of a DataObject. This + * sample shows listing properties of a dataObject, determining if the properties are + * set, are simple properties or represent DataObjects <br> + * <br> + * This sample is from the <a href="http://incubator.apache.org/tuscany" + * target="_blank"> Apache Tuscany</a> project. It was written to help users + * understand and experiement with SDO. It is based upon code snippets contained + * within, and is meant for use with, and reference to the <a + * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1" + * target="_bank">SDO Specification</a>. This specific examples was based upon code + * snippets in the section titled 'Property Indexes'. <br> + * <b>Usage:</b> <br> + * This sample can easily be run from within Eclipse as a Java Application if tuscany or + * the sample-sdo project is imported into Eclipse as an existing project. + * <br><br> + * If executing as a standalone application please do the following: + * <br> + * <UL> + * <LI>Include the following jar files on your classpath : + * <UL> + * <LI>SDO API and Tuscany Implementation + * <UL> + * <LI>sdo-api-{version}.jar - SDO API + * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation + * </UL> + * </LI> + * <LI>EMF dependencies. + * <UL> + * <LI>emf-common-{version}.jar - some common framework utility and base classes + * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel) + * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework + * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader + * <LI>xsd-{version}.jar - the XML Schema model + * </UL> + * </LI> + * </UL> + * + * These jar files can be obtained from directly from Tuscany and EMF projects or from <a + * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies" + * target="_bank">SDO Execution Dependancies </a> </LI> + * <LI>Execute: <br> + * java org.apache.tuscany.samples.sdo.specCodeSnippets.PrintPropertiesOfDataObject</LI> + * </UL> + * + * @author Robbie Minshall + */ + +public class PrintPropertiesOfDataObject { + + /** + * Calls recursive method to inspect and print properties of a DataObject. + * + * @param DataObject. + * The DataObject to print the properties of + */ + public static void printDataObjectProperties(DataObject myDo) throws Exception { + System.out.println("Recursivly inspecting and printing properties of DataObject"); + printAndInspectDataObject(" ", myDo); + } + + /** + * Recursivly inspects and prints DataObject. This method will list all set + * properties + * + * @param buffer. + * Blank string to help with formating + * @param myDo. + * DataObject to recursivly inspect and print the properties of. + * @throws Exception + */ + private static void printAndInspectDataObject(String buffer, DataObject myDo) throws Exception { + + for (int i = 0; i < myDo.getInstanceProperties().size(); i++) { + Property p = (Property) myDo.getInstanceProperties().get(i); + // just print out the name, type and string value of the property + Type type = p.getType(); + System.out.print(buffer + " " + p.getName() + ",type=" + type.getName()); + if (myDo.isSet(p)) { + // see if this represents a simple property or if the type is also a + // datatype + if (type.isDataType()) { + System.out.println(", simple type : " + myDo.get(i)); + } else { + + // check to see if the property has many values + if (p.isMany()) { + System.out.println(buffer + ", many valued with list of DataObjects : "); + List dataObjects = myDo.getList(p); + for (int x = 0; x < dataObjects.size(); x++) { + printAndInspectDataObject(buffer + " ", (DataObject) dataObjects.get(x)); + System.out.println(); + } + + } else { + + System.out.println(buffer + ", data object : "); + DataObject newDataObject = myDo.getDataObject(p); + printAndInspectDataObject(buffer + " ", newDataObject); + System.out.println(""); + } + } + } else { + System.out.println(buffer + ", is not set"); + } + } + + } + + /** + * @param args. + * none required. + */ + public static void main(String[] args) { + // information + System.out.println("***************************************"); + System.out.println("SDO Sample InspectAndPrintPropertiesOfDataObject"); + System.out.println("***************************************"); + System.out.println("Demonstrates a common pattern of looping through and inspecting the META-DATA for all instance properties." + + "This example uses recursion to iterate through properties which themselves are DataObjects."); + System.out.println("***************************************"); + + // create a DataObejct + DataObject purchaseOrder = null; + try { + XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null); + purchaseOrder = XMLHelper.INSTANCE.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; + } + + //print and inspect properties of the DataObject + try { + System.out.println("Will iterate and inspect the properties of the following data object " + purchaseOrder.toString()); + printDataObjectProperties(purchaseOrder); + + } catch (Exception e) { + System.out.println("Sorry, an error was encountered inspecting DataObject " + e.toString()); + } + System.out.println("GoodBye"); + } +} diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/UsingXPath.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/UsingXPath.java new file mode 100644 index 0000000000..b243bc0361 --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/UsingXPath.java @@ -0,0 +1,149 @@ +/** + * + * 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.tuscany.samples.sdo.specCodeSnippets; + +import org.apache.tuscany.samples.sdo.SdoSampleConstants; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; + +/** + * Demonstrates accessing a created DataObject's properties using xPath. + * + * This sample is from the <a href="http://incubator.apache.org/tuscany" target="_blank"> Apache Tuscany</a> project. It was written to help users + * understand and experiement with SDO. It is based upon code snippets contained within, and is meant for use with, and reference to the <a + * href="http://www.osoa.org/download/attachments/791/SDO_Specification_Java_V2.01.pdf?version=1" target="_bank">SDO Specification</a>. + * <br> + * <br> + * It is worth noting that the Examples section of the specification, and the + * {@link org.apache.tuscany.samples.sdo.specExampleSection.AccessingDataObjectsUsingXPath} use unpure xpath expression. This sample attempts to + * demonstrate a more appropiate subset of xPath operations.<br> + * <br> + * <b>Usage:</b> <br> + * This sample can easily be run from within Eclipse as a Java Application if tuscany or the sample-sdo project is imported into Eclipse as an + * existing project. <br> + * <br> + * If executing as a standalone application please do the following: <br> + * <UL> + * <LI>Include the following jar files on your classpath : + * <UL> + * <LI>SDO API and Tuscany Implementation + * <UL> + * <LI>sdo-api-{version}.jar - SDO API + * <LI>tuscany-sdo-impl-{version}.jar - Tuscany SDO implementation + * </UL> + * </LI> + * <LI>EMF dependencies. + * <UL> + * <LI>emf-common-{version}.jar - some common framework utility and base classes + * <LI>emf-ecore-{version}.jar - the EMF core runtime implementation classes (the Ecore metamodel) + * <LI>emf-ecore-change-{version}.jar - the EMF change recorder and framework + * <LI>emf-ecore-xmi-{version}.jar - EMF's default XML (and XMI) serializer and loader + * <LI>xsd-{version}.jar - the XML Schema model + * </UL> + * </LI> + * </UL> + * + * These jar files can be obtained from directly from Tuscany and EMF projects or from <a + * href="http://wiki.apache.org/ws-data/attachments/Tuscany(2f)TuscanyJava(2f)SDO_Java_Overview/attachments/SDO%20Execution%20Dependencies" + * target="_bank">SDO Execution Dependancies </a> </LI> + * <LI>Execute: <br> + * java org.apache.tuscany.samples.sdo.specCodeSnippets.UsingXPath</LI> + * </UL> + * + * @author Robbie Minshall + */ +public class UsingXPath { + + /** + * 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) throws Exception { + // TODO: use variety of xpath expressions such as items/item[1] + // TODO: add to junit test cases for test cases above + + // xpath expressions for obtaining various items + String[] xPathExpressions = { "items/item[1]","items/item[productName=\"Baby Monitor\"]" }; + + for (int i = 0; i < xPathExpressions.length; i++) { + try { + System.out.println("Accessing individual item from list using xpath expression" + xPathExpressions[i]); + + DataObject item = purchaseOrder.getDataObject(xPathExpressions[i]); + System.out.println(" DataObject toString : " + item.toString()); + + System.out.println(" Item name:" + item.get("productName")); + System.out.println(" Part num: " + item.get("partNum")); + } catch (Exception e) { + System.out.println(" Sorry there was an error executing expression " + xPathExpressions[i]); + throw e; + } + } + + //TODO: select various lists of properties from DataObjects using xPath. + } + + /** + * 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 populated by xml + * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_RESOURCE} ) + * + * @param args. + * No parameters required. + */ + public static void main(String[] args) { + + // information + System.out.println("***************************************"); + System.out.println("SDO Sample UsingXPath"); + System.out.println("***************************************"); + System.out.println("Demonstrats accessing a created DataObject's properties using xPath."); + System.out.println("***************************************"); + + // create a DataObejct + DataObject purchaseOrder = null; + try { + XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null); + purchaseOrder = XMLHelper.INSTANCE.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; + } + + // start of sample + try { + + accessDataObjectUsingXPath(purchaseOrder); + } catch (Exception e) { + System.out.println("Sorry there was an error accessing properties by name " + e.toString()); + e.printStackTrace(); + } + System.out.println("GoodBye"); + // end of sample + } + +} diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/package.html b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/package.html new file mode 100644 index 0000000000..ac784deabc --- /dev/null +++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specCodeSnippets/package.html @@ -0,0 +1,40 @@ +<html> +<!-- + * + * 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. + * --> +<head> + <title>package information</title> +</head> +<body> + +Provides examples containing code snippets located throughout the SDO Specification. In general these samples attempt to use the +code and comments contained within the specification but allow a certain degree of flexibility to extend or improve +upon the code snippets where desirable. + The user is encouraged to modify and execute these code examples. + + +<h2>Related Documentation</h2> + +For overviews, tutorials, examples, guides, and tool documentation, please see the +<a href="http://incubator.apache.org/tuscany/java_sdo_overview.html"> SDO Java Overview +</a> +section of the Tuscany website. + +</body> +</html>
\ No newline at end of file |