View Javadoc

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