You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ke...@apache.org on 2007/07/04 18:38:30 UTC
svn commit: r553262 - in
/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo:
./ advanced/ intermediate/ specExampleSection/
Author: kelvingoodson
Date: Wed Jul 4 09:38:28 2007
New Revision: 553262
URL: http://svn.apache.org/viewvc?view=rev&rev=553262
Log:
More sample cleanup -- primary changes are updating the accessing via int index sample, and updating the PrintDataGraph code to handle xml attributes when traversing a sequence from a type that was derived from an XSD
Added:
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessingDataObjectsViaPropertyIndex.java
- copied, changed from r553203, incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java
Removed:
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java
Modified:
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleInfrastructure.java
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/MedicalScenario.java
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/PrintDataGraph.java
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java
incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java
Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java?view=diff&rev=553262&r1=553261&r2=553262
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples.java Wed Jul 4 09:38:28 2007
@@ -27,11 +27,11 @@
import org.apache.tuscany.samples.sdo.basic.AccessDataObjectPropertiesByName;
import org.apache.tuscany.samples.sdo.basic.CreatePurchaseOrder;
import org.apache.tuscany.samples.sdo.basic.ReadPurchaseOrder;
+import org.apache.tuscany.samples.sdo.intermediate.AccessingDataObjectsViaPropertyIndex;
import org.apache.tuscany.samples.sdo.intermediate.CreateDataObjectFromXmlString;
import org.apache.tuscany.samples.sdo.intermediate.DynamicCustomerTypeSample;
import org.apache.tuscany.samples.sdo.intermediate.SerializingDeserializingADataObject;
import org.apache.tuscany.samples.sdo.otherSources.PurchaseOrderCmdLine;
-import org.apache.tuscany.samples.sdo.specExampleSection.AccessingDataObjectsViaPropertyIndex;
import org.apache.tuscany.samples.sdo.specExampleSection.AccessingTheContentsOfASequence;
import org.apache.tuscany.samples.sdo.specExampleSection.CreatingDataObjectTreesFromXMLDocuments;
import org.apache.tuscany.samples.sdo.specExampleSection.UsingTypeAndPropertyWithDataObjects;
Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java?view=diff&rev=553262&r1=553261&r2=553262
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/ExecuteSamples2.java Wed Jul 4 09:38:28 2007
@@ -29,11 +29,15 @@
import org.apache.tuscany.samples.sdo.advanced.ObtainingDataGraphFromXml;
import org.apache.tuscany.samples.sdo.advanced.PrintDataGraph;
import org.apache.tuscany.samples.sdo.basic.AccessDataObjectPropertiesByName;
+import org.apache.tuscany.samples.sdo.basic.CreateCompany;
import org.apache.tuscany.samples.sdo.basic.CreatePurchaseOrder;
import org.apache.tuscany.samples.sdo.basic.ReadPurchaseOrder;
import org.apache.tuscany.samples.sdo.intermediate.AccessDataObjectUsingValidXPath;
+import org.apache.tuscany.samples.sdo.intermediate.AccessingDataObjectsViaPropertyIndex;
+import org.apache.tuscany.samples.sdo.intermediate.CreateCompanyTuscanyAPI;
import org.apache.tuscany.samples.sdo.intermediate.CreateDataObjectFromXmlString;
import org.apache.tuscany.samples.sdo.intermediate.DynamicCustomerTypeSample;
+import org.apache.tuscany.samples.sdo.intermediate.SerializingDeserializingADataObject;
/**
* A Program to execute a sequence of SDO sample programs.
@@ -46,15 +50,19 @@
* Here's the list of sample programs that we are going to execute, in this sequence.
*/
Class[] sampleClasses = {
- org.apache.tuscany.samples.sdo.basic.CreateCompany.class,
+ CreateCompany.class,
ReadPurchaseOrder.class,
CreatePurchaseOrder.class,
AccessDataObjectPropertiesByName.class,
+
AccessDataObjectUsingValidXPath.class,
DynamicCustomerTypeSample.class,
- ObtainingDataGraphFromXml.class,
- org.apache.tuscany.samples.sdo.intermediate.CreateCompanyTuscanyAPI.class,
+ CreateCompanyTuscanyAPI.class,
CreateDataObjectFromXmlString.class,
+ SerializingDeserializingADataObject.class,
+ AccessingDataObjectsViaPropertyIndex.class,
+
+ ObtainingDataGraphFromXml.class,
PrintDataGraph.class,
MedicalScenario.class
@@ -76,6 +84,10 @@
* Edit this to run fewer samples.
*/
int runSamplesUpToLevel = SampleInfrastructure.SAMPLE_LEVEL_ADVANCED.intValue();
+ /**
+ * Edit this value to cause the program to pause between each sample program
+ * and wait for the user to hit the enter key.
+ */
boolean pauseBetweenSamples = false;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleInfrastructure.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleInfrastructure.java?view=diff&rev=553262&r1=553261&r2=553262
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleInfrastructure.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/SampleInfrastructure.java Wed Jul 4 09:38:28 2007
@@ -121,6 +121,11 @@
public void banner(String text) {
banner('-', text);
}
+
+ protected void commentary(String text, String repeatText) {
+ commentary(new Integer(sampleComplexityLevel), text, repeatText);
+
+ }
protected void commentary(Integer commentLevel, String text, String repeatText) {
Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/MedicalScenario.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/MedicalScenario.java?view=diff&rev=553262&r1=553261&r2=553262
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/MedicalScenario.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/MedicalScenario.java Wed Jul 4 09:38:28 2007
@@ -25,10 +25,8 @@
import org.apache.tuscany.samples.sdo.SampleBase;
import org.apache.tuscany.sdo.api.SDOUtil;
-
import commonj.sdo.DataObject;
import commonj.sdo.Property;
-import commonj.sdo.Sequence;
import commonj.sdo.Type;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.HelperContext;
@@ -47,7 +45,7 @@
private static final String medicalURI = "www.example.org/MedicalTest";
- boolean typesViaAPI = true;
+ boolean typesViaAPI = false;
private static final String usage = " [-api |-xsd]\n"
+ " -api : define the types using the SDO API\n"
@@ -252,9 +250,12 @@
+ "relationship to Joe Johnson Jnr didn't displace him from the\n"
+ "set of 'relatives', because the 'target' Property is a\n"
+ "non-containment Property. This non-containment relationship\n"
- + "is reflected in the XML by the reference to the unique identifier\n"
- + "for Joe Johnson Jnr, \"2\" ...\n\n"
- + " ...\n <relative target=\"2\" relationship=\"child\"/>\n ...\n");
+ + "is reflected in the XML by a reference to the Person DataObject\n"
+ + "describing Joe Johnson Jnr, \"2\" ...\n"
+ + "If the Type system has been created from an XML schema then the\n"
+ + "unique ID of the target can be used in the serialization.\n"
+ + "If however the type system was defined dynamically, then the reference\n"
+ + "will be represented as an XPath from the root of the data graph.");
System.out.println(scope.getXMLHelper().save(test,
"www.example.org/MedicalTest", "test"));
Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/PrintDataGraph.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/PrintDataGraph.java?view=diff&rev=553262&r1=553261&r2=553262
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/PrintDataGraph.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/advanced/PrintDataGraph.java Wed Jul 4 09:38:28 2007
@@ -27,6 +27,8 @@
import org.apache.tuscany.samples.sdo.SampleBase;
import org.apache.tuscany.samples.sdo.SampleInfrastructure;
+import test.DefaultHelperProvider;
+
import commonj.sdo.DataObject;
import commonj.sdo.Property;
@@ -34,6 +36,7 @@
import commonj.sdo.Type;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.XMLDocument;
+import commonj.sdo.helper.XSDHelper;
/**
*
@@ -46,6 +49,7 @@
public class PrintDataGraph extends SampleBase {
StringBuffer buf = null;
+ HelperContext scope = DefaultHelperProvider.getDefaultContext();
private int indent;
@@ -118,8 +122,6 @@
printXMLDocument((XMLDocument) sdoObject);
} else if (sdoObject instanceof DataObject) {
printDataObject((DataObject) sdoObject);
- } else if (sdoObject instanceof Sequence) {
- printSequence((Sequence) sdoObject);
}
}
@@ -186,8 +188,66 @@
"We've encountered another sequenced DataObject instance, and so will traverse the Property\n"
+ "values in the order preerved by the instance, as we saw before\n\n"
+ "dataObject.getType().isSequenced();");
+
+ commentary(
+ "There's a subtlety here which we must deal with if this sample code is to\n" +
+ "handle both Type systems that derive from XML schema, and those that come from elsewhere,\n" +
+ "e.g. using the SDO API. If a Sequenced DataObject has a Type that comes from XML schema\n" +
+ "then its Properties that derive from XML attributes are not ordered, whereas those that\n" +
+ "derive from XML elements are ordered. The SDO specification doesn't say whether\n" +
+ "the attribute related Properties should appear at the start of a Sequence or not.\n" +
+ "Currently in Tuscany we leave them out of the Sequence; other SDO implementations may\n" +
+ "include the XML attributes in the Sequence. This sample code is written to deal with\n" +
+ "either approach\n." +
+ "We use the XSDHelper.isAttribute(Property) and isElement(Property) methods to distinguish\n" +
+ "between the two kinds of Property",
+
+ "Examining the xml attributes and elements of a Sequenced DataObject again."
+ );
+
+ XSDHelper xsdHelper = getScope().getXSDHelper();
+ incrementIndent();
+ for(Iterator it=dataObject.getInstanceProperties().iterator(); it.hasNext();) {
+ Property property = (Property)it.next();
+ if (xsdHelper.isAttribute(property)) {
+ indent();
+ buf.append("Property (XML Attribute): ").append(property.getName()).append(" - ").append(dataObject.get(property));
+ lineBreak();
+ }
- printSequence(dataObject.getSequence());
+ }
+ decrementIndent();
+ Sequence seq = dataObject.getSequence();
+
+ commentary(
+ "The Property/Value pairs of a Sequence can be accessed via the getProperty(int) and getValue(int)\n"
+ + "accessor methods of the Sequence interface. The size() method of the Sequence tells us how many there are.\n"
+ + "If the getProperty(int) method retunes null, then the value is text. These text values may be encountered\n"
+ + "when the DataObject's type is 'mixed' (dataObject.getType().isMixed() == true). A typical example of this\n"
+ + "is when the data graph represents a form letter.",
+
+ "Inspecting the Property/Value pairs of another Sequence");
+
+ incrementIndent();
+ indent();
+ buf.append("Sequence: {\n");
+
+ incrementIndent();
+ for (int i = 0; i < seq.size(); i++) {
+ Property p = seq.getProperty(i);
+ if (p == null) {
+ indent();
+ buf.append("text: ").append(seq.getValue(i));
+ lineBreak();
+ } else if(!xsdHelper.isAttribute(p)){
+ printPropertyValuePair(p, seq.getValue(i));
+ }
+ }
+ decrementIndent();
+
+ indent();
+ buf.append("}\n");
+ decrementIndent();
} else {
incrementIndent();
@@ -222,39 +282,7 @@
- public void printSequence(Sequence seq) {
- commentary(
- COMMENTARY_FOR_INTERMEDIATE,
- "The Property/Value pairs of a Sequence can be accessed via the getProperty(int) and getValue(int)\n"
- + "accessor methods of the Sequence interface. The size() method of the Sequence tells us how many there are.\n"
- + "If the getProperty(int) method retunes null, then the value is text. These text values may be encountered\n"
- + "when the DataObject's type is 'mixed' (dataObject.getType().isMixed() == true). A typical example of this\n"
- + "is when the data graph represents a form letter.",
-
- "Inspecting the Property/Value pairs of another Sequence");
-
- incrementIndent();
- indent();
- buf.append("Sequence: {\n");
-
- incrementIndent();
- for (int i = 0; i < seq.size(); i++) {
- Property p = seq.getProperty(i);
- if (p == null) {
- indent();
- buf.append("text: ").append(seq.getValue(i));
- lineBreak();
- } else {
- printPropertyValuePair(p, seq.getValue(i));
- }
- }
- decrementIndent();
-
- indent();
- buf.append("}\n");
- decrementIndent();
- }
private void printPropertyValuePair(Property p, Object value) {
@@ -440,6 +468,14 @@
public void setBuf(StringBuffer b) {
buf = b;
+ }
+
+ public HelperContext getScope() {
+ return scope;
+ }
+
+ public void setScope(HelperContext scope) {
+ this.scope = scope;
}
}
Copied: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessingDataObjectsViaPropertyIndex.java (from r553203, incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java)
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessingDataObjectsViaPropertyIndex.java?view=diff&rev=553262&p1=incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java&r1=553203&p2=incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessingDataObjectsViaPropertyIndex.java&r2=553262
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingDataObjectsViaPropertyIndex.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/intermediate/AccessingDataObjectsViaPropertyIndex.java Wed Jul 4 09:38:28 2007
@@ -18,120 +18,44 @@
* under the License.
*/
-package org.apache.tuscany.samples.sdo.specExampleSection;
+package org.apache.tuscany.samples.sdo.intermediate;
-import java.io.InputStream;
import java.util.List;
import org.apache.tuscany.samples.sdo.SampleBase;
import org.apache.tuscany.samples.sdo.SampleInfrastructure;
+import org.apache.tuscany.samples.sdo.advanced.PrintDataGraph;
import commonj.sdo.DataObject;
import commonj.sdo.helper.HelperContext;
/**
* 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.SampleInfrastructure#COMPANY_XSD} which is
- * provided in the resources directory of these samples <br>
- * The xml file
- * {@link org.apache.tuscany.samples.sdo.SampleInfrastructure#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.basic.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 by downloading and unpacking a <a href="http://cwiki.apache.org/TUSCANY/sdo-downloads.html" target="_blank">Tuscany binary distribution </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 extends SampleBase {
HelperContext scope;
+ PrintDataGraph printer;
public AccessingDataObjectsViaPropertyIndex(Integer userLevel) {
- super(userLevel, SAMPLE_LEVEL_BASIC);
+ /*
+ * Classifying this sample as intermediate, not because it is complex
+ * but because it's a scenario not to be encouraged for use by
+ * people new to SDO.
+ */
+ super(userLevel, SAMPLE_LEVEL_INTERMEDIATE);
+ printer = new PrintDataGraph(SAMPLE_LEVEL_ADVANCED);
}
/**
* 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 COMPANY_NAME = 1;
+ private static final int COMPANY_EMPLOYEE_OF_MONTH = 2;
private static final int DEPARTMENT_EMPLOYEES = 0;
@@ -146,107 +70,83 @@
* @param args
*/
public static void main(String[] args) {
- // TODO make the default level COMMENTARY_FOR_NOVICE, once the rest of the sample has been
- // converted to using commentary()
- AccessingDataObjectsViaPropertyIndex sample = new AccessingDataObjectsViaPropertyIndex(COMMENTARY_FOR_INTERMEDIATE);
-
- try {
- sample.run();
- }
- catch (Exception e) {
- sample.somethingUnexpectedHasHappened(e);
- }
+ AccessingDataObjectsViaPropertyIndex sample =
+ new AccessingDataObjectsViaPropertyIndex(COMMENTARY_FOR_NOVICE);
+
+ sample.run();
+
}
public void runSample () throws Exception {
- 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("***************************************");
- scope = createScopeForTypes();
+
+ commentary(
+ "This sample demonstrates the use of integer index to get and set\n" +
+ "Property values of a DataObject. This approach is optimized for\n" +
+ "performance, but is fragile to changes to the Type system. It will be\n" +
+ "broken if someone alters the XML schema that is used to load the type system." +
+ "This kind of approach is well suited to a situation where the SDO code is being\n" +
+ "generated.");
+
+ scope = createScopeForTypes();
- // TODO: what happens if not type is defined
- try {
- System.out.println("Defining Types using XSD");
- InputStream is = null;
- is = ClassLoader.getSystemResourceAsStream(SampleInfrastructure.COMPANY_XSD);
- scope.getXSDHelper().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 = scope.getXMLHelper().load(ClassLoader.getSystemResourceAsStream(SampleInfrastructure.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 = scope.getXMLHelper().save(company, SampleInfrastructure.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 = scope.getXMLHelper().save(company, SampleInfrastructure.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");
+ loadTypesFromXMLSchemaFile(scope, SampleInfrastructure.COMPANY_XSD);
+
+ DataObject company = getDataObjectFromFile(scope, SampleInfrastructure.COMPANY_DATAOBJECT_XML);
+
+ commentary("We've loaded a data graph that looks like this ...");
+
+ printer.print(company);
+ System.out.println(printer.getBuf());
+ printer.reset();
+
+ commentary("Here's how the data graph looks rendered in XML ...");
+ System.out.println(
+ scope.getXMLHelper().save(company, SampleInfrastructure.COMPANY_NAMESPACE, "company"));
+
+ commentary(
+ "This sample class has primitive int static constants defined for the Properties\n" +
+ "of the Types that the program is designed to work with, e.g.\n\n" +
+ "private static final int COMPANY_NAME = 2;\n\n" +
+ "The value of the integers is defined by the sequence the Properties\n" +
+ "appear in the List returned by dataObject.getType().getDeclaredProperties()\n" +
+ "For a type derived from an XML schema this will be the sequence they appeard in the\n" +
+ "schema document.\n" +
+ "We can use these integer values to get and set Properties on the company DataObject\n\n" +
+ "company.setString(COMPANY_NAME, \"MegaCorp\");\n" +
+ "List departments = company.getList(COMPANY_DEPARTMENT);\n" +
+ "List employees = department.getList(DEPARTMENT_EMPLOYEES);\n");
+
+ company.setString(COMPANY_NAME, "MegaCorp");
+
+
+
+ commentary("The sample continues, altering the data graph by using the getter\n" +
+ "and setter methods that take int arguments");
+
+ List departments = company.getList(COMPANY_DEPARTMENT);
+ DataObject department = (DataObject) departments.get(0);
+ List employees = department.getList(DEPARTMENT_EMPLOYEES);
+ DataObject employeeFromList = (DataObject) employees.get(2);
+ employeeFromList.detach();
+
+ DataObject newEmployee = department.createDataObject(DEPARTMENT_EMPLOYEES);
+
+ newEmployee.set(EMPLOYEE_NAME, "Al Smith");
+ newEmployee.set(EMPLOYEE_SN, "E0005");
+ newEmployee.setBoolean(EMPLOYEE_MANAGER, true);
+
+ company.set(COMPANY_EMPLOYEE_OF_MONTH, newEmployee.get(EMPLOYEE_SN));
+
+ commentary("After some more manipulation (take a look at the sample code to see the detail)\n" +
+ "we can examine the state of the modified graph");
+
+ printer.print(company);
+ System.out.println(printer.getBuf());
+ printer.reset();
+
+ commentary("Or rendered in XML it now looks like this ...");
+ System.out.println(scope.getXMLHelper().save(company, SampleInfrastructure.COMPANY_NAMESPACE, "company"));
}
}
Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java?view=diff&rev=553262&r1=553261&r2=553262
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/AccessingTheContentsOfASequence.java Wed Jul 4 09:38:28 2007
@@ -87,6 +87,7 @@
*/
public class AccessingTheContentsOfASequence extends SampleBase {
+ // FIXME integrate this sample
HelperContext scope;
Modified: incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java?view=diff&rev=553262&r1=553261&r2=553262
==============================================================================
--- incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java (original)
+++ incubator/tuscany/java/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/CreatingDataObjectTreesFromXMLDocuments.java Wed Jul 4 09:38:28 2007
@@ -86,6 +86,7 @@
public class CreatingDataObjectTreesFromXMLDocuments extends SampleBase {
+ // FIXME decide on the fate of this sample
HelperContext scope;
public CreatingDataObjectTreesFromXMLDocuments(Integer userLevel) {
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org