1 /* 2 * Copyright (c) 1997, 2015, 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 26 package com.sun.xml.internal.bind.v2.schemagen; 27 28 29 /** 30 * TODO: JAX-WS dependes on this class - consider moving it somewhere more stable, Notify JAX-WS before modifying anything... 31 * 32 * Other miscellaneous utility methods. 33 * 34 * @author 35 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 36 */ 37 public final class Util { 38 private Util() {} // no instanciation please 39 40 /** 41 * Escape any characters that would cause the single arg constructor 42 * of java.net.URI to complain about illegal chars. 43 * 44 * @param s source string to be escaped 45 */ 46 public static String escapeURI(String s) { 47 StringBuilder sb = new StringBuilder(); 48 for( int i = 0; i < s.length(); i++ ) { 49 char c = s.charAt(i); 50 if(Character.isSpaceChar(c)) { 51 sb.append("%20"); 52 } else { 53 sb.append(c); 54 } 55 } 56 return sb.toString(); 57 } 58 59 /** 60 * Calculate the parent URI path of the given URI path. 61 * 62 * @param uriPath the uriPath (as returned by java.net.URI#getPath() 63 * @return the parent URI path of the given URI path 64 */ 65 public static String getParentUriPath(String uriPath) { 66 int idx = uriPath.lastIndexOf('/'); 67 68 if (uriPath.endsWith("/")) { 69 uriPath = uriPath.substring(0,idx); // trim trailing slash 70 idx = uriPath.lastIndexOf('/'); // move idx to parent context 71 } 72 73 return uriPath.substring(0, idx)+"/"; 74 } 75 76 /** 77 * Calculate the normalized form of the given uriPath. 78 * <p> 79 * For example: <pre>{@code 80 * /a/b/c/ -> /a/b/c/ 81 * /a/b/c -> /a/b/ 82 * /a/ -> /a/ 83 * /a -> / 84 * }</pre> 85 * 86 * @param uriPath path of a URI (as returned by java.net.URI#getPath() 87 * @return the normalized uri path 88 */ 89 public static String normalizeUriPath(String uriPath) { 90 if (uriPath.endsWith("/")) 91 return uriPath; 92 93 // the uri path should always have at least a leading slash, 94 // so no need to make sure that ( idx == -1 ) 95 int idx = uriPath.lastIndexOf('/'); 96 return uriPath.substring(0, idx+1); 97 } 98 99 /** 100 * determine if two Strings are equal ignoring case allowing null values 101 * 102 * @param s string 1 103 * @param t string 2 104 * @return true iff the given strings are equal ignoring case, false if they aren't 105 * equal or either of them are null. 106 */ 107 public static boolean equalsIgnoreCase(String s, String t) { 108 if (s == t) return true; 109 if ((s != null) && (t != null)) { 110 return s.equalsIgnoreCase(t); 111 } 112 return false; 113 } 114 115 /** 116 * determine if two Strings are iqual allowing null values 117 * 118 * @param s string 1 119 * @param t string 2 120 * @return true iff the strings are equal, false if they aren't equal or either of 121 * them are null. 122 */ 123 public static boolean equal(String s, String t) { 124 if (s == t) return true; 125 if ((s != null) && (t != null)) { 126 return s.equals(t); 127 } 128 return false; 129 } 130 }