private static String HTML_P_START = "<p style=\"margin-top: 0\">";
private static String HTML_P_FIRST_START = "<p class=\"first\" style=\"margin-top: 0\">";
private String[] getBody(String msg) {
msg = msg.replaceAll("<img([^>]*?)/?>", "<img$1/>");
XmlPullParser np = new MXParser();
try {
np.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
np.setInput(new StringReader("<XD>" + msg + "</XD>"));
} catch (XmlPullParserException ex) {
Logger.getLogger(ClientParser.class.getName()).log(Level.SEVERE, null, ex);
}
String ret = null;
String pure = null;
try {
int eventType = np.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if ("body".equals(np.getName())) {
StringBuilder retb = new StringBuilder();
StringBuilder pureb = new StringBuilder();
boolean hasP = false;
while (true) {
eventType = np.nextToken();
if (eventType == XmlPullParser.END_TAG && "body".equals(np.getName())) {
break;
}
if (eventType == XmlPullParser.START_TAG && "p".equals(np.getName())) {
if (hasP == false) {
hasP = true;
retb.append(HTML_P_FIRST_START);
} else {
retb.append(HTML_P_START);
}
} else if (eventType == XmlPullParser.ENTITY_REF) {
String str = getEntityRef(np);
retb.append(str);
pureb.append(str);
} else if (eventType == XmlPullParser.START_TAG || eventType == XmlPullParser.END_TAG) {
if (!(eventType == XmlPullParser.START_TAG && np.isEmptyElementTag())) {
retb.append(np.getText().trim());
}
} else if (eventType != XmlPullParser.IGNORABLE_WHITESPACE) {
String str = np.getText().trim().replaceAll("\n( )*", "\n");
// hack for indent.
retb.append(str.replaceAll(" ", " "));
pureb.append(str);
}
}
if (hasP) {
ret = retb.toString();
} else {
ret = HTML_P_FIRST_START + retb.toString() + "</p>";
}
pure = pureb.toString();
}
}
eventType = np.next();
}
} catch (XmlPullParserException ex) {
Logger.getLogger(ClientParser.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ClientParser.class.getName()).log(Level.SEVERE, null, ex);