1
package org.hisp.dhis.rt.utils;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.io.BufferedReader;
32
import java.io.FileNotFoundException;
33
import java.io.FileOutputStream;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.InputStreamReader;
37
import java.io.OutputStreamWriter;
38
import java.io.UnsupportedEncodingException;
39
import java.io.Writer;
40
import java.util.HashMap;
43
import javax.servlet.ServletContext;
45
import com.opensymphony.webwork.ServletActionContext;
48
* @author Lars Helge Overland
49
* @author Nguyen Dang Quang
50
* @version $Id: FileUtils.java 2871 2007-02-20 16:04:11Z andegje $
52
public class FileUtils
55
* Creates the dhis directory tree with the user.home directory as root. Valid
56
* directories are "dhis", "rt", "design", "xml", "jrxml", "reports", "pdf" and "html".
57
* @return Map with the paths to all directories
59
public static Map getOutputPath()
61
Map<String, String> paths = new HashMap<String, String>();
63
String dhis_dir = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator;
64
String rt_dir = dhis_dir + "rt" + File.separator;
65
String design_dir = rt_dir + "design" + File.separator;
66
String xml_dir = design_dir + "xml" + File.separator;
67
String jrxml_dir = design_dir + "jrxml" + File.separator;
68
String reports_dir = rt_dir + "reports" + File.separator;
69
String pdf_dir = reports_dir + "PDF" + File.separator;
70
String html_dir = reports_dir + "HTML" + File.separator;
72
paths.put( "dhis", dhis_dir );
73
paths.put( "rt", rt_dir );
74
paths.put( "design", design_dir );
75
paths.put( "xml", xml_dir );
76
paths.put( "jrxml", jrxml_dir );
77
paths.put( "reports", reports_dir );
78
paths.put( "pdf", pdf_dir );
79
paths.put( "html", html_dir );
81
File dhisDir = new File( dhis_dir );
82
if ( !dhisDir.isDirectory() )
86
File rtDir = new File( rt_dir );
87
if ( !rtDir.isDirectory() )
91
File designDir = new File( design_dir );
92
if ( !designDir.isDirectory() )
96
File xmlDir = new File( xml_dir );
97
if ( !xmlDir.isDirectory() )
101
File jrxmlDir = new File( jrxml_dir );
102
if ( !jrxmlDir.isDirectory() )
106
File reportsDir = new File( reports_dir );
107
if ( !reportsDir.isDirectory() )
111
File pdfDir = new File( pdf_dir );
112
if ( !pdfDir.isDirectory() )
116
File htmlDir = new File( html_dir );
117
if ( !htmlDir.isDirectory() )
122
checkXmlFile( xml_dir + "Hue_bieu_CoverSignature.xml" );
124
checkXmlFile( xml_dir + "Hue_bieu_1.xml" );
126
checkXmlFile( xml_dir + "Hue_bieu_2.xml" );
128
checkXmlFile( xml_dir + "Hue_bieu_3.xml" );
130
checkXmlFile( xml_dir + "Hue_bieu_4.xml" );
132
checkXmlFile( xml_dir + "Hue_bieu_5.xml" );
134
checkXmlFile( xml_dir + "Hue_bieu_7.xml" );
136
checkXmlFile( xml_dir + "Hue_bieu_9.xml" );
138
checkXmlFile( xml_dir + "Hue_bieu_10.xml" );
140
checkXmlFile( xml_dir + "Hue_bieu_11.xml" );
142
checkXmlFile( xml_dir + "Hue_bieu_12.xml" );
144
checkXmlFile( xml_dir + "Hue_bieu_13.xml" );
146
checkXmlFile( xml_dir + "Hue_bieu_14.xml" );
152
* Creates a valid Java class name by removing illegal characters.
153
* @param input Input string
154
* @return Valid Java class name string
156
public static String getQualifiedFileName( String name )
158
String pattern = "[ (){}\\[\\]\\'\\+\\*\\/\\-\\\\]";
159
String replacement = "";
160
name = name.replaceAll( pattern, replacement );
166
* Gets the extension of a file.
167
* @param file The file
168
* @return The extension of the file
170
public static String getExtension( File file )
174
String path = file.getName();
175
return path.substring( path.lastIndexOf( "." ) + 1, path.length() );
182
* Gets the extension of a file.
183
* @param fileName The file name
184
* @return The extension of the file
186
public static String getExtension( String fileName )
188
return fileName.substring( fileName.lastIndexOf( "." ) + 1, fileName.length() );
192
* Gets the basename of a file.
193
* @param file The file
194
* @return The basename of the file
196
public static String getBaseName( File file )
200
String path = file.getName();
201
return path.substring( 0 , path.lastIndexOf( "." ) );
208
* Gets the basename of a file.
209
* @param file The file name
210
* @return The basename of the file
212
public static String getBaseName( String fileName )
214
return fileName.substring( 0 , fileName.lastIndexOf( "." ) );
218
* Gets the path to the report xml file
219
* @param name The file name
220
* @return Path to the report xml file
222
public static String getXmlFileName( String name )
224
return getOutputPath().get( "xml" ).toString() + getQualifiedFileName( name ) + ".xml";
228
* Gets the path to the report jrxml file
229
* @param name The file name
230
* @return Path to the report jrxml file
232
public static String getJRXmlFileName( String name )
234
return getOutputPath().get( "jrxml" ).toString() + getQualifiedFileName( name ) + ".jrxml";
240
public static InputStream getHcmcJRXmlFileAsStream( String kind, int page )
242
String path = "/dhis-web-reporttool/template/vietnam/" + kind + "/" + "Trang" + "_" + page + ".jrxml";
243
ServletContext context = ServletActionContext.getServletContext();
244
return context.getResourceAsStream( path );
247
public static InputStream getHcmcJasperFileAsStream( String kind, int page )
249
String path = "/dhis-web-reporttool/template/hcmc/" + kind + "/" + "Trang" + "_" + page + ".jasper";
251
ServletContext context = ServletActionContext.getServletContext();
252
System.out.println("chay toi day ko?");
253
InputStream abc = context.getResourceAsStream( path );
254
System.out.println("Sau khi doc jasper file");
255
if (abc != null)System.out.println("stream is not null");
259
private static void makeXmlFile( String outputXmlFilePath )
261
String xmlFileName = outputXmlFilePath.substring(outputXmlFilePath.lastIndexOf(File.separator) + 1, outputXmlFilePath.length());
265
Writer out = new OutputStreamWriter( new FileOutputStream( outputXmlFilePath, true ), "UTF8" );
266
InputStreamReader streamReader = new InputStreamReader( getXmlFile( xmlFileName ), "UTF8" );
267
BufferedReader bufferedReader = new BufferedReader( streamReader );
269
StringBuffer buffer = new StringBuffer();
271
while ( (line = bufferedReader.readLine()) != null )
273
buffer.append( line + "\n" );
276
out.write( buffer.toString() );
280
catch ( UnsupportedEncodingException e )
284
catch ( FileNotFoundException e )
288
catch ( IOException e )
294
private static InputStream getXmlFile( String fileName )
296
String path = "/dhis-web-reporttool/template/vietnam/xml/" + fileName;
297
ServletContext context = ServletActionContext.getServletContext();
298
return context.getResourceAsStream( path );
301
public static InputStream getSignatureJrxmlAsStream()
303
String path = "/dhis-web-reporttool/template/vietnam/TrangChuKy.jrxml";
304
ServletContext context = ServletActionContext.getServletContext();
305
return context.getResourceAsStream( path );
308
private static void checkXmlFile( String xmlPath )
310
File xml_file = new File( xmlPath );
311
if ( !xml_file.exists() )
313
makeXmlFile( xmlPath );