1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application.internal

File SaxDocumentHandler.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

52
103
20
1
316
243
50
0.49
5.15
20
2.5

Classes

Class Line # Actions
SaxDocumentHandler 22 103 0% 50 11
0.9371428593.7%
 

Contributing tests

This file is covered by 18 tests. .

Source view

1    package org.europa.together.application.internal;
2   
3    import java.io.File;
4    import java.util.ArrayList;
5    import java.util.List;
6    import javax.xml.transform.Source;
7    import javax.xml.transform.stream.StreamSource;
8    import org.europa.together.application.LogbackLogger;
9    import org.europa.together.business.Logger;
10    import org.europa.together.domain.LogLevel;
11    import org.europa.together.utils.StringUtils;
12    import org.xml.sax.Attributes;
13    import org.xml.sax.SAXException;
14    import org.xml.sax.SAXParseException;
15    import org.xml.sax.ext.DefaultHandler2;
16   
17    /**
18    * This class extends the SAX2 base handler class to support the SAX2
19    * <b>LexicalHandler</b>, <b>DeclHandler</b>, and <b>EntityResolver2</b>
20    * extensions.
21    */
 
22    public class SaxDocumentHandler extends DefaultHandler2 {
23   
24    private static final Logger LOGGER = new LogbackLogger(SaxDocumentHandler.class);
25   
26    private StringBuilder formattedXml
27    = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
28    private StringBuilder inlineDtd = new StringBuilder();
29   
30    private final List<String> namespace = new ArrayList<>();
31    private int level = 0;
32    private boolean isTagOpend = false;
33    private boolean hasInineDtd = false;
34    private Source[] schematList = null;
35   
36    /**
37    * Constructor.
38    */
 
39  22 toggle public SaxDocumentHandler() {
40  22 super();
41  22 LOGGER.log("instance class", LogLevel.INFO);
42    }
43   
44    /**
45    * Simple XML Beautifier.
46    *
47    * @return xmlBeautified as String
48    */
 
49  16 toggle public String prettyPrintXml() {
50  16 return this.formattedXml.toString();
51    }
52   
53    /**
54    * Return all parsed Schema files in a SchemaFactory for validation.
55    *
56    * @return schemata as SchemaFactory
57    */
 
58  7 toggle public Source[] getSchemaFiles() {
59  7 Source[] copy = null;
60  7 if (this.schematList != null) {
61  3 copy = this.schematList.clone();
62    }
63  7 return copy;
64    }
65   
 
66  29 toggle @Override
67    public void startDocument() throws SAXException {
68  29 LOGGER.log("SAX DefaultHandler start to parse Document.", LogLevel.DEBUG);
69    }
70   
 
71  20 toggle @Override
72    public void endDocument() throws SAXException {
73  20 this.formattedXml.append("\n");
74  20 LOGGER.log("SAX DefaultHandler finish to parse Document.", LogLevel.DEBUG);
75    }
76   
 
77  22 toggle @Override
78    public void startPrefixMapping(final String prefix, final String uri)
79    throws SAXException {
80  22 this.namespace.add(" xmlns:" + prefix + "=\"" + uri + "\" \n");
81    }
82   
 
83  8 toggle @Override
84    public void startDTD(final String name, final String publicId, final String systemId)
85    throws SAXException {
86   
87  8 this.formattedXml.append("\n<!DOCTYPE ")
88    .append(name);
89   
90  8 if (!StringUtils.isEmpty(publicId)) {
91  0 this.formattedXml.append(" PUBLIC \"")
92    .append(publicId).append("\"");
93    }
94   
95  8 if (!StringUtils.isEmpty(systemId)) {
96  5 this.formattedXml.append(" SYSTEM \"")
97    .append(systemId).append("\"");
98    }
99   
100  8 if (StringUtils.isEmpty(publicId) && StringUtils.isEmpty(systemId)) {
101  3 this.formattedXml.append(" [\n");
102  3 this.hasInineDtd = true;
103    }
104    }
105   
 
106  8 toggle @Override
107    public void endDTD() throws SAXException {
108   
109  8 if (this.hasInineDtd) {
110  3 this.formattedXml.append(inlineDtd).append("]");
111    }
112  8 this.formattedXml.append(">");
113    }
114   
 
115  48 toggle @Override
116    public void elementDecl(final String name, final String model)
117    throws SAXException {
118   
119  48 this.inlineDtd.append(" <!ELEMENT ")
120    .append(name)
121    .append(" ")
122    .append(model)
123    .append(">\n");
124    }
125   
 
126  8 toggle @Override
127    public void attributeDecl(final String eName, final String aName, final String type,
128    final String mode, final String value) throws SAXException {
129   
130  8 this.inlineDtd.append(" <!ATTLIST ")
131    .append(eName).append(" ")
132    .append(aName).append(" ")
133    .append(type).append(" ")
134    .append(mode);
135  8 if (!StringUtils.isEmpty(value)) {
136  0 this.inlineDtd.append(" ").append(value);
137    }
138  8 this.inlineDtd.append(">\n");
139    }
140   
 
141  8 toggle @Override
142    public void startCDATA() throws SAXException {
143   
144  8 if (this.level == 0) {
145  0 this.formattedXml.append("\n");
146    }
147  8 if (this.level >= 1) {
148  8 this.formattedXml.append("\n");
149  18 for (int i = 0; i < this.level; i++) {
150  10 this.formattedXml.append(" ");
151    }
152    }
153  8 this.formattedXml.append("<![CDATA[");
154    }
155   
 
156  8 toggle @Override
157    public void endCDATA() throws SAXException {
158  8 this.formattedXml.append("]]>");
159    }
160   
 
161  241 toggle @Override
162    public void startElement(final String uri, final String localName, final String qName,
163    final Attributes attributes) throws SAXException {
164   
165    //TAG
166  241 this.formattedXml.append("\n");
167  615 for (int i = 0; i < level; i++) {
168  374 this.formattedXml.append(" ");
169    }
170  241 this.formattedXml.append("<").append(qName);
171   
172    //NAMESPACE
173  241 if (this.level == 0) {
174  27 this.processNamespace(qName, attributes);
175    }
176   
177    //ATTRIBUTES
178  241 if (attributes != null && attributes.getLength() != 0) {
179  125 for (int i = 0; i < attributes.getLength(); i++) {
180   
181  72 this.formattedXml.append(" ")
182    .append(attributes.getQName(i))
183    .append("=\"")
184    .append(attributes.getValue(i))
185    .append("\"");
186    }
187    }
188   
189    //CLOSE
190  241 this.formattedXml.append(">");
191  241 this.level++;
192  241 this.isTagOpend = true;
193    }
194   
 
195  226 toggle @Override
196    public void endElement(final String uri, final String localName, final String qName)
197    throws SAXException {
198   
199  226 if (!isTagOpend) {
200  61 this.formattedXml.append("\n");
201  106 for (int i = 1; i < this.level; i++) {
202  45 this.formattedXml.append(" ");
203    }
204    } else {
205  165 this.isTagOpend = false;
206    }
207  226 this.formattedXml.append("</").append(qName).append(">");
208  226 this.level--;
209    }
210   
 
211  347 toggle @Override
212    public void characters(final char[] ch, final int start, final int length)
213    throws SAXException {
214   
215  347 String characters = new String(ch, start, length).trim();
216  347 this.formattedXml.append(characters.trim());
217    }
218   
 
219  19 toggle @Override
220    public void comment(final char[] ch, final int start, final int length)
221    throws SAXException {
222   
223  19 if (this.level == 0) {
224  4 this.formattedXml.append("\n");
225    }
226  19 if (this.level >= 1) {
227  15 this.formattedXml.append("\n");
228  38 for (int i = 0; i < this.level; i++) {
229  23 this.formattedXml.append(" ");
230    }
231    }
232   
233  19 String comments = new String(ch, start, length).trim();
234  19 this.formattedXml.append("<!-- ")
235    .append(comments)
236    .append(" -->");
237    }
238   
 
239  0 toggle @Override
240    public void warning(final SAXParseException ex)
241    throws SAXException {
242  0 LOGGER.log("SAX LexicalParser: " + ex.getMessage(), LogLevel.WARN);
243  0 throw new SAXException("SAX Validation Error");
244    }
245   
 
246  3 toggle @Override
247    public void error(final SAXParseException ex)
248    throws SAXException {
249  3 LOGGER.log("SAX LexicalParser: " + ex.getMessage(), LogLevel.ERROR);
250  3 throw new SAXException("SAX Validation Error");
251    }
252   
 
253  6 toggle @Override
254    public void fatalError(final SAXParseException ex)
255    throws SAXException {
256  6 LOGGER.log("SAX LexicalParser: " + ex.getMessage(), LogLevel.ERROR);
257  6 throw new SAXException("SAX Validation Error");
258    }
259   
 
260  27 toggle private void processNamespace(final String qName, final Attributes attributes) {
261  27 if (attributes.getLength() > 0) {
262  13 try {
263    //Extract XSD Files
264  13 String[] xsdExtraction = attributes.getValue(0).split(" ");
265  13 List<String> schemaList = new ArrayList<>();
266  13 StringBuilder attribute = new StringBuilder();
267  13 attribute.append("Scanned Attributes: ");
268  13 for (String schema : xsdExtraction) {
269  50 if (!StringUtils.isEmpty(schema)) {
270  25 attribute.append("\n\t");
271  25 attribute.append(schema);
272  25 if (schema.endsWith(".xsd")) {
273  10 schemaList.add(schema);
274    }
275    }
276    }
277  13 LOGGER.log(attribute.toString(), LogLevel.TRACE);
278  13 int schemaCount = schemaList.size();
279  13 LOGGER.log(schemaCount + " XSD Schemata file(s) extracted.", LogLevel.DEBUG);
280   
281    //Create XSD Sources
282  13 this.schematList = new Source[schemaCount];
283  13 int count = 0;
284  23 for (int i = 0; i < schemaCount; i++) {
285   
286  10 String resource = schemaList.get(count);
287   
288  10 if (resource.contains("http")) {
289   
290  4 schematList[count] = new StreamSource(resource);
291  4 LOGGER.log("ADD URL: " + schemaList.get(count), LogLevel.DEBUG);
292    } else {
293  6 schematList[count] = new StreamSource(new File(resource));
294  6 LOGGER.log("ADD FILE: " + schemaList.get(count), LogLevel.DEBUG);
295    }
296   
297  10 count++;
298    }
299  13 LOGGER.log(schematList.length + " Elements added to schemata[]", LogLevel.DEBUG);
300   
301    } catch (Exception ex) {
302  0 LOGGER.catchException(ex);
303    }
304    }
305   
306  27 int space = qName.length() + 1;
307  27 if (this.namespace != null && this.namespace.size() > 0) {
308  41 for (int x = 0; x < this.namespace.size(); x++) {
309  29 this.formattedXml.append(this.namespace.get(x));
310  339 for (int i = 0; i < space; i++) {
311  310 this.formattedXml.append(" ");
312    }
313    }
314    }
315    }
316    }