View Javadoc
1   package org.apache.maven.shared.io;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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       * writes content to a file, using platform encoding.
75       * @deprecated this API isn't explicit about encoding, use writePlatformFile() or writeXmlFile()
76       * depending on your need
77       */
78      public static void writeToFile( File file, String testStr )
79          throws IOException
80      {
81          writePlatformFile( file, testStr );
82      }
83  
84      /**
85       * reads content from a file and normalize EOLs to simple line feed (\\n), using platform encoding.
86       * @deprecated this API isn't explicit about encoding nor EOL normalization, use readPlatformFile() or
87       * readXmlFile() depending on your need, in conjunction with normalizeEndOfLine()
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      * normalize EOLs to simple line feed (\\n).
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             // should not occur since everything happens in-memory
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 }