~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/StreamUtils.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.system.util;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import java.io.BufferedReader;
 
31
import java.io.BufferedWriter;
 
32
import java.io.File;
 
33
import java.io.FileInputStream;
 
34
import java.io.FileOutputStream;
 
35
import java.io.IOException;
 
36
import java.io.InputStream;
 
37
import java.io.InputStreamReader;
 
38
import java.io.OutputStream;
 
39
import java.io.OutputStreamWriter;
 
40
import java.io.Reader;
 
41
import java.io.Writer;
 
42
import java.util.Map;
 
43
import java.util.Map.Entry;
 
44
import java.util.zip.ZipEntry;
 
45
import java.util.zip.ZipInputStream;
 
46
import java.util.zip.ZipOutputStream;
 
47
 
 
48
/**
 
49
 * @author Lars Helge Overland
 
50
 * @version $Id$
 
51
 */
 
52
public class StreamUtils
 
53
{
 
54
    private static final String LINE_BREAK = "\n";
 
55
    private static final String ENCODING_UTF = "UTF8";
 
56
    
 
57
    /**
 
58
     * Loads a resorce from the classpath defined by the name parameter.
 
59
     * 
 
60
     * @param name the name of the resource.
 
61
     * @return an InputStream.
 
62
     */
 
63
    public static InputStream loadResource( String name )
 
64
    {
 
65
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 
66
            
 
67
        return classLoader.getResourceAsStream( name );
 
68
    }
 
69
 
 
70
    /**
 
71
     * Writes the content of the StringBuffer to the file.
 
72
     * 
 
73
     * @param file the file to write to.
 
74
     * @param content the content to write.
 
75
     * @throws IOException
 
76
     */
 
77
    public static void writeContent( File file, StringBuffer content )
 
78
        throws IOException
 
79
    {
 
80
        BufferedWriter writer = new BufferedWriter( 
 
81
            new OutputStreamWriter( new FileOutputStream( file ), ENCODING_UTF ) );
 
82
        
 
83
        try
 
84
        {
 
85
            writer.write( content.toString() );
 
86
        }
 
87
        finally
 
88
        {
 
89
            try
 
90
            {
 
91
                writer.flush();
 
92
            }
 
93
            catch ( Exception ex )
 
94
            {   
 
95
            }
 
96
            
 
97
            try
 
98
            {
 
99
                writer.close();
 
100
            }
 
101
            catch ( Exception ex )
 
102
            {   
 
103
            }
 
104
        }
 
105
    }
 
106
    
 
107
    /**
 
108
     * Reads the content of the file to a StringBuffer. Each line is compared to
 
109
     * the keys of the argument map. If a line is matched, the line is replaced 
 
110
     * with the keys corresponding value. Passing null as replace map argument skips
 
111
     * value replacement. The reading will stop at the first match for a single 
 
112
     * line. 
 
113
     * 
 
114
     * @param file the file to read from.
 
115
     * @param replaceMap a map containing keys to be matched and values with replacements.
 
116
     * @return a StringBuffer with the content of the file replaced according to the Map.
 
117
     * @throws IOException
 
118
     */
 
119
    public static StringBuffer readContent( File file, Map<String[], String> replaceMap )
 
120
        throws IOException
 
121
    {
 
122
        StringBuffer content = new StringBuffer();
 
123
        
 
124
        BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( file ), ENCODING_UTF ) );
 
125
        
 
126
        String line = null;
 
127
        
 
128
        String currentEndString = null;
 
129
        
 
130
        try
 
131
        {   
 
132
            while ( ( line = reader.readLine() ) != null )
 
133
            {
 
134
                if ( currentEndString != null )
 
135
                {
 
136
                    if ( line.contains( currentEndString ) )
 
137
                    {
 
138
                        currentEndString = null;
 
139
                    }
 
140
                    
 
141
                    continue;
 
142
                }
 
143
                
 
144
                if ( replaceMap != null )
 
145
                {
 
146
                    for ( Entry<String[], String> entry : replaceMap.entrySet() )
 
147
                    {
 
148
                        if ( line.contains( entry.getKey()[0] ) )
 
149
                        {
 
150
                            currentEndString = ( entry.getKey()[1] != null && !line.contains( entry.getKey()[1] ) ) ? entry.getKey()[1] : null;
 
151
                            
 
152
                            line = entry.getValue();
 
153
                            
 
154
                            break;
 
155
                        }
 
156
                    }
 
157
                }
 
158
                
 
159
                content.append( line + LINE_BREAK );
 
160
            }
 
161
        }
 
