KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > Validator


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.xml;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
27 import javax.xml.parsers.SAXParser JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.SAXParseException JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.xml.sax.EntityResolver JavaDoc;
32 import org.xml.sax.helpers.DefaultHandler JavaDoc;
33 import org.jboss.xb.binding.JBossXBRuntimeException;
34
35 /**
36  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
37  * @version <tt>$Revision: 45119 $</tt>
38  */

39 public class Validator
40 {
41    public static void assertValidXml(final String JavaDoc xsd, String JavaDoc xml)
42    {
43       assertValidXml(xsd, xml, null);
44    }
45
46    public static void assertValidXml(String JavaDoc xml, final EntityResolver JavaDoc resolver)
47    {
48       assertValidXml(null, xml, resolver);
49    }
50
51    private static void assertValidXml(final String JavaDoc xsd, String JavaDoc xml, final EntityResolver JavaDoc resolver)
52    {
53       SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
54       factory.setValidating(true);
55       factory.setNamespaceAware(true);
56       SAXParser JavaDoc parser = null;
57       try
58       {
59          parser = factory.newSAXParser();
60       }
61       catch(Exception JavaDoc e)
62       {
63          throw new IllegalStateException JavaDoc("Failed to instantiate a SAX parser: " + e.getMessage());
64       }
65
66       try
67       {
68          parser.getXMLReader().setFeature("http://apache.org/xml/features/validation/schema", true);
69       }
70       catch(SAXException JavaDoc e)
71       {
72          throw new IllegalStateException JavaDoc("Schema validation feature is not supported by the parser: " + e.getMessage());
73       }
74
75       try
76       {
77          parser.parse(new ByteArrayInputStream JavaDoc(xml.getBytes()),
78             new DefaultHandler JavaDoc()
79             {
80                public void warning(SAXParseException JavaDoc e)
81                {
82                }
83
84                public void error(SAXParseException JavaDoc e)
85                {
86                   throw new JBossXBRuntimeException("Error", e);
87                }
88
89                public void fatalError(SAXParseException JavaDoc e)
90                {
91                   throw new JBossXBRuntimeException("Fatal error", e);
92                }
93
94                public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
95                {
96                   if(resolver != null)
97                   {
98                      try
99                      {
100                         return resolver.resolveEntity(publicId, systemId);
101                      }
102                      catch(Exception JavaDoc e)
103                      {
104                         throw new IllegalStateException JavaDoc("Failed to resolveEntity " + systemId + ": " + systemId);
105                      }
106                   }
107                   else
108                   {
109                      return new InputSource JavaDoc(new StringReader JavaDoc(xsd));
110                   }
111                }
112             }
113          );
114       }
115       catch(Exception JavaDoc e)
116       {
117          throw new JBossXBRuntimeException("Parsing failed.", e);
118       }
119    }
120 }
121
Popular Tags