< prev index next >

jaxws/src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/util/ParseUtil.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.util;
  27 
  28 
  29 // Cut&paste from sun.net.www.ParseUtil: decode, unescape
  30 
  31 public class ParseUtil {
  32     /**
  33      * Un-escape and return the character at position i in string s.
  34      */
  35     private static char unescape(String s, int i) {
  36         return (char) Integer.parseInt(s.substring(i+1,i+3),16);
  37     }
  38 
  39     /**
  40      * Returns a new String constructed from the specified String by replacing
  41      * the URL escape sequences and UTF8 encoding with the characters they
  42      * represent.




  43      */
  44     public static String decode(String s) {
  45         StringBuilder sb = new StringBuilder();
  46 
  47         int i=0;
  48         while (i<s.length()) {
  49             char c = s.charAt(i);
  50             char c2, c3;
  51 
  52             if (c != '%') {
  53                 i++;
  54             } else {
  55                 try {
  56                     c = unescape(s, i);
  57                     i += 3;
  58 
  59                     if ((c & 0x80) != 0) {
  60                         switch (c >> 4) {
  61                             case 0xC: case 0xD:
  62                                 c2 = unescape(s, i);


   1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.messaging.saaj.util;
  27 
  28 
  29 // Cut&paste from sun.net.www.ParseUtil: decode, unescape
  30 
  31 public class ParseUtil {
  32     /**
  33      * Un-escape and return the character at position i in string s.
  34      */
  35     private static char unescape(String s, int i) {
  36         return (char) Integer.parseInt(s.substring(i+1,i+3),16);
  37     }
  38 
  39     /**
  40      * Returns a new String constructed from the specified String by replacing
  41      * the URL escape sequences and UTF8 encoding with the characters they
  42      * represent.
  43      *
  44      * @param s string
  45      *
  46      * @return decoded string
  47      */
  48     public static String decode(String s) {
  49         StringBuilder sb = new StringBuilder();
  50 
  51         int i=0;
  52         while (i<s.length()) {
  53             char c = s.charAt(i);
  54             char c2, c3;
  55 
  56             if (c != '%') {
  57                 i++;
  58             } else {
  59                 try {
  60                     c = unescape(s, i);
  61                     i += 3;
  62 
  63                     if ((c & 0x80) != 0) {
  64                         switch (c >> 4) {
  65                             case 0xC: case 0xD:
  66                                 c2 = unescape(s, i);


< prev index next >