1- package com .codepressed .CSVtoXML ;
1+ package com .codepressed .csvToXml ;
22
33import org .w3c .dom .Document ;
44import org .w3c .dom .Element ;
55import org .w3c .dom .Text ;
66
7+ import javax .xml .XMLConstants ;
78import javax .xml .parsers .DocumentBuilder ;
89import javax .xml .parsers .DocumentBuilderFactory ;
910import javax .xml .parsers .ParserConfigurationException ;
1415import javax .xml .transform .dom .DOMSource ;
1516import javax .xml .transform .stream .StreamResult ;
1617import java .io .BufferedReader ;
17- import java .io .File ;
1818import java .io .FileOutputStream ;
1919import java .io .IOException ;
2020import java .nio .file .Files ;
@@ -28,30 +28,35 @@ public class XMLutils {
2828
2929private static final Logger logger =Logger .getLogger (XMLutils .class .getName ());
3030
31+ private XMLutils (){
32+ throw new IllegalStateException ("This is a utility class." );
33+ }
34+
3135public static boolean writeXmlDocumentToFile (Document xmlDoc ,String xmlFilePath ) {
3236try {
3337TransformerFactory xmlTransformerFactory =TransformerFactory .newInstance ();
38+ //To protect from XXE attacks
39+ xmlTransformerFactory .setAttribute (XMLConstants .ACCESS_EXTERNAL_DTD ,"" );
40+ xmlTransformerFactory .setAttribute (XMLConstants .ACCESS_EXTERNAL_STYLESHEET ,"" );
41+
3442Transformer xmlTransformer =xmlTransformerFactory .newTransformer ();
3543xmlTransformer .setOutputProperty (OutputKeys .INDENT ,"yes" );
3644xmlTransformer .setOutputProperty (OutputKeys .METHOD ,"xml" );
3745xmlTransformer .setOutputProperty (OutputKeys .ENCODING ,"UTF-8" );
3846xmlTransformer .setOutputProperty ("{http://xml.apache.org/xslt}indent-amount" ,"4" );
3947
40- try (FileOutputStream outputStream =new FileOutputStream (new File ( xmlFilePath ) )) {
48+ try (FileOutputStream outputStream =new FileOutputStream (xmlFilePath )) {
4149xmlTransformer .transform (new DOMSource (xmlDoc ),new StreamResult (outputStream ));
4250 }
4351return true ;
52+
4453 }catch (TransformerException |IOException e ) {
4554logger .log (Level .SEVERE ,"Error writing xml document to file" ,e );
4655return false ;
4756 }
4857 }
4958
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 ) {
5560try {
5661DocumentBuilderFactory xmlFactory =DocumentBuilderFactory .newInstance ();
5762DocumentBuilder xmlBuilder =xmlFactory .newDocumentBuilder ();
@@ -63,15 +68,15 @@ public static Document createXmlDocument(List<String[]> XMLelements, String elem
6368rootElement .appendChild (mainElement );
6469
6570boolean headerDefined =false ;
66- String []header =new String [XMLelements .size ()];
71+ String []header =new String [xmlElements .size ()];
6772
68- for (String []node :XMLelements ) {
73+ for (String []node :xmlElements ) {
6974if (headerDefined ) {
7075Element nodesElements =xmlDoc .createElement (elementName );
7176mainElement .appendChild (nodesElements );
7277
7378for (int j =0 ;j <node .length ;j ++) {
74- node [j ] =node [j ].replaceAll ("\" " ,"" ).trim ();
79+ node [j ] =node [j ].replace ("\" " ,"" ).trim ();
7580Element nodesValues =xmlDoc .createElement (header [j ]);
7681nodesElements .appendChild (nodesValues );
7782Text nodeTxt =xmlDoc .createTextNode (node [j ]);
@@ -81,10 +86,8 @@ public static Document createXmlDocument(List<String[]> XMLelements, String elem
8186header =node ;
8287for (int j =0 ;j <node .length ;j ++) {
8388header [j ] =header [j ].replaceAll ("[^a-zA-Z0-9]" ,"" );
84- try {
85- Integer .parseInt (header [j ]);
89+ if (isParsableToInt (header [j ])) {
8690header [j ] ="node" +header [j ];
87- }catch (NumberFormatException e ) {
8891 }
8992 }
9093headerDefined =true ;
@@ -97,6 +100,7 @@ public static Document createXmlDocument(List<String[]> XMLelements, String elem
97100 }
98101 }
99102
103+
100104public static List <String []>readCsvFile (String csvFilePath ,String separator ) {
101105List <String []>elements =new ArrayList <>();
102106
@@ -112,4 +116,14 @@ public static List<String[]> readCsvFile(String csvFilePath, String separator)
112116return elements ;
113117 }
114118
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+
115129}