Coverage Report - org.dozer.loader.xml.XMLParserFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLParserFactory
88%
16/18
N/A
1.6
XMLParserFactory$1
N/A
N/A
1.6
XMLParserFactory$DozerDefaultHandler
50%
4/8
N/A
1.6
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 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  
 package org.dozer.loader.xml;
 17  
 
 18  
 import org.dozer.MappingException;
 19  
 import org.xml.sax.Attributes;
 20  
 import org.xml.sax.SAXException;
 21  
 import org.xml.sax.SAXParseException;
 22  
 import org.xml.sax.helpers.DefaultHandler;
 23  
 import org.slf4j.Logger;
 24  
 import org.slf4j.LoggerFactory;
 25  
 
 26  
 import javax.xml.parsers.DocumentBuilder;
 27  
 import javax.xml.parsers.DocumentBuilderFactory;
 28  
 import javax.xml.parsers.ParserConfigurationException;
 29  
 
 30  
 /**
 31  
  *
 32  
  * Factory class for providing valid XML parsers. Dozer uses DOM approach for XML processing.
 33  
  *
 34  
  * @author dmitry.buzdin
 35  
  */
 36  
 public final class XMLParserFactory {
 37  
 
 38  
   private static final String SCHEMA_FEATURE = "http://apache.org/xml/features/validation/schema";
 39  
 
 40  1
   private static final XMLParserFactory instance = new XMLParserFactory();
 41  
 
 42  
   public static XMLParserFactory getInstance() {
 43  705
     return instance;
 44  
   }
 45  
 
 46  1
   private XMLParserFactory() {
 47  1
   }
 48  
 
 49  
   public DocumentBuilder createParser() {
 50  705
     DocumentBuilderFactory factory = createDocumentBuilderFactory();
 51  
     try {
 52  705
       return createDocumentBuilder(factory);
 53  0
     } catch (ParserConfigurationException e) {
 54  0
       throw new MappingException("Failed to create XML Parser !", e);
 55  
     }
 56  
   }
 57  
 
 58  
   /**
 59  
    * Create a JAXP DocumentBuilderFactory that this bean definition reader will use for parsing XML documents. Can be
 60  
    * overridden in subclasses, adding further initialization of the factory.
 61  
    *
 62  
    * @return the JAXP DocumentBuilderFactory
 63  
    * @throws javax.xml.parsers.ParserConfigurationException
 64  
    *          if thrown by JAXP methods
 65  
    */
 66  
   private DocumentBuilderFactory createDocumentBuilderFactory() {
 67  705
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 68  705
     factory.setValidating(true);
 69  705
     factory.setNamespaceAware(true);
 70  705
     factory.setIgnoringElementContentWhitespace(true);
 71  705
     factory.setAttribute(SCHEMA_FEATURE, true); // For Xerces implementation
 72  705
     return factory;
 73  
   }
 74  
 
 75  
   /**
 76  
    * Create a JAXP DocumentBuilder that this bean definition reader will use for parsing XML documents. Can be
 77  
    * overridden in subclasses, adding further initialization of the builder.
 78  
    *
 79  
    * @param factory the JAXP DocumentBuilderFactory that the DocumentBuilder should be created with
 80  
    * @return the JAXP DocumentBuilder
 81  
    * @throws javax.xml.parsers.ParserConfigurationException
 82  
    *          if thrown by JAXP methods
 83  
    */
 84  
   private DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException {
 85  705
     DocumentBuilder docBuilder = factory.newDocumentBuilder();
 86  705
     docBuilder.setErrorHandler(new DozerDefaultHandler());
 87  705
     docBuilder.setEntityResolver(new DozerResolver());
 88  705
     return docBuilder;
 89  
   }
 90  
 
 91  1410
   private static class DozerDefaultHandler extends DefaultHandler {
 92  
 
 93  705
     private final Logger log = LoggerFactory.getLogger(DozerDefaultHandler.class);
 94  
 
 95  
     @Override
 96  
     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
 97  0
       log.debug("tag: {}", qName);
 98  0
     }
 99  
 
 100  
     @Override
 101  
     public void warning(SAXParseException e) throws SAXException {
 102  
       // you can choose not to handle it
 103  0
       throw new SAXException(getMessage("Warning", e));
 104  
     }
 105  
 
 106  
     @Override
 107  
     public void error(SAXParseException e) throws SAXException {
 108  4
       throw new SAXException(getMessage("Error", e));
 109  
     }
 110  
 
 111  
     @Override
 112  
     public void fatalError(SAXParseException e) throws SAXException {
 113  0
       throw new SAXException(getMessage("Fatal Error", e));
 114  
     }
 115  
 
 116  
     private String getMessage(String level, SAXParseException e) {
 117  4
       return ("Parsing " + level + "\n" + "Line:    " + e.getLineNumber() + "\n" + "URI:     " + e.getSystemId() + "\n"
 118  
               + "Message: " + e.getMessage());
 119  
     }
 120  
   }
 121  
 
 122  
 
 123  
 }