001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.schemagen;
027:
028: /**
029: * TODO: JAX-WS dependes on this class - consider moving it somewhere more stable, Notify JAX-WS before modifying anything...
030: *
031: * Other miscellaneous utility methods.
032: *
033: * @author
034: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
035: */
036: public final class Util {
037: private Util() {
038: } // no instanciation please
039:
040: /**
041: * Escape any characters that would cause the single arg constructor
042: * of java.net.URI to complain about illegal chars.
043: *
044: * @param s source string to be escaped
045: */
046: public static String escapeURI(String s) {
047: StringBuilder sb = new StringBuilder();
048: for (int i = 0; i < s.length(); i++) {
049: char c = s.charAt(i);
050: if (Character.isSpaceChar(c)) {
051: sb.append("%20");
052: } else {
053: sb.append(c);
054: }
055: }
056: return sb.toString();
057: }
058:
059: /**
060: * Calculate the parent URI path of the given URI path.
061: *
062: * @param uriPath the uriPath (as returned by java.net.URI#getPath()
063: * @return the parent URI path of the given URI path
064: */
065: public static String getParentUriPath(String uriPath) {
066: int idx = uriPath.lastIndexOf('/');
067:
068: if (uriPath.endsWith("/")) {
069: uriPath = uriPath.substring(0, idx); // trim trailing slash
070: idx = uriPath.lastIndexOf('/'); // move idx to parent context
071: }
072:
073: return uriPath.substring(0, idx) + "/";
074: }
075:
076: /**
077: * Calculate the normalized form of the given uriPath.
078: *
079: * For example:
080: * /a/b/c/ -> /a/b/c/
081: * /a/b/c -> /a/b/
082: * /a/ -> /a/
083: * /a -> /
084: *
085: * @param uriPath path of a URI (as returned by java.net.URI#getPath()
086: * @return the normalized uri path
087: */
088: public static String normalizeUriPath(String uriPath) {
089: if (uriPath.endsWith("/"))
090: return uriPath;
091:
092: // the uri path should always have at least a leading slash,
093: // so no need to make sure that ( idx == -1 )
094: int idx = uriPath.lastIndexOf('/');
095: return uriPath.substring(0, idx + 1);
096: }
097:
098: /**
099: * 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)
108: 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)
125: return true;
126: if ((s != null) && (t != null)) {
127: return s.equals(t);
128: }
129: return false;
130: }
131: }
|