| 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 | public SaxDocumentHandler() { | |
| 40 | super(); | |
| 41 | LOGGER.log("instance class", LogLevel.INFO); | |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * Simple XML Beautifier. | |
| 46 | * | |
| 47 | * @return xmlBeautified as String | |
| 48 | */ | |
| 49 | public String prettyPrintXml() { | |
| 50 |
1
1. prettyPrintXml : replaced return value with "" for org/europa/together/application/internal/SaxDocumentHandler::prettyPrintXml → KILLED |
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 | public Source[] getSchemaFiles() { | |
| 59 | Source[] copy = null; | |
| 60 |
1
1. getSchemaFiles : negated conditional → KILLED |
if (this.schematList != null) { |
| 61 | copy = this.schematList.clone(); | |
| 62 | } | |
| 63 |
1
1. getSchemaFiles : replaced return value with null for org/europa/together/application/internal/SaxDocumentHandler::getSchemaFiles → KILLED |
return copy; |
| 64 | } | |
| 65 | ||
| 66 | @Override | |
| 67 | public void startDocument() throws SAXException { | |
| 68 | LOGGER.log("SAX DefaultHandler start to parse Document.", LogLevel.DEBUG); | |
| 69 | } | |
| 70 | ||
| 71 | @Override | |
| 72 | public void endDocument() throws SAXException { | |
| 73 | this.formattedXml.append("\n"); | |
| 74 | LOGGER.log("SAX DefaultHandler finish to parse Document.", LogLevel.DEBUG); | |
| 75 | } | |
| 76 | ||
| 77 | @Override | |
| 78 | public void startPrefixMapping(final String prefix, final String uri) | |
| 79 | throws SAXException { | |
| 80 | this.namespace.add(" xmlns:" + prefix + "=\"" + uri + "\" \n"); | |
| 81 | } | |
| 82 | ||
| 83 | @Override | |
| 84 | public void startDTD(final String name, final String publicId, final String systemId) | |
| 85 | throws SAXException { | |
| 86 | ||
| 87 | this.formattedXml.append("\n<!DOCTYPE ") | |
| 88 | .append(name); | |
| 89 | ||
| 90 |
1
1. startDTD : negated conditional → KILLED |
if (!StringUtils.isEmpty(publicId)) { |
| 91 | this.formattedXml.append(" PUBLIC \"") | |
| 92 | .append(publicId).append("\""); | |
| 93 | } | |
| 94 | ||
| 95 |
1
1. startDTD : negated conditional → KILLED |
if (!StringUtils.isEmpty(systemId)) { |
| 96 | this.formattedXml.append(" SYSTEM \"") | |
| 97 | .append(systemId).append("\""); | |
| 98 | } | |
| 99 | ||
| 100 |
2
1. startDTD : negated conditional → KILLED 2. startDTD : negated conditional → KILLED |
if (StringUtils.isEmpty(publicId) && StringUtils.isEmpty(systemId)) { |
| 101 | this.formattedXml.append(" [\n"); | |
| 102 | this.hasInineDtd = true; | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | @Override | |
| 107 | public void endDTD() throws SAXException { | |
| 108 | ||
| 109 |
1
1. endDTD : negated conditional → KILLED |
if (this.hasInineDtd) { |
| 110 | this.formattedXml.append(inlineDtd).append("]"); | |
| 111 | } | |
| 112 | this.formattedXml.append(">"); | |
| 113 | } | |
| 114 | ||
| 115 | @Override | |
| 116 | public void elementDecl(final String name, final String model) | |
| 117 | throws SAXException { | |
| 118 | ||
| 119 | this.inlineDtd.append(" <!ELEMENT ") | |
| 120 | .append(name) | |
| 121 | .append(" ") | |
| 122 | .append(model) | |
| 123 | .append(">\n"); | |
| 124 | } | |
| 125 | ||
| 126 | @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 | this.inlineDtd.append(" <!ATTLIST ") | |
| 131 | .append(eName).append(" ") | |
| 132 | .append(aName).append(" ") | |
| 133 | .append(type).append(" ") | |
| 134 | .append(mode); | |
| 135 |
1
1. attributeDecl : negated conditional → KILLED |
if (!StringUtils.isEmpty(value)) { |
| 136 | this.inlineDtd.append(" ").append(value); | |
| 137 | } | |
| 138 | this.inlineDtd.append(">\n"); | |
| 139 | } | |
| 140 | ||
| 141 | @Override | |
| 142 | public void startCDATA() throws SAXException { | |
| 143 | ||
| 144 |
1
1. startCDATA : negated conditional → KILLED |
if (this.level == 0) { |
| 145 | this.formattedXml.append("\n"); | |
| 146 | } | |
| 147 |
2
1. startCDATA : changed conditional boundary → KILLED 2. startCDATA : negated conditional → KILLED |
if (this.level >= 1) { |
| 148 | this.formattedXml.append("\n"); | |
| 149 |
2
1. startCDATA : negated conditional → KILLED 2. startCDATA : changed conditional boundary → KILLED |
for (int i = 0; i < this.level; i++) { |
| 150 | this.formattedXml.append(" "); | |
| 151 | } | |
| 152 | } | |
| 153 | this.formattedXml.append("<![CDATA["); | |
| 154 | } | |
| 155 | ||
| 156 | @Override | |
| 157 | public void endCDATA() throws SAXException { | |
| 158 | this.formattedXml.append("]]>"); | |
| 159 | } | |
| 160 | ||
| 161 | @Override | |
| 162 | public void startElement(final String uri, final String localName, final String qName, | |
| 163 | final Attributes attributes) throws SAXException { | |
| 164 | ||
| 165 | //TAG | |
| 166 | this.formattedXml.append("\n"); | |
| 167 |
2
1. startElement : negated conditional → KILLED 2. startElement : changed conditional boundary → KILLED |
for (int i = 0; i < level; i++) { |
| 168 | this.formattedXml.append(" "); | |
| 169 | } | |
| 170 | this.formattedXml.append("<").append(qName); | |
| 171 | ||
| 172 | //NAMESPACE | |
| 173 |
1
1. startElement : negated conditional → KILLED |
if (this.level == 0) { |
| 174 |
1
1. startElement : removed call to org/europa/together/application/internal/SaxDocumentHandler::processNamespace → KILLED |
this.processNamespace(qName, attributes); |
| 175 | } | |
| 176 | ||
| 177 | //ATTRIBUTES | |
| 178 |
2
1. startElement : negated conditional → KILLED 2. startElement : negated conditional → KILLED |
if (attributes != null && attributes.getLength() != 0) { |
| 179 |
2
1. startElement : changed conditional boundary → KILLED 2. startElement : negated conditional → KILLED |
for (int i = 0; i < attributes.getLength(); i++) { |
| 180 | ||
| 181 | this.formattedXml.append(" ") | |
| 182 | .append(attributes.getQName(i)) | |
| 183 | .append("=\"") | |
| 184 | .append(attributes.getValue(i)) | |
| 185 | .append("\""); | |
| 186 | } | |
| 187 | } | |
| 188 | ||
| 189 | //CLOSE | |
| 190 | this.formattedXml.append(">"); | |
| 191 |
1
1. startElement : Replaced integer addition with subtraction → KILLED |
this.level++; |
| 192 | this.isTagOpend = true; | |
| 193 | } | |
| 194 | ||
| 195 | @Override | |
| 196 | public void endElement(final String uri, final String localName, final String qName) | |
| 197 | throws SAXException { | |
| 198 | ||
| 199 |
1
1. endElement : negated conditional → KILLED |
if (!isTagOpend) { |
| 200 | this.formattedXml.append("\n"); | |
| 201 |
2
1. endElement : changed conditional boundary → KILLED 2. endElement : negated conditional → KILLED |
for (int i = 1; i < this.level; i++) { |
| 202 | this.formattedXml.append(" "); | |
| 203 | } | |
| 204 | } else { | |
| 205 | this.isTagOpend = false; | |
| 206 | } | |
| 207 | this.formattedXml.append("</").append(qName).append(">"); | |
| 208 |
1
1. endElement : Replaced integer subtraction with addition → KILLED |
this.level--; |
| 209 | } | |
| 210 | ||
| 211 | @Override | |
| 212 | public void characters(final char[] ch, final int start, final int length) | |
| 213 | throws SAXException { | |
| 214 | ||
| 215 | String characters = new String(ch, start, length).trim(); | |
| 216 | this.formattedXml.append(characters.trim()); | |
| 217 | } | |
| 218 | ||
| 219 | @Override | |
| 220 | public void comment(final char[] ch, final int start, final int length) | |
| 221 | throws SAXException { | |
| 222 | ||
| 223 |
1
1. comment : negated conditional → KILLED |
if (this.level == 0) { |
| 224 | this.formattedXml.append("\n"); | |
| 225 | } | |
| 226 |
2
1. comment : changed conditional boundary → KILLED 2. comment : negated conditional → KILLED |
if (this.level >= 1) { |
| 227 | this.formattedXml.append("\n"); | |
| 228 |
2
1. comment : negated conditional → KILLED 2. comment : changed conditional boundary → KILLED |
for (int i = 0; i < this.level; i++) { |
| 229 | this.formattedXml.append(" "); | |
| 230 | } | |
| 231 | } | |
| 232 | ||
| 233 | String comments = new String(ch, start, length).trim(); | |
| 234 | this.formattedXml.append("<!-- ") | |
| 235 | .append(comments) | |
| 236 | .append(" -->"); | |
| 237 | } | |
| 238 | ||
| 239 | @Override | |
| 240 | public void warning(final SAXParseException ex) | |
| 241 | throws SAXException { | |
| 242 | LOGGER.log("SAX LexicalParser: " + ex.getMessage(), LogLevel.WARN); | |
| 243 | throw new SAXException("SAX Validation Error"); | |
| 244 | } | |
| 245 | ||
| 246 | @Override | |
| 247 | public void error(final SAXParseException ex) | |
| 248 | throws SAXException { | |
| 249 | LOGGER.log("SAX LexicalParser: " + ex.getMessage(), LogLevel.ERROR); | |
| 250 | throw new SAXException("SAX Validation Error"); | |
| 251 | } | |
| 252 | ||
| 253 | @Override | |
| 254 | public void fatalError(final SAXParseException ex) | |
| 255 | throws SAXException { | |
| 256 | LOGGER.log("SAX LexicalParser: " + ex.getMessage(), LogLevel.ERROR); | |
| 257 | throw new SAXException("SAX Validation Error"); | |
| 258 | } | |
| 259 | ||
| 260 | private void processNamespace(final String qName, final Attributes attributes) { | |
| 261 |
2
1. processNamespace : changed conditional boundary → KILLED 2. processNamespace : negated conditional → KILLED |
if (attributes.getLength() > 0) { |
| 262 | try { | |
| 263 | //Extract XSD Files | |
| 264 | String[] xsdExtraction = attributes.getValue(0).split(" "); | |
| 265 | List<String> schemaList = new ArrayList<>(); | |
| 266 | StringBuilder attribute = new StringBuilder(); | |
| 267 | attribute.append("Scanned Attributes: "); | |
| 268 | for (String schema : xsdExtraction) { | |
| 269 |
1
1. processNamespace : negated conditional → KILLED |
if (!StringUtils.isEmpty(schema)) { |
| 270 | attribute.append("\n\t"); | |
| 271 | attribute.append(schema); | |
| 272 |
1
1. processNamespace : negated conditional → KILLED |
if (schema.endsWith(".xsd")) { |
| 273 | schemaList.add(schema); | |
| 274 | } | |
| 275 | } | |
| 276 | } | |
| 277 | LOGGER.log(attribute.toString(), LogLevel.TRACE); | |
| 278 | int schemaCount = schemaList.size(); | |
| 279 | LOGGER.log(schemaCount + " XSD Schemata file(s) extracted.", LogLevel.DEBUG); | |
| 280 | ||
| 281 | //Create XSD Sources | |
| 282 | this.schematList = new Source[schemaCount]; | |
| 283 | int count = 0; | |
| 284 |
2
1. processNamespace : changed conditional boundary → KILLED 2. processNamespace : negated conditional → KILLED |
for (int i = 0; i < schemaCount; i++) { |
| 285 | ||
| 286 | String resource = schemaList.get(count); | |
| 287 | ||
| 288 |
1
1. processNamespace : negated conditional → KILLED |
if (resource.contains("http")) { |
| 289 | ||
| 290 | schematList[count] = new StreamSource(resource); | |
| 291 | LOGGER.log("ADD URL: " + schemaList.get(count), LogLevel.DEBUG); | |
| 292 | } else { | |
| 293 | schematList[count] = new StreamSource(new File(resource)); | |
| 294 | LOGGER.log("ADD FILE: " + schemaList.get(count), LogLevel.DEBUG); | |
| 295 | } | |
| 296 | ||
| 297 |
1
1. processNamespace : Changed increment from 1 to -1 → KILLED |
count++; |
| 298 | } | |
| 299 | LOGGER.log(schematList.length + " Elements added to schemata[]", LogLevel.DEBUG); | |
| 300 | ||
| 301 | } catch (Exception ex) { | |
| 302 | LOGGER.catchException(ex); | |
| 303 | } | |
| 304 | } | |
| 305 | ||
| 306 |
1
1. processNamespace : Replaced integer addition with subtraction → KILLED |
int space = qName.length() + 1; |
| 307 |
3
1. processNamespace : changed conditional boundary → SURVIVED 2. processNamespace : negated conditional → KILLED 3. processNamespace : negated conditional → KILLED |
if (this.namespace != null && this.namespace.size() > 0) { |
| 308 |
2
1. processNamespace : changed conditional boundary → KILLED 2. processNamespace : negated conditional → KILLED |
for (int x = 0; x < this.namespace.size(); x++) { |
| 309 | this.formattedXml.append(this.namespace.get(x)); | |
| 310 |
2
1. processNamespace : negated conditional → KILLED 2. processNamespace : changed conditional boundary → KILLED |
for (int i = 0; i < space; i++) { |
| 311 | this.formattedXml.append(" "); | |
| 312 | } | |
| 313 | } | |
| 314 | } | |
| 315 | } | |
| 316 | } | |
Mutations | ||
| 50 |
1.1 |
|
| 60 |
1.1 |
|
| 63 |
1.1 |
|
| 90 |
1.1 |
|
| 95 |
1.1 |
|
| 100 |
1.1 2.2 |
|
| 109 |
1.1 |
|
| 135 |
1.1 |
|
| 144 |
1.1 |
|
| 147 |
1.1 2.2 |
|
| 149 |
1.1 2.2 |
|
| 167 |
1.1 2.2 |
|
| 173 |
1.1 |
|
| 174 |
1.1 |
|
| 178 |
1.1 2.2 |
|
| 179 |
1.1 2.2 |
|
| 191 |
1.1 |
|
| 199 |
1.1 |
|
| 201 |
1.1 2.2 |
|
| 208 |
1.1 |
|
| 223 |
1.1 |
|
| 226 |
1.1 2.2 |
|
| 228 |
1.1 2.2 |
|
| 261 |
1.1 2.2 |
|
| 269 |
1.1 |
|
| 272 |
1.1 |
|
| 284 |
1.1 2.2 |
|
| 288 |
1.1 |
|
| 297 |
1.1 |
|
| 306 |
1.1 |
|
| 307 |
1.1 2.2 3.3 |
|
| 308 |
1.1 2.2 |
|
| 310 |
1.1 2.2 |