Subversion Repository Public Repository

litesoft

Diff Revisions 521 vs 526 for /trunk/GWT_Sandbox/Upload/src/org/litesoft/sandbox/csapp/server/UploadSmallFileReflectorServlet.java

Diff revisions: vs.
  @@ -13,7 +13,9 @@
13 13
14 14 public static final int MAX_FILE_SIZE = 16 * 1024;
15 15
16 - private static final char[] SEPARATOR_OPTIONS = {'-', '+', '=', '*'};
16 + private static final char[] SPACE_OPTIONS = {'~', '^'};
17 + private static final char[] NEWLINE_OPTIONS = {'|', '#'};
18 + private static final char[] SEPARATOR_OPTIONS = {'=', '*'};
17 19
18 20 private SmallFileValidator validator = SmallFileValidatorOnlyDisplayable7BitAscii.INSTANCE;
19 21
  @@ -46,15 +48,60 @@
46 48
47 49 private void reflectSuccess( PrintWriter writer, SmallFileValidator.AcceptedResult results )
48 50 {
49 - writer.println( "Success: " + results.getFileName() );
50 - String separator = determineLineSeparator( results.getDisplayText(), results.getEncodedContents() );
51 + String space = determineSeparator( SPACE_OPTIONS, 1, results.getFileName(), results.getDisplayText(), results.getEncodedContents() );
52 + String newLine = determineSeparator( NEWLINE_OPTIONS, 1, results.getFileName(), results.getDisplayText(), results.getEncodedContents() );
53 + String separator = determineSeparator( SEPARATOR_OPTIONS, 8, results.getFileName(), results.getDisplayText(), results.getEncodedContents() );
54 + writer.println( "Success:" + newLine + " "+ space + " " + separator + newLine );
55 + writer.println( results.getFileName() );
51 56 writer.println( separator );
52 - writer.println( results.getDisplayText() );
57 + writer.println( encodeSpacesAndNewLinesAndEscapeHtml( results.getDisplayText(), space, newLine ) );
53 58 writer.println( separator );
54 59 writer.println( results.getEncodedContents() );
55 60 writer.println( separator );
56 61 }
57 62
63 + private String encodeSpacesAndNewLinesAndEscapeHtml( String text, String space, String newLine )
64 + {
65 + StringBuilder sb = new StringBuilder();
66 + for ( int i = 0; i < text.length(); i++ )
67 + {
68 + char c = text.charAt( i );
69 + switch ( c )
70 + {
71 + case ' ':
72 + sb.append( space );
73 + break;
74 + case '\n':
75 + sb.append( newLine ).append( '\n' );
76 + break;
77 + case '&':
78 + sb.append( "&amp;" );
79 + break;
80 + case '<':
81 + sb.append( "&lt;" );
82 + break;
83 + case '>':
84 + sb.append( "&gt;" );
85 + break;
86 + case '"':
87 + sb.append( "&quot;" );
88 + break;
89 + default:
90 + if ( (' ' < c) && (c <= 126) )
91 + {
92 + sb.append( c );
93 + }
94 + else
95 + {
96 + sb.append( "&#" ).append( (int)c ).append( ';' );
97 +
98 + }
99 + break;
100 + }
101 + }
102 + return sb.toString();
103 + }
104 +
58 105 private void reflectError( PrintWriter writer, Exception exception )
59 106 {
60 107 // LOGGER.warn.log( exception );
  @@ -118,18 +165,18 @@
118 165 return fileName;
119 166 }
120 167
121 - private String determineLineSeparator( String data1, String data2 )
168 + private String determineSeparator( char[] options, int lengthFactor, String data1, String data2, String data3 )
122 169 {
123 - for ( int blockSize = 8; true; blockSize += 8 )
170 + for ( int blockSize = lengthFactor; true; blockSize += lengthFactor )
124 171 {
125 - for ( char option : SEPARATOR_OPTIONS )
172 + for ( char option : options )
126 173 {
127 174 StringBuilder sb = new StringBuilder( blockSize );
128 175 for ( int i = 0; i < blockSize; i++ )
129 176 {
130 177 sb.append( option );
131 178 }
132 - if ( !data1.contains( sb ) && !data2.contains( sb ) )
179 + if ( !data1.contains( sb ) && !data2.contains( sb ) && !data3.contains( sb ) )
133 180 {
134 181 return sb.toString(); // The Separator we'll use
135 182 }