001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.bind.v2.schemagen;
037:
038: /**
039: * TODO: JAX-WS dependes on this class - consider moving it somewhere more stable, Notify JAX-WS before modifying anything...
040: *
041: * Other miscellaneous utility methods.
042: *
043: * @author
044: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
045: */
046: public final class Util {
047: private Util() {
048: } // no instanciation please
049:
050: /**
051: * Escape any characters that would cause the single arg constructor
052: * of java.net.URI to complain about illegal chars.
053: *
054: * @param s source string to be escaped
055: */
056: public static String escapeURI(String s) {
057: StringBuilder sb = new StringBuilder();
058: for (int i = 0; i < s.length(); i++) {
059: char c = s.charAt(i);
060: if (Character.isSpaceChar(c)) {
061: sb.append("%20");
062: } else {
063: sb.append(c);
064: }
065: }
066: return sb.toString();
067: }
068:
069: /**
070: * Calculate the parent URI path of the given URI path.
071: *
072: * @param uriPath the uriPath (as returned by java.net.URI#getPath()
073: * @return the parent URI path of the given URI path
074: */
075: public static String getParentUriPath(String uriPath) {
076: int idx = uriPath.lastIndexOf('/');
077:
078: if (uriPath.endsWith("/")) {
079: uriPath = uriPath.substring(0, idx); // trim trailing slash
080: idx = uriPath.lastIndexOf('/'); // move idx to parent context
081: }
082:
083: return uriPath.substring(0, idx) + "/";
084: }
085:
086: /**
087: * Calculate the normalized form of the given uriPath.
088: *
089: * For example:
090: * /a/b/c/ -> /a/b/c/
091: * /a/b/c -> /a/b/
092: * /a/ -> /a/
093: * /a -> /
094: *
095: * @param uriPath path of a URI (as returned by java.net.URI#getPath()
096: * @return the normalized uri path
097: */
098: public static String normalizeUriPath(String uriPath) {
099: if (uriPath.endsWith("/"))
100: return uriPath;
101:
102: // the uri path should always have at least a leading slash,
103: // so no need to make sure that ( idx == -1 )
104: int idx = uriPath.lastIndexOf('/');
105: return uriPath.substring(0, idx + 1);
106: }
107:
108: /**
109: * determine if two Strings are equal ignoring case allowing null values
110: *
111: * @param s string 1
112: * @param t string 2
113: * @return true iff the given strings are equal ignoring case, false if they aren't
114: * equal or either of them are null.
115: */
116: public static boolean equalsIgnoreCase(String s, String t) {
117: if (s == t)
118: return true;
119: if ((s != null) && (t != null)) {
120: return s.equalsIgnoreCase(t);
121: }
122: return false;
123: }
124:
125: /**
126: * determine if two Strings are iqual allowing null values
127: *
128: * @param s string 1
129: * @param t string 2
130: * @return true iff the strings are equal, false if they aren't equal or either of
131: * them are null.
132: */
133: public static boolean equal(String s, String t) {
134: if (s == t)
135: return true;
136: if ((s != null) && (t != null)) {
137: return s.equals(t);
138: }
139: return false;
140: }
141: }
|