| 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 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| |
|
| 93.7% |
Uncovered Elements: 11 (175) |
Complexity: 50 |
Complexity Density: 0.49 |
|
| 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 |
|
|
| 38 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 39 |
22 |
public SaxDocumentHandler() {... |
| 40 |
22 |
super(); |
| 41 |
22 |
LOGGER.log("instance class", LogLevel.INFO); |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@return |
| 48 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 49 |
16 |
public String prettyPrintXml() {... |
| 50 |
16 |
return this.formattedXml.toString(); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
@return |
| 57 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 58 |
7 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 66 |
29 |
@Override... |
| 67 |
|
public void startDocument() throws SAXException { |
| 68 |
29 |
LOGGER.log("SAX DefaultHandler start to parse Document.", LogLevel.DEBUG); |
| 69 |
|
} |
| 70 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 71 |
20 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 77 |
22 |
@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 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 5 |
Complexity Density: 0.62 |
|
| 83 |
8 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 106 |
8 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 115 |
48 |
@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 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 126 |
8 |
@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 |
|
|
| |
|
| 76.9% |
Uncovered Elements: 3 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 141 |
8 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 156 |
8 |
@Override... |
| 157 |
|
public void endCDATA() throws SAXException { |
| 158 |
8 |
this.formattedXml.append("]]>"); |
| 159 |
|
} |
| 160 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 6 |
Complexity Density: 0.5 |
|
| 161 |
241 |
@Override... |
| 162 |
|
public void startElement(final String uri, final String localName, final String qName, |
| 163 |
|
final Attributes attributes) throws SAXException { |
| 164 |
|
|
| 165 |
|
|
| 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 |
|
|
| 173 |
241 |
if (this.level == 0) { |
| 174 |
27 |
this.processNamespace(qName, attributes); |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
|
| 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 |
|
|
| 190 |
241 |
this.formattedXml.append(">"); |
| 191 |
241 |
this.level++; |
| 192 |
241 |
this.isTagOpend = true; |
| 193 |
|
} |
| 194 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 195 |
226 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 211 |
347 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 219 |
19 |
@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 |
|
|
| |
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 239 |
0 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 246 |
3 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 253 |
6 |
@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 |
|
|
| |
|
| 98% |
Uncovered Elements: 1 (49) |
Complexity: 11 |
Complexity Density: 0.33 |
|
| 260 |
27 |
private void processNamespace(final String qName, final Attributes attributes) {... |
| 261 |
27 |
if (attributes.getLength() > 0) { |
| 262 |
13 |
try { |
| 263 |
|
|
| 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 |
|
|
| 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 |
|
} |