1 57 58 package org.apache.wsif.compiler.schema.tools; 59 60 import java.io.File ; 61 import java.util.Hashtable ; 62 import java.util.StringTokenizer ; 63 64 import com.ibm.wsdl.util.xml.DOMUtils; 65 import org.w3c.dom.Element ; 66 import org.w3c.dom.Node ; 67 import org.w3c.dom.NodeList ; 68 69 76 public class Conventions { 77 78 private static Hashtable postfixTable = init(); 79 private static boolean verbose = true; 80 81 85 private static Hashtable init() { 86 87 Hashtable postfixTable = new Hashtable (11); 88 89 postfixTable.put("simpleType", ""); postfixTable.put("complexType", ""); postfixTable.put("group", "_Group"); 92 postfixTable.put("attributeGroup", "_AttrGp"); 93 postfixTable.put("element", ""); 94 postfixTable.put("attribute", "_Attr"); 95 101 return postfixTable; 102 103 } 104 105 111 public static String namespaceURI2JavaPath(String namespaceURI) 112 throws IllegalArgumentException { 113 114 if (namespaceURI == null) 115 throw new IllegalArgumentException ( 116 "Argument to " + "'namespaceURI2JavaPath' cannot " + "be null."); 117 118 if (namespaceURI.startsWith("http://")) 119 namespaceURI = namespaceURI.substring(7); 120 121 if (namespaceURI.compareTo("") == 0) 122 return namespaceURI; 123 124 if (namespaceURI.endsWith("/")) 125 namespaceURI = namespaceURI.substring(0, namespaceURI.lastIndexOf("/")); 126 127 StringTokenizer tokens = new StringTokenizer (namespaceURI, "/", false); 128 129 String javaPath = tokens.nextToken(); 130 while (tokens.hasMoreTokens()) { 131 javaPath = tokens.nextToken() + "." + javaPath; 132 } 133 134 tokens = new StringTokenizer (javaPath, ".", false); 135 javaPath = tokens.nextToken(); 136 137 while (tokens.hasMoreTokens()) { 138 javaPath = tokens.nextToken() + "." + javaPath; 139 } 140 141 javaPath = javaPath.replace(':', '_'); 142 javaPath = javaPath.replace('-', '_'); 143 144 return javaPath; 145 } 146 147 159 public static String schema2JavaName( 160 String schemaType, 161 String schemaName, 162 boolean isClass) 163 throws IllegalArgumentException { 164 if (schemaType == null) 165 throw new IllegalArgumentException ( 166 "Illegal arguments to " + "'schema2JavaName'."); 167 168 if (schemaType.compareTo("any") == 0) 169 return "any"; 170 else if ( 171 schemaType.compareTo("simpleType") == 0) { 172 if (schemaName == null) 174 throw new IllegalArgumentException ( 175 "Illegal arguments to " + "'schema2JavaName'."); 176 schemaName = schemaName.replace('-', '_'); 177 return schemaName; 178 } else if (schemaType.compareTo("attribute") == 0) { 179 if (schemaName == null) 180 throw new IllegalArgumentException ( 181 "Illegal arguments to " + "'schema2JavaName'."); 182 schemaName = schemaName.replace('-', '_'); 183 return schemaName + postfixTable.get("attribute"); 184 } else if (schemaType.compareTo("anyAttribute") == 0) 185 return "anyAttribute"; 186 else if (schemaType.compareTo("all") == 0) 187 schemaName = "all"; 188 else if (schemaType.compareTo("choice") == 0) 189 schemaName = "choice"; 190 else if (schemaType.compareTo("sequence") == 0) 191 schemaName = "sequence"; 192 193 if (schemaName == null) 194 throw new IllegalArgumentException ( 195 "Illegal arguments to " + "'schema2JavaName'."); 196 197 schemaName = schemaName.replace('-', '_'); 198 200 String postfix = (String ) postfixTable.get(schemaType); 201 if (postfix == null) 202 postfix = ""; 203 204 String javaName = schemaName; 205 if (isClass) 206 javaName = 207 Character.toUpperCase(schemaName.charAt(0)) + schemaName.substring(1); 208 return javaName + postfix; 209 210 } 211 212 221 public static String schema2JavaName(Node node, String targetURI) { 222 if (node == null || targetURI == null) 223 throw new IllegalArgumentException ( 224 "Illegal arguments to " + "'schema2JavaName'."); 225 226 String targetNSPrefix = namespaceURI2JavaPath(targetURI) + "."; 227 String name = DOMUtils.getAttribute((Element ) node, "name"); 228 String type = node.getLocalName(); 229 230 if (name == null || type == null) 231 return null; 232 else { 233 name = schema2JavaName(type, name, true); 234 return targetNSPrefix + name; 235 } 236 237 } 238 239 public static String schema2JavaName(NodeList nl, NodeList targetURI) { 240 if (nl.getLength() == 0) { 241 throw new IllegalArgumentException ( 242 "No type name found for serializer " + "class."); 243 } 244 Node node = nl.item(0); 245 246 if (targetURI.getLength() == 0) { 247 throw new IllegalArgumentException ( 248 "No type name found for serializer " + "class."); 249 } 250 Node tnode = targetURI.item(0); 251 String targetNS = tnode.getNodeValue(); 252 253 255 return schema2JavaName(node, targetNS); 256 257 } 258 259 public static String schema2NonQualifiedJavaName( 260 NodeList nl, 261 NodeList targetURI) { 262 263 String fullname = schema2JavaName(nl, targetURI); 264 StringTokenizer st = new StringTokenizer (fullname, "."); 265 String name = null; 266 while (st.hasMoreTokens()) { 267 name = st.nextToken(); 268 } 269 270 return name; 271 } 272 273 public static String getJavaPathName( 274 String targetDirectoryName, 275 String packageName) { 276 if (packageName != null && !packageName.equals("")) { 277 targetDirectoryName += File.separatorChar 278 + packageName.replace('.', File.separatorChar); 279 } 280 281 return targetDirectoryName; 282 } 283 284 public static String getJavaFileName( 285 NodeList nl, 286 NodeList targetURI, 287 String javaFileSuffix) { 288 String javaFileName = 289 schema2NonQualifiedJavaName(nl, targetURI) + javaFileSuffix + ".java"; 290 291 return javaFileName; 292 } 293 294 public static boolean JDKcompile(String fileName, String workingDirectory) 295 throws IllegalArgumentException { 296 String classPath = System.getProperty("java.class.path"); 297 298 if (workingDirectory != null && !workingDirectory.equals("")) { 299 classPath += System.getProperty("path.separator") + workingDirectory; 300 } 301 302 String args[] = { "-classpath", classPath, fileName }; 303 304 try { 305 return new sun.tools.javac.Main(System.err, "javac").compile(args); 306 } catch (Throwable th) { 307 System.err.println("Unable to load JDK compiler."); 308 309 return false; 310 } 311 } 312 313 public static void setVerbose(boolean ver) { 314 verbose = ver; 315 } 316 317 public static boolean getVerbose() { 318 return verbose; 319 } 320 } | Popular Tags |