aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.sax; aoqi@0: aoqi@0: import java.io.*; aoqi@0: aoqi@0: public class SystemIdResolver { aoqi@0: aoqi@0: public SystemIdResolver() { aoqi@0: } aoqi@0: aoqi@0: public static String getAbsoluteURIFromRelative(String localPath) { aoqi@0: if (localPath == null || localPath.length() == 0) { aoqi@0: return ""; aoqi@0: } aoqi@0: aoqi@0: String absolutePath = localPath; aoqi@0: if (!isAbsolutePath(localPath)) { aoqi@0: try { aoqi@0: absolutePath = getAbsolutePathFromRelativePath(localPath); aoqi@0: } aoqi@0: catch (SecurityException se) { aoqi@0: return "file:" + localPath; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: String urlString; aoqi@0: if (null != absolutePath) { aoqi@0: urlString = absolutePath.startsWith(File.separator) ? aoqi@0: ("file://" + absolutePath) : aoqi@0: ("file:///" + absolutePath); aoqi@0: } aoqi@0: else { aoqi@0: urlString = "file:" + localPath; aoqi@0: } aoqi@0: aoqi@0: return replaceChars(urlString); aoqi@0: } aoqi@0: aoqi@0: private static String getAbsolutePathFromRelativePath(String relativePath) { aoqi@0: return new File(relativePath).getAbsolutePath(); aoqi@0: } aoqi@0: aoqi@0: public static boolean isAbsoluteURI(String systemId) { aoqi@0: if (systemId == null) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: if (isWindowsAbsolutePath(systemId)) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: final int fragmentIndex = systemId.indexOf('#'); aoqi@0: final int queryIndex = systemId.indexOf('?'); aoqi@0: final int slashIndex = systemId.indexOf('/'); aoqi@0: final int colonIndex = systemId.indexOf(':'); aoqi@0: aoqi@0: int index = systemId.length() -1; aoqi@0: if (fragmentIndex > 0) { aoqi@0: index = fragmentIndex; aoqi@0: } aoqi@0: if (queryIndex > 0 && queryIndex < index) { aoqi@0: index = queryIndex; aoqi@0: } aoqi@0: if (slashIndex > 0 && slashIndex 0) && (colonIndex < index); aoqi@0: } aoqi@0: aoqi@0: public static boolean isAbsolutePath(String systemId) { aoqi@0: if(systemId == null) aoqi@0: return false; aoqi@0: final File file = new File(systemId); aoqi@0: return file.isAbsolute(); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: private static boolean isWindowsAbsolutePath(String systemId) { aoqi@0: if(!isAbsolutePath(systemId)) aoqi@0: return false; aoqi@0: if (systemId.length() > 2 aoqi@0: && systemId.charAt(1) == ':' aoqi@0: && Character.isLetter(systemId.charAt(0)) aoqi@0: && (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/')) aoqi@0: return true; aoqi@0: else aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: private static String replaceChars(String str) { aoqi@0: StringBuffer buf = new StringBuffer(str); aoqi@0: int length = buf.length(); aoqi@0: for (int i = 0; i < length; i++) { aoqi@0: char currentChar = buf.charAt(i); aoqi@0: // Replace space with "%20" aoqi@0: if (currentChar == ' ') { aoqi@0: buf.setCharAt(i, '%'); aoqi@0: buf.insert(i+1, "20"); aoqi@0: length = length + 2; aoqi@0: i = i + 2; aoqi@0: } aoqi@0: // Replace backslash with forward slash aoqi@0: else if (currentChar == '\\') { aoqi@0: buf.setCharAt(i, '/'); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return buf.toString(); aoqi@0: } aoqi@0: aoqi@0: public static String getAbsoluteURI(String systemId) { aoqi@0: String absoluteURI = systemId; aoqi@0: if (isAbsoluteURI(systemId)) { aoqi@0: if (systemId.startsWith("file:")) { aoqi@0: String str = systemId.substring(5); aoqi@0: aoqi@0: if (str != null && str.startsWith("/")) { aoqi@0: if (str.startsWith("///") || !str.startsWith("//")) { aoqi@0: int secondColonIndex = systemId.indexOf(':', 5); aoqi@0: if (secondColonIndex > 0) { aoqi@0: String localPath = systemId.substring(secondColonIndex-1); aoqi@0: try { aoqi@0: if (!isAbsolutePath(localPath)) aoqi@0: absoluteURI = systemId.substring(0, secondColonIndex-1) + aoqi@0: getAbsolutePathFromRelativePath(localPath); aoqi@0: } aoqi@0: catch (SecurityException se) { aoqi@0: return systemId; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: else { aoqi@0: return getAbsoluteURIFromRelative(systemId.substring(5)); aoqi@0: } aoqi@0: aoqi@0: return replaceChars(absoluteURI); aoqi@0: } aoqi@0: else { aoqi@0: return systemId; aoqi@0: } aoqi@0: } aoqi@0: else { aoqi@0: return getAbsoluteURIFromRelative(systemId); aoqi@0: } aoqi@0: } aoqi@0: }