Coverage Report - org.yaml.snakeyaml.util.UriEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
UriEncoder
66%
6/9
N/A
2
 
 1  
 /**
 2  
  * Copyright (c) 2008-2011, http://www.snakeyaml.org
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.yaml.snakeyaml.util;
 18  
 
 19  
 import java.io.UnsupportedEncodingException;
 20  
 import java.net.URLDecoder;
 21  
 import java.nio.ByteBuffer;
 22  
 import java.nio.CharBuffer;
 23  
 import java.nio.charset.CharacterCodingException;
 24  
 import java.nio.charset.Charset;
 25  
 import java.nio.charset.CharsetDecoder;
 26  
 import java.nio.charset.CodingErrorAction;
 27  
 
 28  
 import org.yaml.snakeyaml.error.YAMLException;
 29  
 import org.yaml.snakeyaml.external.com.google.gdata.util.common.base.Escaper;
 30  
 import org.yaml.snakeyaml.external.com.google.gdata.util.common.base.PercentEscaper;
 31  
 
 32  0
 public abstract class UriEncoder {
 33  1
     private static final CharsetDecoder UTF8Decoder = Charset.forName("UTF-8").newDecoder()
 34  
             .onMalformedInput(CodingErrorAction.REPORT);
 35  
     // Include the [] chars to the SAFEPATHCHARS_URLENCODER to avoid
 36  
     // its escape as required by spec. See
 37  
     // http://yaml.org/spec/1.1/#escaping%20in%20URI/
 38  
     private static final String SAFE_CHARS = PercentEscaper.SAFEPATHCHARS_URLENCODER + "[]/";
 39  1
     private static final Escaper escaper = new PercentEscaper(SAFE_CHARS, false);
 40  
 
 41  
     /**
 42  
      * Escape special characters with '%'
 43  
      */
 44  
     public static String encode(String uri) {
 45  47797
         return escaper.escape(uri);
 46  
     }
 47  
 
 48  
     /**
 49  
      * Decode '%'-escaped characters. Decoding fails in case of invalid UTF-8
 50  
      */
 51  
     public static String decode(ByteBuffer buff) throws CharacterCodingException {
 52  9
         CharBuffer chars = UTF8Decoder.decode(buff);
 53  3
         return chars.toString();
 54  
     }
 55  
 
 56  
     public static String decode(String buff) {
 57  
         try {
 58  308
             return URLDecoder.decode(buff, "UTF-8");
 59  0
         } catch (UnsupportedEncodingException e) {
 60  0
             throw new YAMLException(e);
 61  
         }
 62  
     }
 63  
 }