View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
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 org.springmodules.validation.bean.conf.loader.xml;
18  
19  import java.io.IOException;
20  import java.util.Map;
21  
22  import javax.xml.parsers.DocumentBuilder;
23  import javax.xml.parsers.DocumentBuilderFactory;
24  import javax.xml.parsers.ParserConfigurationException;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.core.io.Resource;
29  import org.springmodules.validation.bean.conf.ResourceConfigurationLoadingException;
30  import org.w3c.dom.Document;
31  import org.xml.sax.SAXException;
32  
33  /**
34   * An {@link AbstractResourceBasedBeanValidationConfigurationLoader} implementation that serves as a base class
35   * for all xml baed bean validation configuration loaders.
36   *
37   * @author Uri Boness
38   */
39  public abstract class AbstractXmlBeanValidationConfigurationLoader extends AbstractResourceBasedBeanValidationConfigurationLoader {
40  
41      private final Logger logger = LoggerFactory.getLogger(AbstractXmlBeanValidationConfigurationLoader.class);
42  
43      /**
44       * todo: document
45       *
46       * @see org.springmodules.validation.bean.conf.loader.xml.AbstractResourceBasedBeanValidationConfigurationLoader#loadConfigurations(org.springframework.core.io.Resource)
47       */
48      protected final Map loadConfigurations(Resource resource) {
49          try {
50              DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
51              builderFactory.setNamespaceAware(true);
52              DocumentBuilder builder = builderFactory.newDocumentBuilder();
53              Document document = builder.parse(resource.getInputStream());
54              return loadConfigurations(document, resource.getDescription());
55          } catch (IOException ioe) {
56              logger.error("Could not read resource '" + resource.getDescription() + "'", ioe);
57              throw new ResourceConfigurationLoadingException(resource, ioe);
58          } catch (ParserConfigurationException pce) {
59              logger.error("Could not parse xml resource '" + resource.getDescription() + "'", pce);
60              throw new ResourceConfigurationLoadingException(resource, pce);
61          } catch (SAXException se) {
62              logger.error("Could not parse xml resource '" + resource.getDescription() + "'", se);
63              throw new ResourceConfigurationLoadingException(resource, se);
64          } catch (Throwable t) {
65              logger.error("Could not parse xml resource '" + resource.getDescription() + "'", t);
66              throw new ResourceConfigurationLoadingException(resource, t);
67          }
68      }
69  
70      /**
71       * todo: document
72       */
73      protected abstract Map loadConfigurations(Document document, String resourceName);
74  
75  
76  }