1
- package com .codepressed .CSVtoXML ;
1
+ package com .codepressed .csvToXml ;
2
2
3
3
import org .w3c .dom .Document ;
4
4
import org .w3c .dom .Element ;
5
5
import org .w3c .dom .Text ;
6
6
7
+ import javax .xml .XMLConstants ;
7
8
import javax .xml .parsers .DocumentBuilder ;
8
9
import javax .xml .parsers .DocumentBuilderFactory ;
9
10
import javax .xml .parsers .ParserConfigurationException ;
14
15
import javax .xml .transform .dom .DOMSource ;
15
16
import javax .xml .transform .stream .StreamResult ;
16
17
import java .io .BufferedReader ;
17
- import java .io .File ;
18
18
import java .io .FileOutputStream ;
19
19
import java .io .IOException ;
20
20
import java .nio .file .Files ;
@@ -28,30 +28,35 @@ public class XMLutils {
28
28
29
29
private static final Logger logger = Logger .getLogger (XMLutils .class .getName ());
30
30
31
+ private XMLutils (){
32
+ throw new IllegalStateException ("This is a utility class." );
33
+ }
34
+
31
35
public static boolean writeXmlDocumentToFile (Document xmlDoc , String xmlFilePath ) {
32
36
try {
33
37
TransformerFactory xmlTransformerFactory = TransformerFactory .newInstance ();
38
+ //To protect from XXE attacks
39
+ xmlTransformerFactory .setAttribute (XMLConstants .ACCESS_EXTERNAL_DTD , "" );
40
+ xmlTransformerFactory .setAttribute (XMLConstants .ACCESS_EXTERNAL_STYLESHEET , "" );
41
+
34
42
Transformer xmlTransformer = xmlTransformerFactory .newTransformer ();
35
43
xmlTransformer .setOutputProperty (OutputKeys .INDENT , "yes" );
36
44
xmlTransformer .setOutputProperty (OutputKeys .METHOD , "xml" );
37
45
xmlTransformer .setOutputProperty (OutputKeys .ENCODING , "UTF-8" );
38
46
xmlTransformer .setOutputProperty ("{http://xml.apache.org/xslt}indent-amount" , "4" );
39
47
40
- try (FileOutputStream outputStream = new FileOutputStream (new File ( xmlFilePath ) )) {
48
+ try (FileOutputStream outputStream = new FileOutputStream (xmlFilePath )) {
41
49
xmlTransformer .transform (new DOMSource (xmlDoc ), new StreamResult (outputStream ));
42
50
}
43
51
return true ;
52
+
44
53
} catch (TransformerException | IOException e ) {
45
54
logger .log (Level .SEVERE , "Error writing xml document to file" , e );
46
55
return false ;
47
56
}
48
57
}
49
58
50
- public static Document createXmlDocument (List <String []> XMLelements , String elementName ) {
51
- if (elementName == null ) {
52
- elementName = "element" ;
53
- }
54
-
59
+ public static Document createXmlDocument (List <String []> xmlElements , String elementName ) {
55
60
try {
56
61
DocumentBuilderFactory xmlFactory = DocumentBuilderFactory .newInstance ();
57
62
DocumentBuilder xmlBuilder = xmlFactory .newDocumentBuilder ();
@@ -63,15 +68,15 @@ public static Document createXmlDocument(List<String[]> XMLelements, String elem
63
68
rootElement .appendChild (mainElement );
64
69
65
70
boolean headerDefined = false ;
66
- String [] header = new String [XMLelements .size ()];
71
+ String [] header = new String [xmlElements .size ()];
67
72
68
- for (String [] node : XMLelements ) {
73
+ for (String [] node : xmlElements ) {
69
74
if (headerDefined ) {
70
75
Element nodesElements = xmlDoc .createElement (elementName );
71
76
mainElement .appendChild (nodesElements );
72
77
73
78
for (int j = 0 ; j < node .length ; j ++) {
74
- node [j ] = node [j ].replaceAll ("\" " , "" ).trim ();
79
+ node [j ] = node [j ].replace ("\" " , "" ).trim ();
75
80
Element nodesValues = xmlDoc .createElement (header [j ]);
76
81
nodesElements .appendChild (nodesValues );
77
82
Text nodeTxt = xmlDoc .createTextNode (node [j ]);
@@ -81,10 +86,8 @@ public static Document createXmlDocument(List<String[]> XMLelements, String elem
81
86
header = node ;
82
87
for (int j = 0 ; j < node .length ; j ++) {
83
88
header [j ] = header [j ].replaceAll ("[^a-zA-Z0-9]" , "" );
84
- try {
85
- Integer .parseInt (header [j ]);
89
+ if (isParsableToInt (header [j ])) {
86
90
header [j ] = "node" + header [j ];
87
- } catch (NumberFormatException e ) {
88
91
}
89
92
}
90
93
headerDefined = true ;
@@ -97,6 +100,7 @@ public static Document createXmlDocument(List<String[]> XMLelements, String elem
97
100
}
98
101
}
99
102
103
+
100
104
public static List <String []> readCsvFile (String csvFilePath , String separator ) {
101
105
List <String []> elements = new ArrayList <>();
102
106
@@ -112,4 +116,14 @@ public static List<String[]> readCsvFile(String csvFilePath, String separator)
112
116
return elements ;
113
117
}
114
118
119
+ public static boolean isParsableToInt (String input ){
120
+ try {
121
+ Integer .parseInt (input );
122
+ return true ;
123
+ }catch (NumberFormatException e ){
124
+ logger .log (Level .INFO , "One of the columns is a int. We will add to it a 'node' prefix." );
125
+ return false ;
126
+ }
127
+ }
128
+
115
129
}
0 commit comments