1 /*
   2  * Copyright (c) 1997, 2012, 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      *
  79      * For example:
  80      *    /a/b/c/ -> /a/b/c/
  81      *    /a/b/c  -> /a/b/
  82      *    /a/     -> /a/
  83      *    /a      -> /
  84      *
  85      * @param uriPath path of a URI (as returned by java.net.URI#getPath()
  86      * @return the normalized uri path
  87      */
  88     public static String normalizeUriPath(String uriPath) {
  89         if (uriPath.endsWith("/"))
  90             return uriPath;
  91 
  92         // the uri path should always have at least a leading slash,
  93         // so no need to make sure that ( idx == -1 )
  94         int idx = uriPath.lastIndexOf('/');
  95         return uriPath.substring(0, idx+1);
  96     }
  97 
  98     /**
  99      * determine if two Strings are equal ignoring case allowing null values
 100      *
 101      * @param s string 1
 102      * @param t string 2
 103      * @return true iff the given strings are equal ignoring case, false if they aren't
 104      * equal or either of them are null.
 105      */
 106     public static boolean equalsIgnoreCase(String s, String t) {
 107         if (s == t) return true;
 108         if ((s != null) && (t != null)) {
 109             return s.equalsIgnoreCase(t);
 110         }
 111         return false;
 112     }
 113 
 114     /**
 115      * determine if two Strings are iqual allowing null values
 116      *
 117      * @param s string 1
 118      * @param t string 2
 119      * @return true iff the strings are equal, false if they aren't equal or either of
 120      * them are null.
 121      */
 122     public static boolean equal(String s, String t) {
 123         if (s == t) return true;
 124         if ((s != null) && (t != null)) {
 125             return s.equals(t);
 126         }
 127         return false;
 128     }
 129 }