First Commit from Source Code Reply

This commit is contained in:
vincenzofariello
2024-05-09 17:40:24 +02:00
parent 11e3b57c5b
commit 107a016cb9
35225 changed files with 1111346 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
Sample: SchemaEnum
Author: Steven Traut (straut@bea.com)
Last Updated: Aug. 11th, 2004
Versions:
xmlbeans-1.0.3
xmlbeans-v2
-----------------------------------------------------------------------------
This sample illustrates how you can access XML values that are
defined in schema as enumerations. When a schema containing
enumerations is compiled, the generated Java types represent the
schema enumerations with Java enumerations. You can access these through
their constants and corresponding int values.
When you run this sample, you'll see it print three blocks of information:
- The XML it got from the PurchaseOrder.xml file.
- The XML it generated by taking data from the PurchaseOrder.xml file and
copying it into a new XML document created from another schema.
- A few lines of data extracted from the newly generated XML file.
To try out this sample:
1. Set XMLBEANS_HOME in your environment
2. Ant must be on your PATH
3. To compile the schemas and sample source, run "ant build"
4. To execute the sample, run "ant run"

View File

@@ -0,0 +1,120 @@
<!--
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="SchemaEnum" default="build">
<property environment="env"/>
<path id="SchemaEnum.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,SchemaEnum.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="SchemaEnum.classes" depends="init">
<mkdir dir="build/classes"/>
<javac srcdir="src"
destdir="build/classes"
classpathref="SchemaEnum.path"
debug="on"
source="1.4"
/>
</target>
<!-- ========================== run ==== -->
<target name="run" depends="init,build">
<echo message="============================== running SchemaEnum"/>
<java
classname="org.apache.xmlbeans.samples.enumeration.SchemaEnum"
classpathref="SchemaEnum.path"
fork="true">
<arg line="xml/PurchaseOrder.xml"/>
</java>
</target>
<!-- ========================== test ==== -->
<target name="test" depends="init,build">
<echo message="============================== testing SchemaEnum"/>
<java
classname="org.apache.xmlbeans.samples.enumeration.SchemaEnumTest"
classpathref="SchemaEnum.path"
fork="true">
<arg line="xml/PurchaseOrder.xml"/>
</java>
</target>
</project>

View File

@@ -0,0 +1,50 @@
<!--
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/enumeration/schemaenum/easypo"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:po="http://xmlbeans.apache.org/samples/enumeration/schemaenum/easypo"
elementFormDefault="qualified">
<xs:element name="purchase-order">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="po:customer"/>
<xs:element name="date" type="xs:dateTime" />
<xs:element name="line-item" type="po:line-item" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="shipper" type="po:shipper" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="customer">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="line-item">
<xs:sequence>
<xs:element name="description" type="xs:string"/>
<xs:element name="per-unit-ounces" type="xs:decimal"/>
<xs:element name="price" type="xs:double"/>
<xs:element name="quantity" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="shipper">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="per-ounce-rate" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

View File

@@ -0,0 +1,63 @@
<!--
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/enumeration/schemaenum/pricesummary"
xmlns:ps="http://xmlbeans.apache.org/samples/enumeration/schemaenum/pricesummary"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<!-- Define an item type that specifies information about an item.
item elements are children of the price element. -->
<xs:complexType name="itemType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="amount" type="xs:double"/>
<xs:element name="quantity" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
<!-- Define a price type that specifies price information. A price may
contain multiple item elements (of type "itemType"), and has a single
threshold attribute whose value may be "Below10Dollars", "Between10And20Dollars",
or "Above20Dollars". These values are enumerated, and may be accessed as
enumerations through XMLBeans. -->
<xs:complexType name="priceType">
<xs:sequence>
<xs:element name="item" type="ps:itemType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="threshold">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Below10Dollars"/>
<xs:enumeration value="Between10And20Dollars"/>
<xs:enumeration value="Above20Dollars"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<!-- Define a price type that specifies price information. A price may
contain multiple item elements (of type "itemType"), and has a single
threshold attribute whose value may be "Below10Dollars", "Between10And20Dollars",
or "Above20Dollars". These values are enumerated, and may be accessed as
enumerations through XMLBeans. -->
<xs:element name="price-summary">
<xs:complexType>
<xs:sequence>
<xs:element name="price" type="ps:priceType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

View File

