summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/gui/SourceFormatter.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/cuchaz/enigma/gui/SourceFormatter.java')
-rw-r--r--src/cuchaz/enigma/gui/SourceFormatter.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/gui/SourceFormatter.java b/src/cuchaz/enigma/gui/SourceFormatter.java
new file mode 100644
index 0000000..f387840
--- /dev/null
+++ b/src/cuchaz/enigma/gui/SourceFormatter.java
@@ -0,0 +1,62 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.gui;
12
13import java.io.BufferedReader;
14import java.io.IOException;
15import java.io.StringReader;
16
17public class SourceFormatter
18{
19 public static String format( String in )
20 {
21 return collapseNewlines( in );
22 }
23
24 private static String collapseNewlines( String in )
25 {
26 StringBuffer buf = new StringBuffer();
27 int numBlankLines = 0;
28
29 BufferedReader reader = new BufferedReader( new StringReader( in ) );
30 String line = null;
31 try
32 {
33 while( ( line = reader.readLine() ) != null )
34 {
35 // how blank lines is this?
36 boolean isBlank = line.trim().length() == 0;
37 if( isBlank )
38 {
39 numBlankLines++;
40
41 // stop printing blank lines after the first one
42 if( numBlankLines < 2 )
43 {
44 buf.append( line );
45 buf.append( "\n" );
46 }
47 }
48 else
49 {
50 numBlankLines = 0;
51 buf.append( line );
52 buf.append( "\n" );
53 }
54 }
55 }
56 catch( IOException ex )
57 {
58 // StringReader will never throw an IOExecption here...
59 }
60 return buf.toString();
61 }
62}