src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/sax/SystemIdResolver.java

changeset 286
f50545b5e2f1
parent 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/sax/SystemIdResolver.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,173 @@
     1.4 +/*
     1.5 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + *
    1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    1.29 + */
    1.30 +
    1.31 +package com.sun.xml.internal.fastinfoset.sax;
    1.32 +
    1.33 +import java.io.*;
    1.34 +
    1.35 +public class SystemIdResolver {
    1.36 +
    1.37 +    public SystemIdResolver() {
    1.38 +    }
    1.39 +
    1.40 +    public static String getAbsoluteURIFromRelative(String localPath) {
    1.41 +        if (localPath == null || localPath.length() == 0) {
    1.42 +            return "";
    1.43 +        }
    1.44 +
    1.45 +        String absolutePath = localPath;
    1.46 +        if (!isAbsolutePath(localPath)) {
    1.47 +            try {
    1.48 +                absolutePath = getAbsolutePathFromRelativePath(localPath);
    1.49 +            }
    1.50 +            catch (SecurityException se) {
    1.51 +                return "file:" + localPath;
    1.52 +            }
    1.53 +        }
    1.54 +
    1.55 +        String urlString;
    1.56 +        if (null != absolutePath) {
    1.57 +            urlString = absolutePath.startsWith(File.separator) ?
    1.58 +                ("file://" + absolutePath) :
    1.59 +                ("file:///" + absolutePath);
    1.60 +        }
    1.61 +        else {
    1.62 +            urlString = "file:" + localPath;
    1.63 +        }
    1.64 +
    1.65 +        return replaceChars(urlString);
    1.66 +    }
    1.67 +
    1.68 +    private static String getAbsolutePathFromRelativePath(String relativePath) {
    1.69 +        return new File(relativePath).getAbsolutePath();
    1.70 +    }
    1.71 +
    1.72 +    public static boolean isAbsoluteURI(String systemId) {
    1.73 +        if (systemId == null) {
    1.74 +            return false;
    1.75 +        }
    1.76 +
    1.77 +        if (isWindowsAbsolutePath(systemId)) {
    1.78 +            return false;
    1.79 +        }
    1.80 +
    1.81 +        final int fragmentIndex = systemId.indexOf('#');
    1.82 +        final int queryIndex = systemId.indexOf('?');
    1.83 +        final int slashIndex = systemId.indexOf('/');
    1.84 +        final int colonIndex = systemId.indexOf(':');
    1.85 +
    1.86 +        int index = systemId.length() -1;
    1.87 +        if (fragmentIndex > 0) {
    1.88 +            index = fragmentIndex;
    1.89 +        }
    1.90 +        if (queryIndex > 0 && queryIndex < index) {
    1.91 +            index = queryIndex;
    1.92 +        }
    1.93 +        if (slashIndex > 0 && slashIndex <index) {
    1.94 +            index = slashIndex;
    1.95 +        }
    1.96 +        return (colonIndex > 0) && (colonIndex < index);
    1.97 +    }
    1.98 +
    1.99 +    public static boolean isAbsolutePath(String systemId) {
   1.100 +        if(systemId == null)
   1.101 +            return false;
   1.102 +        final File file = new File(systemId);
   1.103 +        return file.isAbsolute();
   1.104 +
   1.105 +    }
   1.106 +
   1.107 +    private static boolean isWindowsAbsolutePath(String systemId) {
   1.108 +        if(!isAbsolutePath(systemId))
   1.109 +            return false;
   1.110 +        if (systemId.length() > 2
   1.111 +        && systemId.charAt(1) == ':'
   1.112 +        && Character.isLetter(systemId.charAt(0))
   1.113 +        && (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/'))
   1.114 +            return true;
   1.115 +        else
   1.116 +            return false;
   1.117 +    }
   1.118 +
   1.119 +    private static String replaceChars(String str) {
   1.120 +        StringBuffer buf = new StringBuffer(str);
   1.121 +        int length = buf.length();
   1.122 +        for (int i = 0; i < length; i++) {
   1.123 +            char currentChar = buf.charAt(i);
   1.124 +            // Replace space with "%20"
   1.125 +            if (currentChar == ' ') {
   1.126 +                buf.setCharAt(i, '%');
   1.127 +                buf.insert(i+1, "20");
   1.128 +                length = length + 2;
   1.129 +                i = i + 2;
   1.130 +            }
   1.131 +            // Replace backslash with forward slash
   1.132 +            else if (currentChar == '\\') {
   1.133 +                buf.setCharAt(i, '/');
   1.134 +            }
   1.135 +        }
   1.136 +
   1.137 +        return buf.toString();
   1.138 +    }
   1.139 +
   1.140 +    public static String getAbsoluteURI(String systemId) {
   1.141 +        String absoluteURI = systemId;
   1.142 +        if (isAbsoluteURI(systemId)) {
   1.143 +            if (systemId.startsWith("file:")) {
   1.144 +                String str = systemId.substring(5);
   1.145 +
   1.146 +                if (str != null && str.startsWith("/")) {
   1.147 +                    if (str.startsWith("///") || !str.startsWith("//")) {
   1.148 +                        int secondColonIndex = systemId.indexOf(':', 5);
   1.149 +                        if (secondColonIndex > 0) {
   1.150 +                            String localPath = systemId.substring(secondColonIndex-1);
   1.151 +                            try {
   1.152 +                                if (!isAbsolutePath(localPath))
   1.153 +                                    absoluteURI = systemId.substring(0, secondColonIndex-1) +
   1.154 +                                    getAbsolutePathFromRelativePath(localPath);
   1.155 +                            }
   1.156 +                            catch (SecurityException se) {
   1.157 +                                return systemId;
   1.158 +                            }
   1.159 +                        }
   1.160 +                    }
   1.161 +                }
   1.162 +                else {
   1.163 +                    return getAbsoluteURIFromRelative(systemId.substring(5));
   1.164 +                }
   1.165 +
   1.166 +                return replaceChars(absoluteURI);
   1.167 +            }
   1.168 +            else  {
   1.169 +                return systemId;
   1.170 +            }
   1.171 +        }
   1.172 +        else {
   1.173 +            return getAbsoluteURIFromRelative(systemId);
   1.174 +        }
   1.175 +    }
   1.176 +}

mercurial