com.sun.xml.messaging.saaj.util.ParseUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of saaj-impl Show documentation
Show all versions of saaj-impl Show documentation
Open source Reference Implementation of JSR-67: SOAP with Attachments API for Java (SAAJ MR: 1.4)
The newest version!
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.messaging.saaj.util;
// Cut&paste from sun.net.www.ParseUtil: decode, unescape
public final class ParseUtil {
private ParseUtil() {}
/**
* Un-escape and return the character at position i in string s.
*/
private static char unescape(String s, int i) {
return (char) Integer.parseInt(s.substring(i+1,i+3),16);
}
/**
* Returns a new String constructed from the specified String by replacing
* the URL escape sequences and UTF8 encoding with the characters they
* represent.
*
* @param s string
*
* @return decoded string
*/
public static String decode(String s) {
StringBuilder sb = new StringBuilder();
int i=0;
while (i> 4) {
case 0xC: case 0xD:
c2 = unescape(s, i);
i += 3;
c = (char)(((c & 0x1f) << 6) | (c2 & 0x3f));
break;
case 0xE:
c2 = unescape(s, i);
i += 3;
c3 = unescape(s, i);
i += 3;
c = (char)(((c & 0x0f) << 12) |
((c2 & 0x3f) << 6) |
(c3 & 0x3f));
break;
default:
throw new IllegalArgumentException();
}
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
}
sb.append(c);
}
return sb.toString();
}
}