OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
174 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
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
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.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.xml;
15
 
16
import javax.xml.stream.XMLStreamConstants;
17
import javax.xml.stream.XMLStreamException;
18
import javax.xml.stream.XMLStreamReader;
19
 
20
public class XMLUtils {
21
 
22
    public static final String escapeAttribute(final String value) {
23
        final int len = value.length();
24
        int idx = 0;
25
 
26
        checkloop: while (idx < len) {
27
            final char ch = value.charAt(idx);
28
            if (ch == '<' || ch == '>' || ch == '&' || ch == '\r' || ch == '\n' || ch == '"' || ch == '\t') {
29
                break checkloop;
30
            }
31
            idx++;
32
        }
33
 
34
        if (idx == len) {
35
            return value;
36
        }
37
 
38
        final StringBuilder sb = new StringBuilder(len + 5);
39
        sb.append(value, 0, idx);
40
        while (idx < len) {
41
            final char ch = value.charAt(idx++);
42
            switch (ch) {
43
            case '<':
44
                sb.append("&lt;");
45
                break;
46
            case '>':
47
                sb.append("&gt;");
48
                break;
49
            case '&':
50
                sb.append("&amp;");
51
                break;
52
            case '\r':
53
                sb.append("&#xD;");
54
                break;
55
            case '"':
56
                sb.append("&quot;");
57
                break;
58
            case '\t':
59
                sb.append("&#x9;");
60
                break;
61
            case '\n':
62
                sb.append("&#xA;");
63
                break;
64
            default:
65
                sb.append(ch);
66
                break;
67
            }
68
        }
69
 
70
        return sb.toString();
71
    }
72
 
73
    public static final String escapeText(final String value) {
74
        final int right = value.length();
75
        int idx = 0;
76
        checkloop: while (idx < right) {
77
            final char ch = value.charAt(idx);
78
            if (ch == '<' || ch == '>' || ch == '&' || ch == '\r' || ch == '\n') {
79
                break checkloop;
80
            }
81
            idx++;
82
        }
83
 
84
        if (idx == right) {
85
            // no escape needed.
86
            return value;
87
        }
88
 
89
        final StringBuilder sb = new StringBuilder();
90
        if (idx > 0) {
91
            sb.append(value, 0, idx);
92
        }
93
 
94
        while (idx < right) {
95
            final char ch = value.charAt(idx++);
96
            switch (ch) {
97
            case '<':
98
                sb.append("&lt;");
99
                break;
100
            case '>':
101
                sb.append("&gt;");
102
                break;
103
            case '&':
104
                sb.append("&amp;");
105
                break;
106
            case '\r':
107
                sb.append("&#xD;");
108
                break;
109
            case '\n':
110
                sb.append('\n');
111
                break;
112
            default:
113
                sb.append(ch);
114
                break;
115
            }
116
        }
117
        return sb.toString();
118
    }
119
 
120
    public static final void skipToEndElement(final XMLStreamReader reader) throws XMLStreamException {
121
        if (!reader.isStartElement())
122
            throw new IllegalStateException("Not at a start of an element : " + reader.getEventType() + ", " + reader.getLocation());
123
        final String name = reader.getLocalName();
124
        int depth = 1;
125
        while (!(depth == 0 && reader.isEndElement() && reader.getLocalName().equals(name))) {
126
            final int eventType = reader.next();
127
            if (eventType == XMLStreamConstants.START_ELEMENT)
128
                depth++;
129
            else if (eventType == XMLStreamConstants.END_ELEMENT)
130
                depth--;
131
        }
132
    }
133
}