All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ocpsoft.pretty.faces.url.URL Maven / Gradle / Ivy

package com.ocpsoft.pretty.faces.url;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ocpsoft.pretty.PrettyException;

public class URL
{
   private Metadata metadata = new Metadata();
   private String originalURL = "";
   private List segments;

   private final Map encodedURLs = new HashMap();
   private final Map decodedURLs = new HashMap();
   private final Map> decodedSegments = new HashMap>();

   /**
    * Create a URL object for the given url String. The input string must not
    * yet have been decoded.
    * 
    * @param url The raw, un-decoded url String
    */
   public URL(final String url)
   {
      if (url != null)
      {
         originalURL = url.trim();
         if (originalURL.endsWith("/"))
         {
            metadata.setTrailingSlash(true);
         }

         String trimmedUrl = trimSurroundingSlashes(url);
         String[] segments = trimmedUrl.split("/");

         this.segments = Arrays.asList(segments);
      }
      else
      {
         throw new IllegalArgumentException("URL cannot be null.");
      }
   }

   public URL(final List segments, final Metadata metadata)
   {
      this.metadata = metadata;
      this.segments = segments;
      this.originalURL = metadata.buildURLFromSegments(segments);
   }

   /**
    * Decode each segment and return the URL built using this instance's
    * {@link Metadata}
    */
   public String getDecodedURL()
   {
      String encoding = metadata.getEncoding();
      if (!decodedURLs.containsKey(encoding))
      {
         List resultSegments = new ArrayList();
         for (String segment : segments)
         {
            try
            {
               String encoded = URLDecoder.decode(segment, encoding);
               resultSegments.add(encoded);
            }
            catch (UnsupportedEncodingException e)
            {
               throw new PrettyException("Could not decode URL with specified format: " + encoding, e);
            }
         }
         String result = metadata.buildURLFromSegments(resultSegments);
         decodedURLs.put(encoding, result);
      }
      return decodedURLs.get(encoding);
   }

   /**
    * Encode each segment and return the URL built using this instance's
    * {@link Metadata}
    */
   public String getEncodedURL()
   {
      String encoding = metadata.getEncoding();
      if (!encodedURLs.containsKey(encoding))
      {
         List resultSegments = new ArrayList();
         for (String segment : segments)
         {
            try
            {
               String encoded = URLEncoder.encode(segment, encoding);
               resultSegments.add(encoded);
            }
            catch (UnsupportedEncodingException e)
            {
               throw new PrettyException("Could not encode URL with specified format: " + encoding, e);
            }
         }
         String result = metadata.buildURLFromSegments(resultSegments);
         encodedURLs.put(encoding, result);
      }
      return encodedURLs.get(encoding);
   }

   public List getDecodedSegments()
   {
      String encoding = metadata.getEncoding();
      if (!decodedSegments.containsKey(encoding))
      {
         List result = new ArrayList();
         for (String segment : segments)
         {
            try
            {
               String decoded = URLDecoder.decode(segment, encoding);
               result.add(decoded);
            }
            catch (UnsupportedEncodingException e)
            {
               throw new PrettyException("Could not decode URL with specified format: " + encoding, e);
            }
         }
         decodedSegments.put(encoding, Collections.unmodifiableList(result));
      }
      return decodedSegments.get(encoding);
   }

   private String trimSurroundingSlashes(final String url)
   {
      String result = null;
      if (url != null)
      {
         result = url.trim();
         if (result.startsWith("/"))
         {
            result = result.substring(1);
         }
         if (result.endsWith("/"))
         {
            result.substring(result.length() - 1);
         }
      }
      return result;
   }

   public int numSegments()
   {
      return segments.size();
   }

   @Override
   public String toString()
   {
      return getURL();
   }

   /*
    * Getters & Setters
    */
   public String getURL()
   {
      return originalURL;
   }

   public List getSegments()
   {
      return Collections.unmodifiableList(segments);
   }

   public boolean hasTrailingSlash()
   {
      return metadata.hasTrailingSlash();
   }

   public String getEncoding()
   {
      return metadata.getEncoding();
   }

   public void setEncoding(final String encoding)
   {
      metadata.setEncoding(encoding);
   }

   public Metadata getMetadata()
   {
      return metadata;
   }

   public void setMetadata(final Metadata metadata)
   {
      this.metadata = metadata;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy