Added escaping "<" and ">" in attribute values before passing XML to SAX parser.

This commit is contained in:
Evgeny Gerashchenko
2012-08-03 18:05:15 +04:00
parent 2e35e9b2dd
commit d1b1438fb3
@@ -131,7 +131,7 @@ public abstract class BaseExternalAnnotationsManager extends ExternalAnnotations
try {
VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) continue;
document = JDOMUtil.loadDocument(StreamUtil.readText(virtualFile.getInputStream()));
document = JDOMUtil.loadDocument(escapeAttributes(StreamUtil.readText(virtualFile.getInputStream())));
}
catch (IOException e) {
LOG.error(e);
@@ -226,4 +226,29 @@ public abstract class BaseExternalAnnotationsManager extends ExternalAnnotations
protected void dropCache() {
cache.clear();
}
// This method is used for legacy reasons.
// Old external annotations sometimes are bad XML: they have "<" and ">" characters in attributes values. To prevent SAX parser from
// failing, we escape attributes values.
@NotNull
private static String escapeAttributes(@NotNull String invalidXml) {
// We assume that XML has single- and double-quote characters only for attribute values, therefore we don't any complex parsing,
// just have binary inAttribute state
StringBuilder buf = new StringBuilder();
boolean inAttribute = false;
for (int i = 0; i < invalidXml.length(); i++) {
char c = invalidXml.charAt(i);
if (inAttribute && c == '<') {
buf.append("&lt;");
} else if (inAttribute && c == '>') {
buf.append("&gt;");
} else if (c == '\"' || c == '\'') {
buf.append('\"');
inAttribute = !inAttribute;
} else {
buf.append(c);
}
}
return buf.toString();
}
}