Coverage Report - org.owasp.dependencycheck.xml.pom.PomHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
PomHandler
77%
35/45
60%
23/38
5.75
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 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  
  * Copyright (c) 2015 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.xml.pom;
 19  
 
 20  
 import java.util.ArrayDeque;
 21  
 import java.util.Deque;
 22  
 import org.xml.sax.Attributes;
 23  
 import org.xml.sax.SAXException;
 24  
 import org.xml.sax.helpers.DefaultHandler;
 25  
 
 26  
 /**
 27  
  * A handler to read the pom.xml model.
 28  
  *
 29  
  * @author Jeremy Long
 30  
  */
 31  2
 public class PomHandler extends DefaultHandler {
 32  
 
 33  
     /**
 34  
      * The project element.
 35  
      */
 36  
     public static final String PROJECT = "project";
 37  
     /**
 38  
      * The artifactId element.
 39  
      */
 40  
     public static final String GROUPID = "groupId";
 41  
     /**
 42  
      * The artifactId element.
 43  
      */
 44  
     public static final String ARTIFACTID = "artifactId";
 45  
     /**
 46  
      * The version element.
 47  
      */
 48  
     public static final String VERSION = "version";
 49  
     /**
 50  
      * The parent element.
 51  
      */
 52  
     public static final String PARENT = "parent";
 53  
     /**
 54  
      * The name element.
 55  
      */
 56  
     public static final String NAME = "name";
 57  
     /**
 58  
      * The organization element.
 59  
      */
 60  
     public static final String ORGANIZATION = "organization";
 61  
     /**
 62  
      * The description element.
 63  
      */
 64  
     public static final String DESCRIPTION = "description";
 65  
     /**
 66  
      * The licenses element.
 67  
      */
 68  
     public static final String LICENSES = "licenses";
 69  
     /**
 70  
      * The license element.
 71  
      */
 72  
     public static final String LICENSE = "license";
 73  
     /**
 74  
      * The url element.
 75  
      */
 76  
     public static final String URL = "url";
 77  
 
 78  
     /**
 79  
      * The pom model.
 80  
      */
 81  2
     private final Model model = new Model();
 82  
 
 83  
     /**
 84  
      * Returns the model obtained from the pom.xml.
 85  
      *
 86  
      * @return the model object
 87  
      */
 88  
     public Model getModel() {
 89  2
         return model;
 90  
     }
 91  
     /**
 92  
      * The stack of elements processed; used to determine the parent node.
 93  
      */
 94  2
     private final Deque<String> stack = new ArrayDeque<String>();
 95  
     /**
 96  
      * The license object.
 97  
      */
 98  2
     private License license = null;
 99  
 
 100  
     /**
 101  
      * The current node text being extracted from the element.
 102  
      */
 103  
     private StringBuilder currentText;
 104  
 
 105  
     /**
 106  
      * Handles the start element event.
 107  
      *
 108  
      * @param uri the uri of the element being processed
 109  
      * @param localName the local name of the element being processed
 110  
      * @param qName the qName of the element being processed
 111  
      * @param attributes the attributes of the element being processed
 112  
      * @throws SAXException thrown if there is an exception processing
 113  
      */
 114  
     @Override
 115  
     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
 116  471
         currentText = new StringBuilder();
 117  471
         stack.push(qName);
 118  471
         if (LICENSE.equals(qName)) {
 119  0
             license = new License();
 120  
         }
 121  471
     }
 122  
 
 123  
     /**
 124  
      * Handles the end element event.
 125  
      *
 126  
      * @param uri the URI of the element
 127  
      * @param localName the local name of the element
 128  
      * @param qName the qName of the element
 129  
      * @throws SAXException thrown if there is an exception processing
 130  
      */
 131  
     @Override
 132  
     public void endElement(String uri, String localName, String qName) throws SAXException {
 133  471
         stack.pop();
 134  471
         final String parentNode = stack.peek();
 135  471
         if (PROJECT.equals(parentNode)) {
 136  29
             if (GROUPID.equals(qName)) {
 137  2
                 model.setGroupId(currentText.toString());
 138  27
             } else if (ARTIFACTID.equals(qName)) {
 139  2
                 model.setArtifactId(currentText.toString());
 140  25
             } else if (VERSION.equals(qName)) {
 141  1
                 model.setVersion(currentText.toString());
 142  24
             } else if (NAME.equals(qName)) {
 143  2
                 model.setName(currentText.toString());
 144  22
             } else if (ORGANIZATION.equals(qName)) {
 145  0
                 model.setOrganization(currentText.toString());
 146  22
             } else if (DESCRIPTION.equals(qName)) {
 147  1
                 model.setDescription(currentText.toString());
 148  
             }
 149  442
         } else if (PARENT.equals(parentNode)) {
 150  3
             if (GROUPID.equals(qName)) {
 151  1
                 model.setParentGroupId(currentText.toString());
 152  2
             } else if (ARTIFACTID.equals(qName)) {
 153  1
                 model.setParentArtifactId(currentText.toString());
 154  1
             } else if (VERSION.equals(qName)) {
 155  1
                 model.setParentVersion(currentText.toString());
 156  
             }
 157  439
         } else if (LICENSE.equals(parentNode)) {
 158  0
             if (license != null) {
 159  0
                 if (NAME.equals(qName)) {
 160  0
                     license.setName(currentText.toString());
 161  0
                 } else if (URL.equals(qName)) {
 162  0
                     license.setUrl(currentText.toString());
 163  
                 }
 164  
                 //} else {
 165  
                 //TODO add error logging
 166  
             }
 167  439
         } else if (LICENSES.equals(parentNode)) {
 168  0
             if (LICENSE.equals(qName)) {
 169  0
                 if (license != null) {
 170  0
                     model.addLicense(license);
 171  
                     //} else {
 172  
                     //TODO add error logging
 173  
                 }
 174  
             }
 175  
         }
 176  471
     }
 177  
 
 178  
     /**
 179  
      * Collects the body text of the node being processed.
 180  
      *
 181  
      * @param ch the char array of text
 182  
      * @param start the start position to copy text from in the char array
 183  
      * @param length the number of characters to copy from the char array
 184  
      * @throws SAXException thrown if there is a parsing exception
 185  
      */
 186  
     @Override
 187  
     public void characters(char[] ch, int start, int length) throws SAXException {
 188  953
         currentText.append(ch, start, length);
 189  953
     }
 190  
 }