Skip to content

Commit a862428

Browse files
committed
Don't load XML schema if validation is disabled
1 parent 2ea0716 commit a862428

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class JAXBPrettyConfigParser implements PrettyConfigParser
4343
private SAXParserFactory saxParserFactory;
4444

4545
private Schema schema;
46-
46+
4747
/**
4848
* Constructor
4949
*/
@@ -57,25 +57,12 @@ public JAXBPrettyConfigParser()
5757
// create a namespace aware SAXParserFactory
5858
saxParserFactory = SAXParserFactory.newInstance();
5959
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);
6960

7061
}
7162
catch (JAXBException e)
7263
{
7364
throw new IllegalStateException(e);
7465
}
75-
catch (SAXException e)
76-
{
77-
throw new IllegalStateException(e);
78-
}
7966
}
8067

8168
/*
@@ -102,7 +89,16 @@ public void parse(PrettyConfigBuilder builder, InputStream resource, boolean val
10289
// optional validation
10390
if (validate)
10491
{
105-
unmarshaller.setSchema(schema);
92+
93+
// lazily load the schema from the classpath
94+
if (schema == null)
95+
{
96+
schema = loadSchema();
97+
}
98+
99+
// enable validation
100+
unmarshaller.setSchema( schema );
101+
106102
}
107103

108104
// parse the document and get the PrettyConfigElement
@@ -124,6 +120,21 @@ public void parse(PrettyConfigBuilder builder, InputStream resource, boolean val
124120
}
125121
}
126122

123+
/**
124+
* Loads the PrettyFaces XML schema from the classpath.
125+
*/
126+
private static Schema loadSchema() throws SAXException
127+
{
128+
// load schema from classpath
129+
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
130+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
131+
URL schemaUrl = classLoader.getResource(SCHEMA_LOCATION);
132+
if(schemaUrl == null) {
133+
throw new IllegalStateException("Unable to load schema from: "+SCHEMA_LOCATION);
134+
}
135+
return schemaFactory.newSchema(schemaUrl);
136+
}
137+
127138
/**
128139
* Returns the current PrettyFaces XML configuration namespace URI.
129140
*/

0 commit comments

Comments
 (0)