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,29 @@
Sample: MixedContent
Author: Eric Vasilik (ericvas@bea.com)
Last Updated: Oct. 28, 2004
Versions:
xmlbeans-1.0.3
xmlbeans-v2
-----------------------------------------------------------------------------
This samples gives an quick overview of how to use XmlBeans with both the
strongly typed XmlObjects (StatementDocument, Transaction) and with the
XmlCursor.
In the sample, a instance of a statement is iterated over twice --
once using the strongly typed array approach and once with an XmlCursor. When
walking over the array the programmer naivly adds up deposit amounts before
the withdrawal amounts. The end result is a positive balance. When walking
over the array using XmlCursor, the transaction amounts are processed in order
and the end result is a negative balance.
In this situation, the order of the xml elements matters!
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="OrderMatters" default="build">
<property environment="env"/>
<path id="OrderMatters.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,OrderMatters.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="OrderMatters.classes" depends="init">
<mkdir dir="build/classes"/>
<javac srcdir="src"
destdir="build/classes"
classpathref="OrderMatters.path"
debug="on"
source="1.4"
/>
</target>
<!-- ========================== run ==== -->
<target name="run" depends="init,build">
<echo message="============================== running OrderMatters"/>
<java
classname="org.apache.xmlbeans.samples.cursor.OrderMatters"
classpathref="OrderMatters.path"
fork="true">
<arg line="xml/stmt.xml"/>
</java>
</target>
<!-- ========================== test ==== -->
<target name="test" depends="init,build">
<echo message="============================== testing OrderMatters"/>
<java
classname="org.apache.xmlbeans.samples.cursor.OrderMattersTest"
classpathref="OrderMatters.path"
fork="true">
<arg line="xml/stmt.xml"/>
</java>
</target>
</project>

View File

@@ -0,0 +1,38 @@
<!--
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://statement"
elementFormDefault="qualified"
xmlns:tns="http://statement"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="statement">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="deposit" type="tns:transaction"/>
<xs:element name="withdrawal" type="tns:transaction"/>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="transaction">
<xs:sequence>
<xs:element name="description" type="xs:string"/>
<xs:element name="amount" type="xs:float"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

View File

@@ -0,0 +1,102 @@
/* 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.cursor;
import statement.StatementDocument;
import statement.StatementDocument.Statement;
import statement.Transaction;
import java.io.File;
import org.apache.xmlbeans.XmlCursor;
import javax.xml.namespace.QName;
public class OrderMatters
{
private static QName deposit = new QName( "http://statement", "deposit" );
public static void main ( String[] args ) throws Exception
{
// load the xml instance into the store and return a
// strongly typed instance of StatementDocument
StatementDocument stmtDoc = StatementDocument.Factory.parse( new File( args[ 0 ] ) );
System.out.println( "Valid statement instance? " + stmtDoc.validate() );
float balance = balanceOutOfOrder(stmtDoc);
System.out.println( "Ending balance: $" + balance );
balance = balanceInOrder(stmtDoc);
System.out.println( "Ending balance: $" + balance );
}
/**
* Out of order balance: the ease of stronly-typed XmlObjects!
*/
public static float balanceOutOfOrder(StatementDocument stmtDoc)
{
Statement stmt = stmtDoc.getStatement();
float balance = 0;
Transaction[] deposits = stmt.getDepositArray();
Transaction[] withdrawals = stmt.getWithdrawalArray();
for ( int i = 0 ; i < deposits.length ; i++ )
balance += deposits[ i ].getAmount();
for ( int i = 0 ; i < withdrawals.length ; i++ )
balance -= withdrawals[ i ].getAmount();
return balance;
}
/**
* In order balance: the power of XmlCursor!
*/
public static float balanceInOrder(StatementDocument stmtDoc)
{
float balance = 0;
XmlCursor cursor = stmtDoc.newCursor();
// use xpath to select elements
cursor.selectPath( "*/*" );
// iterate over the selection
while ( cursor.toNextSelection() )
{
// two views of the same data:
// move back and forth between XmlObject <-> XmlCursor
Transaction trans = (Transaction) cursor.getObject();
float amount = trans.getAmount();
if (cursor.getName().equals( deposit ))
balance += amount;
else if ((balance -= amount) < 0)
{
// doh!
System.out.println( "Negative balance: $" + balance );
balance -= 50;
}
}
return balance;
}
}

View File

@@ -0,0 +1,46 @@
/* 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.cursor;
import statement.StatementDocument;
import statement.StatementDocument.Statement;
import statement.Transaction;
import java.io.File;
import org.apache.xmlbeans.XmlCursor;
import javax.xml.namespace.QName;
public class OrderMattersTest
{
private static QName deposit = new QName( "http://statement", "deposit" );
public static void main ( String[] args ) throws Exception
{
StatementDocument stmtDoc = StatementDocument.Factory.parse( new File( args[ 0 ] ) );
if (!stmtDoc.validate())
throw new RuntimeException("expected valid instance: " + args[0]);
float balance = OrderMatters.balanceOutOfOrder(stmtDoc);
if (1010F != balance)
throw new RuntimeException("expected out of order to return $1010.0: " + balance);
balance = OrderMatters.balanceInOrder(stmtDoc);
if (960F != balance)
throw new RuntimeException("expected in order to return $960.0: " + balance);
}
}

View File

@@ -0,0 +1,33 @@
<!--
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.
-->
<statement xmlns="http://statement">
<deposit>
<description>Vegas winnings!</description>
<amount>3000.00</amount>
</deposit>
<withdrawal>
<description>OSCON Registration</description>
<amount>1890.00</amount>
</withdrawal>
<withdrawal>
<description>Four Seasons Hotel</description>
<amount>1200.00</amount>
</withdrawal>
<deposit>
<description>Paycheck</description>
<amount>1100.00</amount>
</deposit>
</statement>