PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libreoffice-3.6.0.2/jurt/com/sun/star/lib/util/UrlToFileMapper.java

#
Java | 161 lines | 95 code | 8 blank | 58 comment | 39 complexity | 3cb404e77b86d66e1a34b89e955c0f6b MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, AGPL-1.0, BSD-3-Clause-No-Nuclear-License-2014, GPL-3.0, LGPL-3.0
  1. /*************************************************************************
  2. *
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * Copyright 2000, 2010 Oracle and/or its affiliates.
  6. *
  7. * OpenOffice.org - a multi-platform office productivity suite
  8. *
  9. * This file is part of OpenOffice.org.
  10. *
  11. * OpenOffice.org is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License version 3
  13. * only, as published by the Free Software Foundation.
  14. *
  15. * OpenOffice.org is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License version 3 for more details
  19. * (a copy is included in the LICENSE file that accompanied this code).
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * version 3 along with OpenOffice.org. If not, see
  23. * <http://www.openoffice.org/license.html>
  24. * for a copy of the LGPLv3 License.
  25. *
  26. ************************************************************************/
  27. package com.sun.star.lib.util;
  28. import java.io.File;
  29. import java.lang.reflect.Constructor;
  30. import java.lang.reflect.InvocationTargetException;
  31. import java.lang.reflect.Method;
  32. import java.net.URL;
  33. import java.net.URLDecoder;
  34. import java.net.URLEncoder;
  35. /**
  36. * Maps Java URL representations to File representations, on any Java version.
  37. *
  38. * @since UDK 3.2.8
  39. */
  40. public final class UrlToFileMapper {
  41. // java.net.URLEncoder.encode(String, String) and java.net.URI are only
  42. // available since Java 1.4:
  43. private static Method urlEncoderEncode;
  44. private static Constructor uriConstructor;
  45. private static Constructor fileConstructor;
  46. static {
  47. try {
  48. urlEncoderEncode = URLEncoder.class.getMethod(
  49. "encode", new Class[] { String.class, String.class });
  50. Class uriClass = Class.forName("java.net.URI");
  51. uriConstructor = uriClass.getConstructor(
  52. new Class[] { String.class });
  53. fileConstructor = File.class.getConstructor(
  54. new Class[] { uriClass });
  55. } catch (ClassNotFoundException e) {
  56. } catch (NoSuchMethodException e) {
  57. }
  58. }
  59. /**
  60. * Maps Java URL representations to File representations.
  61. *
  62. * @param url some URL, possibly null.
  63. * @return a corresponding File, or null on failure.
  64. */
  65. public static File mapUrlToFile(URL url) {
  66. if (url == null) {
  67. return null;
  68. } else if (fileConstructor == null) {
  69. // If java.net.URI is not available, hope that the following works
  70. // well: First, check that the given URL has a certain form.
  71. // Second, use the URLDecoder to decode the URL path (taking care
  72. // not to change any plus signs to spaces), hoping that the used
  73. // default encoding is the proper one for file URLs. Third, create
  74. // a File from the decoded path.
  75. return url.getProtocol().equalsIgnoreCase("file")
  76. && url.getAuthority() == null && url.getQuery() == null
  77. && url.getRef() == null
  78. ? new File(URLDecoder.decode(
  79. StringHelper.replace(url.getPath(), '+', "%2B")))
  80. : null;
  81. } else {
  82. // If java.net.URI is avaliable, do
  83. // URI uri = new URI(encodedUrl);
  84. // try {
  85. // return new File(uri);
  86. // } catch (IllegalArgumentException e) {
  87. // return null;
  88. // }
  89. // where encodedUrl is url.toString(), but since that may contain
  90. // unsafe characters (e.g., space, " "), it is encoded, as otherwise
  91. // the URI constructor might throw java.net.URISyntaxException (in
  92. // Java 1.5, URL.toURI might be used instead).
  93. String encodedUrl = encode(url.toString());
  94. try {
  95. Object uri = uriConstructor.newInstance(
  96. new Object[] { encodedUrl });
  97. try {
  98. return (File) fileConstructor.newInstance(
  99. new Object[] { uri });
  100. } catch (InvocationTargetException e) {
  101. if (e.getTargetException() instanceof
  102. IllegalArgumentException) {
  103. return null;
  104. } else {
  105. throw e;
  106. }
  107. }
  108. } catch (InstantiationException e) {
  109. throw new RuntimeException("This cannot happen: " + e);
  110. } catch (IllegalAccessException e) {
  111. throw new RuntimeException("This cannot happen: " + e);
  112. } catch (InvocationTargetException e) {
  113. if (e.getTargetException() instanceof Error) {
  114. throw (Error) e.getTargetException();
  115. } else if (e.getTargetException() instanceof RuntimeException) {
  116. throw (RuntimeException) e.getTargetException();
  117. } else {
  118. throw new RuntimeException("This cannot happen: " + e);
  119. }
  120. }
  121. }
  122. }
  123. private static String encode(String url) {
  124. StringBuffer buf = new StringBuffer();
  125. for (int i = 0; i < url.length(); ++i) {
  126. char c = url.charAt(i);
  127. // The RFC 2732 <uric> characters: !$&'()*+,-./:;=?@[]_~ plus digits
  128. // and letters; additionally, do not encode % again.
  129. if (c >= 'a' && c <= 'z' || c >= '?' && c <= '['
  130. || c >= '$' && c <= ';' || c == '!' || c == '=' || c == ']'
  131. || c == '_' || c == '~')
  132. {
  133. buf.append(c);
  134. } else if (c == ' ') {
  135. buf.append("%20");
  136. } else {
  137. String enc;
  138. try {
  139. enc = (String) urlEncoderEncode.invoke(
  140. null,
  141. new Object[] {Character.toString(c), "UTF-8" });
  142. } catch (IllegalAccessException e) {
  143. throw new RuntimeException("This cannot happen: " + e);
  144. } catch (InvocationTargetException e) {
  145. throw new RuntimeException("This cannot happen: " + e);
  146. }
  147. buf.append(enc);
  148. }
  149. }
  150. return buf.toString();
  151. }
  152. private UrlToFileMapper() {}
  153. }