GrokHandler.java

  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) 2019 Jeremy Long. All Rights Reserved.
  17.  */
  18. package org.owasp.dependencycheck.xml.assembly;

  19. import javax.annotation.concurrent.NotThreadSafe;
  20. import org.xml.sax.Attributes;
  21. import org.xml.sax.SAXException;
  22. import org.xml.sax.helpers.DefaultHandler;

  23. /**
  24.  * A handler to read Grok Assembly XML files.
  25.  *
  26.  * @author Jeremy Long
  27.  */
  28. @NotThreadSafe
  29. public class GrokHandler extends DefaultHandler {

  30.     /**
  31.      * An XML node name.
  32.      */
  33.     private static final String ERROR = "error";
  34.     /**
  35.      * An XML node name.
  36.      */
  37.     private static final String WARNING = "warning";
  38.     /**
  39.      * An XML node name.
  40.      */
  41.     private static final String COMPANY_NAME = "companyName";
  42.     /**
  43.      * An XML node name.
  44.      */
  45.     private static final String PRODUCT_NAME = "productName";
  46.     /**
  47.      * An XML node name.
  48.      */
  49.     private static final String PRODUCT_VERSION = "productVersion";
  50.     /**
  51.      * An XML node name.
  52.      */
  53.     private static final String COMMENTS = "comments";
  54.     /**
  55.      * An XML node name.
  56.      */
  57.     private static final String FILE_DESCRIPTION = "fileDescription";
  58.     /**
  59.      * An XML node name.
  60.      */
  61.     private static final String FILE_NAME = "fileName";
  62.     /**
  63.      * An XML node name.
  64.      */
  65.     private static final String FILE_VERSION = "fileVersion";
  66.     /**
  67.      * An XML node name.
  68.      */
  69.     private static final String INTERNAL_NAME = "internalName";
  70.     /**
  71.      * An XML node name.
  72.      */
  73.     private static final String ORIGINAL_FILE_NAME = "originalFilename";
  74.     /**
  75.      * An XML node name.
  76.      */
  77.     private static final String FULLNAME = "fullName";
  78.     /**
  79.      * An XML node name.
  80.      */
  81.     private static final String NAMESPACE = "namespace";

  82.     /**
  83.      * The current rule being read.
  84.      */
  85.     private final AssemblyData data = new AssemblyData();
  86.     /**
  87.      * The current node text being extracted from the element.
  88.      */
  89.     private StringBuilder currentText;

  90.     /**
  91.      * Handles the start element event.
  92.      *
  93.      * @param uri the uri of the element being processed
  94.      * @param localName the local name of the element being processed
  95.      * @param qName the qName of the element being processed
  96.      * @param attributes the attributes of the element being processed
  97.      * @throws SAXException thrown if there is an exception processing
  98.      */
  99.     @Override
  100.     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  101.         currentText = new StringBuilder();
  102.     }

  103.     /**
  104.      * Handles the end element event.
  105.      *
  106.      * @param uri the URI of the element
  107.      * @param localName the local name of the element
  108.      * @param qName the qName of the element
  109.      * @throws SAXException thrown if there is an exception processing
  110.      */
  111.     @Override
  112.     public void endElement(String uri, String localName, String qName) throws SAXException {
  113.         if (null != qName) {
  114.             switch (qName) {
  115.                 case COMPANY_NAME:
  116.                     data.setCompanyName(currentText.toString());
  117.                     break;
  118.                 case PRODUCT_NAME:
  119.                     data.setProductName(currentText.toString());
  120.                     break;
  121.                 case PRODUCT_VERSION:
  122.                     data.setProductVersion(currentText.toString());
  123.                     break;
  124.                 case COMMENTS:
  125.                     data.setComments(currentText.toString());
  126.                     break;
  127.                 case FILE_DESCRIPTION:
  128.                     data.setFileDescription(currentText.toString());
  129.                     break;
  130.                 case FILE_NAME:
  131.                     data.setFileName(currentText.toString());
  132.                     break;
  133.                 case FILE_VERSION:
  134.                     data.setFileVersion(currentText.toString());
  135.                     break;
  136.                 case INTERNAL_NAME:
  137.                     data.setInternalName(currentText.toString());
  138.                     break;
  139.                 case ORIGINAL_FILE_NAME:
  140.                     data.setOriginalFilename(currentText.toString());
  141.                     break;
  142.                 case FULLNAME:
  143.                     data.setFullName(currentText.toString());
  144.                     break;
  145.                 case NAMESPACE:
  146.                     data.addNamespace(currentText.toString());
  147.                     break;
  148.                 case ERROR:
  149.                     data.setError(currentText.toString());
  150.                     break;
  151.                 case WARNING:
  152.                     data.setWarning(currentText.toString());
  153.                     break;
  154.                 default:
  155.                     break;
  156.             }
  157.         }
  158.     }

  159.     /**
  160.      * Collects the body text of the node being processed.
  161.      *
  162.      * @param ch the char array of text
  163.      * @param start the start position to copy text from in the char array
  164.      * @param length the number of characters to copy from the char array
  165.      * @throws SAXException thrown if there is a parsing exception
  166.      */
  167.     @Override
  168.     public void characters(char[] ch, int start, int length) throws SAXException {
  169.         currentText.append(ch, start, length);
  170.     }

  171.     AssemblyData getAssemblyData() {
  172.         return data;
  173.     }
  174. }