11package csv2pdf ;
22
33import freemarker .template .Configuration ;
4- import freemarker .template .MalformedTemplateNameException ;
54import freemarker .template .Template ;
6- import java .io .FileOutputStream ;
75import java .io .OutputStream ;
86import java .io .StringWriter ;
97import java .nio .file .FileSystems ;
8+ import java .nio .file .Files ;
9+ import java .nio .file .Paths ;
1010import java .sql .Connection ;
1111import java .sql .DriverManager ;
1212import java .sql .ResultSet ;
2626
2727public class App {
2828 private static String getUniqueFilename (String name ) {
29- String result = FileSystems .getDefault ().getPath (name , new String [ 0 ] ).toFile ().exists ()
29+ return FileSystems .getDefault ().getPath (name ).toFile ().exists ()
3030 ? System .currentTimeMillis () + "_" + name
3131 : name ;
32- return result ;
3332 }
3433
3534 private static ArrayList <HashMap <String , Object >> getData (String folder , String separator , String extension ,
@@ -39,7 +38,7 @@ private static ArrayList<HashMap<String, Object>> getData(String folder, String
3938 Connection conn = DriverManager .getConnection ("jdbc:relique:csv:" + folder + "?separator=" + separator
4039 + "&fileExtension=" + extension + "&charset=" + charset );
4140
42- Statement stmt = conn .createStatement (1005 , 1007 );
41+ Statement stmt = conn .createStatement (ResultSet . TYPE_SCROLL_SENSITIVE , ResultSet . CONCUR_READ_ONLY );
4342 ResultSet results = stmt .executeQuery (query );
4443 while (results .next ()) {
4544 int columns = results .getMetaData ().getColumnCount ();
@@ -53,16 +52,15 @@ private static ArrayList<HashMap<String, Object>> getData(String folder, String
5352 }
5453
5554 public static void main (String [] args ) throws ClassNotFoundException , SQLException ,
56- freemarker .template .TemplateNotFoundException , MalformedTemplateNameException ,
57- freemarker .core .ParseException , java .io .IOException , freemarker .template .TemplateException {
55+ java .io .IOException , freemarker .template .TemplateException {
5856 System .out .println ("·------------------------------------------·" );
5957 System .out .println ("| ___ ______ |" );
6058 System .out .println ("| ___________ _|__ \\ ____ ____/ / __/ |" );
6159 System .out .println ("| / ___/ ___/ | / /_/ // __ \\ / __ / /_ |" );
6260 System .out .println ("| / /__(__ )| |/ / __// /_/ / /_/ / __/ |" );
6361 System .out .println ("| \\ ___/____/ |___/____/ .___/\\ __,_/_/ |" );
6462 System .out .println ("| /_/ |" );
65- System .out .println ("| v.1.0.2 |" );
63+ System .out .println ("| v.1.0.3 |" );
6664 System .out .println ("·------------------------------------------·" );
6765
6866 Options options = new Options ();
@@ -133,7 +131,7 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
133131 String ftlFile = cmd .getOptionValue ("ftl_file" );
134132 String ftlEncoding = cmd .getOptionValue ("ftl_encoding" , "UTF-8" );
135133 Configuration cfg = new Configuration (Configuration .VERSION_2_3_30 );
136- cfg .setDirectoryForTemplateLoading (FileSystems .getDefault ().getPath ("." , new String [ 0 ] ).toFile ());
134+ cfg .setDirectoryForTemplateLoading (FileSystems .getDefault ().getPath ("." ).toFile ());
137135 cfg .setDefaultEncoding (ftlEncoding );
138136 Template template = cfg .getTemplate (ftlFile );
139137 ITextRenderer renderer = cmd .hasOption ("pdf" ) ? new ITextRenderer () : null ;
@@ -146,16 +144,16 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
146144 getUniqueFilename (StringSubstitutor .replace (cmd .getOptionValue ("pdf" ), row )):
147145 getUniqueFilename (StringSubstitutor .replace (cmd .getOptionValue ("txt" ), row ));
148146 System .out .print ("[+] Generating file: " + fileName );
149- OutputStream outputStream = new FileOutputStream ( fileName );
150- if (renderer ==null )
147+ OutputStream outputStream = Files . newOutputStream ( Paths . get ( fileName ) );
148+ if (renderer ==null )
151149 outputStream .write (sw .toString ().getBytes (cmd .getOptionValue ("txt_charset" , "UTF-8" )));
152150 else {
153151 Document doc = Jsoup .parse (sw .toString ());
154152 doc .outputSettings ().syntax (Document .OutputSettings .Syntax .xml ).prettyPrint (false );
155153 renderer .setDocumentFromString (doc .html ());
156154 renderer .layout ();
157155 renderer .createPDF (outputStream );
158- }
156+ }
159157 outputStream .close ();
160158 System .out .println (" OK" );
161159 }
@@ -165,11 +163,11 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
165163 model .put ("rows" , rows );
166164 StringWriter sw = new StringWriter ();
167165 template .process (model , sw );
168- String fileName = (renderer != null )?
166+ String fileName = (renderer != null )?
169167 getUniqueFilename (cmd .getOptionValue ("pdf" )):
170168 getUniqueFilename (cmd .getOptionValue ("txt" ));
171169 System .out .print ("[+] Generating file: " + fileName );
172- OutputStream outputStream = new FileOutputStream ( fileName );
170+ OutputStream outputStream = Files . newOutputStream ( Paths . get ( fileName ) );
173171 if (renderer ==null )
174172 outputStream .write (sw .toString ().getBytes (cmd .getOptionValue ("txt_charset" , "UTF-8" )));
175173 else {
@@ -178,7 +176,7 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
178176 renderer .setDocumentFromString (doc .html ());
179177 renderer .layout ();
180178 renderer .createPDF (outputStream );
181- }
179+ }
182180 outputStream .close ();
183181 System .out .println (" OK" );
184182 }
0 commit comments