1 package org.apache.maven.shared.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.io.Reader;
27 import java.io.StringReader;
28 import java.io.StringWriter;
29 import java.io.Writer;
30
31 import org.apache.maven.shared.utils.io.IOUtil;
32 import org.apache.maven.shared.utils.ReaderFactory;
33 import org.apache.maven.shared.utils.WriterFactory;
34
35 public final class Utils
36 {
37
38 private Utils()
39 {
40 }
41
42 private static void write( Writer writer, String content )
43 throws IOException
44 {
45 try
46 {
47 writer.write( content );
48 }
49 finally
50 {
51 IOUtil.close( writer );
52 }
53 }
54
55 public static void writePlatformFile( File file, String content )
56 throws IOException
57 {
58 write( WriterFactory.newPlatformWriter( file ), content );
59 }
60
61 public static void writeFileWithEncoding( File file, String content, String encoding )
62 throws IOException
63 {
64 write( WriterFactory.newWriter( file, encoding ), content );
65 }
66
67 public static void writeXmlFile( File file, String content )
68 throws IOException
69 {
70 write( WriterFactory.newXmlWriter( file ), content );
71 }
72
73
74
75
76
77
78 public static void writeToFile( File file, String testStr )
79 throws IOException
80 {
81 writePlatformFile( file, testStr );
82 }
83
84
85
86
87
88
89 public static String readFile( File file ) throws IOException
90 {
91 return normalizeEndOfLine( readPlatformFile( file ) );
92 }
93
94 public static String readPlatformFile( File file ) throws IOException
95 {
96 StringWriter buffer = new StringWriter();
97
98 Reader reader = ReaderFactory.newPlatformReader( file );
99
100 IOUtil.copy( reader, buffer );
101
102 return buffer.toString();
103 }
104
105 public static String readXmlFile( File file ) throws IOException
106 {
107 StringWriter buffer = new StringWriter();
108
109 Reader reader = ReaderFactory.newXmlReader( file );
110
111 IOUtil.copy( reader, buffer );
112
113 return buffer.toString();
114 }
115
116
117
118
119 public static String normalizeEndOfLine( String content )
120 {
121 StringBuffer buffer = new StringBuffer();
122
123 BufferedReader reader = new BufferedReader( new StringReader( content ) );
124
125 String line = null;
126
127 try {
128 while( ( line = reader.readLine() ) != null )
129 {
130 if ( buffer.length() > 0 )
131 {
132 buffer.append( '\n' );
133 }
134
135 buffer.append( line );
136 }
137 }
138 catch ( IOException ioe )
139 {
140
141 }
142
143 return buffer.toString();
144 }
145
146 public static String toString( Throwable error )
147 {
148 StringWriter sw = new StringWriter();
149 PrintWriter pw = new PrintWriter( sw );
150
151 error.printStackTrace( pw );
152
153 return sw.toString();
154 }
155 }