if (currentNote == null)
return null;
logger.log(logger.HIGH, "Entering NeverNote.rebuildNoteHTML");
logger.log(logger.EXTREME, "Note guid: " +currentNoteGuid);
logger.log(logger.EXTREME, "Note Text:" +currentNote);
QDomDocument doc = new QDomDocument();
QDomDocument.Result result = doc.setContent(currentNote.getContent());
// Handle any errors
if (!result.success) {
logger.log(logger.LOW, "Error parsing document. Attempting to restructure");
Tidy tidy = new Tidy();
TidyListener tidyListener = new TidyListener(logger);
tidy.setMessageListener(tidyListener);
tidy.getStderr().close(); // the listener will capture messages
tidy.setXmlTags(true);
QTextCodec codec;
codec = QTextCodec.codecForName("UTF-8");
QByteArray unicode = codec.fromUnicode(currentNote.getContent());
logger.log(logger.MEDIUM, "Starting JTidy check");
logger.log(logger.MEDIUM, "Start of JTidy Input");
logger.log(logger.MEDIUM, currentNote.getContent());
logger.log(logger.MEDIUM, "End Of JTidy Input");
ByteArrayInputStream is = new ByteArrayInputStream(unicode.toByteArray());
ByteArrayOutputStream os = new ByteArrayOutputStream();
tidy.setInputEncoding("UTF-8");
tidy.parse(is, os);
String tidyContent = os.toString();
if (tidyListener.errorFound) {
logger.log(logger.LOW, "Restructure failed!!!");
} else {
doc = null;
doc = new QDomDocument();
result = doc.setContent(tidyContent);
}
}
if (!result.success) {
logger.log(logger.MEDIUM, "Parse error when rebuilding XML to HTML");
logger.log(logger.MEDIUM, "Note guid: " +currentNoteGuid);
logger.log(logger.MEDIUM, "Error: "+result.errorMessage);
logger.log(logger.MEDIUM, "Line: " +result.errorLine + " Column: " +result.errorColumn);
System.out.println("Error: "+result.errorMessage);
System.out.println("Line: " +result.errorLine + " Column: " +result.errorColumn);
logger.log(logger.EXTREME, "**** Start of unmodified note HTML");
logger.log(logger.EXTREME, currentNote.getContent());
logger.log(logger.EXTREME, "**** End of unmodified note HTML");
formatError = true;
readOnly = true;
return currentNote.getContent();
}
if (tempFiles == null)
tempFiles = new ArrayList<QTemporaryFile>();
tempFiles.clear();
doc = modifyTags(doc);
if (addHighlight)
doc = addHilight(doc);
QDomElement docElem = doc.documentElement();
docElem.setTagName("Body");
// docElem.setAttribute("bgcolor", "green");
logger.log(logger.EXTREME, "Rebuilt HTML:");
logger.log(logger.EXTREME, doc.toString());
logger.log(logger.HIGH, "Leaving NeverNote.rebuildNoteHTML");
// Fix the stupid problem where inserting an <img> tag after an <a> tag (which is done
// to get the <en-media> application tag to work properly) causes spaces to be inserted
// between the <a> & <img>. This messes things up later. This is an ugly hack.
StringBuffer html = new StringBuffer(doc.toString());
for (int i=html.indexOf("<a en-tag=\"en-media\" ", 0); i>-1; i=html.indexOf("<a en-tag=\"en-media\" ", i)) {
i=html.indexOf(">\n",i+1);
int z = html.indexOf("<img",i);
for (int j=z-1; j>i; j--)
html.deleteCharAt(j);