Make a URL from the given string : URI « Network Protocol « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Network Protocol » URIScreenshots 
Make a URL from the given string
   


import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Map;


/*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */



public class Main{


  /**
   * Make a URL from the given string.
   *
   * <p>
   * If the string is a properly formatted file URL, then the file
   * portion will be made canonical.
   *
   * <p>
   * If the string is an invalid URL then it will be converted into a
   * file URL.
   *
   @param urlspec           The string to construct a URL for.
   @param relativePrefix    The string to prepend to relative file
   *                          paths, or null to disable prepending.
   @return                  A URL for the given string.
   *
   @throws MalformedURLException  Could not make a URL for the given string.
   */
  public static URL toURL(String urlspec, final String relativePrefixthrows MalformedURLException
  {
     urlspec = urlspec.trim();

     URL url;

     try
     {
        url = new URL(urlspec);
        if (url.getProtocol().equals("file"))
        {
           url = makeURLFromFilespec(url.getFile(), relativePrefix);
        }
     }
     catch (Exception e)
     {
        // make sure we have a absolute & canonical file url
        try
        {
           url = makeURLFromFilespec(urlspec, relativePrefix);
        }
        catch (IOException n)
        {
           //
           // jason: or should we rethrow e?
           //
           throw new MalformedURLException(n.toString());
        }
     }

     return url;
  }

  public static URI toURI(String urispec, final String relativePrefix)
     throws URISyntaxException
  {
     urispec = urispec.trim();

     URI uri;

     ifurispec.startsWith("file:") )
     {
        uri = makeURIFromFilespec(urispec.substring(5), relativePrefix);
     }
     else
     {
        uri = new URI(urispec);
     }

     return uri;
  }

  /** A helper to make a URL from a filespec. */
  private static URL makeURLFromFilespec(final String filespec, final String relativePrefix)
     throws IOException
  {
     // make sure the file is absolute & canonical file url
     File file = new File(decode(filespec));
     
     // if we have a prefix and the file is not abs then prepend
     if (relativePrefix != null && !file.isAbsolute())
     {
        file = new File(relativePrefix, filespec);
     }
     
     // make sure it is canonical (no ../ and such)
     file = file.getCanonicalFile();

     return file.toURI().toURL();
  }
  
  private static String decode(String filespec)
  {
     try
     {
        return URLDecoder.decode(filespec, "UTF-8");
     }
     catch (UnsupportedEncodingException e)
     {
        throw new RuntimeException("Error decoding filespec: " + filespec, e);
     }
  }
  
  private static URI makeURIFromFilespec(final String filespec, final String relativePrefix)
  {
     // make sure the file is absolute & canonical file url
     File file = new File(decode(filespec));
     
     // if we have a prefix and the file is not abs then prepend
     if (relativePrefix != null && !file.isAbsolute())
     {
        file = new File(relativePrefix, filespec);
     }
     
     return file.toURI();
  }

  /**
   * Make a URL from the given string.
   *
   @see #toURL(String,String)
   *
   @param urlspec    The string to construct a URL for.
   @return           A URL for the given string.
   *
   @throws MalformedURLException  Could not make a URL for the given string.
   */
  public static URL toURL(final String urlspecthrows MalformedURLException
  {
     return toURL(urlspec, null);
  }

  /**
   
   @param urispec
   @return the uri
   @throws URISyntaxException for any error
   */
  public static URI toURI(final String urispec)
     throws URISyntaxException
  {
     return toURI(urispec, null);
  }

}

   
    
    
  
Related examples in the same category
1. Encode a URI path
2. Add two URI path segments
3. Remove parameters from a uri.
4. Remove any authorisation details from a URI
5. A class to represent a Uniform Resource Identifier (URI).
6. Utility to convert File to URL.
7. Return True if the uri has a scheme
8. Normalize a uri containing ../ and ./ paths.
9. URI Support
10. URI utilities
11. Get Parent URI
12. Create and resolve URI
13. Add parameters stored in the Map to the uri string.
14. Is Equal URI
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.