aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/test/java/com/juick/server/XmlUtils.java
blob: 85fd352cdde995ded1dd976de16eda3c1445e805 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Juick
 * Copyright (C) 2008-2011, Ugnich Anton
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.juick.server;

import java.io.IOException;

import org.apache.commons.text.StringEscapeUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 *
 * @author Ugnich Anton
 */
public class XmlUtils {

    public static void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        String tag = parser.getName();
        while (parser.getName() != null && !(parser.next() == XmlPullParser.END_TAG && parser.getName().equals(tag))) {
        }
    }

    public static String getTagText(XmlPullParser parser) throws XmlPullParserException, IOException {
        String ret = "";
        String tag = parser.getName();

        if (parser.next() == XmlPullParser.TEXT) {
            ret = parser.getText();
        }

        while (!(parser.getEventType() == XmlPullParser.END_TAG && parser.getName().equals(tag))) {
            parser.next();
        }

        return ret;
    }

    public static String parseToString(XmlPullParser parser, boolean skipXMLNS) throws XmlPullParserException, IOException {
        String tag = parser.getName();
        StringBuilder ret = new StringBuilder("<").append(tag);

        // skipXMLNS for xmlns="jabber:client"

        String ns = parser.getNamespace();
        if (!skipXMLNS && ns != null && !ns.isEmpty()) {
            ret.append(" xmlns=\"").append(ns).append("\"");
        }

        for (int i = 0; i < parser.getAttributeCount(); i++) {
            String attr = parser.getAttributeName(i);
            if ((!skipXMLNS || !attr.equals("xmlns")) && !attr.contains(":")) {
                ret.append(" ").append(attr).append("=\"").append(StringEscapeUtils.escapeXml10(parser.getAttributeValue(i))).append("\"");
            }
        }
        ret.append(">");

        while (!(parser.next() == XmlPullParser.END_TAG && parser.getName().equals(tag))) {
            int event = parser.getEventType();
            if (event == XmlPullParser.START_TAG) {
                if (!parser.getName().contains(":")) {
                    ret.append(parseToString(parser, false));
                } else {
                    skip(parser);
                }
            } else if (event == XmlPullParser.TEXT) {
                ret.append(StringEscapeUtils.escapeXml10(parser.getText()));
            }
        }

        ret.append("</").append(tag).append(">");
        return ret.toString();
    }
}