162
        finally
 
163
        {
 
164
            try
 
165
            {
 
166
                reader.close();
 
167
            }
 
168
            catch ( Exception ex )
 
169
            {   
 
170
            }
 
171
        }
 
172
        
 
173
        return content;
 
174
    }
 
175
    
 
176
    /**
 
177
     * Closes the given Reader.
 
178
     * 
 
179
     * @param reader the Reader to close.
 
180
     */
 
181
    public static void closeReader( Reader reader )
 
182
    {
 
183
        if ( reader != null )
 
184
        {
 
185
            try
 
186
            {
 
187
                reader.close();
 
188
            }
 
189
            catch ( Exception ex )
 
190
            {
 
191
                ex.printStackTrace();
 
192
            }
 
193
        }
 
194
    }
 
195
    
 
196
    /**
 
197
     * Closes the given Writer.
 
198
     * 
 
199
     * @param writer the Writer to close.
 
200
     */
 
201
    public static void closeWriter( Writer writer )
 
202
    {
 
203
        if ( writer != null )
 
204
        {
 
205
            try
 
206
            {
 
207
                writer.flush();
 
208
            }
 
209
            catch ( IOException ex )
 
210
            {
 
211
                ex.printStackTrace();
 
212
            }
 
213
            
 
214
            try
 
215
            {
 
216
                writer.close();
 
217
            }
 
218
            catch ( Exception ex )
 
219
            {
 
220
                ex.printStackTrace();
 
221
            }
 
222
        }
 
223
    }
 
224
    
 
225
    /**
 
226
     * Closes the given InputStream.
 
227
     * 
 
228
     * @param in the InputStream to close.
 
229
     */
 
230
    public static void closeInputStream( InputStream in )
 
231
    {
 
232
        if ( in != null )
 
233
        {
 
234
            try
 
235
            {
 
236
                in.close();
 
237
            }
 
238
            catch ( Exception ex )
 
239
            {
 
240
                ex.printStackTrace();
 
241
            }
 
242
        }
 
243
    }
 
244
    
 
245
    /**
 
246
     * Closes and flushes the given OutputStream.
 
247
     * 
 
248
     * @param out the OutputStream to close.
 
249
     */
 
250
    public static void closeOutputStream( OutputStream out )
 
251
    {
 
252
        if ( out != null )
 
253
        {
 
254
            try
 
255
            {
 
256
                out.flush();
 
257
            }
 
258
            catch ( Exception ex )
 
259
            {
 
260
                ex.printStackTrace();
 
261
            }
 
262
            
 
263
            try
 
264
            {
 
265
                out.close();
 
266
            }
 
267
            catch ( Exception ex )
 
268
            {
 
269
                ex.printStackTrace();
 
270
            }
 
271
        }
 
272
    }
 
273
    
 
274
    /**
 
275
     * Reads the next ZIP file entry from the ZipInputStream and positions the 
 
276
     * stream at the beginning of the entry data.
 
277
     * 
 
278
     * @param in the ZipInputStream to read from.
 
279
     * @return a ZipEntry.
 
280
     */
 
281
    public static ZipEntry getNextZipEntry( ZipInputStream in )
 
282
    {
 
283
        try
 
284
        {
 
285
            return in.getNextEntry();
 
286
        }
 
287
        catch ( Exception ex )
 
288
        {
 
289
            throw new RuntimeException( "Failed to get next entry in ZIP-file", ex );
 
290
        }
 
291
    }
 
292
    
 
293
    /**
 
294
     * Finishes writing the contents of the ZIP output stream without closing the underlying stream.
 
295
     * 
 
296
     * @param out the ZipOutputStream to write to.
 
297
     */
 
298
    public static void finishZipEntry( ZipOutputStream out )
 
299
    {
 
300
        try
 
301
        {
 
302
            out.finish();
 
303
        }
 
304
        catch ( Exception ex )
 
305
        {
 
306
            throw new RuntimeException( "Failed to finish ZipOutputStream", ex );
 
307
        }
 
308
    }
 
309
}