diff options
author | rbuj <robert.buj@gmail.com> | 2014-08-10 19:34:40 +0200 |
---|---|---|
committer | David Tardon <dtardon@redhat.com> | 2014-08-11 06:22:26 +0000 |
commit | 608f330e22e25772eb888fa33d64f9b79c0ffcc8 (patch) | |
tree | 07cbb30fc185e03013376974690a1d2482b03cb3 /javaunohelper | |
parent | ad85ce18d3939170e5ef7b1a1f8e5dda2c8aeb9c (diff) |
javaunohelper: Number parsing
Change-Id: Ie6856ea4c196479aef1ce00a7cf2709fea8b3e42
Reviewed-on: https://gerrit.libreoffice.org/10860
Reviewed-by: David Tardon <dtardon@redhat.com>
Tested-by: David Tardon <dtardon@redhat.com>
Diffstat (limited to 'javaunohelper')
-rw-r--r-- | javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java b/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java index baeee6049bde..0bae9d0efc2a 100644 --- a/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java +++ b/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java @@ -203,44 +203,48 @@ public class UnoUrl { return connection.getUninterpretedString(); } - private static int hexToInt(int ch) - throws com.sun.star.lang.IllegalArgumentException { - int c = Character.toLowerCase((char) ch); - boolean isDigit = ('0' <= c && c <= '9'); - boolean isValidChar = ('a' <= c && c <= 'f') || isDigit; - - if (!isValidChar) - throw new com.sun.star.lang.IllegalArgumentException( - "Invalid UTF-8 hex byte '" + c + "'."); - - return isDigit ? ch - '0' : 10 + ((char) c - 'a') & 0xF; - } - private static String decodeUTF8(String s) throws com.sun.star.lang.IllegalArgumentException { - ArrayList<Integer> v = new ArrayList<Integer>(); - for (int i = 0; i < s.length(); i++) { - int ch = s.charAt(i); + try { + if (s.contains("%")) { + ArrayList<Integer> v = new ArrayList<Integer>(); + int length = s.length(); + for (int i = 0; i < length; i++) { + int ch = s.charAt(i); + + if (ch == '%') { + if (i+3 > length) + throw new com.sun.star.lang.IllegalArgumentException( + "Incomplete trailing escape (%) pattern"); + try { + ch = Integer.parseInt(s.substring(i+1,i+3),16); + } catch (NumberFormatException e) { + throw new com.sun.star.lang.IllegalArgumentException(e.toString()); + } + if (ch < 0) + throw new com.sun.star.lang.IllegalArgumentException( + "Illegal hex characters in escape (%) pattern - negative value"); + i+=2; + } + + v.add(new Integer(ch)); + } - if (ch == '%') { - int hb = hexToInt(s.charAt(++i)); - int lb = hexToInt(s.charAt(++i)); - ch = (hb << 4) | lb; - } + int size = v.size(); + byte[] bytes = new byte[size]; + for (int i = 0; i < size; i++) { + Integer anInt = v.get(i); + bytes[i] = (byte) (anInt.intValue() & 0xFF); + } - v.add(new Integer(ch)); - } + return new String(bytes, "UTF-8"); - int size = v.size(); - byte[] bytes = new byte[size]; - for (int i = 0; i < size; i++) { - Integer anInt = v.get(i); - bytes[i] = (byte) (anInt.intValue() & 0xFF); - } + } else { - try { - return new String(bytes, "UTF-8"); + return new String(s.getBytes(), "UTF-8"); + + } } catch (UnsupportedEncodingException e) { throw new com.sun.star.lang.IllegalArgumentException( "Couldn't convert parameter string to UTF-8 string:" + e.getMessage()); |