01: /*
02: * Created on Aug 12, 2005
03: */
04: package uk.org.ponder.rsf.util;
05:
06: import java.io.Reader;
07: import java.io.StringReader;
08:
09: import org.xmlpull.mxp1.MXParser;
10: import org.xmlpull.v1.XmlPullParser;
11:
12: import uk.org.ponder.streamutil.write.PrintOutputStream;
13: import uk.org.ponder.streamutil.write.StringPOS;
14: import uk.org.ponder.util.UniversalRuntimeException;
15:
16: public class XMLFlattener {
17: public final static String FEATURE_XML_ROUNDTRIP = "http://xmlpull.org/v1/doc/features.html#xml-roundtrip";
18:
19: public static void flatten(Reader xmlstream, PrintOutputStream pos) {
20: XmlPullParser parser = new MXParser();
21: try {
22: parser.setFeature(FEATURE_XML_ROUNDTRIP, true);
23: parser.setInput(xmlstream);
24: //int[] limits = new int[2];
25: while (true) {
26: int token = parser.nextToken();
27: if (token == XmlPullParser.END_DOCUMENT)
28: break;
29: if (token == XmlPullParser.TEXT
30: || token == XmlPullParser.ENTITY_REF) {
31: // char[] chars = parser.getTextCharacters(limits);
32: // String text = new String(chars, limits[0], limits[1]);
33: String text = parser.getText();
34: pos.print(text);
35: }
36: }
37: } catch (Exception e) {
38: throw UniversalRuntimeException.accumulate(e,
39: "Error flattening HTML document");
40: }
41: }
42:
43: public static String flatten(String toflatten) {
44: StringPOS pos = new StringPOS();
45: StringReader reader = new StringReader(toflatten);
46: flatten(reader, pos);
47: pos.close();
48: String togo = pos.toString();
49: return togo;
50: }
51: }
|