1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.sun.org.apache.xml.internal.resolver.helpers;
19
20import java.net.URL;
21import java.net.MalformedURLException;
22import java.io.File;
23
24/**
25 * Static method for dealing with file: URLs.
26 *
27 * <p>This class defines a static method that can be used to construct
28 * an appropriate file: URL from parts. It's defined here so that it
29 * can be reused throught the resolver.</p>
30 *
31 * <p>(Yes, I'd rather have called this class FileUR<b>I</b>, but
32 * given that a jave.net.URL is returned, it seemed...even more
33 * confusing.)</p>
34 *
35 * @author Norman Walsh
36 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
37 *
38 * @version 1.0
39 */
40public abstract class FileURL {
41  protected FileURL() { }
42
43  /**
44   * Construct a file: URL for a path name.
45   *
46   * <p>URLs in the file: scheme can be constructed for paths on
47   * the local file system. Several possibilities need to be considered:
48   * </p>
49   *
50   * <ul>
51   * <li>If the path does not begin with a slash, then it is assumed
52   * to reside in the users current working directory
53   * (System.getProperty("user.dir")).</li>
54   * <li>On Windows machines, the current working directory uses
55   * backslashes (\\, instead of /).</li>
56   * <li>If the current working directory is "/", don't add an extra
57   * slash before the base name.</li>
58   * </ul>
59   *
60   * <p>This method is declared static so that other classes
61   * can use it directly.</p>
62   *
63   * @param pathname The path name component for which to construct a URL.
64   *
65   * @return The appropriate file: URL.
66   *
67   * @throws MalformedURLException if the pathname can't be turned into
68   *         a proper URL.
69   */
70  public static URL makeURL(String pathname) throws MalformedURLException {
71    /*if (pathname.startsWith("/")) {
72      return new URL("file://" + pathname);
73    }
74
75    String userdir = System.getProperty("user.dir");
76    userdir.replace('\\', '/');
77
78    if (userdir.endsWith("/")) {
79      return new URL("file:///" + userdir + pathname);
80    } else {
81      return new URL("file:///" + userdir + "/" + pathname);
82    }
83     */
84      File file = new File(pathname);
85      return file.toURI().toURL();
86  }
87}
88