Advertisement
Guest User

Column Separation for Screen Readers maybe!!!

a guest
Jun 29th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4. public class ColumnedString {
  5.     private final String raw;
  6.     private final String screenReader;
  7.     public ColumnedString(String raw) {
  8.         this.raw = raw;
  9.         this.screenReader = this.divideColumns(raw);
  10.     }
  11.     private String divideColumns(String source) {
  12.         StringBuilder result = new StringBuilder();
  13.         StringBuilder curSB;
  14.         ArrayList<StringBuilder> columns = new ArrayList<StringBuilder>();
  15.         ArrayList<Integer> curColumns = new ArrayList<Integer>();
  16.         String[] lineColumns;
  17.         BufferedReader br = new BufferedReader(new StringReader(source));
  18.         String line;
  19.         try {
  20.             while ((line = br.readLine()) != null) {
  21.                 // split lines into columns, where columns are separated by one or more |
  22.                 // also, strip any excess white space
  23.                 lineColumns = line.split(" *\\|+ *");
  24.                 while (lineColumns.length < curColumns.size()) {
  25.                     // truncate columns no longer in use
  26.                     curColumns.remove(curColumns.size()-1);
  27.                 }
  28.                 while (lineColumns.length > curColumns.size()) {
  29.                     // add new columns
  30.                     curColumns.add(columns.size());
  31.                     columns.add(new StringBuilder());
  32.                 }
  33.                 for (int i = 0; i < lineColumns.length; i++) {
  34.                     curSB = columns.get(curColumns.get(i));
  35.                     if (curSB.length() > 0 && curSB.charAt(curSB.length()-1) != ' ') {
  36.                         // separate lines of the same column with a space if they aren't already
  37.                         curSB.append(' ');
  38.                     }
  39.                     curSB.append(lineColumns[i]);
  40.                 }
  41.             }
  42.         } catch (IOException e) {
  43.             // why would a StringReader even throw an exception on readLine()? *_*
  44.         }
  45.         String separator = "";
  46.         for (int i = 0; i < columns.size(); i++) {
  47.             if (columns.get(i).length() == 0) {
  48.                 // don't add empty columns - they're presumably just visual artifacts anyway
  49.                 continue;
  50.             }
  51.             result.append(separator).append(columns.get(i));
  52.             separator = "\n\n";
  53.         }
  54.         return result.toString();
  55.     }
  56.     public String getRaw() {
  57.         return this.raw;
  58.     }
  59.     public String getScreenReader() {
  60.         return this.screenReader;
  61.     }
  62.     // driver function
  63.     public static void main(String args[]) {
  64.         String test = "| Column 1 text  | column 2 text | column 3\n" +
  65.                 "| could go here | could go here\n" +
  66.                 "| end of column 1 | | column 4";
  67.         ColumnedString cs = new ColumnedString(test);
  68.         System.out.println("-ORIGINAL-");
  69.         System.out.println(cs.getRaw());
  70.         System.out.println("-SCREEN READER-");
  71.         System.out.println(cs.getScreenReader());
  72.         System.out.println("-END-");
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement