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,41 @@
Sample: XSDConfig
Author: Rashmi Banthia (rjain29@gmail.com)
Last Updated: Oct. 18th, 2004
Versions:
xmlbeans-1.0.3
-----------------------------------------------------------------------------
This sample illustrates how you can specify package names to be used for xml
namespaces. You can also specify class names to be used for individual qualified names.
To customize the package names and the class names, you'll need to create .xsdconfig file.
For eg: "filename.xsdconfig"
Schema for xsdconfig can be obtained from XMLBeans source.
For this sample:
(1) The java class names generated without the XsdConfig are:
* org.apache.xmlbeans.samples.catalog.ArticleDocument
* org.apache.xmlbeans.samples.catalog.AVeryLongDescriptionElementDocument
* org.apache.xmlbeans.samples.catalog.CatalogDocument
* org.apache.xmlbeans.samples.catalog.JournalDocument
(CatalogXsd.java uses above mentioned classes)
(2) The java class names generated with XsdConfig are:
* com.catalog.XmlArticleDocumentBean
* com.catalog.XmlShortItemBean
* com.catalog.XmlCatalogDocumentBean
* com.catalog.XmlJournalDocumentBean
(CatalogXsdConfig.java uses above mentioned classes)
When you run this sample, you will see it print all the element values from XML document
instance (with and without XsdConfig 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"

View File

@@ -0,0 +1,175 @@
<!--
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="xsdconfig" default="build">
<property environment="env"/>
<path id="XsdConfig.path">
<path refid="xmlbeans.path"/>
<fileset dir="build/lib" includes="schemas_xsdconfig.jar"/>
<pathelement path="build/classes"/>
</path>
<path id="Xsd.path">
<path refid="xmlbeans.path"/>
<fileset dir="build/lib" includes="schemas_xsd.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_xsd.jar, schemas_xsdconfig.jar,XsdConfig.classes">
</target>
<target name="schemas_xsd.check">
<uptodate property="schemas_xsd.notRequired"
targetfile="build/lib/schemas_xsd.jar">
<srcfiles dir="schemas" includes="**/*.xsd"/>
</uptodate>
</target>
<target name="schemas_xsdconfig.check">
<uptodate property="schemas_xsdconfig.notRequired"
targetfile="build/lib/schemas_xsdconfig.jar">
<srcfiles dir="schemas" includes="**/*.xsd **/*.xsdconfig"/>
</uptodate>
</target>
<target name="schemas_xsdconfig.jar" depends="init,schemas_xsdconfig.check"
unless="schemas_xsdconfig.notRequired">
<mkdir dir="build/lib"/>
<!-- Generating with XSD and XsdConfig file -->
<xmlbean schema="schemas"
destfile="build/lib/schemas_xsdconfig.jar"
srcgendir="build/src/xsdconfig"
classpathref="xmlbeans.path"
debug="on"
/>
</target>
<target name="schemas_xsd.jar" depends="init,schemas_xsd.check"
unless="schemas_xsd.notRequired">
<mkdir dir="build/lib"/>
<!-- Generating with XSD only (no config file) -->
<xmlbean schema="schemas/catalog.xsd"
destfile="build/lib/schemas_xsd.jar"
srcgendir="build/src/xsd"
classpathref="xmlbeans.path"
debug="on"
/>
</target>
<target name="XsdConfig.classes" depends="init">
<mkdir dir="build/classes"/>
<javac srcdir="src" includes="org/apache/xmlbeans/samples/xsdconfig/CatalogXsdConfig.java org/apache/xmlbeans/samples/xsdconfig/XsdConfigTest.java"
destdir="build/classes"
classpathref="XsdConfig.path"
debug="on"
source="1.4"
/>
<javac srcdir="src" includes="org/apache/xmlbeans/samples/xsdconfig/CatalogXsd.java org/apache/xmlbeans/samples/xsdconfig/XsdTest.java "
destdir="build/classes"
classpathref="Xsd.path"
debug="on"
source="1.4"
/>
</target>
<!-- ========================== run ==== -->
<target name="run" depends="init,build">
<echo message="============================== running Xsd Sample (With XSD only - no Config)"/>
<java
classname="org.apache.xmlbeans.samples.xsdconfig.CatalogXsd"
classpathref="Xsd.path"
fork="true">
<arg line="xml/catalog.xml"/>
</java>
<echo message="============================== running Xsd Sample (With XSD and XsdConfig)"/>
<java
classname="org.apache.xmlbeans.samples.xsdconfig.CatalogXsdConfig"
classpathref="XsdConfig.path"
fork="true">
<arg line="xml/catalog.xml"/>
</java>
</target>
<!-- ========================== test ==== -->
<target name="test" depends="init,build">
<echo message="============================== testing XsdTest"/>
<java
classname="org.apache.xmlbeans.samples.xsdconfig.XsdTest"
classpathref="Xsd.path"
fork="true">
<arg line="xml/catalog.xml"/>
</java>
<echo message="============================== testing XsdConfigTest"/>
<java
classname="org.apache.xmlbeans.samples.xsdconfig.XsdConfigTest"
classpathref="XsdConfig.path"
fork="true">
<arg line="xml/catalog.xml"/>
</java>
</target>
</project>

View File

@@ -0,0 +1,57 @@
<!-- 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
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ct="http://xmlbeans.apache.org/samples/catalog"
targetNamespace="http://xmlbeans.apache.org/samples/catalog"
elementFormDefault="qualified"
>
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element ref="ct:journal" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="journal">
<xs:complexType>
<xs:sequence>
<xs:element ref="ct:article" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="article">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="author" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="ct:a-very-long-description-element" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a-very-long-description-element">
<xs:complexType>
<xs:sequence>
<xs:element name ="forsample" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

View File

@@ -0,0 +1,49 @@
<!-- 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. -->
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config"
xmlns:ct="http://xmlbeans.apache.org/samples/catalog">
<!--
<namespace> element specifies configuration for namespace.
Attribute uri contains namespace list. <package> element specifies package name
to be used for types generated in this namespace.
-->
<xb:namespace uri="http://xmlbeans.apache.org/samples/catalog http://anotherurihere.org">
<xb:package>com.catalog</xb:package>
</xb:namespace>
<!-- ##any indicates all URIs.
A <prefix> is used to prepend to top-level Java type names generated in this namespace.
A <suffix> is used to append to top-level Java types names generated in this namespace.
The <prefix> and <suffix> are not used for inner Java type definitions.
-->
<xb:namespace uri="##any">
<xb:prefix>Xml</xb:prefix>
<xb:suffix>Bean</xb:suffix>
</xb:namespace>
<!--<qname> specifies a Java class name for a Qualified name -->
<xb:qname name="ct:forsample" javaname="GoodName" />
<xb:qname name="ct:a-very-long-description-element" javaname="XmlShortItemBean" />
</xb:config>

View File

@@ -0,0 +1,155 @@
/* 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.xsdconfig;
/*
*This class uses the auto generated package names and class names. It doesn't consider XsdConfig file.
*Note the difference between the imports in two files (CatalogXsdConfig.java and CatalogXsd.java)
*/
import org.apache.xmlbeans.samples.catalog.CatalogDocument;
import org.apache.xmlbeans.samples.catalog.JournalDocument;
import org.apache.xmlbeans.samples.catalog.ArticleDocument;
import org.apache.xmlbeans.samples.catalog.AVeryLongDescriptionElementDocument;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;
public class CatalogXsd
{
public static void main(String[] args)
{
// Create an instance of this class to work with.
CatalogXsd catalogxsd = new CatalogXsd();
// Create an instance of a type based on the received XML's schema
CatalogDocument catdoc = catalogxsd.parseXml(args[0]);
//Prints the element values from the XML.
catalogxsd.printElements(catdoc);
}
/**
* Creates a File from the XML path provided in main arguments, then
* parses the file's contents into a type (CatalogDocument) generated from schema.
*
* @param xmlFilePath A path to XML based on the schema in EasyPo.xsd
* @return An instance of a generated schema type (CatalogDocument) that contains the
* parsed XML.
*/
public CatalogDocument parseXml(String xmlFilePath)
{
File xmlfile = new File(xmlFilePath);
CatalogDocument catdoc = null;
try
{
catdoc = CatalogDocument.Factory.parse(xmlfile);
}
catch (XmlException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return catdoc;
}
/**
* This method prints all the element values in the given XML document based on Catalog.xsd
*/
public void printElements(CatalogDocument catdoc)
{
// Get object reference of root element.
CatalogDocument.Catalog catalogelement = catdoc.getCatalog();
//Get all <journal> element from the root element.
JournalDocument.Journal[] journalarray = catalogelement.getJournalArray();
//Loop through <journal> element array.
for (int i = 0; i < journalarray.length; i++)
{
//Retrieve all <article> elements within each <journal> element
ArticleDocument.Article[] articlearray = journalarray[i].getArticleArray();
//Loop through <article> array retrieved above
for (int j = 0; j < articlearray.length; j++)
{
System.out.println(articlearray[j].getTitle());
String[] str = articlearray[j].getAuthorArray();
for (int k = 0; k < str.length; k++)
System.out.println(str[k]);
//Note the method for retrieving <forsample> element.
System.out.println(articlearray[j].getAVeryLongDescriptionElement()
.getForsample());
}
}
System.out.println("\n\n\n");
}
/**
* <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 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;
}
}

View File

@@ -0,0 +1,155 @@
/* 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.xsdconfig;
/**
*This class uses the package names and class names mentioned in XsdConfig.
*Note the difference between the imports in two files (CatalogXsdConfig.java and CatalogXsd.java)
*/
import com.catalog.XmlCatalogDocumentBean;
import com.catalog.XmlJournalDocumentBean;
import com.catalog.XmlArticleDocumentBean;
import com.catalog.XmlShortItemBean;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;
public class CatalogXsdConfig
{
public static void main(String[] args)
{
// Create an instance of this class to work with.
CatalogXsdConfig catxsdconfig = new CatalogXsdConfig();
// Create an instance of a type based on the received XML's schema
XmlCatalogDocumentBean catdoc = catxsdconfig.parseXml(args[0]);
//Prints the element values from the XML.
catxsdconfig.printElements(catdoc);
}
/**
* Creates a File from the XML path provided in main arguments, then
* parses the file's contents into a type (CatalogDocument) generated from schema.
*
* @param xmlFilePath A path to XML based on the schema in EasyPo.xsd
* @return An instance of a generated schema type (CatalogDocument) that contains the
* parsed XML.
*/
public XmlCatalogDocumentBean parseXml(String xmlFilePath)
{
File xmlfile = new File(xmlFilePath);
XmlCatalogDocumentBean catdoc = null;
try
{
catdoc = XmlCatalogDocumentBean.Factory.parse(xmlfile);
}
catch (XmlException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return catdoc;
}
/*
* This method prints all the element values in the given XML document based on Catalog.xsd
*/
public void printElements(XmlCatalogDocumentBean catdoc)
{
// Get object reference of root element.
XmlCatalogDocumentBean.Catalog catalogelement = catdoc.getCatalog();
//Get all <journal> element from the root element.
XmlJournalDocumentBean.Journal[] journalarray = catalogelement.getJournalArray();
//Loop through <journal> element array.
for (int i = 0; i < journalarray.length; i++)
{
//Retrieve all <article> elements within each <journal> element
XmlArticleDocumentBean.Article[] articlearray = journalarray[i].getArticleArray();
//Loop through <article> array retrieved above
for (int j = 0; j < articlearray.length; j++)
{
System.out.println(articlearray[j].getTitle());
String[] str = articlearray[j].getAuthorArray();
for (int k = 0; k < str.length; k++)
System.out.println(str[k]);
//Note the method for retrieving <forsample> element.
System.out.println(
articlearray[j].getXmlShortItemBean().getGoodName());
}
}
System.out.println("\n\n\n");
}
/**
* <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 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;
}
}

View File

@@ -0,0 +1,41 @@
/* 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.xsdconfig;
import com.catalog.XmlCatalogDocumentBean;
public class XsdConfigTest
{
/**
* Tests the CatalogXsdConfig.java class. This does consider XsdConfig file.
*/
public static void main(String[] args)
{
// Create an instance of the sample to test.
CatalogXsdConfig sample = new CatalogXsdConfig();
XmlCatalogDocumentBean catdoc = sample.parseXml(args[0]);
//Prints the element values from the XML.
sample.printElements(catdoc);
// Validate the XML.
assert sample.validateXml(catdoc);
}
}

View File

@@ -0,0 +1,40 @@
/* 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.xsdconfig;
import org.apache.xmlbeans.samples.catalog.CatalogDocument;
public class XsdTest
{
/**
* Tests the CatalogXsd.java class. This does not consider XsdConfig file.
*/
public static void main(String[] args)
{
// Create an instance of the sample to test.
CatalogXsd sample = new CatalogXsd();
CatalogDocument catdoc = sample.parseXml(args[0]);
//Prints the element values from the XML.
sample.printElements(catdoc);
// Validate the XML.
assert sample.validateXml(catdoc);
}
}

View File

@@ -0,0 +1,38 @@
<?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.
-->
<catalog xmlns="http://xmlbeans.apache.org/samples/catalog">
<journal>
<article>
<title>Java programming for high-performance numerical computing</title>
<author>J. E. Moreira et.al.</author>
<a-very-long-description-element>
<forsample>This is a sample element for journal-1</forsample>
</a-very-long-description-element>
</article>
</journal>
<journal>
<article>
<title>Dynamically Binding EJBs Using the Java Reflection API</title>
<author>Douglas A. Clark</author>
<a-very-long-description-element>
<forsample>This document can be found at http://www.cuj.com/documents/s=8467/cujjsup2010clark/clark.htm </forsample>
</a-very-long-description-element>
</article>
</journal>
</catalog>