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-2012, 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  
 package org.yaml.snakeyaml.util;
 17  
 
 18  
 import java.io.UnsupportedEncodingException;
 19  
 import java.net.URLDecoder;
 20  
 import java.nio.ByteBuffer;
 21  
 import java.nio.CharBuffer;
 22  
 import java.nio.charset.CharacterCodingException;
 23  
 import java.nio.charset.Charset;
 24  
 import java.nio.charset.CharsetDecoder;
 25  
 import java.nio.charset.CodingErrorAction;
 26  
 
 27  
 import org.yaml.snakeyaml.error.YAMLException;
 28  
 import org.yaml.snakeyaml.external.com.google.gdata.util.common.base.Escaper;
 29  
 import org.yaml.snakeyaml.external.com.google.gdata.util.common.base.PercentEscaper;
 30  
 
 31  0
 public abstract class UriEncoder {
 32  1
     private static final CharsetDecoder UTF8Decoder = Charset.forName("UTF-8").newDecoder()
 33  
             .onMalformedInput(CodingErrorAction.REPORT);
 34  
     // Include the [] chars to the SAFEPATHCHARS_URLENCODER to avoid
 35  
     // its escape as required by spec. See
 36  
     // http://yaml.org/spec/1.1/#escaping%20in%20URI/
 37  
     private static final String SAFE_CHARS = PercentEscaper.SAFEPATHCHARS_URLENCODER + "[]/";
 38  1
     private static final Escaper escaper = new PercentEscaper(SAFE_CHARS, false);
 39  
 
 40  
     /**
 41  
      * Escape special characters with '%'
 42  
      */
 43  
     public static String encode(String uri) {
 44  51469
         return escaper.escape(uri);
 45  
     }
 46  
 
 47  
     /**
 48  
      * Decode '%'-escaped characters. Decoding fails in case of invalid UTF-8
 49  
      */
 50  
     public static String decode(ByteBuffer buff) throws CharacterCodingException {
 51  9
         CharBuffer chars = UTF8Decoder.decode(buff);
 52  3
         return chars.toString();
 53  
     }
 54  
 
 55  
     public static String decode(String buff) {
 56  
         try {
 57  318
             return URLDecoder.decode(buff, "UTF-8");
 58  0
         } catch (UnsupportedEncodingException e) {
 59  0
             throw new YAMLException(e);
 60  
         }
 61  
     }
 62  
 }