View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.config;
18  
19  import javax.xml.parsers.DocumentBuilder;
20  import javax.xml.parsers.DocumentBuilderFactory;
21  
22  import org.opensaml.xml.parse.ClasspathResolver;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  import org.springframework.beans.factory.xml.DocumentLoader;
26  import org.w3c.dom.Document;
27  import org.xml.sax.EntityResolver;
28  import org.xml.sax.ErrorHandler;
29  import org.xml.sax.InputSource;
30  import org.xml.sax.SAXException;
31  import org.xml.sax.SAXParseException;
32  
33  /**
34   * A document loader for Spring that uses a {@link ClasspathResolver} for resolving schema information.
35   */
36  public class SpringDocumentLoader implements DocumentLoader {
37  
38      /** Class logger. */
39      private final Logger log = LoggerFactory.getLogger(SpringDocumentLoader.class);
40  
41      /** {@inheritDoc} */
42      public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler,
43              int validationMode, boolean namespaceAware) throws Exception {
44          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
45          factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
46                  "http://www.w3.org/2001/XMLSchema");
47          factory.setCoalescing(true);
48          factory.setIgnoringComments(true);
49          factory.setNamespaceAware(true);
50          factory.setValidating(true);
51  
52          DocumentBuilder builder = factory.newDocumentBuilder();
53          builder.setErrorHandler(new LoggingErrorHandler(log));
54          builder.setEntityResolver(new ClasspathResolver());
55          return builder.parse(inputSource);
56      }
57      
58      /**
59       * A SAX error handler that logs errors a {@link Logger} before rethrowing them.
60       */
61      public class LoggingErrorHandler implements ErrorHandler{
62          
63          /** Error logger. */
64          private Logger log;
65          
66          /**
67           * Constructor.
68           *
69           * @param logger logger errors will be written to
70           */
71          public LoggingErrorHandler(Logger logger){
72              log = logger;
73          }
74  
75          /** {@inheritDoc} */
76          public void error(SAXParseException exception) throws SAXException {
77              log.trace("Error parsing XML", exception);
78              throw exception;
79          }
80  
81          /** {@inheritDoc} */
82          public void fatalError(SAXParseException exception) throws SAXException {
83              log.trace("Fatal XML parsing XML error", exception);
84              throw exception;
85          }
86  
87          /** {@inheritDoc} */
88          public void warning(SAXParseException exception) throws SAXException {
89              log.trace("XML parsing warning", exception);
90              throw exception;
91          }
92      }
93  }