2121import java .io .FileNotFoundException ;
2222import java .io .IOException ;
2323import java .io .InputStream ;
24+ import java .io .OutputStream ;
2425import java .io .OutputStreamWriter ;
26+ import java .io .PrintWriter ;
2527import java .io .UnsupportedEncodingException ;
2628import java .net .MalformedURLException ;
2729import java .net .URL ;
4143import java .util .logging .Logger ;
4244import java .util .stream .Collectors ;
4345import javax .annotation .Nullable ;
46+ import javax .xml .XMLConstants ;
4447import javax .xml .parsers .DocumentBuilder ;
4548import javax .xml .parsers .DocumentBuilderFactory ;
4649import javax .xml .transform .Transformer ;
4750import javax .xml .transform .TransformerException ;
48- import javax .xml .transform .TransformerFactory ;
4951import javax .xml .transform .TransformerFactoryConfigurationError ;
5052import javax .xml .transform .dom .DOMResult ;
5153import javax .xml .transform .dom .DOMSource ;
54+ import javax .xml .transform .stream .StreamResult ;
5255import javax .xml .transform .stream .StreamSource ;
5356import javax .xml .validation .Schema ;
5457import javax .xml .validation .SchemaFactory ;
5558import javax .xml .validation .Validator ;
5659import org .geotools .util .logging .Logging ;
60+ import org .geotools .xml .XMLUtils ;
5761import org .geowebcache .GeoWebCacheEnvironment ;
5862import org .geowebcache .GeoWebCacheException ;
5963import org .geowebcache .GeoWebCacheExtensions ;
@@ -117,6 +121,10 @@ public class XMLConfiguration
117121
118122 public static final String DEFAULT_CONFIGURATION_FILE_NAME = "geowebcache.xml" ;
119123
124+ public static final String SCHEMA_NAMESPACE_PREFIX = "http://geowebcache.org/schema/" ;
125+
126+ public static final String DEFAULT_SCHEMA_RESOURCE = "geowebcache.xsd" ;
127+
120128 private static Logger log = Logging .getLogger (XMLConfiguration .class .getName ());
121129
122130 /** Web app context, used to look up {@link XMLConfigurationProvider}s. */
@@ -614,9 +622,9 @@ private synchronized void addOrReplaceGridSet(final XMLGridSet gridSet) throws I
614622 static Node loadDocument (InputStream xmlFile ) throws ConfigurationException , IOException {
615623 Node topNode = null ;
616624 try {
617- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory . newInstance ();
625+ DocumentBuilderFactory docBuilderFactory = XMLUtils . newDocumentBuilderFactory ();
618626 docBuilderFactory .setNamespaceAware (true );
619- DocumentBuilder docBuilder = docBuilderFactory .newDocumentBuilder ();
627+ DocumentBuilder docBuilder = XMLUtils .newDocumentBuilder (docBuilderFactory );
620628 topNode = checkAndTransform (docBuilder .parse (xmlFile ));
621629 } catch (Exception e ) {
622630 throw (IOException ) new IOException (e .getMessage ()).initCause (e );
@@ -629,14 +637,12 @@ private static Node checkAndTransform(Document doc) throws ConfigurationExceptio
629637 Node rootNode = doc .getDocumentElement ();
630638
631639 // debugPrint(rootNode);
632-
633640 if (!rootNode .getNodeName ().equals ("gwcConfiguration" )) {
634641 log .config ("The configuration file is of the pre 1.0 type, trying to convert." );
635642 rootNode = applyTransform (rootNode , "geowebcache_pre10.xsl" ).getFirstChild ();
636643 }
637644
638645 // debugPrint(rootNode);
639-
640646 if (rootNode .getNamespaceURI ().equals ("http://geowebcache.org/schema/1.0.0" )) {
641647 log .config ("Updating configuration from 1.0.0 to 1.0.1" );
642648 rootNode = applyTransform (rootNode , "geowebcache_100.xsl" ).getFirstChild ();
@@ -716,6 +722,11 @@ private static Node checkAndTransform(Document doc) throws ConfigurationExceptio
716722 rootNode = applyTransform (rootNode , "geowebcache_151.xsl" ).getFirstChild ();
717723 }
718724
725+ if (!rootNode .getNamespaceURI ().equals ("http://geowebcache.org/schema/2.0.0" )) {
726+ log .config ("Updating configuration to 2.0.0" );
727+ rootNode = applyTransform (rootNode , "geowebcache_200.xsl" ).getFirstChild ();
728+ }
729+
719730 // Check again after transform
720731 if (!rootNode .getNodeName ().equals ("gwcConfiguration" )) {
721732 log .log (Level .SEVERE , "Unable to parse file, expected gwcConfiguration at root after transform." );
@@ -726,8 +737,8 @@ private static Node checkAndTransform(Document doc) throws ConfigurationExceptio
726737 validate (rootNode );
727738 log .config ("TileLayerConfiguration file validated fine." );
728739 } catch (SAXException e ) {
729- log .warning ("GWC configuration validation error: " + e .getMessage ());
730- log .warning (
740+ log .fine ("GWC configuration validation error: " + e .getMessage ());
741+ log .fine (
731742 "Will try to use configuration anyway. Please check the order of declared elements against the schema." );
732743 } catch (IOException e ) {
733744 throw new RuntimeException (e .getMessage (), e );
@@ -739,11 +750,14 @@ private static Node checkAndTransform(Document doc) throws ConfigurationExceptio
739750
740751 static void validate (Node rootNode ) throws SAXException , IOException {
741752 // Perform validation
742- // TODO dont know why this one suddenly failed to look up, revert to
743- // XMLConstants.W3C_XML_SCHEMA_NS_URI
744- SchemaFactory factory = SchemaFactory .newInstance ("http://www.w3.org/2001/XMLSchema" );
753+ SchemaFactory factory = XMLUtils .newSchemaFactory (XMLConstants .W3C_XML_SCHEMA_NS_URI );
754+ // We do not need access to external schemas
755+ factory .setProperty (XMLConstants .ACCESS_EXTERNAL_SCHEMA , "" );
756+ factory .setProperty (XMLConstants .ACCESS_EXTERNAL_DTD , "" );
745757 try (InputStream is = XMLConfiguration .class .getResourceAsStream ("geowebcache.xsd" )) {
746-
758+ if (is == null ) {
759+ throw new IOException ("Could not load schema resource 'geowebcache.xsd'" );
760+ }
747761 Schema schema = factory .newSchema (new StreamSource (is ));
748762 Validator validator = schema .newValidator ();
749763
@@ -758,7 +772,7 @@ static String getCurrentSchemaVersion() {
758772
759773 Document dom ;
760774 try (InputStream is = XMLConfiguration .class .getResourceAsStream ("geowebcache.xsd" )) {
761- dom = DocumentBuilderFactory . newInstance () .newDocumentBuilder ().parse (is );
775+ dom = XMLUtils .newDocumentBuilder ().parse (is );
762776 } catch (Exception e ) {
763777 throw new RuntimeException (e );
764778 }
@@ -777,7 +791,7 @@ private static Node applyTransform(Node oldRootNode, String xslFilename) {
777791 try (InputStream is = XMLConfiguration .class .getResourceAsStream (xslFilename )) {
778792
779793 try {
780- transformer = TransformerFactory . newInstance () .newTransformer (new StreamSource (is ));
794+ transformer = XMLUtils .newTransformer (new StreamSource (is ));
781795 transformer .transform (new DOMSource (oldRootNode ), result );
782796 } catch (TransformerFactoryConfigurationError | TransformerException e ) {
783797 log .log (Level .FINE , e .getMessage (), e );
@@ -1462,4 +1476,17 @@ public void deinitialize() throws Exception {
14621476 this .layers = null ;
14631477 this .gwcConfig = null ;
14641478 }
1479+
1480+ /** Print a DOM tree to an output stream or if there is an exception while doing so, print the stack trace. */
1481+ public static void printDom (Node dom , OutputStream os ) {
1482+ Transformer trans ;
1483+ PrintWriter w = new PrintWriter (os );
1484+ try {
1485+ trans = XMLUtils .newTransformer (); // XMLUtils.newTransformer()
1486+ trans .transform (new DOMSource (dom ), new StreamResult (new OutputStreamWriter (os )));
1487+ } catch (TransformerException e ) {
1488+ w .println ("An error ocurred while transforming the given DOM:" );
1489+ e .printStackTrace (w );
1490+ }
1491+ }
14651492}
0 commit comments