Skip to content

Commit 6412e99

Browse files
committed
Support for validating the XML configuration
1 parent c8f24aa commit 6412e99

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

‎core/src/main/java/com/ocpsoft/pretty/faces/config/JAXBPrettyConfigParser.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.net.URL;
56

7+
import javax.xml.XMLConstants;
68
import javax.xml.bind.JAXBContext;
79
import javax.xml.bind.JAXBElement;
810
import javax.xml.bind.JAXBException;
@@ -11,6 +13,8 @@
1113
import javax.xml.parsers.ParserConfigurationException;
1214
import javax.xml.parsers.SAXParserFactory;
1315
import javax.xml.transform.sax.SAXSource;
16+
import javax.xml.validation.Schema;
17+
import javax.xml.validation.SchemaFactory;
1418

1519
import org.xml.sax.Attributes;
1620
import org.xml.sax.InputSource;
@@ -30,12 +34,16 @@
3034
public 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

Comments
 (0)