Coverage Report - com.mattunderscore.filter.contentnegotiation.variantsource.FilterVariantXMLSource
 
Classes in this File Line Coverage Branch Coverage Complexity
FilterVariantXMLSource
55%
20/36
70%
7/10
5.667
 
 1  
 /* Copyright © 2013 Matthew Champion
 2  
 All rights reserved.
 3  
 
 4  
 Redistribution and use in source and binary forms, with or without
 5  
 modification, are permitted provided that the following conditions are met:
 6  
  * Redistributions of source code must retain the above copyright
 7  
       notice, this list of conditions and the following disclaimer.
 8  
  * Redistributions in binary form must reproduce the above copyright
 9  
       notice, this list of conditions and the following disclaimer in the
 10  
       documentation and/or other materials provided with the distribution.
 11  
  * Neither the name of mattunderscore.com nor the
 12  
       names of its contributors may be used to endorse or promote products
 13  
       derived from this software without specific prior written permission.
 14  
 
 15  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 16  
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 17  
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 18  
 DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
 19  
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 20  
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 21  
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 22  
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 23  
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 24  
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 25  
 
 26  
 package com.mattunderscore.filter.contentnegotiation.variantsource;
 27  
 
 28  
 import java.io.ByteArrayInputStream;
 29  
 import java.io.FileInputStream;
 30  
 import java.io.FileNotFoundException;
 31  
 import java.util.Enumeration;
 32  
 import java.util.HashMap;
 33  
 import java.util.List;
 34  
 import java.util.Map;
 35  
 
 36  
 import javax.servlet.FilterConfig;
 37  
 import javax.servlet.ServletContext;
 38  
 
 39  
 import com.mattunderscore.filter.contentnegotiation.parser.Variant;
 40  
 
 41  
 /**
 42  
  * A variant source that parses XML for variants and is configured from a filter. It is intended to
 43  
  * be called from filters and is configured with a filter config. Any configuration this needs
 44  
  * should be set in the filter init-params of the web.xml. The {@link VariantSAXHandler} that is
 45  
  * used to parse the XML as be configured.
 46  
  * <P>
 47  
  * It uses the {@link BaseVariantXMLSource} and enhances it by supporting configuration from a
 48  
  * {@link FilterConfig} object.
 49  
  * 
 50  
  * @author Matt Champion
 51  
  * @since 0.2.0
 52  
  */
 53  
 public class FilterVariantXMLSource implements VariantSourceForFilter
 54  
 {
 55  
     /**
 56  
      * Name of the parameter that identifies the SAX hander to use.
 57  
      */
 58  
     public static final String SAX_HANDLER = "saxHandlerClass";
 59  
     public static final String XML_INPUT_FILE = "xmlInputFile";
 60  
     public static final String XML_INPUT_STRING = "xmlInputString";
 61  
     /**
 62  
      * Name of parameter that identifies a resource that can be retrieved as a stream from the
 63  
      * servlet context.
 64  
      */
 65  
     public static final String XML_INPUT_RESOURCE = "xmlInputResource";
 66  
 
 67  8
     private final Map<String, String> parameterMap = new HashMap<String, String>();
 68  8
     private final BaseVariantXMLSource source = new BaseVariantXMLSource();
 69  
 
 70  
     public FilterVariantXMLSource()
 71  8
     {
 72  8
     }
 73  
 
 74  
     @Override
 75  
     public void configureSource(FilterConfig config) throws VariantSourceConfigurationException
 76  
     {
 77  8
         final ServletContext context = config.getServletContext();
 78  
         @SuppressWarnings("unchecked")
 79  8
         final Enumeration<String> parameterNames = config.getInitParameterNames();
 80  14
         while (parameterNames.hasMoreElements())
 81  
         {
 82  6
             final String parameterName = parameterNames.nextElement();
 83  6
             final String parameterValue = config.getInitParameter(parameterName);
 84  6
             parameterMap.put(parameterName, parameterValue);
 85  6
         }
 86  
 
 87  8
         if (parameterMap.containsKey(SAX_HANDLER))
 88  
         {
 89  
             try
 90  
             {
 91  4
                 final Class<?> handlerClass = Class.forName(parameterMap.get(SAX_HANDLER));
 92  4
                 final Object handlerObject = handlerClass.newInstance();
 93  4
                 source.setHandler((VariantSAXHandler) handlerObject);
 94  
             }
 95  0
             catch (final InstantiationException ex)
 96  
             {
 97  0
                 throw new VariantSourceConfigurationException("Unable to construct SAX handler",
 98  
                         ex);
 99  
             }
 100  0
             catch (final IllegalAccessException ex)
 101  
             {
 102  0
                 throw new VariantSourceConfigurationException("Unable to construct SAX handler",
 103  
                         ex);
 104  
             }
 105  0
             catch (final ClassNotFoundException ex)
 106  
             {
 107  0
                 throw new VariantSourceConfigurationException("Unable to construct SAX handler",
 108  
                         ex);
 109  2
             }
 110  
         }
 111  6
         if (parameterMap.containsKey(XML_INPUT_RESOURCE))
 112  
         {
 113  0
             source.setInputStream(context.getResourceAsStream(
 114  0
                     parameterMap.get(XML_INPUT_RESOURCE)));
 115  
         }
 116  6
         else if (parameterMap.containsKey(XML_INPUT_FILE))
 117  
         {
 118  
             try
 119  
             {
 120  0
                 source.setInputStream(new FileInputStream(parameterMap.get(XML_INPUT_FILE)));
 121  
             }
 122  0
             catch (final FileNotFoundException exception)
 123  
             {
 124  0
                 throw new VariantSourceConfigurationException("Unable get XML file", exception);
 125  0
             }
 126  
         }
 127  6
         else if (parameterMap.containsKey(XML_INPUT_STRING))
 128  
         {
 129  0
             source.setInputStream(new ByteArrayInputStream(parameterMap.get(XML_INPUT_STRING)
 130  0
                     .getBytes()));
 131  
         }
 132  
         else
 133  
         {
 134  6
             throw new VariantSourceConfigurationException("Input stream not set");
 135  
         }
 136  0
     }
 137  
 
 138  
     @Override
 139  
     public List<Variant> getVariants() throws VariantSourceException
 140  
     {
 141  0
         return source.getVariants();
 142  
     }
 143  
 }