DumpFacade.java
001 package net.sourceforge.pmd.lang.jsp.ast;
002 
003 import java.io.IOException;
004 import java.io.PrintWriter;
005 import java.io.Writer;
006 import java.util.ArrayList;
007 import java.util.List;
008 
009 import net.sourceforge.pmd.lang.ast.Node;
010 
011 public class DumpFacade extends JspParserVisitorAdapter {
012 
013     private PrintWriter writer;
014     private boolean recurse;
015 
016     public void initializeWith(Writer writer, String prefix, boolean recurse, JspNode node) {
017   this.writer = (writer instanceof PrintWriter(PrintWriterwriter : new PrintWriter(writer);
018   this.recurse = recurse;
019   this.visit(node, prefix);
020   try {
021       writer.flush();
022   catch (IOException e) {
023       throw new RuntimeException("Problem flushing PrintWriter.", e);
024   }
025     }
026 
027     @Override
028     public Object visit(JspNode node, Object data) {
029   dump(node, (Stringdata);
030   if (recurse) {
031       return super.visit(node, data + " ");
032   else {
033       return data;
034   }
035     }
036 
037     private void dump(Node node, String prefix) {
038   //
039   // Dump format is generally composed of the following items...
040   //
041 
042   // 1) Dump prefix
043   writer.print(prefix);
044 
045   // 2) JJT Name of the Node
046   writer.print(node.toString());
047 
048   //
049   // If there are any additional details, then:
050   // 1) A colon
051   // 2) The Node.getImage() if it is non-empty
052   // 3) Extras in parentheses
053   //
054 
055   // Standard image handling
056   String image = node.getImage();
057 
058   // Extras
059   List<String> extras = new ArrayList<String>();
060 
061   // Other extras
062   if (node instanceof ASTAttribute) {
063       extras.add("name=[" ((ASTAttributenode).getName() "]");
064   else if (node instanceof ASTDeclaration) {
065       extras.add("name=[" ((ASTDeclarationnode).getName() "]");
066   else if (node instanceof ASTDoctypeDeclaration) {
067       extras.add("name=[" ((ASTDoctypeDeclarationnode).getName() "]");
068   else if (node instanceof ASTDoctypeExternalId) {
069       extras.add("uri=[" ((ASTDoctypeExternalIdnode).getUri() "]");
070       if (((ASTDoctypeExternalIdnode).getPublicId().length() 0) {
071     extras.add("publicId=[" ((ASTDoctypeExternalIdnode).getPublicId() "]");
072       }
073   else if (node instanceof ASTElement) {
074       extras.add("name=[" ((ASTElementnode).getName() "]");
075       if (((ASTElementnode).isEmpty()) {
076     extras.add("empty");
077       }
078   else if (node instanceof ASTJspDirective) {
079       extras.add("name=[" ((ASTJspDirectivenode).getName() "]");
080   else if (node instanceof ASTJspDirectiveAttribute) {
081       extras.add("name=[" ((ASTJspDirectiveAttributenode).getName() "]");
082       extras.add("value=[" ((ASTJspDirectiveAttributenode).getValue() "]");
083   }
084 
085   // Output image and extras
086   if (image != null || !extras.isEmpty()) {
087       writer.print(":");
088       if (image != null) {
089     writer.print(image);
090       }
091       for (String extra : extras) {
092     writer.print("(");
093     writer.print(extra);
094     writer.print(")");
095       }
096   }
097 
098   writer.println();
099     }
100 }