| 1 | package org.europa.together.application; | |
| 2 | ||
| 3 | import java.io.ByteArrayInputStream; | |
| 4 | import java.io.File; | |
| 5 | import java.io.IOException; | |
| 6 | import java.io.StringWriter; | |
| 7 | import java.io.Writer; | |
| 8 | import java.nio.charset.StandardCharsets; | |
| 9 | import javax.xml.XMLConstants; | |
| 10 | import javax.xml.parsers.SAXParser; | |
| 11 | import javax.xml.parsers.SAXParserFactory; | |
| 12 | import javax.xml.transform.OutputKeys; | |
| 13 | import javax.xml.transform.Source; | |
| 14 | import javax.xml.transform.Transformer; | |
| 15 | import javax.xml.transform.TransformerFactory; | |
| 16 | import javax.xml.transform.stream.StreamResult; | |
| 17 | import javax.xml.transform.stream.StreamSource; | |
| 18 | import javax.xml.validation.SchemaFactory; | |
| 19 | import org.europa.together.application.internal.SaxDocumentHandler; | |
| 20 | import org.europa.together.business.Logger; | |
| 21 | import org.europa.together.domain.LogLevel; | |
| 22 | import org.europa.together.utils.FileUtils; | |
| 23 | import org.europa.together.business.XmlTools; | |
| 24 | import org.europa.together.utils.StringUtils; | |
| 25 | import org.springframework.stereotype.Repository; | |
| 26 | ||
| 27 | /** | |
| 28 | * Implementation of XML Tools. | |
| 29 | */ | |
| 30 | @Repository | |
| 31 | public class SaxTools implements XmlTools { | |
| 32 | ||
| 33 | private static final long serialVersionUID = 10L; | |
| 34 | private static final Logger LOGGER = new LogbackLogger(SaxTools.class); | |
| 35 | ||
| 36 | private File schemaFile = null; | |
| 37 | private String prettyPrint = null; | |
| 38 | private String xmlContent = null; | |
| 39 | private boolean wellformed = false; | |
| 40 | //Sax | |
| 41 | private SaxDocumentHandler saxHandler; | |
| 42 | private SAXParserFactory parserFactory; | |
| 43 | private SAXParser parser = null; | |
| 44 | ||
| 45 | /** | |
| 46 | * Constructor. | |
| 47 | */ | |
| 48 | public SaxTools() { | |
| 49 | this.parserFactory = SAXParserFactory.newInstance(); | |
| 50 | LOGGER.log("instance class", LogLevel.INFO); | |
| 51 | } | |
| 52 | ||
| 53 | @Override | |
| 54 | public String parseXmlFile(final File xmlFile) { | |
| 55 | try { | |
| 56 | LOGGER.log("parse XML File: " + xmlFile.getName(), LogLevel.DEBUG); | |
| 57 | xmlContent = FileUtils.readFileStream(xmlFile); | |
| 58 |
1
1. parseXmlFile : removed call to org/europa/together/application/SaxTools::parse → KILLED |
parse(xmlContent); |
| 59 | } catch (Exception ex) { | |
| 60 | LOGGER.catchException(ex); | |
| 61 | } | |
| 62 |
1
1. parseXmlFile : replaced return value with "" for org/europa/together/application/SaxTools::parseXmlFile → KILLED |
return xmlContent; |
| 63 | } | |
| 64 | ||
| 65 | @Override | |
| 66 | public String parseXmlString(final String xml) { | |
| 67 | try { | |
| 68 | LOGGER.log("parse XML String: " + xml.length() + " characters.", LogLevel.DEBUG); | |
| 69 | xmlContent = xml; | |
| 70 |
1
1. parseXmlString : removed call to org/europa/together/application/SaxTools::parse → KILLED |
parse(xmlContent); |
| 71 | ||
| 72 | } catch (Exception ex) { | |
| 73 | LOGGER.catchException(ex); | |
| 74 | } | |
| 75 |
1
1. parseXmlString : replaced return value with "" for org/europa/together/application/SaxTools::parseXmlString → KILLED |
return xmlContent; |
| 76 | } | |
| 77 | ||
| 78 | @Override | |
| 79 | public String prettyPrintXml() { | |
| 80 | String content = null; | |
| 81 |
1
1. prettyPrintXml : negated conditional → KILLED |
if (!StringUtils.isEmpty(prettyPrint)) { |
| 82 | content = prettyPrint; | |
| 83 | } | |
| 84 |
1
1. prettyPrintXml : replaced return value with "" for org/europa/together/application/SaxTools::prettyPrintXml → KILLED |
return content; |
| 85 | } | |
| 86 | ||
| 87 | @Override | |
| 88 | public String shrinkContent(final String content) { | |
| 89 |
1
1. shrinkContent : replaced return value with "" for org/europa/together/application/SaxTools::shrinkContent → KILLED |
return StringUtils.shrink(content); |
| 90 | } | |
| 91 | ||
| 92 | @Override | |
| 93 | public String transformXslt(final File xml, final File xslt) { | |
| 94 | Writer writer = new StringWriter(); | |
| 95 | try { | |
| 96 | Source template = new StreamSource(xslt); | |
| 97 | Source input = new StreamSource(xml); | |
| 98 | StreamResult output = new StreamResult(writer); | |
| 99 | TransformerFactory factory = TransformerFactory.newInstance(); | |
| 100 | Transformer transformer = factory.newTransformer(template); | |
| 101 |
1
1. transformXslt : removed call to javax/xml/transform/Transformer::setOutputProperty → SURVIVED |
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); |
| 102 |
1
1. transformXslt : removed call to javax/xml/transform/Transformer::transform → KILLED |
transformer.transform(input, output); |
| 103 | } catch (Exception ex) { | |
| 104 | LOGGER.catchException(ex); | |
| 105 | } | |
| 106 |
1
1. transformXslt : replaced return value with "" for org/europa/together/application/SaxTools::transformXslt → KILLED |
return writer.toString(); |
| 107 | } | |
| 108 | ||
| 109 | @Override | |
| 110 | public boolean hasExternalSchemaFile() { | |
| 111 | boolean success = false; | |
| 112 |
1
1. hasExternalSchemaFile : negated conditional → KILLED |
if (this.schemaFile != null) { |
| 113 | success = true; | |
| 114 | } | |
| 115 |
2
1. hasExternalSchemaFile : replaced boolean return with false for org/europa/together/application/SaxTools::hasExternalSchemaFile → KILLED 2. hasExternalSchemaFile : replaced boolean return with true for org/europa/together/application/SaxTools::hasExternalSchemaFile → KILLED |
return success; |
| 116 | } | |
| 117 | ||
| 118 | @Override | |
| 119 | public boolean isValid() { | |
| 120 | boolean success = false; | |
| 121 | try { | |
| 122 | LOGGER.log("Start Validation. ", LogLevel.DEBUG); | |
| 123 |
1
1. isValid : removed call to javax/xml/parsers/SAXParser::reset → SURVIVED |
parser.reset(); |
| 124 |
1
1. isValid : removed call to javax/xml/parsers/SAXParserFactory::setNamespaceAware → SURVIVED |
parserFactory.setNamespaceAware(true); |
| 125 |
1
1. isValid : removed call to javax/xml/parsers/SAXParserFactory::setValidating → KILLED |
parserFactory.setValidating(true); |
| 126 | parser = parserFactory.newSAXParser(); | |
| 127 |
1
1. isValid : removed call to javax/xml/parsers/SAXParser::setProperty → SURVIVED |
parser.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler); |
| 128 |
1
1. isValid : removed call to javax/xml/parsers/SAXParser::setProperty → SURVIVED |
parser.setProperty("http://xml.org/sax/properties/declaration-handler", saxHandler); |
| 129 | //External XSD | |
| 130 |
1
1. isValid : negated conditional → KILLED |
if (hasExternalSchemaFile()) { |
| 131 | LOGGER.log("Validate by explicit XSD (" + this.schemaFile.getName() + ") :: " | |
| 132 | + this.schemaFile.getAbsolutePath(), LogLevel.DEBUG); | |
| 133 |
1
1. isValid : removed call to javax/xml/parsers/SAXParser::setProperty → KILLED |
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", |
| 134 | "http://www.w3.org/2001/XMLSchema"); | |
| 135 |
1
1. isValid : removed call to javax/xml/parsers/SAXParserFactory::setSchema → SURVIVED |
parserFactory.setSchema(SchemaFactory |
| 136 | .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) | |
| 137 | .newSchema(schemaFile)); | |
| 138 |
1
1. isValid : negated conditional → KILLED |
} else if (saxHandler.getSchemaFiles() != null) { |
| 139 | LOGGER.log("Validate by implicit XSD (" + saxHandler.getSchemaFiles().length + ")", | |
| 140 | LogLevel.DEBUG); | |
| 141 |
1
1. isValid : removed call to javax/xml/parsers/SAXParser::setProperty → KILLED |
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", |
| 142 | XMLConstants.W3C_XML_SCHEMA_NS_URI); | |
| 143 |
1
1. isValid : removed call to javax/xml/parsers/SAXParserFactory::setSchema → SURVIVED |
parserFactory.setSchema(SchemaFactory |
| 144 | .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) | |
| 145 | .newSchema(saxHandler.getSchemaFiles())); | |
| 146 | } | |
| 147 |
1
1. isValid : removed call to javax/xml/parsers/SAXParser::parse → KILLED |
parser.parse(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8)), |
| 148 | saxHandler); | |
| 149 | success = true; | |
| 150 | LOGGER.log("XML validation successful", LogLevel.DEBUG); | |
| 151 | } catch (Exception ex) { | |
| 152 | LOGGER.catchException(ex); | |
| 153 | } | |
| 154 |
2
1. isValid : replaced boolean return with true for org/europa/together/application/SaxTools::isValid → KILLED 2. isValid : replaced boolean return with false for org/europa/together/application/SaxTools::isValid → KILLED |
return success; |
| 155 | } | |
| 156 | ||
| 157 | @Override | |
| 158 | public boolean isWellFormed() { | |
| 159 | LOGGER.log("xml is wellformed.", LogLevel.DEBUG); | |
| 160 |
2
1. isWellFormed : replaced boolean return with false for org/europa/together/application/SaxTools::isWellFormed → KILLED 2. isWellFormed : replaced boolean return with true for org/europa/together/application/SaxTools::isWellFormed → KILLED |
return this.wellformed; |
| 161 | } | |
| 162 | ||
| 163 | @Override | |
| 164 | public void resetExternalSchema() { | |
| 165 | this.schemaFile = null; | |
| 166 | LOGGER.log("External Schema File is reset to NULL.", LogLevel.DEBUG); | |
| 167 | } | |
| 168 | ||
| 169 | @Override | |
| 170 | public void setSchemaFile(final File schema) { | |
| 171 |
2
1. setSchemaFile : negated conditional → KILLED 2. setSchemaFile : negated conditional → KILLED |
if (schema == null || !schema.exists()) { |
| 172 | LOGGER.log("External Schema not add Schema because file does not exist.", | |
| 173 | LogLevel.ERROR); | |
| 174 | } else { | |
| 175 | schemaFile = schema; | |
| 176 | LOGGER.log(schemaFile.getName() + " schema file added.", | |
| 177 | LogLevel.DEBUG); | |
| 178 | } | |
| 179 | } | |
| 180 | ||
| 181 | @Override | |
| 182 | public void writeXmlToFile(final String content, final String destinationFile) | |
| 183 | throws IOException { | |
| 184 | FileUtils.writeStringToFile(content, destinationFile); | |
| 185 | } | |
| 186 | ||
| 187 | private void parse(final String xml) { | |
| 188 | wellformed = false; | |
| 189 | prettyPrint = null; | |
| 190 | try { | |
| 191 | //Sax | |
| 192 | saxHandler = new SaxDocumentHandler(); | |
| 193 |
1
1. parse : removed call to javax/xml/parsers/SAXParserFactory::setValidating → SURVIVED |
parserFactory.setValidating(false); |
| 194 |
1
1. parse : removed call to javax/xml/parsers/SAXParserFactory::setNamespaceAware → KILLED |
parserFactory.setNamespaceAware(true); |
| 195 |
1
1. parse : removed call to javax/xml/parsers/SAXParserFactory::setXIncludeAware → SURVIVED |
parserFactory.setXIncludeAware(true); |
| 196 | parser = parserFactory.newSAXParser(); | |
| 197 |
1
1. parse : removed call to javax/xml/parsers/SAXParser::setProperty → KILLED |
parser.setProperty("http://xml.org/sax/properties/lexical-handler", saxHandler); |
| 198 |
1
1. parse : removed call to javax/xml/parsers/SAXParser::setProperty → KILLED |
parser.setProperty("http://xml.org/sax/properties/declaration-handler", saxHandler); |
| 199 |
1
1. parse : removed call to javax/xml/parsers/SAXParser::parse → KILLED |
parser.parse( |
| 200 | new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)), | |
| 201 | saxHandler); | |
| 202 | wellformed = true; | |
| 203 | prettyPrint = saxHandler.prettyPrintXml(); | |
| 204 | } catch (Exception ex) { | |
| 205 | LOGGER.log("PARSING EXCEPTION", LogLevel.WARN); | |
| 206 | LOGGER.catchException(ex); | |
| 207 | } | |
| 208 |
1
1. parse : removed call to javax/xml/parsers/SAXParser::reset → SURVIVED |
parser.reset(); |
| 209 | } | |
| 210 | } | |
Mutations | ||
| 58 |
1.1 |
|
| 62 |
1.1 |
|
| 70 |
1.1 |
|
| 75 |
1.1 |
|
| 81 |
1.1 |
|
| 84 |
1.1 |
|
| 89 |
1.1 |
|
| 101 |
1.1 |
|
| 102 |
1.1 |
|
| 106 |
1.1 |
|
| 112 |
1.1 |
|
| 115 |
1.1 2.2 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 125 |
1.1 |
|
| 127 |
1.1 |
|
| 128 |
1.1 |
|
| 130 |
1.1 |
|
| 133 |
1.1 |
|
| 135 |
1.1 |
|
| 138 |
1.1 |
|
| 141 |
1.1 |
|
| 143 |
1.1 |
|
| 147 |
1.1 |
|
| 154 |
1.1 2.2 |
|
| 160 |
1.1 2.2 |
|
| 171 |
1.1 2.2 |
|
| 193 |
1.1 |
|
| 194 |
1.1 |
|
| 195 |
1.1 |
|
| 197 |
1.1 |
|
| 198 |
1.1 |
|
| 199 |
1.1 |
|
| 208 |
1.1 |