• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

igniterealtime / Smack / #2856

pending completion
#2856

push

github-actions

web-flow
Merge pull request #561 from Flowdalic/github-ci

[github ci] Java 15 → 17

16274 of 41793 relevant lines covered (38.94%)

0.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.11
/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java
1
/**
2
 *
3
 * Copyright © 2014-2023 Florian Schmaus
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
package org.jivesoftware.smack.util;
18

19
import java.io.IOException;
20
import java.net.URI;
21
import java.net.URISyntaxException;
22
import java.text.ParseException;
23
import java.util.Date;
24
import java.util.Locale;
25

26
import javax.xml.namespace.QName;
27

28
import org.jivesoftware.smack.datatypes.UInt16;
29
import org.jivesoftware.smack.datatypes.UInt32;
30
import org.jivesoftware.smack.packet.XmlEnvironment;
31
import org.jivesoftware.smack.parsing.SmackParsingException;
32
import org.jivesoftware.smack.parsing.SmackParsingException.RequiredAttributeMissingException;
33
import org.jivesoftware.smack.parsing.SmackParsingException.SmackUriSyntaxParsingException;
34
import org.jivesoftware.smack.xml.XmlPullParser;
35
import org.jivesoftware.smack.xml.XmlPullParserException;
36

37
import org.jxmpp.jid.EntityBareJid;
38
import org.jxmpp.jid.EntityFullJid;
39
import org.jxmpp.jid.EntityJid;
40
import org.jxmpp.jid.Jid;
41
import org.jxmpp.jid.impl.JidCreate;
42
import org.jxmpp.jid.parts.Resourcepart;
43
import org.jxmpp.stringprep.XmppStringprepException;
44
import org.jxmpp.util.XmppDateTime;
45

46
public class ParserUtils {
1✔
47

48
    /**
49
     * The constant String "jid".
50
     */
51
    public static final String JID = "jid";
52

53
    public static void assertAtStartTag(XmlPullParser parser) throws XmlPullParserException {
54
        assert parser.getEventType() == XmlPullParser.Event.START_ELEMENT;
1✔
55
    }
1✔
56

57
    public static void assertAtStartTag(XmlPullParser parser, String name) throws XmlPullParserException {
58
        assertAtStartTag(parser);
1✔
59
        assert name.equals(parser.getName());
1✔
60
    }
1✔
61

62
    public static void assertAtEndTag(XmlPullParser parser) throws XmlPullParserException {
63
        assert parser.getEventType() == XmlPullParser.Event.END_ELEMENT;
1✔
64
    }
1✔
65

66
    public static void forwardToStartElement(XmlPullParser parser) throws XmlPullParserException, IOException {
67
         // Wind the parser forward to the first start tag
68
        XmlPullParser.Event event = parser.getEventType();
1✔
69
        while (event != XmlPullParser.Event.START_ELEMENT) {
1✔
70
            if (event == XmlPullParser.Event.END_DOCUMENT) {
1✔
71
                throw new IllegalArgumentException("Document contains no start tag");
×
72
            }
73
            event = parser.next();
1✔
74
        }
75
    }
1✔
76

77
    public static void forwardToEndTagOfDepth(XmlPullParser parser, int depth)
78
                    throws XmlPullParserException, IOException {
79
        XmlPullParser.Event event = parser.getEventType();
1✔
80
        while (!(event == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == depth)) {
1✔
81
            assert event != XmlPullParser.Event.END_DOCUMENT;
1✔
82
            event = parser.next();
1✔
83
        }
84
    }
1✔
85

86
    public static Jid getJidAttribute(XmlPullParser parser) throws XmppStringprepException {
87
        return getJidAttribute(parser, JID);
1✔
88
    }
89

90
    public static Jid getJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
91
        final String jidString = parser.getAttributeValue("", name);
1✔
92
        if (jidString == null) {
1✔
93
            return null;
1✔
94
        }
95
        return JidCreate.from(jidString);
1✔
96
    }
97

98
    public static EntityBareJid getBareJidAttribute(XmlPullParser parser) throws XmppStringprepException {
99
        return getBareJidAttribute(parser, JID);
×
100
    }
101

102
    public static EntityBareJid getBareJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
103
        final String jidString = parser.getAttributeValue("", name);
×
104
        if (jidString == null) {
×
105
            return null;
×
106
        }
107
        return JidCreate.entityBareFrom(jidString);
×
108
    }
109

110
    public static EntityFullJid getFullJidAttribute(XmlPullParser parser) throws XmppStringprepException {
111
        return getFullJidAttribute(parser, JID);
×
112
    }
113

114
    public static EntityFullJid getFullJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
115
        final String jidString = parser.getAttributeValue("", name);
1✔
116
        if (jidString == null) {
1✔
117
            return null;
1✔
118
        }
119
        return JidCreate.entityFullFrom(jidString);
1✔
120
    }
121

122
    public static EntityJid getEntityJidAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
123
        final String jidString = parser.getAttributeValue("", name);
×
124
        if (jidString == null) {
×
125
            return null;
×
126
        }
