Coverage Report - com.mattunderscore.filter.contentnegotiation.variantsource.VariantXMLParser
 
Classes in this File Line Coverage Branch Coverage Complexity
VariantXMLParser
95%
70/73
97%
47/48
4.333
 
 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.InputStream;
 29  
 import java.util.ArrayList;
 30  
 import java.util.List;
 31  
 import java.util.Stack;
 32  
 
 33  
 import javax.xml.XMLConstants;
 34  
 import javax.xml.transform.stream.StreamSource;
 35  
 import javax.xml.validation.Schema;
 36  
 import javax.xml.validation.SchemaFactory;
 37  
 
 38  
 import org.xml.sax.Attributes;
 39  
 import org.xml.sax.SAXException;
 40  
 import org.xml.sax.SAXParseException;
 41  
 
 42  
 import com.mattunderscore.filter.contentnegotiation.parser.Header;
 43  
 import com.mattunderscore.filter.contentnegotiation.parser.HeaderPart;
 44  
 import com.mattunderscore.filter.contentnegotiation.parser.Output;
 45  
 import com.mattunderscore.filter.contentnegotiation.parser.Redirect;
 46  
 import com.mattunderscore.filter.contentnegotiation.parser.Variant;
 47  
 
 48  
 /**
 49  
  * Parser for the Negotiation XML file.
 50  
  * <P>
 51  
  * It is not intended to be called directly. This parser is called by {@link FilterVariantXMLSource}
 52  
  * and that should be used to get variants from an XML file. This returns the schema
 53  
  * "http://www.mattunderscore.com/schema/negotiation.xsd".
 54  
  * 
 55  
  * @author Matt Champion
 56  
  * @since 0.2.0
 57  
  */
 58  
 public final  class VariantXMLParser extends VariantSAXHandler
 59  
 {
 60  34
     private Stack<StringBuilder> elementCharacterData = new Stack<StringBuilder>();
 61  
     private List<Variant> variants;
 62  
     private Variant currentVariant;
 63  
     private Output currentOutput;
 64  
     private Header currentHeader;
 65  
     private Redirect currentRedirect;
 66  
     private HeaderPart currentHeaderPart;
 67  
     private StringBuilder tempString;
 68  
     private List<HeaderPart> currentHeaderParts;
 69  
 
 70  
     public VariantXMLParser()
 71  34
     {
 72  34
     }
 73  
 
 74  
     @Override
 75  
     public void startElement(String s, String s1, String elementName, Attributes attributes)
 76  
             throws SAXException
 77  
     {
 78  282
         tempString = new StringBuilder(50);
 79  282
         elementCharacterData.push(tempString);
 80  282
         if ("variants".equals(elementName))
 81  
         {
 82  20
             variants = new ArrayList<Variant>();
 83  
         }
 84  262
         else if ("variant".equals(elementName))
 85  
         {
 86  34
             currentVariant = new Variant();
 87  
         }
 88  228
         else if ("output".equals(elementName))
 89  
         {
 90  34
             currentOutput = new Output();
 91  
         }
 92  194
         else if ("header".equals(elementName))
 93  
         {
 94  4
             currentHeader = new Header();
 95  4
             currentHeaderParts = new ArrayList<HeaderPart>();
 96  
         }
 97  190
         else if ("redirect".equals(elementName))
 98  
         {
 99  18
             currentRedirect = new Redirect();
 100  
         }
 101  172
         else if ("fail".equals(elementName))
 102  
         {
 103  8
             currentOutput.setFail(true);
 104  
         }
 105  164
         else if ("part".equals(elementName))
 106  
         {
 107  6
             currentHeaderPart = new HeaderPart();
 108  
         }
 109  282
     }
 110  
 
 111  
     @Override
 112  
     public void endElement(String prefix, String localName, String qualifiedName)
 113  
             throws SAXException
 114  
     {
 115  280
         if ("variant".equals(qualifiedName))
 116  
         {
 117  34
             variants.add(currentVariant);
 118  
         }
 119  246
         else if ("urlPattern".equals(qualifiedName))
 120  
         {
 121  34
             currentVariant.setUrlPattern(tempString.toString());
 122  
         }
 123  212
         else if ("content-type".equals(qualifiedName))
 124  
         {
 125  34
             currentVariant.setContentType(tempString.toString());
 126  
         }
 127  178
         else if ("language".equals(qualifiedName))
 128  
         {
 129  34
             currentVariant.setLanguage(tempString.toString());
 130  
         }
 131  144
         else if ("output".equals(qualifiedName))
 132  
         {
 133  34
             currentVariant.setOutput(currentOutput);
 134  
         }
 135  110
         else if ("forward".equals(qualifiedName))
 136  
         {
 137  4
             currentOutput.setForward(tempString.toString());
 138  
         }
 139  106
         else if ("redirect".equals(qualifiedName))
 140  
         {
 141  18
             currentOutput.setRedirect(currentRedirect);
 142  
         }
 143  88
         else if ("header".equals(qualifiedName))
 144  
         {
 145  4
             currentHeader.setPart(currentHeaderParts);
 146  4
             currentOutput.setHeader(currentHeader);
 147  
         }
 148  84
         else if ("fail".equals(qualifiedName))
 149  
         {
 150  8
             currentOutput.setFail(true);
 151  
         }
 152  76
         else if ("part".equals(qualifiedName))
 153  
         {
 154  6
             currentHeaderParts.add(currentHeaderPart);
 155  
         }
 156  70
         else if ("code".equals(qualifiedName))
 157  
         {
 158  22
             int code = Integer.parseInt(tempString.toString());
 159  22
             if (currentRedirect != null)
 160  
             {
 161  18
                 currentRedirect.setCode(code);
 162  
             }
 163  4
             else if (currentHeader != null)
 164  
             {
 165  4
                 currentHeader.setCode(code);
 166  
             }
 167  22
         }
 168  48
         else if ("location".equals(qualifiedName))
 169  
         {
 170  18
             currentRedirect.setLocation(tempString.toString());
 171  
         }
 172  30
         else if ("name".equals(qualifiedName))
 173  
         {
 174  6
             currentHeaderPart.setName(tempString.toString());
 175  
         }
 176  24
         else if ("value".equals(qualifiedName))
 177  
         {
 178  6
             currentHeaderPart.setValue(tempString.toString());
 179  
         }
 180  280
         elementCharacterData.pop();
 181  280
         if (!elementCharacterData.isEmpty())
 182  
         {
 183  262
             tempString = elementCharacterData.peek();
 184  
         }
 185  280
     }
 186  
 
 187  
     @Override
 188  
     public void characters(char[] chars, int start, int length)
 189  
     {
 190  166
         tempString.append(new String(chars, start, length));
 191  166
     }
 192  
 
 193  
     @Override
 194  
     public List<Variant> getVariants()
 195  
     {
 196  18
         return variants;
 197  
     }
 198  
 
 199  
     @Override
 200  
     public void error(SAXParseException exception) throws SAXParseException
 201  
     {
 202  2
         throw exception;
 203  
     }
 204  
 
 205  
     @Override
 206  
     public void fatalError(SAXParseException exception) throws SAXParseException
 207  
     {
 208  2
         throw exception;
 209  
     }
 210  
 
 211  
     @Override
 212  
     public void warning(SAXParseException exception) throws SAXParseException
 213  
     {
 214  0
         throw exception;
 215  
     }
 216  
 
 217  
     @Override
 218  
     public Schema getSchema() throws NoSchemaException
 219  
     {
 220  
         try
 221  
         {
 222  22
             InputStream inputStream = VariantXMLParser.class
 223  22
                     .getResourceAsStream("/negotiation.xsd");
 224  22
             StreamSource source = new StreamSource(inputStream);
 225  22
             SchemaFactory schemaFactory = SchemaFactory
 226  22
                     .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 227  22
             return schemaFactory.newSchema(source);
 228  
         }
 229  0
         catch (SAXException exception)
 230  
         {
 231  0
             throw new NoSchemaException(exception);
 232  
         }
 233  
     }
 234  
 }