OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 174 Rev 180
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.xml;
14
 package org.openconcerto.xml;
15
 
15
 
-
 
16
import org.openconcerto.utils.Tuple2.List2;
-
 
17
 
16
import javax.xml.stream.XMLStreamConstants;
18
import javax.xml.stream.XMLStreamConstants;
17
import javax.xml.stream.XMLStreamException;
19
import javax.xml.stream.XMLStreamException;
18
import javax.xml.stream.XMLStreamReader;
20
import javax.xml.stream.XMLStreamReader;
19
 
21
 
20
public class XMLUtils {
22
public class XMLUtils {
21
 
23
 
22
    public static final String escapeAttribute(final String value) {
24
    public static final String escapeAttribute(final String value) {
23
        final int len = value.length();
25
        final int len = value.length();
24
        int idx = 0;
26
        int idx = 0;
25
 
27
 
26
        checkloop: while (idx < len) {
28
        checkloop: while (idx < len) {
27
            final char ch = value.charAt(idx);
29
            final char ch = value.charAt(idx);
28
            if (ch == '<' || ch == '>' || ch == '&' || ch == '\r' || ch == '\n' || ch == '"' || ch == '\t') {
30
            if (ch == '<' || ch == '>' || ch == '&' || ch == '\r' || ch == '\n' || ch == '"' || ch == '\t') {
29
                break checkloop;
31
                break checkloop;
30
            }
32
            }
31
            idx++;
33
            idx++;
32
        }
34
        }
33
 
35
 
34
        if (idx == len) {
36
        if (idx == len) {
35
            return value;
37
            return value;
36
        }
38
        }
37
 
39
 
38
        final StringBuilder sb = new StringBuilder(len + 5);
40
        final StringBuilder sb = new StringBuilder(len + 5);
39
        sb.append(value, 0, idx);
41
        sb.append(value, 0, idx);
40
        while (idx < len) {
42
        while (idx < len) {
41
            final char ch = value.charAt(idx++);
43
            final char ch = value.charAt(idx++);
42
            switch (ch) {
44
            switch (ch) {
43
            case '<':
45
            case '<':
44
                sb.append("&lt;");
46
                sb.append("&lt;");
45
                break;
47
                break;
46
            case '>':
48
            case '>':
47
                sb.append("&gt;");
49
                sb.append("&gt;");
48
                break;
50
                break;
49
            case '&':
51
            case '&':
50
                sb.append("&amp;");
52
                sb.append("&amp;");
51
                break;
53
                break;
52
            case '\r':
54
            case '\r':
53
                sb.append("&#xD;");
55
                sb.append("&#xD;");
54
                break;
56
                break;
55
            case '"':
57
            case '"':
56
                sb.append("&quot;");
58
                sb.append("&quot;");
57
                break;
59
                break;
58
            case '\t':
60
            case '\t':
59
                sb.append("&#x9;");
61
                sb.append("&#x9;");
60
                break;
62
                break;
61
            case '\n':
63
            case '\n':
62
                sb.append("&#xA;");
64
                sb.append("&#xA;");
63
                break;
65
                break;
64
            default:
66
            default:
65
                sb.append(ch);
67
                sb.append(ch);
66
                break;
68
                break;
67
            }
69
            }
68
        }
70
        }
69
 
71
 
70
        return sb.toString();
72
        return sb.toString();
71
    }
73
    }
72
 
74
 
73
    public static final String escapeText(final String value) {
75
    public static final String escapeText(final String value) {
74
        final int right = value.length();
76
        final int right = value.length();
75
        int idx = 0;
77
        int idx = 0;
76
        checkloop: while (idx < right) {
78
        checkloop: while (idx < right) {
77
            final char ch = value.charAt(idx);
79
            final char ch = value.charAt(idx);
78
            if (ch == '<' || ch == '>' || ch == '&' || ch == '\r' || ch == '\n') {
80
            if (ch == '<' || ch == '>' || ch == '&' || ch == '\r' || ch == '\n') {
79
                break checkloop;
81
                break checkloop;
80
            }
82
            }
81
            idx++;
83
            idx++;
82
        }
84
        }
83
 
85
 
84
        if (idx == right) {
86
        if (idx == right) {
85
            // no escape needed.
87
            // no escape needed.
86
            return value;
88
            return value;
87
        }
89
        }
88
 
90
 
89
        final StringBuilder sb = new StringBuilder();
91
        final StringBuilder sb = new StringBuilder();
90
        if (idx > 0) {
92
        if (idx > 0) {
91
            sb.append(value, 0, idx);
93
            sb.append(value, 0, idx);
92
        }
94
        }
93
 
95
 
94
        while (idx < right) {
96
        while (idx < right) {
95
            final char ch = value.charAt(idx++);
97
            final char ch = value.charAt(idx++);
96
            switch (ch) {
98
            switch (ch) {
97
            case '<':
99
            case '<':
98
                sb.append("&lt;");
100
                sb.append("&lt;");
99
                break;
101
                break;
100
            case '>':
102
            case '>':
101
                sb.append("&gt;");
103
                sb.append("&gt;");
102
                break;
104
                break;
103
            case '&':
105
            case '&':
104
                sb.append("&amp;");
106
                sb.append("&amp;");
105
                break;
107
                break;
106
            case '\r':
108
            case '\r':
107
                sb.append("&#xD;");
109
                sb.append("&#xD;");
108
                break;
110
                break;
109
            case '\n':
111
            case '\n':
110
                sb.append('\n');
112
                sb.append('\n');
111
                break;
113
                break;
112
            default:
114
            default:
113
                sb.append(ch);
115
                sb.append(ch);
114
                break;
116
                break;
115
            }
117
            }
116
        }
118
        }
117
        return sb.toString();
119
        return sb.toString();
118
    }
120
    }
-
 
121
 
-
 
122
    /**
-
 
123
     * Split prefix and local name.
-
 
124
     * 
-
 
125
     * @param qName a qualified name, e.g. "ns:foo" or "foo".
-
 
126
     * @return first the prefix (<code>null</code> if none), then the local name
-
 
127
     * @throws IllegalArgumentException if invalid syntax.
-
 
128
     * @see <a href="https://www.w3.org/TR/xml-names/#ns-qualnames">Qualified Names</a>
-
 
129
     */
-
 
130
    public static List2<String> splitQualifiedName(final String qName) throws IllegalArgumentException {
-
 
131
        final int indexOfColon = qName.indexOf(':');
-
 
132
        if (indexOfColon < 0)
-
 
133
            return new List2<>(null, qName);
-
 
134
 
-
 
135
        if (indexOfColon == 0)
-
 
136
            throw new IllegalArgumentException("Starts with colon : '" + qName + "'");
-
 
137
        final int l = qName.length();
-
 
138
        assert indexOfColon < l;
-
 
139
        if (indexOfColon == l - 1)
-
 
140
            throw new IllegalArgumentException("Ends with colon : '" + qName + "'");
-
 
141
        final int secondColon = qName.indexOf(':', indexOfColon + 1);
-
 
142
        if (secondColon >= 0)
-
 
143
            throw new IllegalArgumentException("Two colons : '" + qName + "'");
-
 
144
        return new List2<>(qName.substring(0, indexOfColon), qName.substring(indexOfColon + 1));
-
 
145
    }
119
 
146
 
120
    public static final void skipToEndElement(final XMLStreamReader reader) throws XMLStreamException {
147
    public static final void skipToEndElement(final XMLStreamReader reader) throws XMLStreamException {
121
        if (!reader.isStartElement())
148
        if (!reader.isStartElement())
122
            throw new IllegalStateException("Not at a start of an element : " + reader.getEventType() + ", " + reader.getLocation());
149
            throw new IllegalStateException("Not at a start of an element : " + reader.getEventType() + ", " + reader.getLocation());
123
        final String name = reader.getLocalName();
150
        final String name = reader.getLocalName();
124
        int depth = 1;
151
        int depth = 1;
125
        while (!(depth == 0 && reader.isEndElement() && reader.getLocalName().equals(name))) {
152
        while (!(depth == 0 && reader.isEndElement() && reader.getLocalName().equals(name))) {
126
            final int eventType = reader.next();
153
            final int eventType = reader.next();
127
            if (eventType == XMLStreamConstants.START_ELEMENT)
154
            if (eventType == XMLStreamConstants.START_ELEMENT)
128
                depth++;
155
                depth++;
129
            else if (eventType == XMLStreamConstants.END_ELEMENT)
156
            else if (eventType == XMLStreamConstants.END_ELEMENT)
130
                depth--;
157
                depth--;
131
        }
158
        }
132
    }
159
    }
133
}
160
}