1 /* Copyright 2004 The Apache Software Foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package org.apache.xmlbeans.samples.template; 17 18 import org.apache.xmlbeans; 19 import org.apache.xmlbeans.samples.template.sampletemplate.HelloDocument; 20 21 import java.io.File; 22 import java.util.ArrayList; 23 import java.util.Iterator; 24 25 /** 26 * This is a template sample. A nice description would go here. 27 */ 28 public class SampleTemplate 29 { 30 /** 31 * Prints out the names in the xml instance conforming to hello.xsd. 32 */ 33 public static void main(String[] args) 34 throws org.apache.xmlbeans.XmlException, java.io.IOException 35 { 36 SampleTemplate sample = new SampleTemplate(); 37 sample.start(args); 38 } 39 40 public void start(String[] args) 41 throws org.apache.xmlbeans.XmlException, java.io.IOException 42 { 43 for (int i = 0; i < args.length; i++) 44 { 45 validate(args[i]); 46 } 47 } 48 49 public void validate(String filename) 50 throws org.apache.xmlbeans.XmlException, java.io.IOException 51 { 52 System.out.println("parsing document: " + filename); 53 HelloDocument doc = HelloDocument.Factory.parse(new File(filename)); 54 55 ArrayList errors = new ArrayList(); 56 XmlOptions opts = new XmlOptions(); 57 opts.setErrorListener(errors); 58 59 if (doc.validate(opts)) 60 { 61 System.out.println("document is valid."); 62 63 String[] names = doc.getHello().getNameArray(); 64 for (int i = 0; i < names.length; i++) 65 { 66 System.out.println(" hi there, " + names[i] + "!"); 67 } 68 } 69 else 70 { 71 System.out.println("document is invalid!"); 72 73 Iterator iter = errors.iterator(); 74 while (iter.hasNext()) 75 { 76 System.out.println(">> " + iter.next()); 77 } 78 } 79 System.out.println(); 80 } 81 }