22
33import java .io .IOException ;
44import java .io .InputStream ;
5+ import java .net .URL ;
56
7+ import javax .xml .XMLConstants ;
68import javax .xml .bind .JAXBContext ;
79import javax .xml .bind .JAXBElement ;
810import javax .xml .bind .JAXBException ;
1113import javax .xml .parsers .ParserConfigurationException ;
1214import javax .xml .parsers .SAXParserFactory ;
1315import javax .xml .transform .sax .SAXSource ;
16+ import javax .xml .validation .Schema ;
17+ import javax .xml .validation .SchemaFactory ;
1418
1519import org .xml .sax .Attributes ;
1620import org .xml .sax .InputSource ;
3034public class JAXBPrettyConfigParser implements PrettyConfigParser
3135{
3236
37+ private static final String SCHEMA_LOCATION = "META-INF/ocpsoft-pretty-faces-3.2.1.xsd" ;
38+
3339 private final static String JAXB_CLASS_PACKAGE = "com.ocpsoft.pretty.faces.config.types" ;
3440
3541 private JAXBContext jaxbContext ;
3642
3743 private SAXParserFactory saxParserFactory ;
3844
45+ private Schema schema ;
46+
3947 /**
4048 * Constructor
4149 */
@@ -49,9 +57,21 @@ public JAXBPrettyConfigParser()
4957 // create a namespace aware SAXParserFactory
5058 saxParserFactory = SAXParserFactory .newInstance ();
5159 saxParserFactory .setNamespaceAware (true );
60+
61+ // load schema from classpath
62+ SchemaFactory schemaFactory = SchemaFactory .newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
63+ ClassLoader classLoader = Thread .currentThread ().getContextClassLoader ();
64+ URL schemaUrl = classLoader .getResource (SCHEMA_LOCATION );
65+ if (schemaUrl == null ) {
66+ throw new IllegalStateException ("Unable to load schema from: " +SCHEMA_LOCATION );
67+ }
68+ schema = schemaFactory .newSchema (schemaUrl );
5269
5370 }
5471 catch (JAXBException e )
72+ {
73+ }
74+ catch (SAXException e )
5575 {
5676 throw new IllegalStateException (e );
5777 }
@@ -77,6 +97,9 @@ public void parse(PrettyConfigBuilder builder, InputStream resource) throws IOEx
7797 xmlReader .setContentHandler (unmarshaller .getUnmarshallerHandler ());
7898 SAXSource source = new SAXSource (namespaceFilter , new InputSource (resource ));
7999
100+ // validate configuration
101+ //unmarshaller.setSchema(schema);
102+
80103 // parse the document and get the PrettyConfigElement
81104 JAXBElement <?> e = (JAXBElement <?>) unmarshaller .unmarshal (source );
82105 PrettyConfigElement prettyConfigElement = (PrettyConfigElement ) e .getValue ();
0 commit comments