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.jaxws.utility;
021:
022: import java.net.MalformedURLException;
023: import java.net.URL;
024: import java.util.ArrayList;
025: import java.util.StringTokenizer;
026:
027: /** Common Java Utilites */
028: public class JavaUtils extends org.apache.axis2.util.JavaUtils {
029:
030: /** Private Constructor...All methods of this class are static */
031: private JavaUtils() {
032: }
033:
034: /**
035: * Namespace 2 Package algorithm as defined by the JAXB Specification
036: *
037: * @param Namespace
038: * @return String represeting Namespace
039: */
040: public static String getPackageFromNamespace(String namespace) {
041: // The following steps correspond to steps described in the JAXB Specification
042:
043: // Step 1: Scan off the host name
044: String hostname = null;
045: String path = null;
046: try {
047: URL url = new URL(namespace);
048: hostname = url.getHost();
049: path = url.getPath();
050: } catch (MalformedURLException e) {
051: // No FFDC code needed
052: if (namespace.indexOf(":") > -1) {
053: // Brain-dead code to skip over the protocol
054: hostname = namespace
055: .substring(namespace.indexOf(":") + 1);
056: } else {
057: hostname = namespace;
058: }
059: }
060:
061: // Step 3: Tokenize the host name using ":" and "/"
062: StringTokenizer st = new StringTokenizer(hostname, ":/");
063:
064: ArrayList<String> wordList = new ArrayList<String>();
065:
066: //Read Hostname first.
067: for (int i = 0; st != null && i < st.countTokens(); ++i) {
068: wordList.add(st.nextToken());
069: }
070: //Read rest Of the path now
071: if (path != null) {
072: StringTokenizer pathst = new StringTokenizer(path, "/");
073: while (pathst != null && pathst.hasMoreTokens()) {
074: wordList.add(pathst.nextToken());
075: }
076: }
077: String[] words = wordList.toArray(new String[0]);
078:
079: // Now do step 2: Strip off the trailing "." (i.e. strip off .html)
080: if (words != null && words.length > 1) {
081: String lastWord = words[words.length - 1];
082: int index = lastWord.lastIndexOf('.');
083: if (index > 0) {
084: words[words.length - 1] = lastWord.substring(0, index);
085: }
086: }
087:
088: // Step 4: Unescape each escape sequence
089: // TODO I don't know what to do here.
090:
091: // Step 5: If protocol is urn, replace - with . in the first word
092: if (namespace.startsWith("urn:")) {
093: words[0] = replace(words[0], "-", ".");
094: }
095:
096: // Step 6: Tokenize the first word with "." and reverse the order. (the www is also removed).
097: // TODO This is not exactly equivalent to the JAXB Rule.
098: StringTokenizer st2 = new StringTokenizer(words[0], ".");
099: ArrayList<String> list = new ArrayList<String>();
100: while (st2.hasMoreTokens()) {
101: // Add the strings so they are in reverse order
102: list.add(0, st2.nextToken());
103: }
104: // Remove www
105: String last = list.get(list.size() - 1);
106: if (last.equals("www")) {
107: list.remove(list.size() - 1);
108: }
109: // Now each of words is represented by list
110: for (int i = 1; i < words.length; i++) {
111: list.add(words[i]);
112: }
113:
114: // Step 7: lowercase each word
115: for (int i = 0; i < list.size(); i++) {
116: String word = list.remove(i);
117: word = word.toLowerCase();
118: list.add(i, word);
119: }
120:
121: // Step 8: make into and an appropriate java word
122: for (int i = 0; i < list.size(); i++) {
123: String word = list.get(i);
124:
125: // 8a: Convert special characters to underscore
126: // Convert non-java words to underscore.
127: // TODO: Need to do this for all chars..not just hyphens
128: word = replace(word, "-", "_");
129:
130: // 8b: Append _ to java keywords
131: if (JavaUtils.isJavaKeyword(word)) {
132: word = word + "_";
133: }
134: // 8c: prepend _ if first character cannot be the first character of a java identifier
135: if (!Character.isJavaIdentifierPart(word.charAt(0))) {
136: word = "_" + word;
137: }
138:
139: list.set(i, word);
140: }
141:
142: // Step 9: Concatenate and return
143: String name = "";
144: for (int i = 0; i < list.size(); i++) {
145: if (i == 0) {
146: name = list.get(0);
147: } else {
148: name = name + "." + list.get(i);
149: }
150: }
151: return name;
152: }
153:
154: /**
155: * replace: Like String.replace except that the old new items are strings.
156: *
157: * @param name string
158: * @param oldT old text to replace
159: * @param newT new text to use
160: * @return replacement string
161: */
162: public static final String replace(String name, String oldT,
163: String newT) {
164:
165: if (name == null)
166: return "";
167:
168: // Create a string buffer that is twice initial length.
169: // This is a good starting point.
170: StringBuffer sb = new StringBuffer(name.length() * 2);
171:
172: int len = oldT.length();
173: try {
174: int start = 0;
175: int i = name.indexOf(oldT, start);
176:
177: while (i >= 0) {
178: sb.append(name.substring(start, i));
179: sb.append(newT);
180: start = i + len;
181: i = name.indexOf(oldT, start);
182: }
183: if (start < name.length())
184: sb.append(name.substring(start));
185: } catch (NullPointerException e) {
186: // No FFDC code needed
187: }
188:
189: return new String(sb);
190: }
191:
192: /**
193: * Get a string containing the stack of the current location
194: *
195: * @return String
196: */
197: public static String stackToString() {
198: return stackToString(new RuntimeException());
199: }
200:
201: /**
202: * Get a string containing the stack of the specified exception
203: *
204: * @param e
205: * @return
206: */
207: public static String stackToString(Throwable e) {
208: java.io.StringWriter sw = new java.io.StringWriter();
209: java.io.BufferedWriter bw = new java.io.BufferedWriter(sw);
210: java.io.PrintWriter pw = new java.io.PrintWriter(bw);
211: e.printStackTrace(pw);
212: pw.close();
213: String text = sw.getBuffer().toString();
214: // Jump past the throwable
215: text = text.substring(text.indexOf("at"));
216: text = replace(text, "at ", "DEBUG_FRAME = ");
217: return text;
218: }
219: }
|