127
        Jid jid = JidCreate.from(jidString);
×
128

129
        if (!jid.hasLocalpart()) return null;
×
130

131
        EntityFullJid fullJid = jid.asEntityFullJidIfPossible();
×
132
        if (fullJid != null) {
×
133
            return fullJid;
×
134
        }
135

136
        EntityBareJid bareJid = jid.asEntityBareJidIfPossible();
×
137
        return bareJid;
×
138
    }
139

140
    public static Resourcepart getResourcepartAttribute(XmlPullParser parser, String name) throws XmppStringprepException {
141
        final String resourcepartString = parser.getAttributeValue("", name);
×
142
        if (resourcepartString == null) {
×
143
            return null;
×
144
        }
145
        return Resourcepart.from(resourcepartString);
×
146
    }
147

148
    /**
149
     * Prase a string to a boolean value as per "xs:boolean". Valid input strings are "true", "1" for true, and "false", "0" for false.
150
     *
151
     * @param booleanString the input string.
152
     * @return the boolean representation of the input string
153
     * @throws IllegalArgumentException if the input string is not valid.
154
     * @since 4.3.2
155
     */
156
    public static boolean parseXmlBoolean(String booleanString) {
157
        switch (booleanString) {
1✔
158
        case "true":
159
        case "1":
160
            return true;
1✔
161
        case "false":
162
        case "0":
163
            return false;
1✔
164
        default:
165
            throw new IllegalArgumentException(booleanString + " is not a valid boolean string");
×
166
        }
167
    }
168

169
    /**
170
     * Get the boolean value of an argument.
171
     *
172
     * @param parser TODO javadoc me please
173
     * @param name TODO javadoc me please
174
     * @return the boolean value or null of no argument of the given name exists
175
     */
176
    public static Boolean getBooleanAttribute(XmlPullParser parser, String name) {
177
        String valueString = parser.getAttributeValue("", name);
1✔
178
        if (valueString == null)
1✔
179
            return null;
1✔
180
        valueString = valueString.toLowerCase(Locale.US);
1✔
181
        return parseXmlBoolean(valueString);
1✔
182
    }
183

184
    public static boolean getBooleanAttribute(XmlPullParser parser, String name,
185
                    boolean defaultValue) {
186
        Boolean bool = getBooleanAttribute(parser, name);
1✔
187
        if (bool == null) {
1✔
188
            return defaultValue;
1✔
189
        }
190
        else {
191
            return bool;
1✔
192
        }
193
    }
194

195
    public static Byte getByteAttributeFromNextText(XmlPullParser parser) throws IOException, XmlPullParserException {
196
        String nextText = parser.nextText();
1✔
197
        return Byte.valueOf(nextText);
1✔
198
    }
199

200
    public static int getIntegerAttributeOrThrow(XmlPullParser parser, String name, String throwMessage)
201
                    throws IOException {
202
        Integer res = getIntegerAttribute(parser, name);
1✔
203
        if (res == null) {
1✔
204
            // TODO Should be SmackParseException.
205
            throw new IOException(throwMessage);
×
206
        }
207
        return res;
1✔
208
    }
209

210
    public static Integer getIntegerAttribute(XmlPullParser parser, String name) {
211
        String valueString = parser.getAttributeValue("", name);
1✔
212
        if (valueString == null)
1✔
213
            return null;
1✔
214
        return Integer.valueOf(valueString);
1✔
215
    }
216

217
    public static int getIntegerAttribute(XmlPullParser parser, String name, int defaultValue) {
218
        Integer integer = getIntegerAttribute(parser, name);
1✔
219
        if (integer == null) {
1✔
220
            return defaultValue;
1✔
221
        }
222
        else {
223
            return integer;
1✔
224
        }
225
    }
226

227
    public static UInt16 getUInt16Attribute(XmlPullParser parser, String name) {
228
        Integer integer = getIntegerAttribute(parser, name);
1✔
229
        if (integer == null) {
1✔
230
            return null;
1✔
231
        }
232
        return UInt16.from(integer);
1✔
233
    }
234

235
    public static UInt16 getRequiredUInt16Attribute(XmlPullParser parser, String name) throws RequiredAttributeMissingException {
236
        UInt16 uint16 = getUInt16Attribute(parser, name);
×
237
        if (uint16 == null) {
×
238
            throw new SmackParsingException.RequiredAttributeMissingException(name);
×
239
        }
240
        return uint16;
×
241
    }
242

243
    public static int getIntegerFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException {
244
        String intString = parser.nextText();
1✔
245
        return Integer.valueOf(intString);
1✔
246
    }
247

248
    public static Long getLongAttribute(XmlPullParser parser, String name) {
249
        String valueString = parser.getAttributeValue("", name);
1✔
250
        if (valueString == null)
1✔
251
            return null;
×
252
        return Long.valueOf(valueString);
1✔
253
    }
254

255
    public static long getLongAttribute(XmlPullParser parser, String name, long defaultValue) {
256
        Long l = getLongAttribute(parser, name);
×
257
        if (l == null) {
×
258
            return defaultValue;
×
259
        }
260
        else {
261
            return l;
×
262
        }
263
    }
