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: */package org.apache.cxf.tools.util;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URISyntaxException;
023: import java.net.URL;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.HashSet;
027: import java.util.List;
028: import java.util.Set;
029: import java.util.StringTokenizer;
030:
031: public final class URIParserUtil {
032: private static final Set<String> KEYWORDS = new HashSet<String>(
033: Arrays
034: .asList(new String[] { "abstract", "boolean",
035: "break", "byte", "case", "catch", "char",
036: "class", "const", "continue", "default",
037: "do", "double", "else", "extends", "final",
038: "finally", "float", "for", "goto", "if",
039: "implements", "import", "instanceof",
040: "int", "interface", "long", "native",
041: "new", "package", "private", "protected",
042: "public", "return", "short", "static",
043: "strictfp", "super", "switch",
044: "synchronized", "this", "throw", "throws",
045: "transient", "try", "void", "volatile",
046: "while", "true", "false", "null", "assert",
047: "enum" }));
048:
049: private URIParserUtil() {
050: // complete
051: }
052:
053: public static URL[] pathToURLs(String path) {
054: StringTokenizer st = new StringTokenizer(path,
055: File.pathSeparator);
056: URL[] urls = new URL[st.countTokens()];
057: int count = 0;
058: while (st.hasMoreTokens()) {
059: File file = new File(st.nextToken());
060: URL url = null;
061: try {
062: url = file.toURI().toURL();
063: } catch (MalformedURLException e) {
064: e.printStackTrace();
065: }
066: if (url != null) {
067: urls[count++] = url;
068: }
069: }
070: if (urls.length != count) {
071: URL[] tmp = new URL[count];
072: System.arraycopy(urls, 0, tmp, 0, count);
073: urls = tmp;
074: }
075: return urls;
076: }
077:
078: public static String parsePackageName(String namespace,
079: String defaultPackageName) {
080: String packageName = (defaultPackageName != null && defaultPackageName
081: .trim().length() > 0) ? defaultPackageName : null;
082:
083: if (packageName == null) {
084: packageName = getPackageName(namespace);
085: }
086: return packageName;
087: }
088:
089: public static String getPackageName(String nameSpaceURI) {
090: int idx = nameSpaceURI.indexOf(':');
091: String scheme = "";
092: if (idx >= 0) {
093: scheme = nameSpaceURI.substring(0, idx);
094: if ("http".equalsIgnoreCase(scheme)
095: || "urn".equalsIgnoreCase(scheme)) {
096: nameSpaceURI = nameSpaceURI.substring(idx + 1);
097: }
098: }
099:
100: List<String> tokens = tokenize(nameSpaceURI, "/: ");
101: if (tokens.size() == 0) {
102: return null;
103: }
104:
105: if (tokens.size() > 1) {
106: String lastToken = tokens.get(tokens.size() - 1);
107: idx = lastToken.lastIndexOf('.');
108: if (idx > 0) {
109: lastToken = lastToken.substring(0, idx);
110: tokens.set(tokens.size() - 1, lastToken);
111: }
112: }
113:
114: String domain = tokens.get(0);
115: idx = domain.indexOf(':');
116: if (idx >= 0) {
117: domain = domain.substring(0, idx);
118: }
119: List<String> r = reverse(tokenize(domain,
120: "urn".equals(scheme) ? ".-" : "."));
121: if ("www".equalsIgnoreCase(r.get(r.size() - 1))) {
122: // remove leading www
123: r.remove(r.size() - 1);
124: }
125:
126: // replace the domain name with tokenized items
127: tokens.addAll(1, r);
128: tokens.remove(0);
129:
130: // iterate through the tokens and apply xml->java name algorithm
131: for (int i = 0; i < tokens.size(); i++) {
132:
133: // get the token and remove illegal chars
134: String token = tokens.get(i);
135: token = removeIllegalIdentifierChars(token);
136:
137: // this will check for reserved keywords
138: if (containsReservedKeywords(token)) {
139: token = '_' + token;
140: }
141:
142: tokens.set(i, token.toLowerCase());
143: }
144:
145: // concat all the pieces and return it
146: return combine(tokens, '.');
147: }
148:
149: public static String getNamespace(String packageName) {
150: if (packageName == null || packageName.length() == 0) {
151: return null;
152: }
153: StringTokenizer tokenizer = new StringTokenizer(packageName,
154: ".");
155: String[] tokens;
156: if (tokenizer.countTokens() == 0) {
157: tokens = new String[0];
158: } else {
159: tokens = new String[tokenizer.countTokens()];
160: for (int i = tokenizer.countTokens() - 1; i >= 0; i--) {
161: tokens[i] = tokenizer.nextToken();
162: }
163: }
164: StringBuffer namespace = new StringBuffer("http://");
165: String dot = "";
166: for (int i = 0; i < tokens.length; i++) {
167: if (i == 1) {
168: dot = ".";
169: }
170: namespace.append(dot + tokens[i]);
171: }
172: namespace.append('/');
173: return namespace.toString();
174: }
175:
176: private static List<String> tokenize(String str, String sep) {
177: StringTokenizer tokens = new StringTokenizer(str, sep);
178: List<String> r = new ArrayList<String>();
179:
180: while (tokens.hasMoreTokens()) {
181: r.add(tokens.nextToken());
182: }
183: return r;
184: }
185:
186: private static String removeIllegalIdentifierChars(String token) {
187: StringBuffer newToken = new StringBuffer();
188: for (int i = 0; i < token.length(); i++) {
189: char c = token.charAt(i);
190:
191: if (i == 0 && !Character.isJavaIdentifierStart(c)) {
192: // prefix an '_' if the first char is illegal
193: newToken.append("_" + c);
194: } else if (!Character.isJavaIdentifierPart(c)) {
195: // replace the char with an '_' if it is illegal
196: newToken.append('_');
197: } else {
198: // add the legal char
199: newToken.append(c);
200: }
201: }
202: return newToken.toString();
203: }
204:
205: private static String combine(List r, char sep) {
206: StringBuilder buf = new StringBuilder(r.get(0).toString());
207:
208: for (int i = 1; i < r.size(); i++) {
209: buf.append(sep);
210: buf.append(r.get(i));
211: }
212:
213: return buf.toString();
214: }
215:
216: private static <T> List<T> reverse(List<T> a) {
217: List<T> r = new ArrayList<T>();
218:
219: for (int i = a.size() - 1; i >= 0; i--) {
220: r.add(a.get(i));
221: }
222: return r;
223: }
224:
225: public static boolean containsReservedKeywords(String token) {
226: return KEYWORDS.contains(token);
227: }
228:
229: public static String normalize(final String uri) {
230: URL url = null;
231: try {
232: url = new URL(uri);
233: return url.toString().replace("\\", "/");
234: } catch (MalformedURLException e1) {
235: try {
236: String f = null;
237: if (uri.indexOf(":") != -1 && !uri.startsWith("/")) {
238: f = "file:/" + uri;
239: } else {
240: f = "file:" + uri;
241: }
242:
243: url = new URL(f);
244: return url.toString().replace("\\", "/");
245: } catch (MalformedURLException e2) {
246: return uri.replace("\\", "/");
247: }
248: }
249: }
250:
251: public static String getAbsoluteURI(final String arg) {
252: if (arg == null) {
253: return null;
254: }
255:
256: try {
257: URL url = new URL(normalize(arg));
258: if (url.toURI().isOpaque()
259: && "file".equalsIgnoreCase(url.getProtocol())) {
260: return new File("").toURI().resolve(url.getPath())
261: .toString();
262: } else {
263: return normalize(arg);
264: }
265: } catch (MalformedURLException e1) {
266: return normalize(arg);
267: } catch (URISyntaxException e2) {
268: return normalize(arg);
269: }
270: }
271: }
|