private void initializeContent() { InputStream is = null; try { File tempFile = new File(kloWorkspaceFile.getTempName(owner)); if (tempFile.exists()) { is = new FileInputStream(tempFile); } else { if (kloWorkspaceFile.getFileName() == null) { throw new IOException("The file doesn't exist."); } File file = new File(kloWorkspaceFile.getFileName()); if (!file.exists()) { throw new IOException("Can't access the file: " + file.toURI()); } is = new FileInputStream(file); } splitSourceFile(highlightSource(is)); } catch (IOException exception) { sourceCode = "Can't read file: " + exception.getLocalizedMessage(); } catch (RuntimeException re) { sourceCode = "Problem for display the source code content: " + re.getLocalizedMessage(); } finally { IOUtils.closeQuietly(is); } }
/** * Splits the source code into three blocks: the line to highlight and the source code before and * after this line. * * @param sourceFile the source code of the whole file as rendered HTML string */ public final void splitSourceFile(final String sourceFile) { StringBuilder output = new StringBuilder(sourceFile.length()); KloFile kloFile = kloWorkspaceFile.getKloFile(); LineIterator lineIterator = IOUtils.lineIterator(new StringReader(sourceFile)); int lineNumber = 1; // ---header while (lineNumber < SOURCE_GENERATOR_OFFSET) { copyLine(output, lineIterator); lineNumber++; } lineNumber = 1; // ---iterate before the error line while (lineNumber < Integer.parseInt(((String) kloFile.get("line")))) { copyLine(output, lineIterator); lineNumber++; } output.append("</code>\n"); // ---Error message output.append("</td></tr>\n"); output.append("<tr><td bgcolor=\""); appendRangeColor(output); output.append("\">\n"); output.append("<div tooltip=\""); // AM // outputEscaped(output, kloFile.getProblemId()+":"+kloFile.getMessage()); outputEscaped(output, kloFile.get("problemID") + ":" + kloFile.get("message")); output.append("\" nodismiss=\"\">\n"); output.append("<code><b>\n"); // The current line error copyLine(output, lineIterator); lineNumber++; // End of the code output.append("</b></code>\n"); output.append("</div>\n"); output.append("</td></tr>\n"); output.append("<tr><td>\n"); output.append("<code>\n"); while (lineIterator.hasNext()) { copyLine(output, lineIterator); } output.append("</code>\n"); output.append("</td></tr>\n"); sourceCode = output.toString(); }