summaryrefslogtreecommitdiffstats
path: root/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection
diff options
context:
space:
mode:
Diffstat (limited to 'sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection')
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessDataObjectsUsingXPath.java251
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java232
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java194
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java136
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingXmlFromDataObjects.java160
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/SerializingDesearializingADataObject.java183
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java190
-rw-r--r--sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/package.html40
8 files changed, 1386 insertions, 0 deletions
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessDataObjectsUsingXPath.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessDataObjectsUsingXPath.java
new file mode 100644
index 0000000000..97441c7473
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessDataObjectsUsingXPath.java
@@ -0,0 +1,251 @@
+/**
+ *
+ * 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.specExampleSection;
+
+import java.util.List;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+
+/**
+ * Demonstrates accessing the properties of a DataObject using XPath queries.
+ *
+ * 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>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * This specific sample is based upon the AccessDataObjectsUsingXPath example from
+ * the Examples section of the SDO specification. It shows the use of DataObjects and
+ * the XMLHelper and demonstrates accessing the properties of a DataObject using
+ * XPath queries. <br>
+ * <br>
+ * This sample reads an xml file representing a DataObject of a company. A
+ * DataAccessService (DAS) is simply a component which creates a DataGraph or
+ * DataObject, it can also be responcible for creating SDO Types for created
+ * DataObjects. In order to create a DataObject or DataGraph this sample relies upon
+ * XMLHelper class which is essentially an example of a XML DAS. The code shown here
+ * would work just as well against an equivalent DataGraph or DataObject that was
+ * provided by any DAS that creates an identical DataObject/DataGraph of the same
+ * Type used here. <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_DATAOBJECT_XML}
+ * used to load the DataObject is also located in this resources directory. The xsd
+ * the xml was generated by
+ * {@link org.apache.tuscany.samples.sdo.otherSources.CreateCompany} which is a good
+ * resource for how to populate or create DataObjects or DataGraphs dynamically. <br>
+ * <br>
+ * The following example has the same effect as
+ * {@link org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsViaPropertyIndex}.
+ * <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.specExampleSection.AccessDataObjectsUsingXPath </LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ * @see org.apache.tuscany.samples.sdo.specCodeSnippets.ObtainingDataGraphFromXml
+ */
+
+public class AccessDataObjectsUsingXPath {
+
+ /**
+ * 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 AccessDataObjectsUsingXPath");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates accessing the properties of a DataObject using XPath queries");
+ System.out.println("***************************************");
+
+ /**
+ * In order to associate the correct Type, to the DataObjects represented in
+ * the xml Read in the model/schema which is declared within the xsd so that
+ * it can be associated. If the Types were not defined in this manner you can
+ * still populate, access and use a DataObject or DataGraph but would not
+ * have the protection and use of use afforded by the relationships declared
+ * within the model.
+ */
+ try {
+ System.out.println("Defining Types using XSD");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD), null);
+ System.out.println("Type definition completed");
+ } catch (Exception e) {
+ System.out.println("Exception caught defining types " + e.toString());
+ e.printStackTrace();
+ }
+
+ try {
+
+ /**
+ * The specification Example demonstrates using both a DataObject and
+ * DataGraph representation of a compony model. This example simply uses
+ * a DataObject representation.
+ *
+ * Obtain the RootObject from the XMLDocument obtained from the XMLHelper
+ * instance. In this case the root object is a DataObject representing a
+ * company
+ */
+
+ DataObject company = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAOBJECT_XML)).getRootObject();
+
+ // print out some information to show the user what the objects
+ // look like
+ System.out.println("Company DataObject:");
+ System.out.println(company);
+
+ String generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("Company data object xml representation: ");
+ System.out.println(generatedXml);
+
+ /*
+ * If we wish to change the name of the company DataObject from "ACME" to
+ * "MEGACORP" , we could use the following:
+ */
+
+ // Set the "name" property for the company
+ System.out.println("Setting 'name' property for the company DataObject to 'MEGACORP'");
+ company.setString("name", " MegaCorp");
+ System.out.println();
+
+ /*
+ * Now, suppose we wish to access the employee whose serial number is
+ * "E0002" If we know that this employee is located at index 1 within the
+ * department that is located at index 0 within the company object one
+ * way to do this is by traversing each reference in the data graph and
+ * locating each DataObject in the many-valued department property using
+ * it's index in the list. For example, from the company, we can get a
+ * list of departments, from that list we can get the department at index
+ * 0, from there we can get a list of employees, and from that list we
+ * can get the employee at index 1.
+ */
+
+ System.out
+ .println("Obtain employee whose serial number is 'E0002' by acessing list of departments via index, and then accessing specific employee by index. This requies that we know the indices of both the department and employee");
+ // Get the list of departments
+ List departments = company.getList("departments");
+ // Get the department at index 0 on the list
+ DataObject department = (DataObject) departments.get(0);
+ // Get the list of employees for the department
+ List employees = department.getList("employees");
+ // Get the employee at index 2 on the list
+ DataObject employeeFromList = (DataObject) employees.get(2);
+
+ /*
+ * Alternatively, we can write a single XPath expression that directly
+ * accesses the employee from the root company. Please note that the
+ * specification's use of . indexing starting at index 0 is not true
+ * xPath. Please see the
+ * org.apache.tuscany.samples.sdo.specCodeSnippets.UsingXPath
+ * example for more examples of using xPath. A more appropiate use of
+ * xPath might be
+ *
+ * employeeFromXPath =
+ * company.getDataObject("departments[1]/employees[3]");
+ *
+ */
+
+ System.out.println("Obtain employee using xPath expression");
+ DataObject employeeFromXPath = company.getDataObject("departments.0/employees.2");
+
+ /*
+ * In order to remove that employee from the data graph, we could
+ */
+
+ // remove the employee from the list of employees
+ System.out.println("Removing employee " + employeeFromXPath.getString("name") + " from list of employees");
+ employeeFromXPath.detach();
+
+ /*
+ * And, finally, to create a new employee:
+ */
+ // create a new employee
+ System.out.println("Creating new employee (manager) Al Smith and adding to list");
+ DataObject newEmployee = department.createDataObject("employees");
+
+ newEmployee.set("name", "Al Smith");
+ newEmployee.set("SN", "E0005");
+ newEmployee.setBoolean("manager", true);
+
+ // Reset employeeOfTheMonth to be the new employee
+ System.out.println("Setting employee of the month to new employee");
+ company.set("employeeOfTheMonth", newEmployee.getString("SN"));
+
+ // print out some information to show the user what the objects
+ // look like
+ System.out.println("The modified company DataObject: ");
+ System.out.println(company);
+ System.out.println();
+
+ generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("Company data object xml representation: ");
+ System.out.println(generatedXml);
+ 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/specExampleSection/AccessingDataObjectsViaPropertyIndex.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java
new file mode 100644
index 0000000000..f2e2b8f2d7
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java
@@ -0,0 +1,232 @@
+/**
+ *
+ * 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.specExampleSection;
+
+import java.io.InputStream;
+import java.util.List;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+
+/**
+ * Demonstrates accessing the properties of a DataObject using property indices.
+ *
+ * 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 experiment 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>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * The following sample is based upon the 'Accessing DataObjects via Property Index'
+ * example from the Examples section of the SDO specification. It shows the use of
+ * DataObjects and the XMLHelper amd demonstrates accessing the properties of a
+ * DataObject using property indices. <br>
+ * <br>
+ * The following example has the same effect as
+ * {@link org.apache.tuscany.samples.sdo.specExampleSection.AccessDataObjectsUsingXPath}.
+ * The indexes for the properties are defined as constants in this class. <br>
+ * <br>
+ * This sample reads an xml file representing a DataObject of a company. In order to
+ * create a DataObject or DataGraph this sample relies upon XMLHelper class which is
+ * essentially example of a XML DAS implementation. The code shown here would work
+ * just as well against an equivalent DataObject that was provided by any DAS. <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 <br>
+ * The xml file
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#COMPANY_DATAOBJECT_XML}
+ * used to load the DataObject is also located in this resources directory. To help
+ * ensure consistancy with the xsd the xml was generated by
+ * {@link org.apache.tuscany.samples.sdo.otherSources.CreateCompany} which is a good
+ * resource for populating DataObjects, or creating DataGraphs 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.specExampleSection.AccessingDataObjectsViaPropertyIndex </LI>
+ * </UL>
+ *
+ * Note: when reading this source code in conjunction with the specification there is scope for confusion
+ * over what the values of the numeric indices ought to be, depending on your understanding of the
+ * word "lexical" in the context of sequencing. So the SDO specification at the 2.0.1 level (and before) says
+ * <i>The order of Properties in Type.getDeclaredProperties() is the lexical order of declarations in the
+ * XML Schema ComplexType.</i>. So if your background is in computing and in particular in lexical parsing
+ * then you would understand this to mean "the sequence of tokens produced by the lexical analyis (first) phase of a compiler"
+ * and if you ran a lexical parser against the complex type then you would see the elements emerege in the order they were
+ * written down. If however you think more in terms of lexical in the context of lexicons or dictionaries, you may
+ * expect the indices to correspond to the lexically sorted (essentially alphabetically sorted) list of element names.
+ * After some investigation it is understood that the intention of the spec is to convey the first of these meanings.
+ * A clarification in the SDO 2.1 spec is being sought, but may not make it into that document in time.
+ *
+ * @author Robbie Minshall
+ */
+
+public class AccessingDataObjectsViaPropertyIndex {
+
+ /**
+ * Predefine the property indexes.
+ *
+
+ */
+
+ private static final int COMPANY_DEPARTMENT = 0;
+
+ private static final int COMPANY_EMPLOYEE_OF_MONTH = 1;
+
+ private static final int COMPANY_NAME = 2;
+
+ private static final int DEPARTMENT_EMPLOYEES = 0;
+
+ private static final int EMPLOYEE_NAME = 0;
+ private static final int EMPLOYEE_SN = 1;
+ private static final int EMPLOYEE_MANAGER = 2;
+
+
+ /**
+ * 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 AccessingDataObjectsViaPropertyIndex");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates accessing the properties of a DataObject using property indices.");
+ System.out.println("***************************************");
+
+ // TODO: what happens if not type is defined
+ 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 {
+
+ /**
+ * In this example simply use a company DataObject read in from xml
+ */
+
+ // For DataObjects
+ // Obtain the RootObject from the XMLDocument obtained from the XMLHelper
+ // instance.
+ // In this case the root object is a DataObject representing a company
+ DataObject company = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAOBJECT_XML))
+ .getRootObject();
+
+ // print out some information to show the user what the objects look like
+ System.out.println("Company DataObject:");
+ System.out.println(company);
+
+ String generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("Company data object xml representation: ");
+ System.out.println(generatedXml);
+
+ System.out.println("Setting name of company to MegaCorp");
+ // Set the "name" property for the company
+ company.setString(COMPANY_NAME, "MegaCorp");
+ // Get the list of departments
+ List departments = company.getList(COMPANY_DEPARTMENT);
+ // Get the department at index 0 on the list
+ DataObject department = (DataObject) departments.get(0);
+ // Get the list of employees for the department
+ List employees = department.getList(DEPARTMENT_EMPLOYEES);
+ // Get the employee at index 1 on the list
+ DataObject employeeFromList = (DataObject) employees.get(2);
+
+ // remove the employee from the graph
+ System.out.println("Removing employee " + employeeFromList.getString("name") + " from list of employees");
+ employeeFromList.detach();
+
+ // create a new employee
+ System.out.println("Creating new employee (manager) Al Smith and adding to list");
+ DataObject newEmployee = department.createDataObject(DEPARTMENT_EMPLOYEES);
+
+ /**
+ * Properties from Type.getDeclaredProperties, or Type.getProperties
+ * should be in lexical (alphanumerical) ordering of the xml schema
+ * complex type.
+ */
+
+ newEmployee.set(EMPLOYEE_NAME, "Al Smith");
+ newEmployee.set(EMPLOYEE_SN, "E0005");
+ newEmployee.setBoolean(EMPLOYEE_MANAGER, true);
+
+ System.out.println("setting employee of the month to new employee");
+ company.set(COMPANY_EMPLOYEE_OF_MONTH, newEmployee.get(EMPLOYEE_SN));
+
+ // print out some information to show the user what the objects look like
+ System.out.println("The modified company DataObject :");
+ System.out.println(company);
+
+ generatedXml = XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company");
+ System.out.println("Company data object xml representation: ");
+ System.out.println(generatedXml);
+
+ } 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/specExampleSection/AccessingTheContentsOfASequence.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java
new file mode 100644
index 0000000000..04ef5e704f
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java
@@ -0,0 +1,194 @@
+/**
+ *
+ * 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.specExampleSection;
+
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+import commonj.sdo.Sequence;
+import commonj.sdo.Property;
+
+/**
+ * Demonstrates accessing the sequence from a DataObject containing mixed content.
+ *
+ * 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>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ *
+ * Specifically the following sample is based upon the 'Accessing the contents of a Sequence' example from the Examples section of the SDO specification. <br>
+ * <br>
+ * The following code uses the Sequence interface to analyze the contents of a DataObject that conforms to teh Letter model defined in
+ * {@link #LETTER_XSD}. This code first goes through the Sequence looking for unformatted text entrires and prints them out. Then the code checks to
+ * verify that the contents of teh "lastName" property of the DataObject matches the contents of the same property of the Sequence. <br>
+ * <br>
+ * This sample reads an xml file (@link #LETTER_XML} representing a DataObject of a letter. In order to create a DataObject or DataGraph this sample
+ * relies upon XMLHelper class which is an example of a XML DAS. The code shown here would work just as well against an equivalent DataObject that was
+ * provided by any DAS. <br>
+ *
+ * The XSD for {@link #LETTER_XSD} defines the FormLetter complex type as mixed. Mixed content and sequences are often used for semi structured business data.
+ * <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.specExampleSection.AccessingTheContentsOfASequence</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+
+public class AccessingTheContentsOfASequence {
+
+ /**
+ * previously created XSD file used
+ */
+ public static final String LETTER_XSD = "letter.xsd";
+
+ /**
+ * previously created XML file used
+ */
+ public static final String LETTER_XML = "letter.xml";
+
+ /**
+ * 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 AccessingTheContentsOfASequence");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates accessing the sequence from a DataObject containing mixed content.");
+ System.out.println("***************************************");
+
+ try {
+ // define model
+ System.out.println("Defining Types using XSD");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(LETTER_XSD), null);
+
+ // define letter data object
+ System.out.println("Loading Letter DataObject from XML");
+ DataObject letter = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(LETTER_XML)).getRootObject();
+
+ // print letter sequence
+ System.out.println("Printing sequence for " + letter);
+ System.out.println(XMLHelper.INSTANCE.save(letter, "letter.xsd", "letter"));
+ printSequence(letter);
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+
+ }
+
+ /**
+ * Uses the Sequence interface to analyze the contents of a DataObject that conforms to teh Letter model defined in
+ * {@link #LETTER_XSD}. This code first goes through the Sequence looking for unformatted text entrires and prints them out. Then the code checks
+ * to verify that the contents of the "lastName" property of the DataObject matches the contents of the same property of the Sequence. <br>
+ *
+ * @param letter. Letter DataObject conforming to {@link #LETTER_XSD}
+ */
+ public static void printSequence(DataObject letter) {
+ // Access the Sequence of the FormLetter
+ System.out.println("The type is for letter dataObject is mixed " + XSDHelper.INSTANCE.isMixed(letter.getType()));
+
+ Sequence letterSequence = letter.getSequence();
+ // Print out all the settings that contain unstructured text
+ System.out.println("Unstructured text:");
+ for (int i = 0; i < letterSequence.size(); i++) {
+
+ /*
+ * Please note that the following line is a correction to the 2.0 specification which incorrectly calls:
+ *
+ * String propertyName = ((Property) letterSequence.getProperty(i)).getName();
+ *
+ * According to the SDO API sequence.getProperty will return null if the content is mixed, in this case
+ * we want to print it out as unstructured text
+ */
+ Property prop = letterSequence.getProperty(i);
+ if (prop == null) {
+ String text = (String) letterSequence.getValue(i);
+ System.out.println("\t(" + text + ")");
+ }
+
+ }
+
+ /*
+ * Please note that the following line is a correction to the 2.0 Specification which incorrectly uses letterDataObject variable rather than
+ * simply letter
+ */
+ // Verify that the lastName property of the DataObject has the same
+ // value as the lastName property for the Sequence.
+ String dataObjectLastName = letter.getString("lastName");
+ for (int i = 0; i < letterSequence.size(); i++) {
+
+ /*
+ * The following line has been corrected from the 2.0 specification
+ * According to the SDO API sequence.getProperty will return null if the content is mixed.
+ * We want to check that the content is not mixed, and then check that it it is the property which
+ * we are looking for
+ */
+ Property property = letterSequence.getProperty(i);
+
+ if ( (property != null) && ("lastName".equals(property.getName()))) {
+ String sequenceLastName = (String) letterSequence.getValue(i);
+ if (dataObjectLastName == sequenceLastName)
+ System.out.println("Last Name property matches");
+ break;
+ }
+ }
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java
new file mode 100644
index 0000000000..53fc82e06d
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java
@@ -0,0 +1,136 @@
+/**
+ *
+ * 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.specExampleSection;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates creating a DataObject from an XML String.
+ * <br>
+ * 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>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * Specifically this sample is based upon the 'Creating DataObject Trees from XML
+ * documents' example from the Examples section of the SDO Specification. It is
+ * possible to convert to and from XML documents to build DataObject trees, which is
+ * useful when assembling DataObjects from serval data sources. This sample uses the
+ * XMLHelper to convert xml Strings to DataObjects. <br>
+ * <br>
+ * Uses previously defined model
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} to
+ * define types then generates a purchase order DataObject and persists to
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_GENERATED}
+ * <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.specExampleSection.CreatingDataObjectTreesFromXMLDocuments</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ * @see {@link org.apache.tuscany.samples.sdo.otherSources.CreatePurchaseOrder}
+ */
+
+public class CreatingDataObjectTreesFromXMLDocuments {
+
+ /**
+ * Drives sample
+ *
+ * @param args.
+ * none required.
+ */
+ public static void main(String[] args) {
+ try {
+
+ System.out.println("***************************************");
+ System.out.println("SDO Sample CreatingDataObjectTreesFromXMLDocuments");
+ System.out.println("***************************************");
+
+ // use xsd to define purchase order types
+ System.out.println("Definging purchase order types using " + SdoSampleConstants.PO_XSD_RESOURCE);
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ System.out.println("Defined Types using xsd");
+
+ System.out.println("Creating purchase order DataObject using previously created " + SdoSampleConstants.PO_XML_RESOURCE);
+ DataObject purchaseOrder = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE))
+ .getRootObject();
+
+ // To create the shipTo DataObject from XML:
+ String shipToXML = "<shipTo country='US'>" + " <name>Alice Smith</name>" + " <street>123 Maple Street</street>"
+ + " <city>Mill Valley</city>" + " <state>PA</state>" + " <zip>90952</zip>" + "</shipTo>";
+ DataObject shipTo = XMLHelper.INSTANCE.load(shipToXML).getRootObject();
+
+ // Modify shipTo DataObject on purchaseOrder to value created from a
+ // String
+ System.out.println("Modifying shipTo DataObject on purchaseOrder to value created from a String");
+ purchaseOrder.set("shipTo", shipTo);
+
+ // Obtain and convert the billTo DataObject to XML
+ System.out.println("Obtaining billTo dataObject from purchase order and converting to XML");
+ DataObject billTo = purchaseOrder.getDataObject("billTo");
+ String billToXML = XMLHelper.INSTANCE.save(billTo, null, "billTo");
+ System.out.println("billTo DataObject:");
+ System.out.println(billToXML);
+
+ } catch (Exception e) {
+ System.out.println("Sorry an error occured " + 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/specExampleSection/CreatingXmlFromDataObjects.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingXmlFromDataObjects.java
new file mode 100644
index 0000000000..3c5e147677
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingXmlFromDataObjects.java
@@ -0,0 +1,160 @@
+/**
+ *
+ * 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.specExampleSection;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates programmatically creating a DataObject and generating an XML String.
+ * <br>
+ * 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>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * Specifically this sample is based upon the 'Creating XML from DataObjects' example
+ * from the SDO specification's Examples section. <br>
+ * <br>
+ * Uses previously defined model
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XSD_RESOURCE} to
+ * define types then generates a purchase order DataObject and persists to
+ * {@link org.apache.tuscany.samples.sdo.SdoSampleConstants#PO_XML_GENERATED}
+ * <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.specExampleSection.CreatingXmlFromDataObjects</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ * @see {@link org.apache.tuscany.samples.sdo.otherSources.CreatePurchaseOrder}
+ */
+
+public class CreatingXmlFromDataObjects {
+
+ /**
+ * Drives sample
+ *
+ * @param args.
+ * none required.
+ */
+ public static void main(String[] args) {
+ try {
+
+ System.out.println("***************************************");
+ System.out.println("SDO Sample CreatingXmlFromDataObjects");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates programmatically creating a DataObject and generating an XML String");
+ System.out.println("***************************************");
+
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ System.out.println("Defined Types using xsd");
+ DataObject purchaseOrder = DataFactory.INSTANCE.create(SdoSampleConstants.PO_NAMESPACE, "PurchaseOrderType");
+ System.out.println("Created DataObject using DataFactory");
+
+ System.out.println("Populating purchase order data object");
+
+ purchaseOrder.setString("orderDate", "1999-10-20");
+
+ DataObject shipTo = purchaseOrder.createDataObject("shipTo");
+ shipTo.set("country", "US");
+ shipTo.set("name", "Alice Smith");
+ shipTo.set("street", "123 Maple Street");
+ shipTo.set("city", "Mill Valley");
+ shipTo.set("state", "CA");
+ shipTo.setString("zip", "90952");
+ DataObject billTo = purchaseOrder.createDataObject("billTo");
+ billTo.set("country", "US");
+ billTo.set("name", "Robert Smith");
+ billTo.set("street", "8 Oak Avenue");
+ billTo.set("city", "Mill Valley");
+ billTo.set("state", "PA");
+ billTo.setString("zip", "95819");
+ purchaseOrder.set("comment", "Hurry, my lawn is going wild!");
+
+ DataObject items = purchaseOrder.createDataObject("items");
+
+ DataObject item1 = items.createDataObject("item");
+ item1.set("partNum", "872-AA");
+ item1.set("productName", "Lawnmower");
+ item1.setInt("quantity", 1);
+ item1.setString("price", "148.95");
+
+ item1.set("comment", "Confirm this is electric");
+
+ DataObject item2 = items.createDataObject("item");
+ item2.set("partNum", "926-AA");
+ item2.set("productName", "Baby Monitor");
+ item2.setInt("quantity", 1);
+ item2.setString("price", "39.98");
+ item2.setString("shipDate", "1999-05-21");
+
+ OutputStream stream = new FileOutputStream(SdoSampleConstants.PO_XML_GENERATED);
+ XMLHelper.INSTANCE.save(purchaseOrder, SdoSampleConstants.PO_NAMESPACE, "purchaseOrder", stream);
+ System.out.println("Created file " + SdoSampleConstants.PO_XML_GENERATED);
+ System.out.println("Populated with :");
+ System.out.println(XMLHelper.INSTANCE.save(purchaseOrder, SdoSampleConstants.PO_NAMESPACE, "purchaseOrder"));
+ } catch (Exception e) {
+ System.out.println("Sorry an error occured " + 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/specExampleSection/SerializingDesearializingADataObject.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/SerializingDesearializingADataObject.java
new file mode 100644
index 0000000000..97772eaf33
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/SerializingDesearializingADataObject.java
@@ -0,0 +1,183 @@
+/**
+ *
+ * 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.specExampleSection;
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.helper.EqualityHelper;
+import commonj.sdo.DataObject;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.InputStreamReader;
+import java.io.ObjectInputStream;
+import java.io.FileOutputStream;
+import java.io.ObjectOutputStream;
+import java.io.IOException;
+
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates serializing and deserializing a DataObject to disk.
+ *
+ * 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>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * The following sample is based upon the 'Serializing/Deserializing a DataGraph or DataObject' example from the Examples section of the SDO
+ * specification. <br>
+ * <br>
+ * The DataObject and DataGraph interfaces extend java.io.Serializable, so any DataObject
+ * or DataGraph be serialized. The following code serializes the company dataObject to a file
+ * of the user's choosing. The file is not deleted after execution.<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 Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.SerializingDesearializingADataObject</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ */
+
+public class SerializingDesearializingADataObject {
+
+ /**
+ * Default fileName to serialize DataObject to
+ */
+ public static final String DEFAULT_FILE_NAME = "temporarySerializedDataObject.xml";
+
+ public static void serializeDO(DataObject DataObject, String fileName) throws IOException {
+ // serialize data object
+ FileOutputStream fos = new FileOutputStream(fileName);
+ ObjectOutputStream out = new ObjectOutputStream(fos);
+ out.writeObject(DataObject);
+ out.close();
+ }
+
+ /**
+ * 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 AccessingTheContentsOfASequence");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates serializing and deserializing a DataObject to disk.");
+ System.out.println("***************************************");
+
+
+ try {
+ // define Types using XSDHelper and predefined xsd file
+ System.out.println("Defining Types using XSD");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD), null);
+ System.out.println("Type definition completed");
+
+ // create company DataObject using XMLHelper which is an example of a XML DAS
+ System.out.println("Creating company DataObject");
+ DataObject company = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAOBJECT_XML))
+ .getRootObject();
+
+ // prompt user for a fileName
+ BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ System.out.print("Please enter file name, enter for default :");
+ String fileName = in.readLine();
+ if ( (fileName == null) || (fileName.equals(""))){
+ fileName = DEFAULT_FILE_NAME;
+ }
+
+ // serialize dataObject out
+ System.out.println("Serializing DataObject to " + fileName);
+ FileOutputStream fos = new FileOutputStream(fileName);
+ ObjectOutputStream out = new ObjectOutputStream(fos);
+ out.writeObject(company);
+ out.close();
+
+ // read in DataObject
+ System.out.println("Deserialize DataObject from file");
+ FileInputStream fis = new FileInputStream(fileName);
+ ObjectInputStream input = new ObjectInputStream(fis);
+ DataObject newDataObject = (DataObject) input.readObject();
+ input.close();
+
+ /**
+ * Compare DataObjects
+ */
+
+ System.out.println("Comparing original and deserialized DataObject");
+ //determine if two graphs of DataObjects are equal
+ System.out.println("DataObjects are equal: " + EqualityHelper.INSTANCE.equal(company, newDataObject));
+
+ //determine if two DataObjects have the same values for their datatype properties
+ System.out.println("DataObjects have same values for their datatype properties : " + EqualityHelper.INSTANCE.equalShallow(company, newDataObject));
+
+ //print out xml representation
+ System.out.println();
+ System.out.println("Original company DataObject:");
+ System.out.println(XMLHelper.INSTANCE.save(company, SdoSampleConstants.COMPANY_NAMESPACE, "company"));
+
+ System.out.println();
+ System.out.println("Deserialized company DataObject:");
+ System.out.println(XMLHelper.INSTANCE.save(newDataObject, SdoSampleConstants.COMPANY_NAMESPACE, "company"));
+
+ } 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/specExampleSection/UsingTypeAndPropertyWithDataObjects.java b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java
new file mode 100644
index 0000000000..f93b716fb5
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java
@@ -0,0 +1,190 @@
+/**
+ *
+ * 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.specExampleSection;
+
+import commonj.sdo.helper.XMLHelper;
+import commonj.sdo.helper.XSDHelper;
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.Property;
+
+import java.util.List;
+
+import org.apache.tuscany.samples.sdo.SdoSampleConstants;
+
+/**
+ * Demonstrates the use of Type and Property to display and the META-DATA for and access Properties of a DataObject.
+ * <br>
+ * 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>. In general this sample attempts to use the
+ * code and comments contained within the specification, exceptions to this are noted
+ * in comments.<br>
+ * <br>
+ * The following sample is based upon the 'Using Type and Property with DataObjects' example from the Examples section of the SDO specification. <br>
+ * <br>
+ * The Type interface provides access to the metadata for DataObjects. The methods on Type and Property provide information that describes the
+ * properties of a DataObject. To obtain the Type for a DataObject use the getType() method. This example prints out the contents of a DataObject,
+ * each property has it's meta-data displayed, accessed dynamically using Type and Property.<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 Dependancies </a> </LI>
+ * <LI>Execute: <br>
+ * java org.apache.tuscany.samples.sdo.specExampleSection.UsingTypeAndPropertyWithDataObjects</LI>
+ * </UL>
+ *
+ * @author Robbie Minshall
+ * @see org.apache.tuscany.samples.sdo.specCodeSnippets.PrintPropertiesOfDataObject
+ */
+
+public class UsingTypeAndPropertyWithDataObjects {
+
+ /**
+ * 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 UsingTypeAndPropertyWithDataObjects");
+ System.out.println("***************************************");
+ System.out.println("Demonstrates the use of Type and Property to display and the META-DATA for and access Properties of a DataObject.");
+ System.out.println("***************************************");
+
+ try {
+ // define Types using XSDHelper and predefined xsd file
+ System.out.println("Defining company Types using XSD");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_XSD), null);
+ System.out.println("Type definition completed");
+
+ // create company DataObject using XMLHelper which is an example of a XML DAS
+ System.out.println("Creating company DataObject");
+ DataObject company = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.COMPANY_DATAOBJECT_XML))
+ .getRootObject();
+
+ printDataObject(company, 0);
+
+ System.out.println("Defining purchase order types");
+ XSDHelper.INSTANCE.define(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XSD_RESOURCE), null);
+ System.out.println("Type definition completed");
+
+ // create company DataObject using XMLHelper which is an example of a XML DAS
+ System.out.println("Creating purchase order DataObject");
+ DataObject po = XMLHelper.INSTANCE.load(ClassLoader.getSystemResourceAsStream(SdoSampleConstants.PO_XML_RESOURCE))
+ .getRootObject();
+
+ printDataObject(po, 0);
+
+
+
+
+
+
+
+ } catch (Exception e) {
+ System.out.println("Sorry there was an error encountered " + e.toString());
+ e.printStackTrace();
+ }
+ System.out.println("GoodBye");
+ }
+
+ /**
+ * Prints out the contents of a DataObject, each property has it's meta-data displayed, accessed dynamically using Type and Property.
+ * @param dataObject. DataObject to be printed.
+ * @param indent. For formatting
+ */
+ public static void printDataObject(DataObject dataObject, int indent) {
+ // For each Property
+ List properties = dataObject.getInstanceProperties();
+ for (int p = 0, size = properties.size(); p < size; p++) {
+ if (dataObject.isSet(p)) {
+ Property property = (Property) properties.get(p);
+ if (property.isMany()) {
+ // For many-valued properties, process a list of values
+ List values = dataObject.getList(p);
+ for (int v = 0, count = values.size(); v < count; v++) {
+ printValue(values.get(v), property, indent);
+ }
+ } else {
+ // For single-valued properties, print out the value
+ printValue(dataObject.get(p), property, indent);
+ }
+ }
+ }
+ }
+
+ /**
+ * Prints the value of a property
+ * @param value. Value of Property to be printed
+ * @param property. Property to be printed
+ * @param indent. For formatting
+ */
+ public static void printValue(Object value, Property property, int indent) {
+ // Get the name of the property
+ String propertyName = property.getName();
+ // Construct a string for the proper indentation
+ String margin = "";
+ for (int i = 0; i < indent; i++)
+ margin += "\t";
+ if (value != null && property.isContainment()) {
+ // For containment properties, display the value
+ // with printDataObject
+ Type type = property.getType();
+ String typeName = type.getName();
+ System.out.println(margin + propertyName + " (" + typeName + "):");
+ printDataObject((DataObject) value, indent + 1);
+ } else {
+ // For non-containment properties, just print the value
+ System.out.println(margin + propertyName + ": " + value);
+ }
+ }
+
+}
diff --git a/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/package.html b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/package.html
new file mode 100644
index 0000000000..0aa2a9bcb2
--- /dev/null
+++ b/sdo-java/branches/sdo-java-M2/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/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 samples from the Examples section of the SDO Specification. In general these samples attempt to use the
+code and comments contained within the specification; exceptions to this are noted in comments.
+ 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>