Package org.ocpsoft.urlbuilder.util

Source Code of org.ocpsoft.urlbuilder.util.Encoder

package org.ocpsoft.urlbuilder.util;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

public class Encoder
{

   private static final Charset UTF8 = Charset.forName("UTF-8");

   /**
    * Encodes the given string as described in RFC 2396
    */
   public static String path(CharSequence s)
   {
      try
      {
         final URI uri = new URI("http", "0.0.0.0", "/" + s, null);
         return uri.toASCIIString().substring(15);
      }
      catch (URISyntaxException e)
      {
         throw new IllegalArgumentException(e);
      }
   }

   /**
    * Encodes the given string using HTML form encoding
    */
   public static String query(CharSequence s)
   {
      try {
         return URLEncoder.encode(s.toString(), UTF8.name());
      }
      catch (UnsupportedEncodingException e) {
         throw new IllegalStateException(e);
      }
   }

}
TOP

Related Classes of org.ocpsoft.urlbuilder.util.Encoder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.