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

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

     1 /*
     2  * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  *
    25  * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
    26  */
    28 package com.sun.xml.internal.fastinfoset.sax;
    30 import java.io.*;
    32 public class SystemIdResolver {
    34     public SystemIdResolver() {
    35     }
    37     public static String getAbsoluteURIFromRelative(String localPath) {
    38         if (localPath == null || localPath.length() == 0) {
    39             return "";
    40         }
    42         String absolutePath = localPath;
    43         if (!isAbsolutePath(localPath)) {
    44             try {
    45                 absolutePath = getAbsolutePathFromRelativePath(localPath);
    46             }
    47             catch (SecurityException se) {
    48                 return "file:" + localPath;
    49             }
    50         }
    52         String urlString;
    53         if (null != absolutePath) {
    54             urlString = absolutePath.startsWith(File.separator) ?
    55                 ("file://" + absolutePath) :
    56                 ("file:///" + absolutePath);
    57         }
    58         else {
    59             urlString = "file:" + localPath;
    60         }
    62         return replaceChars(urlString);
    63     }
    65     private static String getAbsolutePathFromRelativePath(String relativePath) {
    66         return new File(relativePath).getAbsolutePath();
    67     }
    69     public static boolean isAbsoluteURI(String systemId) {
    70         if (systemId == null) {
    71             return false;
    72         }
    74         if (isWindowsAbsolutePath(systemId)) {
    75             return false;
    76         }
    78         final int fragmentIndex = systemId.indexOf('#');
    79         final int queryIndex = systemId.indexOf('?');
    80         final int slashIndex = systemId.indexOf('/');
    81         final int colonIndex = systemId.indexOf(':');
    83         int index = systemId.length() -1;
    84         if (fragmentIndex > 0) {
    85             index = fragmentIndex;
    86         }
    87         if (queryIndex > 0 && queryIndex < index) {
    88             index = queryIndex;
    89         }
    90         if (slashIndex > 0 && slashIndex <index) {
    91             index = slashIndex;
    92         }
    93         return (colonIndex > 0) && (colonIndex < index);
    94     }
    96     public static boolean isAbsolutePath(String systemId) {
    97         if(systemId == null)
    98             return false;
    99         final File file = new File(systemId);
   100         return file.isAbsolute();
   102     }
   104     private static boolean isWindowsAbsolutePath(String systemId) {
   105         if(!isAbsolutePath(systemId))
   106             return false;
   107         if (systemId.length() > 2
   108         && systemId.charAt(1) == ':'
   109         && Character.isLetter(systemId.charAt(0))
   110         && (systemId.charAt(2) == '\\' || systemId.charAt(2) == '/'))
   111             return true;
   112         else
   113             return false;
   114     }
   116     private static String replaceChars(String str) {
   117         StringBuffer buf = new StringBuffer(str);
   118         int length = buf.length();
   119         for (int i = 0; i < length; i++) {
   120             char currentChar = buf.charAt(i);
   121             // Replace space with "%20"
   122             if (currentChar == ' ') {
   123                 buf.setCharAt(i, '%');
   124                 buf.insert(i+1, "20");
   125                 length = length + 2;
   126                 i = i + 2;
   127             }
   128             // Replace backslash with forward slash
   129             else if (currentChar == '\\') {
   130                 buf.setCharAt(i, '/');
   131             }
   132         }
   134         return buf.toString();
   135     }
   137     public static String getAbsoluteURI(String systemId) {
   138         String absoluteURI = systemId;
   139         if (isAbsoluteURI(systemId)) {
   140             if (systemId.startsWith("file:")) {
   141                 String str = systemId.substring(5);
   143                 if (str != null && str.startsWith("/")) {
   144                     if (str.startsWith("///") || !str.startsWith("//")) {
   145                         int secondColonIndex = systemId.indexOf(':', 5);
   146                         if (secondColonIndex > 0) {
   147                             String localPath = systemId.substring(secondColonIndex-1);
   148                             try {
   149                                 if (!isAbsolutePath(localPath))
   150                                     absoluteURI = systemId.substring(0, secondColonIndex-1) +
   151                                     getAbsolutePathFromRelativePath(localPath);
   152                             }
   153                             catch (SecurityException se) {
   154                                 return systemId;
   155                             }
   156                         }
   157                     }
   158                 }
   159                 else {
   160                     return getAbsoluteURIFromRelative(systemId.substring(5));
   161                 }
   163                 return replaceChars(absoluteURI);
   164             }
   165             else  {
   166                 return systemId;
   167             }
   168         }
   169         else {
   170             return getAbsoluteURIFromRelative(systemId);
   171         }
   172     }
   173 }

mercurial