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