001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: import java.net.MalformedURLException;
023: import java.util.StringTokenizer;
024:
025: public class URLProcessor {
026: public static final String DEFAULT_PACKAGE = "org.apache.axis2";
027:
028: /**
029: * Method makePackageName.
030: *
031: * @param namespace
032: * @return Returns String.
033: */
034: public static String makePackageName(String namespace) {
035:
036: String hostname = null;
037: String path = "";
038:
039: // get the target namespace of the document
040: try {
041: java.net.URL u = new java.net.URL(namespace);
042:
043: hostname = u.getHost();
044: path = u.getPath();
045: } catch (MalformedURLException e) {
046: if (namespace.indexOf(":") > -1) {
047: hostname = namespace
048: .substring(namespace.indexOf(":") + 1);
049:
050: // remove the leading /
051: while (hostname.startsWith("/")) {
052: hostname = hostname.substring(1);
053: }
054:
055: if (hostname.indexOf("/") > -1) {
056: hostname = hostname.substring(0, hostname
057: .indexOf("/"));
058: }
059: } else {
060: hostname = namespace.replace('/', '.');
061: }
062: }
063:
064: // if we didn't file a hostname, bail
065: if (hostname == null || hostname.length() == 0) {
066: return null;
067: }
068:
069: // convert illegal java identifier
070: hostname = hostname.replace('-', '_');
071: path = path.replace('-', '_');
072:
073: path = path.replace(':', '_');
074:
075: // chomp off last forward slash in path, if necessary
076: if ((path.length() > 0)
077: && (path.charAt(path.length() - 1) == '/')) {
078: path = path.substring(0, path.length() - 1);
079: }
080:
081: // tokenize the hostname and reverse it
082: StringTokenizer st = new StringTokenizer(hostname, ".:");
083: String[] words = new String[st.countTokens()];
084:
085: for (int i = 0; i < words.length; ++i) {
086: words[i] = st.nextToken();
087: }
088:
089: StringBuffer sb = new StringBuffer(namespace.length());
090:
091: for (int i = words.length - 1; i >= 0; --i) {
092: addWordToPackageBuffer(sb, words[i],
093: (i == words.length - 1));
094: }
095:
096: // tokenize the path
097: StringTokenizer st2 = new StringTokenizer(path, "/");
098:
099: while (st2.hasMoreTokens()) {
100: addWordToPackageBuffer(sb, st2.nextToken(), false);
101: }
102:
103: return sb.toString().toLowerCase();
104: }
105:
106: /**
107: * Massages <tt>word</tt> into a form suitable for use in a Java package name.
108: * Appends it to the target string buffer with a <tt>.</tt> delimiter if
109: * <tt>word</tt> is not the first word in the package name.
110: *
111: * @param sb the buffer to append to
112: * @param word the word to append
113: * @param firstWord a flag indicating whether this is the first word
114: */
115: private static void addWordToPackageBuffer(StringBuffer sb,
116: String word, boolean firstWord) {
117:
118: if (JavaUtils.isJavaKeyword(word)) {
119: word = JavaUtils.makeNonJavaKeyword(word);
120: }
121:
122: // separate with dot after the first word
123: if (!firstWord) {
124: sb.append('.');
125: }
126:
127: // prefix digits with underscores
128: if (Character.isDigit(word.charAt(0))) {
129: sb.append('_');
130: }
131:
132: // replace periods with underscores
133: if (word.indexOf('.') != -1) {
134: char[] buf = word.toCharArray();
135:
136: for (int i = 0; i < word.length(); i++) {
137: if (buf[i] == '.') {
138: buf[i] = '_';
139: }
140: }
141:
142: word = new String(buf);
143: }
144:
145: sb.append(word);
146: }
147: }
|