View Javadoc

1   /*
2   Copyright (C) 2000 - 2007 Grid Systems, S.A.
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2, as
6   published by the Free Software Foundation.
7   
8   This program is distributed in the hope that it will be useful,
9   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17  package com.gridsystems.utils.text;
18  
19  import java.io.BufferedReader;
20  import java.io.BufferedWriter;
21  import java.io.File;
22  import java.io.FileReader;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  
26  import org.apache.commons.io.IOUtils;
27  
28  /**
29   * End-Of-Line Management Utilities.
30   *
31   * @author Gaston Freire
32   * @author Rodrigo Ruiz
33   */
34  public final class EOLUtils {
35  
36    /**
37     * 64KBytes buffer size.
38     */
39    private static final int BUF_SIZE = 65536;
40  
41    /** Unix scheme. */
42    public static final int UNIX = 0;
43    /** DOS scheme. */
44    public static final int DOS = 1;
45    /** Mac scheme. */
46    public static final int MAC = 2;
47    /** Undefined scheme. */
48    public static final int UNDEFINED = 3;
49  
50    /**
51     * String constants with the names of the different EOL schemes, indexed using
52     * the above constants.
53     */
54    public static final String[] SCHEMENAMES = {"UNIX", "DOS", "MAC", "UNDEFINED"};
55  
56    /**
57     * Strings of characters used for EOL by the different schemes.
58     */
59    private static final String[] EOLS = {"\n", "\r\n", "\r", ""};
60  
61    /**
62     * Private constructor.
63     */
64    private EOLUtils() {
65    }
66  
67    /**
68     * Detects what kind of EOL is present in the source String.
69     *
70     * @param source text to analyze
71     * @return int with the kind of EOL present (see constants above)
72     */
73    public static int detectEOL(String source) {
74      // UNIX is the default
75      int scheme = UNDEFINED;
76      if (source != null) {
77        if (source.indexOf(EOLS[DOS]) != -1) {
78          scheme = DOS;
79        } else if (source.indexOf(EOLS[MAC]) != -1) {
80          scheme = MAC;
81        } else if (source.indexOf(EOLS[UNIX]) != -1) {
82          scheme = UNIX;
83        }
84      }
85      return scheme;
86    }
87  
88  
89    /**
90     * Detects what kind of EOL is present in the source text file.
91     *
92     * @param source text file to analyze
93     * @return int with the kind of EOL present (see constants above)
94     * @throws IOException if error
95     */
96    public static int detectEOL(File source) throws IOException {
97      if (source == null || !source.exists()) {
98        return UNDEFINED;
99      }
100 
101     BufferedReader br = null;
102     try {
103       br = new BufferedReader(new FileReader(source));
104       char[] buffer = new char[BUF_SIZE];
105       int read = br.read(buffer);
106 
107       if (read > 0) {
108         return detectEOL(String.valueOf(buffer, 0, read));
109       } else {
110         return UNDEFINED;
111       }
112     } finally {
113       IOUtils.closeQuietly(br);
114     }
115   }
116 
117 
118   /**
119    * Applies a scheme of EOL to the source.
120    *
121    * @param source text to convert
122    * @param scheme of EOL to apply
123    * @return String with source converted to the scheme
124    */
125   public static String applyEOL(String source, int scheme) {
126     int baseScheme = detectEOL(source);
127     if (baseScheme != UNDEFINED && scheme != baseScheme) {
128       // Replaces all the existing EOLs with the new
129       return source.replaceAll(EOLS[baseScheme], EOLS[scheme]);
130     } else {
131       return source;
132     }
133   }
134 
135 
136   /**
137    * Applies a scheme of EOL to the source text file.
138    *
139    * @param source text file to convert
140    * @param scheme of EOL to apply
141    * @throws IOException if error
142    */
143   public static void applyEOL(File source, int scheme) throws IOException {
144     if (source == null || !source.exists()) {
145       return;
146     }
147 
148     String eol = EOLS[scheme];
149 
150     boolean eolDetected = false;
151 
152     StringBuffer sb = new StringBuffer();
153     BufferedReader br = null;
154     try {
155       br = new BufferedReader(new FileReader(source));
156       int c = br.read();
157 
158       while (c != -1) {
159         if (eolDetected) {
160           eolDetected = (c == '\r');
161           sb.append(eol);
162           if (c != '\n' && c != '\r') {
163             sb.append((char)c);
164           }
165         } else {
166           if (c == '\r') {
167             eolDetected = true;
168           } else if (c == '\n') {
169             sb.append(eol);
170           } else {
171             sb.append((char)c);
172           }
173         }
174         c = br.read();
175       }
176       if (eolDetected) {
177         sb.append(eol);
178       }
179     } finally {
180       IOUtils.closeQuietly(br);
181     }
182 
183     BufferedWriter bw = null;
184     try {
185       bw = new BufferedWriter(new FileWriter(source));
186       bw.write(sb.toString());
187     } finally {
188       IOUtils.closeQuietly(bw);
189     }
190   }
191 }