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