KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > RelaxNG


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: RelaxNG.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.xml;
21
22 import java.io.BufferedWriter JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26
27 import org.xml.sax.InputSource JavaDoc;
28
29 import com.thaiopensource.util.PropertyMapBuilder;
30 import com.thaiopensource.validate.SchemaReader;
31 import com.thaiopensource.validate.ValidateProperty;
32 import com.thaiopensource.validate.ValidationDriver;
33 import com.thaiopensource.validate.auto.AutoSchemaReader;
34 import com.thaiopensource.xml.sax.ErrorHandlerImpl;
35
36 import org.apache.log4j.Category;
37
38 /**
39  * Validate XML Document with RELAX NG Schema
40  */

41 public class RelaxNG {
42     static Category log = Category.getInstance(RelaxNG.class);
43
44     /**
45      *
46      */

47     public static void main(String JavaDoc[] args) {
48         if(args.length == 0) {
49             System.out.println("Usage: relaxng.rng sample.xml");
50             return;
51         }
52
53         try {
54             String JavaDoc message = RelaxNG.validate(new File JavaDoc(args[0]), new File JavaDoc(args[1]));
55             if (message == null) {
56                 System.out.println("Document is valid");
57             } else {
58                 System.out.println("Document not valid: " + message);
59             }
60         } catch (Exception JavaDoc e) {
61             System.err.println("" + e);
62         }
63     }
64
65     /**
66      *
67      */

68     public static String JavaDoc validate(File JavaDoc schema, File JavaDoc xml) throws Exception JavaDoc {
69         InputSource JavaDoc in = ValidationDriver.uriOrFileInputSource(schema.getAbsolutePath());
70         PropertyMapBuilder properties = new PropertyMapBuilder();
71         ByteArrayOutputStream JavaDoc error = new ByteArrayOutputStream JavaDoc();
72         ErrorHandlerImpl eh = new ErrorHandlerImpl(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(error)));
73         ValidateProperty.ERROR_HANDLER.put(properties, eh);
74         SchemaReader schemaReader = new AutoSchemaReader();
75         ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader);
76         if (driver.loadSchema(in)) {
77             if (driver.validate(ValidationDriver.uriOrFileInputSource(xml.getAbsolutePath()))) {
78                 log.debug("" + error);
79                 return null;
80             } else {
81                 log.error("" + error);
82                 return "" + error;
83             }
84         } else {
85             throw new Exception JavaDoc("Could not load schema!\n" + error);
86         }
87     }
88 }
89
Popular Tags