1 package org.ejen.ext.parsers.java_1_2; 22 23 import org.ejen.util.XSLUtil; 24 import org.ejen.util.arl.ArlUtil; 25 import java.io.FileReader ; 26 import java.io.BufferedReader ; 27 import java.util.StringTokenizer ; 28 import java.util.Hashtable ; 29 import org.apache.xalan.extensions.ExpressionContext; 30 import org.apache.xml.utils.WrappedRuntimeException; 31 32 63 public class JavaSourceToXML implements JavaParserTreeConstants, JavaParserConstants { 64 65 private static Hashtable _nodesMapCache = new Hashtable (); 66 private static Hashtable _tokensMapCache = new Hashtable (); 67 68 private final static int[] DEFAULT_NODES_MAP = new int[jjtNodeName.length]; 69 private final static int[] DEFAULT_TOKENS_MAP = new int[tokenImage.length]; 70 static { 71 DEFAULT_NODES_MAP[JJTIMPORTDECLARATION] = ArlUtil.F_REMOVE; 72 DEFAULT_NODES_MAP[JJTTYPEDECLARATION] = ArlUtil.F_CROSS; 73 DEFAULT_NODES_MAP[JJTCLASSBODY] = ArlUtil.F_CROSS; 74 DEFAULT_NODES_MAP[JJTCLASSBODYDECLARATION] = ArlUtil.F_CROSS; 75 DEFAULT_NODES_MAP[JJTFIELDDECLARATION] = ArlUtil.F_REMOVE; 76 DEFAULT_NODES_MAP[JJTFORMALPARAMETERS] = ArlUtil.F_CROSS; 77 DEFAULT_NODES_MAP[JJTINITIALIZER] = ArlUtil.F_REMOVE; 78 DEFAULT_NODES_MAP[JJTBLOCK] = ArlUtil.F_REMOVE; 79 for (int i = 0; i <= SINGLE_LINE_COMMENT; i++) { 80 DEFAULT_TOKENS_MAP[i] = ArlUtil.F_REMOVE; 81 } 82 DEFAULT_TOKENS_MAP[MULTI_LINE_COMMENT] = ArlUtil.F_REMOVE; 83 DEFAULT_TOKENS_MAP[PACKAGE] = ArlUtil.F_REMOVE; 84 DEFAULT_TOKENS_MAP[LPAREN] = ArlUtil.F_REMOVE; 85 DEFAULT_TOKENS_MAP[RPAREN] = ArlUtil.F_REMOVE; 86 DEFAULT_TOKENS_MAP[LBRACE] = ArlUtil.F_REMOVE; 87 DEFAULT_TOKENS_MAP[RBRACE] = ArlUtil.F_REMOVE; 88 DEFAULT_TOKENS_MAP[SEMICOLON] = ArlUtil.F_REMOVE; 89 DEFAULT_TOKENS_MAP[COMMA] = ArlUtil.F_REMOVE; 90 DEFAULT_TOKENS_MAP[THROWS] = ArlUtil.F_REMOVE; 91 DEFAULT_TOKENS_MAP[IMPLEMENTS] = ArlUtil.F_REMOVE; 92 DEFAULT_TOKENS_MAP[EXTENDS] = ArlUtil.F_REMOVE; 93 _nodesMapCache.put("default", DEFAULT_NODES_MAP); 94 _tokensMapCache.put("default", DEFAULT_TOKENS_MAP); 95 } 96 97 100 protected JavaSourceToXML() {} 101 102 122 public static org.apache.xpath.NodeSet process(ExpressionContext context, String fileName) { 123 return process(XSLUtil.getContextDocument(context), 124 XSLUtil.evaluate(context, fileName), null, null, false); 125 } 126 127 148 public static org.apache.xpath.NodeSet process(ExpressionContext context, 149 String fileName, 150 boolean tokensPos) { 151 return process(XSLUtil.getContextDocument(context), 152 XSLUtil.evaluate(context, fileName), null, null, tokensPos); 153 } 154 155 185 public static org.apache.xpath.NodeSet process(ExpressionContext context, 186 String fileName, 187 String nodesArl, 188 String tokensArl) { 189 return process(XSLUtil.getContextDocument(context), 190 XSLUtil.evaluate(context, fileName), 191 XSLUtil.evaluate(context, nodesArl), 192 XSLUtil.evaluate(context, tokensArl), false); 193 } 194 195 226 public static org.apache.xpath.NodeSet process(ExpressionContext context, 227 String fileName, 228 String nodesArl, 229 String tokensArl, 230 boolean tokensPos) { 231 return process(XSLUtil.getContextDocument(context), 232 XSLUtil.evaluate(context, fileName), 233 XSLUtil.evaluate(context, nodesArl), 234 XSLUtil.evaluate(context, tokensArl), tokensPos); 235 } 236 237 248 protected static org.apache.xpath.NodeSet process(org.w3c.dom.Document doc, 249 String fileName, 250 String nodesArl, 251 String tokensArl, 252 boolean tokensPos) { 253 BufferedReader br = null; 254 255 try { 256 br = new BufferedReader (new FileReader (fileName)); 257 if (JavaParser.token_source == null) { 258 new JavaParser(br); 259 } else { 260 JavaParser.ReInit(br); 261 } 262 SimpleNode sn = JavaParser.CompilationUnit(); 263 org.w3c.dom.Node root = doc.createElement("unused"); 264 265 sn.toNode(doc, root, 266 getMap(nodesArl, DEFAULT_NODES_MAP, _nodesMapCache), 267 getMap(tokensArl, DEFAULT_TOKENS_MAP, _tokensMapCache), 268 tokensPos); 269 return new org.apache.xpath.NodeSet(root.getChildNodes()); 270 } catch (Exception e) { 271 throw new WrappedRuntimeException(e); 272 } 273 finally { 274 if (br != null) { 275 try { 276 br.close(); 277 } catch (Exception e) {} 278 finally { 279 br = null; 280 } 281 } 282 } 283 } 284 285 295 protected static int[] getMap(String arl, int[] defaultMap, Hashtable cache) { 296 if (arl == null) { 297 return defaultMap; 298 } 299 int[] map = (int[]) (cache.get(arl)); 300 301 if (map == null) { 302 map = ArlUtil.process(arl, new int[defaultMap.length]); 303 cache.put(arl, map); 304 } 305 return map; 306 } 307 308 352 public static org.apache.xpath.NodeSet parseJavadoc(ExpressionContext context, String comment) { 353 org.w3c.dom.Document doc = XSLUtil.getContextDocument(context); 354 355 try { 356 org.apache.xpath.NodeSet ns = new org.apache.xpath.NodeSet(); 357 StringTokenizer sTok = new StringTokenizer (comment, "\n\r"); 358 359 while (sTok.hasMoreTokens()) { 360 String line = sTok.nextToken(); 361 boolean skip = false; 362 363 if (line.startsWith("/**")) { 364 line = line.substring(3); 365 skip = true; 366 } 367 if (line.endsWith("*/")) { 368 line = line.substring(0, line.length() - 2); 369 } 370 if (!skip) { 371 int iStar = -1; 372 boolean done = false; 373 374 for (int i = 0; !done && i < line.length(); i++) { 375 switch (line.charAt(i)) { 376 case ' ': 377 case '\t': 378 break; 379 380 case '*': 381 iStar = i; 382 383 default: 384 done = true; 385 break; 386 } 387 } 388 if (iStar != -1) { 389 line = line.substring(iStar + 1); 390 } else { 391 line = ""; 392 } 393 } 394 if (line.length() > 0) { 395 org.w3c.dom.Element elt = doc.createElement("doc-line"); 396 397 elt.appendChild(doc.createCDATASection(line)); 398 ns.addElement(elt); 399 } 400 } 401 return ns; 402 } catch (org.w3c.dom.DOMException e) { 403 throw new WrappedRuntimeException(e); 404 } 405 } 406 } 407
| Popular Tags
|