264

265
    public static UInt32 getUInt32Attribute(XmlPullParser parser, String name) {
266
        Long l = getLongAttribute(parser, name);
1✔
267
        if (l == null) {
1✔
268
            return null;
×
269
        }
270
        return UInt32.from(l);
1✔
271
    }
272

273
    public static double getDoubleFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException {
274
        String doubleString = parser.nextText();
1✔
275
        return Double.valueOf(doubleString);
1✔
276
    }
277

278
    public static Double getDoubleAttribute(XmlPullParser parser, String name) {
279
        String valueString = parser.getAttributeValue("", name);
×
280
        if (valueString == null)
×
281
            return null;
×
282
        return Double.valueOf(valueString);
×
283
    }
284

285
    public static double getDoubleAttribute(XmlPullParser parser, String name, long defaultValue) {
286
        Double d = getDoubleAttribute(parser, name);
×
287
        if (d == null) {
×
288
            return defaultValue;
×
289
        }
290
        else {
291
            return d;
×
292
        }
293
    }
294

295
    public static Short getShortAttribute(XmlPullParser parser, String name) {
296
        String valueString = parser.getAttributeValue("", name);
×
297
        if (valueString == null) {
×
298
            return null;
×
299
        }
300
        return Short.valueOf(valueString);
×
301
    }
302

303
    public static short getShortAttribute(XmlPullParser parser, String name, short defaultValue) {
304
        Short s = getShortAttribute(parser, name);
×
305
        if (s == null) {
×
306
            return defaultValue;
×
307
        }
308
        return s;
×
309
    }
310

311
    public static Date getDateFromOptionalXep82String(String dateString) throws ParseException {
312
        if (dateString == null) {
1✔
313
            return null;
×
314
        }
315
        return getDateFromXep82String(dateString);
1✔
316
    }
317

318
    public static Date getDateFromXep82String(String dateString) throws ParseException {
319
        return XmppDateTime.parseXEP0082Date(dateString);
1✔
320
    }
321

322
    public static Date getDateFromString(String dateString) throws ParseException {
323
        return XmppDateTime.parseDate(dateString);
1✔
324
    }
325

326
    public static Date getDateFromNextText(XmlPullParser parser)
327
                    throws XmlPullParserException, IOException, ParseException {
328
        String dateString = parser.nextText();
1✔
329
        return getDateFromString(dateString);
1✔
330
    }
331

332
    public static URI getUriFromNextText(XmlPullParser parser) throws XmlPullParserException, IOException, SmackUriSyntaxParsingException  {
333
        String uriString = parser.nextText();
1✔
334
        try {
335
            return new URI(uriString);
1✔
336
        }
337
        catch (URISyntaxException e) {
×
338
            throw new SmackParsingException.SmackUriSyntaxParsingException(e);
×
339
        }
340
    }
341

342
    public static String getRequiredAttribute(XmlPullParser parser, String name) throws IOException {
343
        String value = parser.getAttributeValue("", name);
1✔
344
        if (StringUtils.isNullOrEmpty(value)) {
1✔
345
            throw new IOException("Attribute " + name + " is null or empty (" + value + ')');
×
346
        }
347
        return value;
1✔
348
    }
349

350
    public static String getRequiredNextText(XmlPullParser parser) throws XmlPullParserException, IOException {
351
        String text = parser.nextText();
1✔
352
        if (StringUtils.isNullOrEmpty(text)) {
1✔
353
            throw new IOException("Next text is null or empty (" + text + ')');
×
354
        }
355
        return text;
1✔
356
    }
357

358
    public static String getXmlLang(XmlPullParser parser, XmlEnvironment xmlEnvironment) {
359
        String currentXmlLang = getXmlLang(parser);
1✔
360
        if (currentXmlLang != null) {
1✔
361
            return currentXmlLang;
1✔
362
        }
363
        return xmlEnvironment.getEffectiveLanguage();
1✔
364
    }
365

366
    public static String getXmlLang(XmlPullParser parser) {
367
        return parser.getAttributeValue("http://www.w3.org/XML/1998/namespace", "lang");
1✔
368
    }
369

370
    /**
371
     * Get the QName of the current element.
372
     *
373
     * @param parser the parser.
374
     * @return the Qname.
375
     * @deprecated use {@link XmlPullParser#getQName()} instead.
376
     */
377
    @Deprecated
378
    // TODO: Remove in Smack 4.5
379
    public static QName getQName(XmlPullParser parser) {
380
        return parser.getQName();
×
381
    }
382

383
    public static InternetAddress getInternetAddressIngoringZoneIdAttribute(XmlPullParser parser, String attribute) {
384
        String inetAddressString = parser.getAttributeValue(attribute);
1✔
385
        if (inetAddressString == null) {
1✔
386
            return null;
×
387
        }
388
        return InternetAddress.fromIgnoringZoneId(inetAddressString);
1✔
389
    }
390
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc