KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > sax > SystemIdResolver


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package com.sun.xml.fastinfoset.sax;
41
42 import java.io.*;
43
44 public class SystemIdResolver {
45     
46     public SystemIdResolver() {
47     }
48     
49     public static String JavaDoc getAbsoluteURIFromRelative(String JavaDoc localPath) {
50         if (localPath == null || localPath.length() == 0) {
51             return "";
52         }
53         
54         String JavaDoc absolutePath = localPath;
55         if (!isAbsolutePath(localPath)) {
56             try {
57                 absolutePath = getAbsolutePathFromRelativePath(localPath);
58             }
59             catch (SecurityException JavaDoc se) {
60                 return "file:" + localPath;
61             }
62         }
63         
64         String JavaDoc urlString;
65         if (null != absolutePath) {
66             urlString = absolutePath.startsWith(File.separator) ?
67                 ("file://" + absolutePath) :
68                 ("file:///" + absolutePath);
69         }
70         else {
71             urlString = "file:" + localPath;
72         }
73         
74         return replaceChars(urlString);
75     }
76     
77     private static String JavaDoc getAbsolutePathFromRelativePath(String JavaDoc relativePath) {
78         return new File(relativePath).getAbsolutePath();
79     }
80     
81     public static boolean isAbsoluteURI(String JavaDoc systemId) {
82         if (systemId == null) {
83             return false;
84         }
85         
86         if (isWindowsAbsolutePath(systemId)) {
87             return false;
88         }
89         
90         final int fragmentIndex = systemId.indexOf('#');
91         final int queryIndex = systemId.indexOf('?');
92         final int slashIndex = systemId.indexOf('/');
93         final int colonIndex = systemId.indexOf(':');
94         
95         int index = systemId.length() -1;
96         if (fragmentIndex > 0) {
97             index = fragmentIndex;
98         }
99         if (queryIndex > 0 && queryIndex < index) {
100             index = queryIndex;
101         }
102         if (slashIndex > 0 && slashIndex <index) {
103             index = slashIndex;
104         }
105         return (colonIndex > 0) && (colonIndex < index);
106     }
107     
108     public static boolean isAbsolutePath(String JavaDoc systemId) {
109         if(systemId == null)
110             return false;
111         final File file = new File(systemId);
112         return file.isAbsolute();
113         
114     }
115     
116     private static boolean isWindowsAbsolutePath(String JavaDoc systemId) {
117         if(!isAbsolutePath(systemId))
118             return false;
119         if (systemId.length() > 2
120         && systemId.charAt(1) == ':'
121         && Character.isLetter(systemId.charAt(0))
122         && (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/'))
123             return true;
124         else
125             return false;
126     }
127     
128     private static String JavaDoc replaceChars(String JavaDoc str) {
129         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str);
130         int length = buf.length();
131         for (int i = 0; i < length; i++) {
132             char currentChar = buf.charAt(i);
133             // Replace space with "%20"
134
if (currentChar == ' ') {
135                 buf.setCharAt(i, '%');
136                 buf.insert(i+1, "20");
137                 length = length + 2;
138                 i = i + 2;
139             }
140             // Replace backslash with forward slash
141
else if (currentChar == '\\') {
142                 buf.setCharAt(i, '/');
143             }
144         }
145         
146         return buf.toString();
147     }
148     
149     public static String JavaDoc getAbsoluteURI(String JavaDoc systemId) {
150         String JavaDoc absoluteURI = systemId;
151         if (isAbsoluteURI(systemId)) {
152             if (systemId.startsWith("file:")) {
153                 String JavaDoc str = systemId.substring(5);
154                 
155                 if (str != null && str.startsWith("/")) {
156                     if (str.startsWith("///") || !str.startsWith("//")) {
157                         int secondColonIndex = systemId.indexOf(':', 5);
158                         if (secondColonIndex > 0) {
159                             String JavaDoc localPath = systemId.substring(secondColonIndex-1);
160                             try {
161                                 if (!isAbsolutePath(localPath))
162                                     absoluteURI = systemId.substring(0, secondColonIndex-1) +
163                                     getAbsolutePathFromRelativePath(localPath);
164                             }
165                             catch (SecurityException JavaDoc se) {
166                                 return systemId;
167                             }
168                         }
169                     }
170                 }
171                 else {
172                     return getAbsoluteURIFromRelative(systemId.substring(5));
173                 }
174                 
175                 return replaceChars(absoluteURI);
176             }
177             else {
178                 return systemId;
179             }
180         }
181         else {
182             return getAbsoluteURIFromRelative(systemId);
183         }
184     }
185 }
186
Popular Tags