View Javadoc
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  
20  import javax.annotation.concurrent.NotThreadSafe;
21  import org.xml.sax.Attributes;
22  import org.xml.sax.SAXException;
23  import org.xml.sax.helpers.DefaultHandler;
24  
25  /**
26   * A handler to read Grok Assembly XML files.
27   *
28   * @author Jeremy Long
29   */
30  @NotThreadSafe
31  public class GrokHandler extends DefaultHandler {
32  
33      /**
34       * An XML node name.
35       */
36      private static final String ERROR = "error";
37      /**
38       * An XML node name.
39       */
40      private static final String WARNING = "warning";
41      /**
42       * An XML node name.
43       */
44      private static final String COMPANY_NAME = "companyName";
45      /**
46       * An XML node name.
47       */
48      private static final String PRODUCT_NAME = "productName";
49      /**
50       * An XML node name.
51       */
52      private static final String PRODUCT_VERSION = "productVersion";
53      /**
54       * An XML node name.
55       */
56      private static final String COMMENTS = "comments";
57      /**
58       * An XML node name.
59       */
60      private static final String FILE_DESCRIPTION = "fileDescription";
61      /**
62       * An XML node name.
63       */
64      private static final String FILE_NAME = "fileName";
65      /**
66       * An XML node name.
67       */
68      private static final String FILE_VERSION = "fileVersion";
69      /**
70       * An XML node name.
71       */
72      private static final String INTERNAL_NAME = "internalName";
73      /**
74       * An XML node name.
75       */
76      private static final String ORIGINAL_FILE_NAME = "originalFilename";
77      /**
78       * An XML node name.
79       */
80      private static final String FULLNAME = "fullName";
81      /**
82       * An XML node name.
83       */
84      private static final String NAMESPACE = "namespace";
85  
86      /**
87       * The current rule being read.
88       */
89      private final AssemblyData data = new AssemblyData();
90      /**
91       * The current node text being extracted from the element.
92       */
93      private StringBuilder currentText;
94  
95      /**
96       * Handles the start element event.
97       *
98       * @param uri the uri of the element being processed
99       * @param localName the local name of the element being processed
100      * @param qName the qName of the element being processed
101      * @param attributes the attributes of the element being processed
102      * @throws SAXException thrown if there is an exception processing
103      */
104     @Override
105     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
106         currentText = new StringBuilder();
107     }
108 
109     /**
110      * Handles the end element event.
111      *
112      * @param uri the URI of the element
113      * @param localName the local name of the element
114      * @param qName the qName of the element
115      * @throws SAXException thrown if there is an exception processing
116      */
117     @Override
118     public void endElement(String uri, String localName, String qName) throws SAXException {
119         if (null != qName) {
120             switch (qName) {
121                 case COMPANY_NAME:
122                     data.setCompanyName(currentText.toString());
123                     break;
124                 case PRODUCT_NAME:
125                     data.setProductName(currentText.toString());
126                     break;
127                 case PRODUCT_VERSION:
128                     data.setProductVersion(currentText.toString());
129                     break;
130                 case COMMENTS:
131                     data.setComments(currentText.toString());
132                     break;
133                 case FILE_DESCRIPTION:
134                     data.setFileDescription(currentText.toString());
135                     break;
136                 case FILE_NAME:
137                     data.setFileName(currentText.toString());
138                     break;
139                 case FILE_VERSION:
140                     data.setFileVersion(currentText.toString());
141                     break;
142                 case INTERNAL_NAME:
143                     data.setInternalName(currentText.toString());
144                     break;
145                 case ORIGINAL_FILE_NAME:
146                     data.setOriginalFilename(currentText.toString());
147                     break;
148                 case FULLNAME:
149                     data.setFullName(currentText.toString());
150                     break;
151                 case NAMESPACE:
152                     data.addNamespace(currentText.toString());
153                     break;
154                 case ERROR:
155                     data.setError(currentText.toString());
156                     break;
157                 case WARNING:
158                     data.setWarning(currentText.toString());
159                     break;
160                 default:
161                     break;
162             }
163         }
164     }
165 
166     /**
167      * Collects the body text of the node being processed.
168      *
169      * @param ch the char array of text
170      * @param start the start position to copy text from in the char array
171      * @param length the number of characters to copy from the char array
172      * @throws SAXException thrown if there is a parsing exception
173      */
174     @Override
175     public void characters(char[] ch, int start, int length) throws SAXException {
176         currentText.append(ch, start, length);
177     }
178 
179     AssemblyData getAssemblyData() {
180         return data;
181     }
182 }