aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.schemagen; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * TODO: JAX-WS dependes on this class - consider moving it somewhere more stable, Notify JAX-WS before modifying anything... aoqi@0: * aoqi@0: * Other miscellaneous utility methods. aoqi@0: * aoqi@0: * @author aoqi@0: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) aoqi@0: */ aoqi@0: public final class Util { aoqi@0: private Util() {} // no instanciation please aoqi@0: aoqi@0: /** aoqi@0: * Escape any characters that would cause the single arg constructor aoqi@0: * of java.net.URI to complain about illegal chars. aoqi@0: * aoqi@0: * @param s source string to be escaped aoqi@0: */ aoqi@0: public static String escapeURI(String s) { aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: for( int i = 0; i < s.length(); i++ ) { aoqi@0: char c = s.charAt(i); aoqi@0: if(Character.isSpaceChar(c)) { aoqi@0: sb.append("%20"); aoqi@0: } else { aoqi@0: sb.append(c); aoqi@0: } aoqi@0: } aoqi@0: return sb.toString(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Calculate the parent URI path of the given URI path. aoqi@0: * aoqi@0: * @param uriPath the uriPath (as returned by java.net.URI#getPath() aoqi@0: * @return the parent URI path of the given URI path aoqi@0: */ aoqi@0: public static String getParentUriPath(String uriPath) { aoqi@0: int idx = uriPath.lastIndexOf('/'); aoqi@0: aoqi@0: if (uriPath.endsWith("/")) { aoqi@0: uriPath = uriPath.substring(0,idx); // trim trailing slash aoqi@0: idx = uriPath.lastIndexOf('/'); // move idx to parent context aoqi@0: } aoqi@0: aoqi@0: return uriPath.substring(0, idx)+"/"; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Calculate the normalized form of the given uriPath. aoqi@0: * aoqi@0: * For example: aoqi@0: * /a/b/c/ -> /a/b/c/ aoqi@0: * /a/b/c -> /a/b/ aoqi@0: * /a/ -> /a/ aoqi@0: * /a -> / aoqi@0: * aoqi@0: * @param uriPath path of a URI (as returned by java.net.URI#getPath() aoqi@0: * @return the normalized uri path aoqi@0: */ aoqi@0: public static String normalizeUriPath(String uriPath) { aoqi@0: if (uriPath.endsWith("/")) aoqi@0: return uriPath; aoqi@0: aoqi@0: // the uri path should always have at least a leading slash, aoqi@0: // so no need to make sure that ( idx == -1 ) aoqi@0: int idx = uriPath.lastIndexOf('/'); aoqi@0: return uriPath.substring(0, idx+1); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * determine if two Strings are equal ignoring case allowing null values aoqi@0: * aoqi@0: * @param s string 1 aoqi@0: * @param t string 2 aoqi@0: * @return true iff the given strings are equal ignoring case, false if they aren't aoqi@0: * equal or either of them are null. aoqi@0: */ aoqi@0: public static boolean equalsIgnoreCase(String s, String t) { aoqi@0: if (s == t) return true; aoqi@0: if ((s != null) && (t != null)) { aoqi@0: return s.equalsIgnoreCase(t); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * determine if two Strings are iqual allowing null values aoqi@0: * aoqi@0: * @param s string 1 aoqi@0: * @param t string 2 aoqi@0: * @return true iff the strings are equal, false if they aren't equal or either of aoqi@0: * them are null. aoqi@0: */ aoqi@0: public static boolean equal(String s, String t) { aoqi@0: if (s == t) return true; aoqi@0: if ((s != null) && (t != null)) { aoqi@0: return s.equals(t); aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: }