PageRenderTime 46ms CodeModel.GetById 13ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. */
  15. package xml;
  16. //{{{ Imports
  17. import java.io.File;
  18. import java.net.URI;
  19. import java.net.URL;
  20. import java.net.MalformedURLException;
  21. import java.util.regex.Pattern;
  22. import org.gjt.sp.util.Log;
  23. //}}}
  24. /**
  25. * pathToURL() and urlToPath()
  26. */
  27. public final class PathUtilities
  28. {
  29. /**
  30. * a pattern for standard windows paths, e.g. : C:\temp\MyClass.java
  31. */
  32. public static final Pattern windowsDrivePattern = Pattern.compile("[A-Z]:\\\\.*");
  33. /**
  34. * a pattern for windows UNC e.g. :
  35. * \\localhost\SHARED_C\temp\MyClass.java
  36. * and long UNC, e.g. :
  37. * \\?\UNC\localhost\SHARED_C\temp\MyClass.java
  38. * \\?\C:\temp\MyClass.java
  39. */
  40. public static final Pattern windowsUNCPattern = Pattern.compile("\\\\\\\\.*");
  41. /**
  42. * a pattern for UNIX paths e.g. :
  43. * /tmp/MyClass.java
  44. */
  45. public static final Pattern unixPattern = Pattern.compile("/.*");
  46. //{{{ pathToURL() method
  47. /**
  48. * @param path UNIX/Windows path or VFS path
  49. * @return path having a scheme
  50. */
  51. public static String pathToURL(String path)
  52. {
  53. if(windowsDrivePattern.matcher(path).matches()
  54. || windowsUNCPattern.matcher(path).matches()
  55. || unixPattern.matcher(path).matches())
  56. {
  57. try
  58. {
  59. //it's a file
  60. return new File(path).toURI().toURL().toString();
  61. }
  62. catch(MalformedURLException ue)
  63. {
  64. Log.log(Log.ERROR,PathUtilities.class,"strange URI (apos added) '"+path+"'");
  65. Log.log(Log.ERROR,PathUtilities.class,ue);
  66. return path;
  67. }
  68. }
  69. else
  70. {
  71. //it's already an URL
  72. return path;
  73. }
  74. }
  75. // }}}
  76. //{{{ urlToPath() method
  77. /**
  78. * @param url file:/ url.
  79. * All illegal characters are required to be escaped,
  80. * likely by a call to File.toURI().toURL()
  81. * @return path without Scheme
  82. */
  83. public static String urlToPath(String url)
  84. {
  85. if(url == null)return null;
  86. if(url.startsWith("file:/"))
  87. {
  88. try
  89. {
  90. //it's a file
  91. return new File(new URI(url)).getPath();
  92. }
  93. catch(java.net.URISyntaxException ue)
  94. {
  95. Log.log(Log.ERROR,SchemaMappingManager.class,"strange URI (apos added) '"+url+"'");
  96. Log.log(Log.ERROR,SchemaMappingManager.class,ue);
  97. return url;
  98. }
  99. }
  100. else
  101. {
  102. //can't convert it
  103. return url;
  104. }
  105. }
  106. // }}}
  107. }