1 package org.objectweb.celtix.tools.utils; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import java.util.HashSet ; 6 import java.util.List ; 7 import java.util.Set ; 8 import java.util.StringTokenizer ; 9 10 public final class URIParserUtil { 11 private static final Set <String > KEYWORDS = new HashSet <String >(Arrays 12 .asList(new String [] {"abstract", "boolean", "break", "byte", "case", "catch", "char", "class", 13 "const", "continue", "default", "do", "double", "else", "extends", "final", 14 "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", 15 "int", "interface", "long", "native", "new", "package", "private", "protected", 16 "public", "return", "short", "static", "strictfp", "super", "switch", 17 "synchronized", "this", "throw", "throws", "transient", "try", "void", 18 "volatile", "while", "true", "false", "null", "assert", "enum"})); 19 20 private URIParserUtil() { 21 } 23 24 public static String getPackageName(String nameSpaceURI) { 25 int idx = nameSpaceURI.indexOf(':'); 26 String scheme = ""; 27 if (idx >= 0) { 28 scheme = nameSpaceURI.substring(0, idx); 29 if ("http".equalsIgnoreCase(scheme) || "urn".equalsIgnoreCase(scheme)) { 30 nameSpaceURI = nameSpaceURI.substring(idx + 1); 31 } 32 } 33 34 List <String > tokens = tokenize(nameSpaceURI, "/: "); 35 if (tokens.size() == 0) { 36 return null; 37 } 38 39 if (tokens.size() > 1) { 40 String lastToken = tokens.get(tokens.size() - 1); 41 idx = lastToken.lastIndexOf('.'); 42 if (idx > 0) { 43 lastToken = lastToken.substring(0, idx); 44 tokens.set(tokens.size() - 1, lastToken); 45 } 46 } 47 48 String domain = tokens.get(0); 49 idx = domain.indexOf(':'); 50 if (idx >= 0) { 51 domain = domain.substring(0, idx); 52 } 53 List <String > r = reverse(tokenize(domain, "urn".equals(scheme) ? ".-" : ".")); 54 if ("www".equalsIgnoreCase(r.get(r.size() - 1))) { 55 r.remove(r.size() - 1); 57 } 58 59 tokens.addAll(1, r); 61 tokens.remove(0); 62 63 for (int i = 0; i < tokens.size(); i++) { 65 66 String token = tokens.get(i); 68 token = removeIllegalIdentifierChars(token); 69 70 if (contiansReservedKeywords(token)) { 72 token = '_' + token; 73 } 74 75 tokens.set(i, token.toLowerCase()); 76 } 77 78 return combine(tokens, '.'); 80 } 81 82 public static String getNamespace(String packageName) { 83 if (packageName == null || packageName.length() == 0) { 84 return null; 85 } 86 StringTokenizer tokenizer = new StringTokenizer (packageName, "."); 87 String [] tokens; 88 if (tokenizer.countTokens() == 0) { 89 tokens = new String [0]; 90 } else { 91 tokens = new String [tokenizer.countTokens()]; 92 for (int i = tokenizer.countTokens() - 1; i >= 0; i--) { 93 tokens[i] = tokenizer.nextToken(); 94 } 95 } 96 StringBuffer namespace = new StringBuffer ("http://"); 97 String dot = ""; 98 for (int i = 0; i < tokens.length; i++) { 99 if (i == 1) { 100 dot = "."; 101 } 102 namespace.append(dot + tokens[i]); 103 } 104 namespace.append('/'); 105 return namespace.toString(); 106 } 107 108 private static List <String > tokenize(String str, String sep) { 109 StringTokenizer tokens = new StringTokenizer (str, sep); 110 List <String > r = new ArrayList <String >(); 111 112 while (tokens.hasMoreTokens()) { 113 r.add(tokens.nextToken()); 114 } 115 return r; 116 } 117 118 private static String removeIllegalIdentifierChars(String token) { 119 StringBuffer newToken = new StringBuffer (); 120 for (int i = 0; i < token.length(); i++) { 121 char c = token.charAt(i); 122 123 if (i == 0 && !Character.isJavaIdentifierStart(c)) { 124 newToken.append("_" + c); 126 } else if (!Character.isJavaIdentifierPart(c)) { 127 newToken.append('_'); 129 } else { 130 newToken.append(c); 132 } 133 } 134 return newToken.toString(); 135 } 136 137 private static String combine(List r, char sep) { 138 StringBuilder buf = new StringBuilder (r.get(0).toString()); 139 140 for (int i = 1; i < r.size(); i++) { 141 buf.append(sep); 142 buf.append(r.get(i)); 143 } 144 145 return buf.toString(); 146 } 147 148 private static <T> List <T> reverse(List <T> a) { 149 List <T> r = new ArrayList <T>(); 150 151 for (int i = a.size() - 1; i >= 0; i--) { 152 r.add(a.get(i)); 153 } 154 return r; 155 } 156 157 private static boolean contiansReservedKeywords(String token) { 158 return KEYWORDS.contains(token); 159 } 160 } 161 | Popular Tags |