001: /* Copyright 2004 The Apache Software Foundation
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.apache.xmlbeans.samples.xsdconfig;
017:
018: /**
019: *This class uses the package names and class names mentioned in XsdConfig.
020: *Note the difference between the imports in two files (CatalogXsdConfig.java and CatalogXsd.java)
021: */
022:
023: import com.catalog.XmlCatalogDocumentBean;
024: import com.catalog.XmlJournalDocumentBean;
025: import com.catalog.XmlArticleDocumentBean;
026: import com.catalog.XmlShortItemBean;
027:
028: import org.apache.xmlbeans.XmlException;
029: import org.apache.xmlbeans.XmlError;
030: import org.apache.xmlbeans.XmlObject;
031: import org.apache.xmlbeans.XmlOptions;
032:
033: import java.util.ArrayList;
034: import java.io.IOException;
035: import java.io.File;
036:
037: public class CatalogXsdConfig {
038:
039: public static void main(String[] args) {
040: // Create an instance of this class to work with.
041: CatalogXsdConfig catxsdconfig = new CatalogXsdConfig();
042:
043: // Create an instance of a type based on the received XML's schema
044: XmlCatalogDocumentBean catdoc = catxsdconfig.parseXml(args[0]);
045:
046: //Prints the element values from the XML.
047: catxsdconfig.printElements(catdoc);
048: }
049:
050: /**
051: * Creates a File from the XML path provided in main arguments, then
052: * parses the file's contents into a type (CatalogDocument) generated from schema.
053: *
054: * @param xmlFilePath A path to XML based on the schema in EasyPo.xsd
055: * @return An instance of a generated schema type (CatalogDocument) that contains the
056: * parsed XML.
057: */
058: public XmlCatalogDocumentBean parseXml(String xmlFilePath) {
059: File xmlfile = new File(xmlFilePath);
060: XmlCatalogDocumentBean catdoc = null;
061:
062: try {
063: catdoc = XmlCatalogDocumentBean.Factory.parse(xmlfile);
064: } catch (XmlException e) {
065: e.printStackTrace();
066: } catch (IOException e) {
067: e.printStackTrace();
068: }
069: return catdoc;
070: }
071:
072: /*
073: * This method prints all the element values in the given XML document based on Catalog.xsd
074: */
075: public void printElements(XmlCatalogDocumentBean catdoc) {
076: // Get object reference of root element.
077: XmlCatalogDocumentBean.Catalog catalogelement = catdoc
078: .getCatalog();
079:
080: //Get all <journal> element from the root element.
081: XmlJournalDocumentBean.Journal[] journalarray = catalogelement
082: .getJournalArray();
083:
084: //Loop through <journal> element array.
085: for (int i = 0; i < journalarray.length; i++) {
086:
087: //Retrieve all <article> elements within each <journal> element
088: XmlArticleDocumentBean.Article[] articlearray = journalarray[i]
089: .getArticleArray();
090:
091: //Loop through <article> array retrieved above
092: for (int j = 0; j < articlearray.length; j++) {
093: System.out.println(articlearray[j].getTitle());
094:
095: String[] str = articlearray[j].getAuthorArray();
096:
097: for (int k = 0; k < str.length; k++)
098: System.out.println(str[k]);
099:
100: //Note the method for retrieving <forsample> element.
101: System.out.println(articlearray[j]
102: .getXmlShortItemBean().getGoodName());
103:
104: }
105: }
106: System.out.println("\n\n\n");
107: }
108:
109: /**
110: * <p>Validates the XML, printing error messages when the XML is invalid. Note
111: * that this method will properly validate any instance of a compiled schema
112: * type because all of these types extend XmlObject.</p>
113: * <p/>
114: * <p>Note that in actual practice, you'll probably want to use an assertion
115: * when validating if you want to ensure that your code doesn't pass along
116: * invalid XML. This sample prints the generated XML whether or not it's
117: * valid so that you can see the result in both cases.</p>
118: *
119: * @param xml The XML to validate.
120: * @return <code>true</code> if the XML is valid; otherwise, <code>false</code>
121: */
122: public static boolean validateXml(XmlObject xml) {
123: boolean isXmlValid = false;
124:
125: // A collection instance to hold validation error messages.
126: ArrayList validationMessages = new ArrayList();
127:
128: // Validate the XML, collecting messages.
129: isXmlValid = xml.validate(new XmlOptions()
130: .setErrorListener(validationMessages));
131:
132: // If the XML isn't valid, print the messages.
133: if (!isXmlValid) {
134: System.out.println("\nInvalid XML: ");
135: for (int i = 0; i < validationMessages.size(); i++) {
136: XmlError error = (XmlError) validationMessages.get(i);
137: System.out.println(error.getMessage());
138: System.out.println(error.getObjectLocation());
139: }
140: }
141: return isXmlValid;
142: }
143:
144: }
|