001: package org.objectweb.celtix.tools.utils;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.HashSet;
006: import java.util.List;
007: import java.util.Set;
008: import java.util.StringTokenizer;
009:
010: public final class URIParserUtil {
011: private static final Set<String> KEYWORDS = new HashSet<String>(
012: Arrays
013: .asList(new String[] { "abstract", "boolean",
014: "break", "byte", "case", "catch", "char",
015: "class", "const", "continue", "default",
016: "do", "double", "else", "extends", "final",
017: "finally", "float", "for", "goto", "if",
018: "implements", "import", "instanceof",
019: "int", "interface", "long", "native",
020: "new", "package", "private", "protected",
021: "public", "return", "short", "static",
022: "strictfp", "super", "switch",
023: "synchronized", "this", "throw", "throws",
024: "transient", "try", "void", "volatile",
025: "while", "true", "false", "null", "assert",
026: "enum" }));
027:
028: private URIParserUtil() {
029: // complete
030: }
031:
032: public static String getPackageName(String nameSpaceURI) {
033: int idx = nameSpaceURI.indexOf(':');
034: String scheme = "";
035: if (idx >= 0) {
036: scheme = nameSpaceURI.substring(0, idx);
037: if ("http".equalsIgnoreCase(scheme)
038: || "urn".equalsIgnoreCase(scheme)) {
039: nameSpaceURI = nameSpaceURI.substring(idx + 1);
040: }
041: }
042:
043: List<String> tokens = tokenize(nameSpaceURI, "/: ");
044: if (tokens.size() == 0) {
045: return null;
046: }
047:
048: if (tokens.size() > 1) {
049: String lastToken = tokens.get(tokens.size() - 1);
050: idx = lastToken.lastIndexOf('.');
051: if (idx > 0) {
052: lastToken = lastToken.substring(0, idx);
053: tokens.set(tokens.size() - 1, lastToken);
054: }
055: }
056:
057: String domain = tokens.get(0);
058: idx = domain.indexOf(':');
059: if (idx >= 0) {
060: domain = domain.substring(0, idx);
061: }
062: List<String> r = reverse(tokenize(domain,
063: "urn".equals(scheme) ? ".-" : "."));
064: if ("www".equalsIgnoreCase(r.get(r.size() - 1))) {
065: // remove leading www
066: r.remove(r.size() - 1);
067: }
068:
069: // replace the domain name with tokenized items
070: tokens.addAll(1, r);
071: tokens.remove(0);
072:
073: // iterate through the tokens and apply xml->java name algorithm
074: for (int i = 0; i < tokens.size(); i++) {
075:
076: // get the token and remove illegal chars
077: String token = tokens.get(i);
078: token = removeIllegalIdentifierChars(token);
079:
080: // this will check for reserved keywords
081: if (contiansReservedKeywords(token)) {
082: token = '_' + token;
083: }
084:
085: tokens.set(i, token.toLowerCase());
086: }
087:
088: // concat all the pieces and return it
089: return combine(tokens, '.');
090: }
091:
092: public static String getNamespace(String packageName) {
093: if (packageName == null || packageName.length() == 0) {
094: return null;
095: }
096: StringTokenizer tokenizer = new StringTokenizer(packageName,
097: ".");
098: String[] tokens;
099: if (tokenizer.countTokens() == 0) {
100: tokens = new String[0];
101: } else {
102: tokens = new String[tokenizer.countTokens()];
103: for (int i = tokenizer.countTokens() - 1; i >= 0; i--) {
104: tokens[i] = tokenizer.nextToken();
105: }
106: }
107: StringBuffer namespace = new StringBuffer("http://");
108: String dot = "";
109: for (int i = 0; i < tokens.length; i++) {
110: if (i == 1) {
111: dot = ".";
112: }
113: namespace.append(dot + tokens[i]);
114: }
115: namespace.append('/');
116: return namespace.toString();
117: }
118:
119: private static List<String> tokenize(String str, String sep) {
120: StringTokenizer tokens = new StringTokenizer(str, sep);
121: List<String> r = new ArrayList<String>();
122:
123: while (tokens.hasMoreTokens()) {
124: r.add(tokens.nextToken());
125: }
126: return r;
127: }
128:
129: private static String removeIllegalIdentifierChars(String token) {
130: StringBuffer newToken = new StringBuffer();
131: for (int i = 0; i < token.length(); i++) {
132: char c = token.charAt(i);
133:
134: if (i == 0 && !Character.isJavaIdentifierStart(c)) {
135: // prefix an '_' if the first char is illegal
136: newToken.append("_" + c);
137: } else if (!Character.isJavaIdentifierPart(c)) {
138: // replace the char with an '_' if it is illegal
139: newToken.append('_');
140: } else {
141: // add the legal char
142: newToken.append(c);
143: }
144: }
145: return newToken.toString();
146: }
147:
148: private static String combine(List r, char sep) {
149: StringBuilder buf = new StringBuilder(r.get(0).toString());
150:
151: for (int i = 1; i < r.size(); i++) {
152: buf.append(sep);
153: buf.append(r.get(i));
154: }
155:
156: return buf.toString();
157: }
158:
159: private static <T> List<T> reverse(List<T> a) {
160: List<T> r = new ArrayList<T>();
161:
162: for (int i = a.size() - 1; i >= 0; i--) {
163: r.add(a.get(i));
164: }
165: return r;
166: }
167:
168: private static boolean contiansReservedKeywords(String token) {
169: return KEYWORDS.contains(token);
170: }
171: }
|