diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-10 19:20:12 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-11-10 19:20:12 +0000 |
commit | 195774c489a1a671aca514b0afa88332bf9c6ee3 (patch) | |
tree | a22cb1f3f70f0c13a9a7049075205072901459aa /sdo-java/tags/1.0-incubating-beta1/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java | |
parent | c54bda13cb403d10675a6244bd00b8b536111ad6 (diff) |
Moving SDO tags
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@834617 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sdo-java/tags/1.0-incubating-beta1/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java')
-rw-r--r-- | sdo-java/tags/1.0-incubating-beta1/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/sdo-java/tags/1.0-incubating-beta1/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java b/sdo-java/tags/1.0-incubating-beta1/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java new file mode 100644 index 0000000000..fd4e142e92 --- /dev/null +++ b/sdo-java/tags/1.0-incubating-beta1/sdo/sample/src/main/java/org/apache/tuscany/samples/sdo/specExampleSection/UsingTypeAndPropertyWithDataObjects.java @@ -0,0 +1,188 @@ +/** + * + * 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 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.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); + } + } + +} |