KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ValidateXMLInput


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  * $Id: ValidateXMLInput.java,v 1.7 2004/02/17 19:11:45 minchau Exp $
18  */

19 import javax.xml.parsers.SAXParserFactory JavaDoc;
20 import javax.xml.transform.Transformer JavaDoc;
21 import javax.xml.transform.TransformerException JavaDoc;
22 import javax.xml.transform.TransformerFactory JavaDoc;
23 import javax.xml.transform.sax.SAXSource JavaDoc;
24 import javax.xml.transform.stream.StreamResult JavaDoc;
25 import javax.xml.transform.stream.StreamSource JavaDoc;
26
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.SAXParseException JavaDoc;
30 import org.xml.sax.XMLReader JavaDoc;
31 import org.xml.sax.helpers.DefaultHandler JavaDoc;
32
33 /**
34  * Validate the XML input by using SAXParserFactory to turn on namespace awareness and
35  * validation, and a SAX XMLReader to parse the input and report problems to an error
36  * handler.
37  *
38  * This sample uses birds.xml with an internal DOCTYPE declaration. As shipped, birds.xml
39  * contains an element that violates the declared document type.
40  */

41 public class ValidateXMLInput
42 {
43   
44   public static void main(String JavaDoc[] args)
45     throws Exception JavaDoc
46   {
47     ValidateXMLInput v = new ValidateXMLInput();
48     v.validate();
49   }
50
51   void validate()
52     throws Exception JavaDoc
53    {
54      // Since we're going to use a SAX feature, the transformer must support
55
// input in the form of a SAXSource.
56
TransformerFactory JavaDoc tfactory = TransformerFactory.newInstance();
57     if(tfactory.getFeature(SAXSource.FEATURE))
58     {
59       // Standard way of creating an XMLReader in JAXP 1.1.
60
SAXParserFactory JavaDoc pfactory= SAXParserFactory.newInstance();
61       pfactory.setNamespaceAware(true); // Very important!
62
// Turn on validation.
63
pfactory.setValidating(true);
64       // Get an XMLReader.
65
XMLReader JavaDoc reader = pfactory.newSAXParser().getXMLReader();
66   
67       // Instantiate an error handler (see the Handler inner class below) that will report any
68
// errors or warnings that occur as the XMLReader is parsing the XML input.
69
Handler JavaDoc handler = new Handler JavaDoc();
70       reader.setErrorHandler(handler);
71   
72       // Standard way of creating a transformer from a URL.
73
Transformer JavaDoc t = tfactory.newTransformer(
74         new StreamSource JavaDoc("birds.xsl"));
75       
76       // Specify a SAXSource that takes both an XMLReader and a URL.
77
SAXSource JavaDoc source = new SAXSource JavaDoc(reader,
78         new InputSource JavaDoc("birds.xml"));
79       
80       // Transform to a file.
81
try
82       {
83         t.transform(source, new StreamResult JavaDoc("birds.out"));
84       }
85       catch (TransformerException JavaDoc te)
86       {
87         // The TransformerException wraps someting other than a SAXParseException
88
// warning or error, either of which should be "caught" by the Handler.
89
System.out.println("Not a SAXParseException warning or error: " + te.getMessage());
90       }
91                                   
92       System.out.println("=====Done=====");
93     }
94     else
95       System.out.println("tfactory does not support SAX features!");
96   }
97
98   // Catch any errors or warnings from the XMLReader.
99
class Handler extends DefaultHandler JavaDoc
100   {
101     public void warning (SAXParseException JavaDoc spe)
102          throws SAXException JavaDoc
103     {
104       System.out.println("SAXParseException warning: " + spe.getMessage());
105     }
106
107     public void error (SAXParseException JavaDoc spe)
108         throws SAXException JavaDoc
109     {
110       System.out.println("SAXParseException error: " + spe.getMessage());
111     }
112   }
113 }
114
Popular Tags