Permalink
Browse files

modified TaskItemBuilder to use encoding that is set for the file that

is read to make sure that characters in discovered task items are
handled correctly
  • Loading branch information...
1 parent 84da089 commit 747cf22b86e758fbc2c6e427710daf7b3c082551 @mirkoseifert mirkoseifert committed Apr 2, 2013
@@ -79,7 +79,8 @@ private void addBuildMethod(JavaComposite sc) {
sc.add(taskItemDetectorClassName + " taskItemDetector = new " + taskItemDetectorClassName + "();");
sc.add("try {");
sc.add(INPUT_STREAM + " inputStream = resource.getContents();");
- sc.add("String content = " + streamUtilClassName + ".getContent(inputStream);");
+ sc.add("String charset = resource.getCharset();");
+ sc.add("String content = " + streamUtilClassName + ".getContent(inputStream, charset);");
sc.add(iTextScannerClassName + " lexer = new " + metaInformationClassName + "().createLexer();");
sc.add("lexer.setText(content);");
sc.addLineBreak();
@@ -34,8 +34,20 @@ public void generateJavaContents(JavaComposite sc) {
sc.addLineBreak();
sc.add("public class " + getResourceClassName() + " {");
sc.addLineBreak();
+ addFields(sc);
+ addCopyMethod(sc);
+ addGetContentMethod1(sc);
+ addGetContentMethod2(sc);
+ addGetContentMethod3(sc);
+ sc.add("}");
+ }
+
+ private void addFields(JavaComposite sc) {
sc.add("private final static int IO_BUFFER_SIZE = 4 * 1024;");
sc.addLineBreak();
+ }
+
+ private void addCopyMethod(JavaComposite sc) {
sc.add("public static void copy(" + INPUT_STREAM + " in, " + OUTPUT_STREAM + " out) throws " + IO_EXCEPTION + " {");
sc.add("byte[] b = new byte[IO_BUFFER_SIZE];");
sc.add("int read;");
@@ -45,15 +57,33 @@ public void generateJavaContents(JavaComposite sc) {
sc.add("out.flush();");
sc.add("}");
sc.addLineBreak();
+ }
+
+ private void addGetContentMethod1(JavaComposite sc) {
sc.add("public static String getContent(" + INPUT_STREAM + " inputStream) throws " + IO_EXCEPTION + " {");
- sc.add("StringBuffer content = new StringBuffer();");
sc.add(INPUT_STREAM_READER + " reader = new " + INPUT_STREAM_READER + "(inputStream);");
+ sc.add("return getContent(reader);");
+ sc.add("}");
+ sc.addLineBreak();
+ }
+
+ private void addGetContentMethod2(JavaComposite sc) {
+ sc.add("public static String getContent(" + INPUT_STREAM + " inputStream, String charset) throws " + IO_EXCEPTION + " {");
+ sc.add(INPUT_STREAM_READER + " reader = new " + INPUT_STREAM_READER + "(inputStream, charset);");
+ sc.add("return getContent(reader);");
+ sc.add("}");
+ sc.addLineBreak();
+ }
+
+ private void addGetContentMethod3(JavaComposite sc) {
+ sc.add("public static String getContent(" + INPUT_STREAM_READER + " reader) throws " + IO_EXCEPTION + " {");
+ sc.add("StringBuffer content = new StringBuffer();");
sc.add("int next = -1;");
sc.add("while ((next = reader.read()) >= 0) {");
sc.add("content.append((char) next);");
sc.add("}");
sc.add("return content.toString();");
sc.add("}");
- sc.add("}");
+ sc.addLineBreak();
}
}

0 comments on commit 747cf22

Please sign in to comment.