Repository navigation
Expand file tree
/
Copy pathXMLParseHelp.java
More file actions
69 lines (55 loc) · 2.67 KB
/
Copy pathXMLParseHelp.java
File metadata and controls
69 lines (55 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class XMLParseHelp {
private String escapeAmpersands(String unescapedMessageText) {
/*
* This escaping is a bit tortured. Apparently there is a mixture of escaped characters and
* un-escaped ampersands being passed in. So any un-escaped ampersands are escaped and then
* the process reversed for already-escaped characters. StringUtil doesn't have any regex-matching
* capability so it is necessary to do it in this manner.
*/
//first the un-escape ampersands and less than are escaped
String partiallyEscapedMessageText = StringUtil.replace(unescapedMessageText, "&", "&", false);
partiallyEscapedMessageText = StringUtil.replace(partiallyEscapedMessageText, "<", "<", false);
//then the process reversed for already-escaped characters
partiallyEscapedMessageText = StringUtil.replace(partiallyEscapedMessageText, "&quot;", """, false);
partiallyEscapedMessageText = StringUtil.replace(partiallyEscapedMessageText, "&apos;", "'", false);
partiallyEscapedMessageText = StringUtil.replace(partiallyEscapedMessageText, "&lt;" , "<" , false);
partiallyEscapedMessageText = StringUtil.replace(partiallyEscapedMessageText, "&gt;" , ">" , false);
String escapedMessageText = StringUtil.replace(partiallyEscapedMessageText, "&amp;" , "&" , false);
//Escape '<' character available in real-time data
escapedMessageText = parseLessThan(escapedMessageText, "<", "<");
if(log.isDebugEnabled())
{
log.debug("Any ampersands and < in the messageText have been escaped to '&' and '<'");
// dump the Text message
log.debug("Escaped message is now:\n<" + escapedMessageText + ">");
}
return escapedMessageText;
}
/*
* Escape less than character in the real-time data
* <P>The following characters are replaced with corresponding character entities :
* <tr><th> Character </th><th> Encoding </th></tr>
* <tr><td> < </td><td> < </td></tr>
* <P>
*/
private String parseLessThan(
String text,
String oldSubString,
String newSubString)
{
String searchString = text;
String searchPattern = oldSubString;
int index = searchString.indexOf("<", 1);
StringBuffer buf = new StringBuffer(text);
while (index != -1) {
if((buf.charAt(index - 1) != '>') && (buf.charAt(index + 1) != '/'))
{
buf.replace(index, index + searchPattern.length(), newSubString);
}
text = buf.toString();
searchString = text;
index = searchString.indexOf(searchPattern, index + newSubString.length());
}
return text;
}
}