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