PageRenderTime 28ms CodeModel.GetById 20ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/XML/xml/PathUtilities.java

#
Java | 115 lines | 56 code | 8 blank | 51 comment | 6 complexity | 014f3b32524abb8355ec6943d64bce25 MD5 | raw file
  1/*
  2 * PathUtilities.java - pathToURL and urlToPath
  3 * :tabSize=4:indentSize=4:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2009, Eric Le Lay
  7 *
  8 * The XML plugin is licensed under the GNU General Public License, with
  9 * the following exception:
 10 *
 11 * "Permission is granted to link this code with software released under
 12 * the Apache license version 1.1, for example used by the Xerces XML
 13 * parser package."
 14 */
 15package xml;
 16
 17//{{{ Imports
 18import java.io.File;
 19import java.net.URI;
 20import java.net.URL;
 21import java.net.MalformedURLException;
 22import java.util.regex.Pattern;
 23
 24import org.gjt.sp.util.Log;
 25//}}}
 26
 27/**
 28 * pathToURL() and urlToPath()
 29 */
 30public final class PathUtilities
 31{
 32	
 33	/**
 34	 * a pattern for standard windows paths, e.g. : C:\temp\MyClass.java 
 35	 */
 36	public static final Pattern windowsDrivePattern = Pattern.compile("[A-Z]:\\\\.*");
 37	
 38	/**
 39	 * a pattern for windows UNC e.g. :
 40	 *  \\localhost\SHARED_C\temp\MyClass.java
 41	 * and long UNC, e.g. :
 42	 *  \\?\UNC\localhost\SHARED_C\temp\MyClass.java
 43	 *  \\?\C:\temp\MyClass.java
 44	 */
 45	 public static final Pattern windowsUNCPattern = Pattern.compile("\\\\\\\\.*");
 46	
 47	/**
 48	 * a pattern for UNIX paths e.g. :
 49	 *  /tmp/MyClass.java
 50	 */
 51	 public static final Pattern unixPattern = Pattern.compile("/.*");
 52	 
 53	//{{{ pathToURL() method
 54	/**
 55	 * @param	path	UNIX/Windows path or VFS path
 56	 * @return	path having a scheme
 57	 */
 58	public static String pathToURL(String path)
 59	{
 60		if(windowsDrivePattern.matcher(path).matches()
 61		  || windowsUNCPattern.matcher(path).matches()
 62	  	  || unixPattern.matcher(path).matches())
 63		{
 64			try
 65			{
 66				//it's a file
 67				return new File(path).toURI().toURL().toString();
 68			}
 69			catch(MalformedURLException ue)
 70			{
 71				Log.log(Log.ERROR,PathUtilities.class,"strange URI (apos added) '"+path+"'");
 72				Log.log(Log.ERROR,PathUtilities.class,ue);
 73				return path;
 74			}
 75		}
 76		else
 77		{
 78			//it's already an URL
 79			return path;
 80		}
 81	}
 82	// }}}
 83	
 84	//{{{ urlToPath() method
 85	/**
 86	 * @param	url	file:/ url. 
 87	 *				All illegal characters are required to be escaped,
 88	 *				likely by a call to File.toURI().toURL()
 89	 * @return	path without Scheme
 90	 */
 91	public static String urlToPath(String url)
 92	{
 93		if(url == null)return null;
 94		if(url.startsWith("file:/"))
 95		{
 96			try
 97			{
 98				//it's a file
 99				return new File(new URI(url)).getPath();
100			}
101			catch(java.net.URISyntaxException ue)
102			{
103				Log.log(Log.ERROR,SchemaMappingManager.class,"strange URI (apos added) '"+url+"'");
104				Log.log(Log.ERROR,SchemaMappingManager.class,ue);
105				return url;
106			}
107		}
108		else
109		{
110			//can't convert it
111			return url;
112		}
113	}
114	// }}}
115}