001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: *
025: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
026: */
027:
028: /*
029: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
030: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
031: *
032: * This code is free software; you can redistribute it and/or modify it
033: * under the terms of the GNU General Public License version 2 only, as
034: * published by the Free Software Foundation. Sun designates this
035: * particular file as subject to the "Classpath" exception as provided
036: * by Sun in the LICENSE file that accompanied this code.
037: *
038: * This code is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
040: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
041: * version 2 for more details (a copy is included in the LICENSE file that
042: * accompanied this code).
043: *
044: * You should have received a copy of the GNU General Public License version
045: * 2 along with this work; if not, write to the Free Software Foundation,
046: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
047: *
048: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
049: * CA 95054 USA or visit www.sun.com if you need additional information or
050: * have any questions.
051: *
052: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
053: *
054: */
055:
056: package com.sun.xml.internal.fastinfoset.sax;
057:
058: import java.io.*;
059:
060: public class SystemIdResolver {
061:
062: public SystemIdResolver() {
063: }
064:
065: public static String getAbsoluteURIFromRelative(String localPath) {
066: if (localPath == null || localPath.length() == 0) {
067: return "";
068: }
069:
070: String absolutePath = localPath;
071: if (!isAbsolutePath(localPath)) {
072: try {
073: absolutePath = getAbsolutePathFromRelativePath(localPath);
074: } catch (SecurityException se) {
075: return "file:" + localPath;
076: }
077: }
078:
079: String urlString;
080: if (null != absolutePath) {
081: urlString = absolutePath.startsWith(File.separator) ? ("file://" + absolutePath)
082: : ("file:///" + absolutePath);
083: } else {
084: urlString = "file:" + localPath;
085: }
086:
087: return replaceChars(urlString);
088: }
089:
090: private static String getAbsolutePathFromRelativePath(
091: String relativePath) {
092: return new File(relativePath).getAbsolutePath();
093: }
094:
095: public static boolean isAbsoluteURI(String systemId) {
096: if (systemId == null) {
097: return false;
098: }
099:
100: if (isWindowsAbsolutePath(systemId)) {
101: return false;
102: }
103:
104: final int fragmentIndex = systemId.indexOf('#');
105: final int queryIndex = systemId.indexOf('?');
106: final int slashIndex = systemId.indexOf('/');
107: final int colonIndex = systemId.indexOf(':');
108:
109: int index = systemId.length() - 1;
110: if (fragmentIndex > 0) {
111: index = fragmentIndex;
112: }
113: if (queryIndex > 0 && queryIndex < index) {
114: index = queryIndex;
115: }
116: if (slashIndex > 0 && slashIndex < index) {
117: index = slashIndex;
118: }
119: return (colonIndex > 0) && (colonIndex < index);
120: }
121:
122: public static boolean isAbsolutePath(String systemId) {
123: if (systemId == null)
124: return false;
125: final File file = new File(systemId);
126: return file.isAbsolute();
127:
128: }
129:
130: private static boolean isWindowsAbsolutePath(String systemId) {
131: if (!isAbsolutePath(systemId))
132: return false;
133: if (systemId.length() > 2
134: && systemId.charAt(1) == ':'
135: && Character.isLetter(systemId.charAt(0))
136: && (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/'))
137: return true;
138: else
139: return false;
140: }
141:
142: private static String replaceChars(String str) {
143: StringBuffer buf = new StringBuffer(str);
144: int length = buf.length();
145: for (int i = 0; i < length; i++) {
146: char currentChar = buf.charAt(i);
147: // Replace space with "%20"
148: if (currentChar == ' ') {
149: buf.setCharAt(i, '%');
150: buf.insert(i + 1, "20");
151: length = length + 2;
152: i = i + 2;
153: }
154: // Replace backslash with forward slash
155: else if (currentChar == '\\') {
156: buf.setCharAt(i, '/');
157: }
158: }
159:
160: return buf.toString();
161: }
162:
163: public static String getAbsoluteURI(String systemId) {
164: String absoluteURI = systemId;
165: if (isAbsoluteURI(systemId)) {
166: if (systemId.startsWith("file:")) {
167: String str = systemId.substring(5);
168:
169: if (str != null && str.startsWith("/")) {
170: if (str.startsWith("///") || !str.startsWith("//")) {
171: int secondColonIndex = systemId.indexOf(':', 5);
172: if (secondColonIndex > 0) {
173: String localPath = systemId
174: .substring(secondColonIndex - 1);
175: try {
176: if (!isAbsolutePath(localPath))
177: absoluteURI = systemId.substring(0,
178: secondColonIndex - 1)
179: + getAbsolutePathFromRelativePath(localPath);
180: } catch (SecurityException se) {
181: return systemId;
182: }
183: }
184: }
185: } else {
186: return getAbsoluteURIFromRelative(systemId
187: .substring(5));
188: }
189:
190: return replaceChars(absoluteURI);
191: } else {
192: return systemId;
193: }
194: } else {
195: return getAbsoluteURIFromRelative(systemId);
196: }
197: }
198: }
|