// First pass through the data. The goal of this pass is to
// validate that we have a good XML document and to repair
// any problems found.
XMLNoteRepair repair = new XMLNoteRepair();
// logger.log(logger.HIGH, "Checking XML Structure");
// newContent = repair.parse(newContent, false);
// logger.log(logger.HIGH, "Check complete");
logger.log(logger.HIGH, "Fixing encryption tags");
newContent = fixEncryptionTags(newContent);
Tidy tidy = new Tidy();
TidyListener tidyListener = new TidyListener(logger);
tidy.setMessageListener(tidyListener);
tidy.getStderr().close(); // the listener will capture messages
tidy.setXmlTags(true);
tidy.setXHTML(true);
QTextCodec codec;
codec = QTextCodec.codecForName("UTF-8");
QByteArray unicode = codec.fromUnicode(newContent);
// byte html[] = newContent.getBytes();
// ByteArrayInputStream is = new ByteArrayInputStream(html);
logger.log(logger.HIGH, "Starting JTidy check");
logger.log(logger.EXTREME, "Start of JTidy Input");
logger.log(logger.EXTREME, newContent);
logger.log(logger.EXTREME, "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, "Note Contents Begin");
logger.log(logger.LOW, content);
logger.log(logger.LOW, "Note Contents End");
tidyContent = null;
} else {
if (newContent.trim().equals(""))
tidyContent = null;
}
// If the repair above returned null, then the XML is foobar.
// We are done here.
if (tidyContent != null) {
newContent = tidyContent;
} else {
// Houston, we've had a problem. Fall back to old method
logger.log(logger.HIGH, "Error converting to JTidy. Falling back to old method");
String repairedContent = repair.parse(newContent, false);
if (repairedContent == null) {
logger.log(logger.EXTREME, "Null returned from repair.parse()");
logger.log(logger.LOW, "Parse error when converting to ENML. Aborting save");
return null;
}
newContent = repairedContent;
logger.log(logger.EXTREME, "Start of repaired content");
logger.log(logger.EXTREME, repairedContent);
logger.log(logger.EXTREME, "End of repaired content");
}
// Second pass through the data. The goal of this pass is to
// remove any things we added in NixNote that do not match
// the ENML schema
XMLCleanup v = new XMLCleanup();
v.setValue(newContent);
logger.log(logger.HIGH, "Beginning ENML Cleanup");
v.validate();
logger.log(logger.HIGH, "Cleanup complete.");
// Final pass through the data. In this one we
// remove any invalid attributes and to save the
// new resources.
logger.log(logger.EXTREME, "Rebuilt ENML:");
logger.log(logger.EXTREME, v.getValue());
logger.log(logger.EXTREME, "End Of Rebuilt ENML:");
resources = v.getResources();
// The XML has the dtd to validate set against Evernote's web
// address. We change it to a local one because otherwise it would
// fail if the user doesn't have internet connectivity. The local copy
// also contains the 3 other PUBLIC definitions at the beginning of the dtd.
newContent = v.getValue();
File dtdFile = Global.getFileManager().getXMLDirFile("enml2.dtd");
String dtd = dtdFile.toURI().toString();
newContent = newContent.replace("<!DOCTYPE en-note SYSTEM \'http://xml.evernote.com/pub/enml2.dtd'>",
"<!DOCTYPE en-note SYSTEM \"" +dtd +"\">");
logger.log(logger.HIGH, "Validating ENML");
String repairedContent = repair.parse(newContent, true);
if (repairedContent == null)
logger.log(logger.EXTREME, "Null returned from repair.parse()");
else
newContent = repairedContent;
logger.log(logger.HIGH, "Validation complete");