You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by da...@apache.org on 2004/10/18 17:55:25 UTC
cvs commit: xml-xmlbeans/samples/substitutiongroup/SubstitutionGroup/xml Purchase-Order.xml newPurchase-Order.xml
daveremy 2004/10/18 08:55:25
Added: samples/substitutiongroup/SubstitutionGroup build.xml
samples/substitutiongroup/SubstitutionGroup/schemas
EasyPo.xsd
samples/substitutiongroup/SubstitutionGroup/src/org/apache/xmlbeans/samples/substitutiongroup
SubstitutionGroup.java SubstitutionGroupTest.java
samples/substitutiongroup/SubstitutionGroup/xml
Purchase-Order.xml newPurchase-Order.xml
Log:
Adding SubstitutionGroup sample sources contributed by Rashmi Banthia.
Revision Changes Path
1.1 xml-xmlbeans/samples/substitutiongroup/SubstitutionGroup/build.xml
Index: build.xml
===================================================================
<!--
Copyright 2004 The Apache Software Foundation
Licensed 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.
-->
<project name="SubstitutionGroup" default="build">
<property environment="env"/>
<path id="SubstitutionGroup.path">
<path refid="xmlbeans.path"/>
<fileset dir="build/lib" includes="*.jar"/>
<pathelement path="build/classes"/>
</path>
<target name="init">
<property name="xmlbeans.home" value="${env.XMLBEANS_HOME}"/>
<echo message="xmlbeans.home: ${xmlbeans.home}"/>
<!-- check for xbean.jar from binary distribution -->
<available
property="xmlbeans.lib"
value="${xmlbeans.home}/lib"
file="${xmlbeans.home}/lib/xbean.jar" />
<!-- check for xbean.jar compiled from source -->
<available
property="xmlbeans.lib"
value="${xmlbeans.home}/build/lib"
file="${xmlbeans.home}/build/lib/xbean.jar" />
<fail message="Set XMLBEANS_HOME in your enviornment."
unless="xmlbeans.lib"/>
<echo message="xmlbeans.lib: ${xmlbeans.lib}"/>
<path id="xmlbeans.path">
<fileset dir="${xmlbeans.lib}" includes="*.jar"/>
</path>
<taskdef name="xmlbean"
classname="org.apache.xmlbeans.impl.tool.XMLBean"
classpathref="xmlbeans.path"/>
</target>
<!-- ========================== clean ==== -->
<target name="clean">
<delete dir="build"/>
</target>
<!-- ========================== build ==== -->
<target name="build" depends="init,schemas.jar,SubstitutionGroup.classes">
</target>
<target name="schemas.check">
<uptodate property="schemas.notRequired"
targetfile="build/lib/schemas.jar">
<srcfiles dir="schemas" includes="**/*.xsd"/>
</uptodate>
</target>
<target name="schemas.jar" depends="init,schemas.check"
unless="schemas.notRequired">
<mkdir dir="build/lib"/>
<xmlbean schema="schemas"
destfile="build/lib/schemas.jar"
srcgendir="build/src"
classpathref="xmlbeans.path"
debug="on"
/>
</target>
<target name="SubstitutionGroup.classes" depends="init">
<mkdir dir="build/classes"/>
<javac srcdir="src"
destdir="build/classes"
classpathref="SubstitutionGroup.path"
debug="on"
source="1.4"
/>
</target>
<!-- ========================== run ==== -->
<target name="run" depends="init,build">
<echo message="============================== running SubstitutionGroup"/>
<java
classname="org.apache.xmlbeans.samples.substitutiongroup.SubstitutionGroup"
classpathref="SubstitutionGroup.path"
fork="true">
<arg line="xml/Purchase-Order.xml xml/newPurchase-Order.xml"/>
</java>
</target>
<!-- ========================== test ==== -->
<target name="test" depends="init,build">
<echo message="============================== testing SchemaEnum"/>
<java
classname="org.apache.xmlbeans.samples.substitutiongroup.SubstitutionGroupTest"
classpathref="SubstitutionGroup.path"
fork="true">
<arg line="xml/Purchase-Order.xml xml/newPurchase-Order.xml"/>
</java>
</target>
</project>
1.1 xml-xmlbeans/samples/substitutiongroup/SubstitutionGroup/schemas/EasyPo.xsd
Index: EasyPo.xsd
===================================================================
<!--
Copyright 2004 The Apache Software Foundation
Licensed 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.
-->
<xs:schema targetNamespace="http://xmlbeans.apache.org/samples/substitutiongroup/easypo"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:po="http://xmlbeans.apache.org/samples/substitutiongroup/easypo"
elementFormDefault="qualified" >
<xs:element name="purchase-order">
<xs:complexType>
<xs:sequence>
<xs:element ref="po:invoice-header" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="invoice-header">
<xs:complexType>
<xs:sequence>
<xs:element name="ship-to" type="po:name-address" />
<xs:element name="bill-to" type="po:name-address" />
<xs:element ref="po:product" minOccurs="0" />
<xs:element ref="po:comment" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="name-address">
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="address" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="product-type" abstract="true">
<xs:attribute name="id" type="xs:int" />
</xs:complexType>
<xs:complexType name="book-type">
<xs:complexContent>
<xs:extension base="po:product-type">
<xs:sequence>
<xs:element name="title" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="clothing-type">
<xs:complexContent>
<xs:extension base="po:product-type">
<xs:sequence>
<xs:element name="color">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="red"/>
<xs:enumeration value="blue"/>
<xs:enumeration value="green"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="product" type="po:product-type" abstract="true"/>
<xs:element name="book" type="po:book-type"
substitutionGroup="po:product"/>
<xs:element name="clothing" type="po:clothing-type"
substitutionGroup="po:product"/>
<xs:element name="comment" type="xs:string" />
<xs:element name="ship-comment" type="xs:string" substitutionGroup="po:comment" />
<xs:element name="bill-comment" type="xs:string" substitutionGroup="po:comment" />
</xs:schema>
1.1 xml-xmlbeans/samples/substitutiongroup/SubstitutionGroup/src/org/apache/xmlbeans/samples/substitutiongroup/SubstitutionGroup.java
Index: SubstitutionGroup.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* Licensed 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.xmlbeans.samples.substitutiongroup;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.samples.substitutiongroup.easypo.PurchaseOrderDocument;
import org.apache.xmlbeans.samples.substitutiongroup.easypo.InvoiceHeaderDocument;
import org.apache.xmlbeans.samples.substitutiongroup.easypo.NameAddress;
import org.apache.xmlbeans.samples.substitutiongroup.easypo.BookType;
import org.apache.xmlbeans.samples.substitutiongroup.easypo.ClothingType;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
* This sample illustrates how you can access substitution group element names and
* values defined in the XML document. This sample also demonstrates how to write
* substitution group elements.
*
* The schema used by this sample are defined in EasyPo.xsd
*/
public class SubstitutionGroup
{
/**
* Receives an XML Instance and prints the substitution group element names and values,
* Also creates a new XML Instance.
*
* @param args An array containing
* (a)Path to the XML Instance conforming to the XML schema in EasyPo.xsd.
* (b)Path for creating a new XML Instance.
*/
public static void main(String args[])
{
// Create an instance of this class to work with.
SubstitutionGroup subGrp = new SubstitutionGroup();
// Create an instance of a type based on the received XML's schema
PurchaseOrderDocument poDoc = subGrp.parseXml(args[0]);
// Validate it
validateXml(poDoc);
// Prints the comments from the XML, also the name of the substitute tokens.
subGrp.printComments(poDoc);
// Creates a new XML and saves the file
subGrp.createDocument(poDoc,args[1]);
}
public PurchaseOrderDocument parseXml(String file)
{
File xmlfile = new File(file);
PurchaseOrderDocument poDoc = null;
try
{
poDoc = PurchaseOrderDocument.Factory.parse(xmlfile);
}
catch(XmlException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
return poDoc;
}
/**
* This method prints the substitution group element names(local part) and values for each Invoice-header
* element in the XML Instance. (The rest of elements are ignored for the sake of simplicity)
*/
public void printComments(PurchaseOrderDocument poDoc)
{
// Get object reference of root element.
PurchaseOrderDocument.PurchaseOrder purchaseOrderElement = poDoc.getPurchaseOrder();
// Get all the invoice-header elements for purchase-order.
InvoiceHeaderDocument.InvoiceHeader[] invHeaders = purchaseOrderElement.getInvoiceHeaderArray();
System.out.println("\n\n=========Contents==========\n");
// Iterate through each invoice-header elements printing only the element name and value for substitution group
// comment as defined in Easypo.xsd.
for (int i=0;i<invHeaders.length;i++){
System.out.println("\nInvoiceHeader["+i+"]");
System.out.println("Element Name (Local Part): " + invHeaders[i].xgetComment().newCursor().getName().getLocalPart());
System.out.println("Element Value: " + invHeaders[i].getComment().trim());
}
}
/**
* This method creates an new invoice-header element and attaches to the existing XML Instance, and saves the
* new Instance to a file(args[1]).
*/
public PurchaseOrderDocument createDocument(PurchaseOrderDocument poDoc, String file)
{
// Get object reference of root element.
PurchaseOrderDocument.PurchaseOrder purchaseOrderElement = poDoc.getPurchaseOrder();
InvoiceHeaderDocument.InvoiceHeader invHeaders = purchaseOrderElement.addNewInvoiceHeader();
// Assign values to the newly created invoice-header element.
NameAddress shipto = invHeaders.addNewShipTo();
shipto.setName("New Company");
shipto.setAddress("NewTown, NewCity");
NameAddress billto = invHeaders.addNewBillTo();
billto.setName("New Company");
billto.setAddress("NewTown, NewCity");
// Create a new Book and add it to the invoice.
BookType book = BookType.Factory.newInstance();
book.setId(1000);
book.setTitle("Where the Red Fern Grows");
invHeaders.setProduct(book);
invHeaders.getProduct().newCursor().setName(new QName("http://xmlbeans.apache.org/samples/substitutiongroup/easypo", "book"));
// Creating a new comment - with substitution group member bill-comment element.
invHeaders.setComment("This is a new bill-comment");
XmlCursor cursor = invHeaders.xgetComment().newCursor();
cursor.setName(new QName("http://xmlbeans.apache.org/samples/substitutiongroup/easypo", "bill-comment"));
cursor.dispose();
// Add another invoice-header.
invHeaders = purchaseOrderElement.addNewInvoiceHeader();
// Assign values to the newly created invoice-header element.
shipto = invHeaders.addNewShipTo();
shipto.setName("Other Company");
shipto.setAddress("OtherTown, OtherCity");
billto = invHeaders.addNewBillTo();
billto.setName("Other Company");
billto.setAddress("OtherTown, OtherCity");
// Create a new Clothing and add it to the invoice.
ClothingType clothing = ClothingType.Factory.newInstance();
clothing.setId(2000);
clothing.setColor(ClothingType.Color.BLUE);
invHeaders.setProduct(clothing);
invHeaders.getProduct().newCursor().setName(new QName("http://xmlbeans.apache.org/samples/substitutiongroup/easypo", "clothing"));
// Creating a new comment - with substitution group member bill-comment element.
invHeaders.setComment("This is a new bill-comment");
cursor = invHeaders.xgetComment().newCursor();
cursor.setName(new QName("http://xmlbeans.apache.org/samples/substitutiongroup/easypo", "ship-comment"));
cursor.dispose();
// Validate it.
validateXml(poDoc);
XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setSavePrettyPrint();
File f = new File(file);
try
{
//Writing the XML Instance to a file.
poDoc.save(f,xmlOptions);
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("\n\n\nXML Instance Document saved at : " + f.getPath());
return poDoc;
}
/**
* <p>Validates the XML, printing error messages when the XML is invalid. Note
* that this method will properly validate any instance of a compiled schema
* type because all of these types extend XmlObject.</p>
*
* <p>Note that in actual practice, you'll probably want to use an assertion
* when validating if you want to ensure that your code doesn't pass along
* invalid XML. This sample prints the generated XML whether or not it's
* valid so that you can see the result in both cases.</p>
*
* @param xml The XML to validate.
* @return <code>true</code> if the XML is valid; otherwise, <code>false</code>
*/
public static boolean validateXml(XmlObject xml)
{
boolean isXmlValid = false;
// A collection instance to hold validation error messages.
ArrayList validationMessages = new ArrayList();
// Validate the XML, collecting messages.
isXmlValid = xml.validate(
new XmlOptions().setErrorListener(validationMessages));
// If the XML isn't valid, print the messages.
if (!isXmlValid)
{
System.out.println("\nInvalid XML: ");
for (int i = 0; i < validationMessages.size(); i++)
{
XmlError error = (XmlError) validationMessages.get(i);
System.out.println(error.getMessage());
System.out.println(error.getObjectLocation());
}
}
return isXmlValid;
}
}
1.1 xml-xmlbeans/samples/substitutiongroup/SubstitutionGroup/src/org/apache/xmlbeans/samples/substitutiongroup/SubstitutionGroupTest.java
Index: SubstitutionGroupTest.java
===================================================================
/* Copyright 2004 The Apache Software Foundation
*
* Licensed 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.xmlbeans.samples.substitutiongroup;
import org.apache.xmlbeans.samples.substitutiongroup.easypo.PurchaseOrderDocument;
/**
* A class to test the SubstitutionGroup sample.
*/
public class SubstitutionGroupTest
{
/**
* Tests the SubstitutionGroup sample.
*
* @param args An array in which the first item is a path to an XML file
* based on the schema in EasyPo.xsd. The second item is a path to an XML
* file that should be created by the sample.
*/
public static void main(String[] args)
{
// Create an instance of the sample to test.
SubstitutionGroup sample = new SubstitutionGroup();
// Create a schema type instance from the XML indicated by the path.
SubstitutionGroup subGrp = new SubstitutionGroup();
PurchaseOrderDocument poDoc = subGrp.parseXml(args[0]);
// Validate the XML.
assert sample.validateXml(poDoc);
// Create a new document that adds two elements.
PurchaseOrderDocument newDoc = sample.createDocument(poDoc, args[1]);
// Validate the XML.
assert sample.validateXml(newDoc);
}
}
1.1 xml-xmlbeans/samples/substitutiongroup/SubstitutionGroup/xml/Purchase-Order.xml
Index: Purchase-Order.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2004 The Apache Software Foundation
Licensed 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.
-->
<purchase-order xmlns="http://xmlbeans.apache.org/samples/substitutiongroup/easypo">
<invoice-header>
<ship-to>
<name>Any Inc.</name>
<address>AnyTown, AnyCity</address>
</ship-to>
<bill-to>
<name>Any Inc.</name>
<address>AnyTown, AnyCity</address>
</bill-to>
<book id="123">
<title>The Little Engine That Could</title>
</book>
<ship-comment>
Please ship this to Any Inc. asap.
</ship-comment>
</invoice-header>
<invoice-header>
<ship-to>
<name>Some Corp.</name>
<address>SomeTown, SomeCity</address>
</ship-to>
<bill-to>
<name>This Corp.</name>
<address>ThisTown, ThisCity</address>
</bill-to>
<clothing id="456">
<color>green</color>
</clothing>
<comment>
This is being billed to This Corp.
</comment>
</invoice-header>
</purchase-order>
1.1 xml-xmlbeans/samples/substitutiongroup/SubstitutionGroup/xml/newPurchase-Order.xml
Index: newPurchase-Order.xml
===================================================================
<?xml version="1.0" encoding="UTF-8"?>
<!--Copyright 2004 The Apache Software Foundation
Licensed 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.-->
<purchase-order xmlns="http://xmlbeans.apache.org/samples/substitutiongroup/easypo">
<invoice-header>
<ship-to>
<name>Any Inc.</name>
<address>AnyTown, AnyCity</address>
</ship-to>
<bill-to>
<name>Any Inc.</name>
<address>AnyTown, AnyCity</address>
</bill-to>
<book id="123">
<title>The Little Engine That Could</title>
</book>
<ship-comment>Please ship this to Any Inc. asap.</ship-comment>
</invoice-header>
<invoice-header>
<ship-to>
<name>Some Corp.</name>
<address>SomeTown, SomeCity</address>
</ship-to>
<bill-to>
<name>This Corp.</name>
<address>ThisTown, ThisCity</address>
</bill-to>
<clothing id="456">
<color>green</color>
</clothing>
<comment>This is being billed to This Corp.</comment>
</invoice-header>
<invoice-header>
<ship-to>
<name>New Company</name>
<address>NewTown, NewCity</address>
</ship-to>
<bill-to>
<name>New Company</name>
<address>NewTown, NewCity</address>
</bill-to>
<book id="1000" xsi:type="book-type" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<title>Where the Red Fern Grows</title>
</book>
<bill-comment>This is a new bill-comment</bill-comment>
</invoice-header>
<invoice-header>
<ship-to>
<name>Other Company</name>
<address>OtherTown, OtherCity</address>
</ship-to>
<bill-to>
<name>Other Company</name>
<address>OtherTown, OtherCity</address>
</bill-to>
<clothing id="2000" xsi:type="clothing-type" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<color>blue</color>
</clothing>
<ship-comment>This is a new bill-comment</ship-comment>
</invoice-header>
</purchase-order>
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org