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:
037: package com.sun.xml.ws.util;
038:
039: import java.util.UUID;
040: import java.util.regex.Pattern;
041: import java.net.URL;
042: import java.net.MalformedURLException;
043: import java.net.URI;
044: import java.net.URISyntaxException;
045: import java.io.File;
046: import java.io.IOException;
047:
048: import javax.xml.namespace.QName;
049:
050: /**
051: * @author Vivek Pandey
052: *
053: * Wrapper utility class to be used from the generated code or run time.
054: */
055: public final class JAXWSUtils {
056: public static String getUUID() {
057: return UUID.randomUUID().toString();
058: }
059:
060: public static String getFileOrURLName(String fileOrURL) {
061: try {
062: try {
063: return escapeSpace(new URL(fileOrURL).toExternalForm());
064: } catch (MalformedURLException e) {
065: return new File(fileOrURL).getCanonicalFile().toURL()
066: .toExternalForm();
067: }
068: } catch (Exception e) {
069: // try it as an URL
070: return fileOrURL;
071: }
072: }
073:
074: public static URL getFileOrURL(String fileOrURL) throws IOException {
075: try {
076: return new URL(fileOrURL);
077: } catch (MalformedURLException e) {
078: return new File(fileOrURL).toURL();
079: }
080: }
081:
082: private static String escapeSpace(String url) {
083: // URLEncoder didn't work.
084: StringBuffer buf = new StringBuffer();
085: for (int i = 0; i < url.length(); i++) {
086: // TODO: not sure if this is the only character that needs to be escaped.
087: if (url.charAt(i) == ' ')
088: buf.append("%20");
089: else
090: buf.append(url.charAt(i));
091: }
092: return buf.toString();
093: }
094:
095: public static String absolutize(String name) {
096: // absolutize all the system IDs in the input,
097: // so that we can map system IDs to DOM trees.
098: try {
099: URL baseURL = new File(".").getCanonicalFile().toURL();
100: return new URL(baseURL, name).toExternalForm();
101: } catch (IOException e) {
102: ; // ignore
103: }
104: return name;
105: }
106:
107: /**
108: * Checks if the system ID is absolute.
109: */
110: public static void checkAbsoluteness(String systemId) {
111: // we need to be able to handle system IDs like "urn:foo", which java.net.URL can't process,
112: // but OTOH we also need to be able to process system IDs like "file://a b c/def.xsd",
113: // which java.net.URI can't process. So for now, let's fail only if both of them fail.
114: // eventually we need a proper URI class that works for us.
115: try {
116: new URL(systemId);
117: } catch (MalformedURLException _) {
118: try {
119: new URI(systemId);
120: } catch (URISyntaxException e) {
121: throw new IllegalArgumentException("system ID '"
122: + systemId + "' isn't absolute", e);
123: }
124: }
125: }
126:
127: /*
128: * To match, both QNames must have the same namespace and the local
129: * part of the target must match the local part of the 'pattern'
130: * QName, which may contain wildcard characters.
131: */
132: public static boolean matchQNames(QName target, QName pattern) {
133: if ((target == null) || (pattern == null)) {
134: // if no service or port is in descriptor
135: return false;
136: }
137: if (pattern.getNamespaceURI().equals(target.getNamespaceURI())) {
138: String regex = pattern.getLocalPart().replaceAll("\\*",
139: ".*");
140: return Pattern.matches(regex, target.getLocalPart());
141: }
142: return false;
143: }
144:
145: }
|