@@ -0,0 +1,279 @@
/* 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.enumeration;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.samples.enumeration.schemaenum.easypo.LineItem;
import org.apache.xmlbeans.samples.enumeration.schemaenum.easypo.PurchaseOrderDocument;
import org.apache.xmlbeans.samples.enumeration.schemaenum.pricesummary.ItemType;
import org.apache.xmlbeans.samples.enumeration.schemaenum.pricesummary.PriceSummaryDocument;
import org.apache.xmlbeans.samples.enumeration.schemaenum.pricesummary.PriceType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
/**
* This sample illustrates how you can access XML values that are
* defined in schema as enumerations. When a schema containing
* enumerations is compiled, the generated Java types represent the
* schema enumerations with Java enumerations. You can access these through
* their constants and corresponding int values.
* <p/>
* The schemas used by this sample are defined in PriceSummary.xsd and
* EasyPO.xsd.
*/
public class SchemaEnum
{
/**
* Receives an PO XML instance and uses its data to create an XML
* document based another schema, and which summarizes the items
* in the PO by price.
*
* @param args An array containing one argument: the path to an XML instance
* conforming to the schema in EasyPO.xsd.
*/
public static void main(String[] args)
{
// Create an instance of this class to work with.
SchemaEnum thisSample = new SchemaEnum();
// Create an instance of a type based on the received XML's schema
// and use it to print what the sample received.
PurchaseOrderDocument poDoc = thisSample.parseXml(args[0]);
System.out.println("Received XML: \n\n" + poDoc.toString());
// Print the summarized items in XML based on a different schema.
PriceSummaryDocument summaryDoc = thisSample.summarizeItems(poDoc);
System.out.println("Summarized items: \n\n" + summaryDoc.toString());
// Print a simple non-XML list of items by threshold.
String sortedItems = thisSample.sortByThreshold(summaryDoc);
System.out.println("Sorted items: \n" + sortedItems);
// Validate the result.
System.out.println("New XML is valid: " +
thisSample.validateXml(summaryDoc));
}
/**
* <p>This method uses values in the incoming XML to construct
* a new XML document of a different schema. PriceSummary.xsd, the schema
* for the new document, defines XML enumerations for a price
* threshold attribute. Items whose price is between $10 and $20 receive
* a threshold value of "Between10And20Dollars"; items above 20 get a threshold
* value of "Above20Dollars".</p>
* <p/>
* <p>This method loops through the purchase order items, creating a summary
* document that specifies their threshold value.</p>
* <p/>
* <p>You can verify this method's work by comparing the resulting XML with
* the XML in PriceSummary.xml. You can also use this method's return value
* to test the sortByThreshold method.</p>
*/
public PriceSummaryDocument summarizeItems(PurchaseOrderDocument poDoc)
{
PurchaseOrderDocument.PurchaseOrder po = poDoc.getPurchaseOrder();
// Create a new instance of the PriceSummary schema. This is the document
// the code creates, extracting values from the purchase order.
PriceSummaryDocument summaryDoc = PriceSummaryDocument.Factory.newInstance();
PriceSummaryDocument.PriceSummary summary = summaryDoc.addNewPriceSummary();
// Create <price> elements to hold <item> elements according to their
// price threshold.
PriceType priceZero = summary.addNewPrice();
PriceType priceTen = summary.addNewPrice();
PriceType priceTwenty = summary.addNewPrice();
// Set the threshold attribute value for the two new elements.
priceZero.setThreshold(PriceType.Threshold.BELOW_10_DOLLARS);
priceTen.setThreshold(PriceType.Threshold.BETWEEN_10_AND_20_DOLLARS);
priceTwenty.setThreshold(PriceType.Threshold.ABOVE_20_DOLLARS);
// Loop through the purchase order <line-item> elements. If their
// <price> child element is between 10.00 and 20.00, add the <line-item>
// to the <price> element whose threshold is 10.00. For those over 20.00,
// add them to the <price> element whose threshold is 20.00.
// There don't happen to be any under 10.00, but handle this case anyway.
LineItem[] items = po.getLineItemArray();
for (int i = 0; i < items.length; i++)
{
LineItem item = items[i];
if (item.getPrice() < 10.00)
{
ItemType newItem = priceZero.addNewItem();
newItem.setTitle(item.getDescription());
newItem.xsetQuantity(item.xgetQuantity());
newItem.setAmount(item.getPrice());
} else if (item.getPrice() >= 10.00 && item.getPrice() < 20.00)
{
ItemType newItem = priceTen.addNewItem();
newItem.setTitle(item.getDescription());
newItem.xsetQuantity(item.xgetQuantity());
newItem.setAmount(item.getPrice());
} else if (item.getPrice() >= 20.00)
{
ItemType newItem = priceTwenty.addNewItem();
newItem.setTitle(item.getDescription());
newItem.xsetQuantity(item.xgetQuantity());
newItem.setAmount(item.getPrice());
}
}
return summaryDoc;
}
/**
* <p>This method loops through a price summary XML document to
* create a string that lists the items grouped by threshold.
* Unlike the summarizeItems method, which creates a new XML
* document that contains an attribute whose value is enumerated,
* this method retrieves values from an enumeration.</p>
* <p/>
* <p>This method illustrates how you can use the int value corresponding
* to enumerations to specify them in Java switch statements.</p>
*/
public String sortByThreshold(PriceSummaryDocument summaryDoc)
{
// Extract the summary element from the incoming XML, then use it
// to extract an array of the price elements.
PriceSummaryDocument.PriceSummary summary = summaryDoc.getPriceSummary();
PriceType[] priceElements = summary.getPriceArray();
StringBuffer responseBuffer = new StringBuffer();
// Create string buffers to hold the sorted results of the values
// retrieved.
StringBuffer zeroBuffer = new StringBuffer("\nItems under 10 dollars: \n");
StringBuffer tenBuffer = new StringBuffer("\nItems between 10 and 20 dollars: \n");
StringBuffer twentyBuffer = new StringBuffer("\nItems more than 20 dollars: \n");
// Loop through the price elements, extracting the array of <item> child
// elements in each.
for (int i = 0; i < priceElements.length; i++)
{
ItemType[] itemElements = priceElements[i].getItemArray();
// Loop through the <item> elements, discovering which threshold
// the item belongs to, then using the element's <title> value
// in in a sorted list.
for (int j = 0; j < itemElements.length; j++)
{
ItemType item = itemElements[j];
// For each <item> element, find out the int value of its <price>
// parent element's threshold attribute value. Append the item's
// title to the appropriate string buffer.
switch (priceElements[i].getThreshold().intValue())
{
case PriceType.Threshold.INT_BELOW_10_DOLLARS:
zeroBuffer.append(" " + item.getTitle() + "\n");
break;
case PriceType.Threshold.INT_BETWEEN_10_AND_20_DOLLARS:
tenBuffer.append(" " + item.getTitle() + "\n");
break;
case PriceType.Threshold.INT_ABOVE_20_DOLLARS:
twentyBuffer.append(" " + item.getTitle() + "\n");
break;
default:
System.out.println("Yo! Something unexpected happened!");
break;
}
}
}
responseBuffer.append(tenBuffer);
responseBuffer.append(twentyBuffer);
return responseBuffer.toString();
}
/**
* <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/>
* <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 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 (!isXmlValid)
{
System.out.println("Invalid 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;
}
/**
* <p>Creates a File from the XML path provided in main arguments, then
* parses the file's contents into a type generated from schema.</p>
* <p/>
* <p>Note that this work might have been done in main. Isolating it here
* makes the code separately available from outside this class.</p>
*
* @param xmlFilePath A path to XML based on the schema in inventory.xsd.
* @return An instance of a generated schema type that contains the parsed
* XML.
*/
public PurchaseOrderDocument parseXml(String xmlFilePath)
{
File poFile = new File(xmlFilePath);
PurchaseOrderDocument poDoc = null;
try
{
poDoc = PurchaseOrderDocument.Factory.parse(poFile);
} catch (XmlException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return poDoc;
}
}

View File

@@ -0,0 +1,52 @@
/* 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.enumeration;
import org.apache.xmlbeans.samples.enumeration.schemaenum.easypo.PurchaseOrderDocument;
import org.apache.xmlbeans.samples.enumeration.schemaenum.pricesummary.PriceSummaryDocument;
/**
* A class to test the SchemaEnum sample.
*/
public class SchemaEnumTest
{
/**
* Tests the SchemaEnum sample.
*
* @param args An array in which the first item is a path to an XML file
* based on the schema in inventory.xsd.
*/
public static void main(String[] args)
{
SchemaEnum sample = new SchemaEnum();
PurchaseOrderDocument poDoc = sample.parseXml(args[0]);
boolean exampleIsValid = sample.validateXml(poDoc);
assert exampleIsValid;
// Create a new document that summarizes the PO doc.
PriceSummaryDocument summaryDoc = sample.summarizeItems(poDoc);
boolean summaryIsValid = sample.validateXml(summaryDoc);
assert summaryIsValid;
// Create a summary of the items based on price.
String sortedItems = sample.sortByThreshold(summaryDoc);
boolean stringExists = (sortedItems != null);
assert stringExists;
}
}

View File

@@ -0,0 +1,36 @@
<!--
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.
-->
<price-summary xmlns="http://xmlbeans.apache.org/samples/enumeration/schemaenum/pricesummary">
<price threshold="Above20Dollars">
<item>
<title>Burnham's Celestial Handbook, Vol 1</title>
<amount>21.79</amount>
<quantity>1</quantity>
</item>
</price>
<price threshold="Between10And20Dollars">
<item>
<title>Burnham's Celestial Handbook, Vol 2</title>
<amount>19.89</amount>
<quantity>2</quantity>
</item>
<item>
<title>Burnham's Celestial Handbook, Vol 3</title>
<amount>19.89</amount>
<quantity>1</quantity>
</item>
</price>
</price-summary>

View File

@@ -0,0 +1,44 @@
<!--
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/enumeration/schemaenum/easypo">
<customer>
<name>Gladys Kravitz</name>
<address>Anytown, PA</address>
</customer>
<date>2001-12-17T09:30:47-05:00</date>
<line-item>
<description>Burnham's Celestial Handbook, Vol 1</description>
<per-unit-ounces>5</per-unit-ounces>
<price>21.79</price>
<quantity>2</quantity>
</line-item>
<line-item>
<description>Burnham's Celestial Handbook, Vol 2</description>
<per-unit-ounces>5</per-unit-ounces>
<price>19.89</price>
<quantity>2</quantity>
</line-item>
<line-item>
<description>Burnham's Celestial Handbook, Vol 3</description>
<per-unit-ounces>5</per-unit-ounces>
<price>19.89</price>
<quantity>1</quantity>
</line-item>
<shipper>
<name>UPS</name>
<per-ounce-rate>0.74</per-ounce-rate>
</shipper>
</purchase-order>