diff options
| author | 2014-07-26 19:24:00 -0400 | |
|---|---|---|
| committer | 2014-07-26 19:24:00 -0400 | |
| commit | 295eceece371b516e771de93b6127bf728999483 (patch) | |
| tree | 20ff6a35de79997fc338d922cae934b8872b11a9 /src/cuchaz/enigma/gui/SourceFormatter.java | |
| download | enigma-fork-295eceece371b516e771de93b6127bf728999483.tar.gz enigma-fork-295eceece371b516e771de93b6127bf728999483.tar.xz enigma-fork-295eceece371b516e771de93b6127bf728999483.zip | |
initial commit
so far source analysis is working. =)
Diffstat (limited to 'src/cuchaz/enigma/gui/SourceFormatter.java')
| -rw-r--r-- | src/cuchaz/enigma/gui/SourceFormatter.java | 62 |
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 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.gui; | ||
| 12 | |||
| 13 | import java.io.BufferedReader; | ||
| 14 | import java.io.IOException; | ||
| 15 | import java.io.StringReader; | ||
| 16 | |||
| 17 | public 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 | } | ||