cleanup warnings & formatting, pull out utility method
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7483 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
1 parent
d18f456864
commit
9e23e6fbbe
4 files changed
+352
-354
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -10,91 +10,91 @@ import net.sourceforge.pmd.lang.ast.Node;
|
||||
|
||||
public class DumpFacade extends JspParserVisitorAdapter {
|
||||
|
||||
private PrintWriter writer;
|
||||
private boolean recurse;
|
||||
private PrintWriter writer;
|
||||
private boolean recurse;
|
||||
|
||||
public void initializeWith(Writer writer, String prefix, boolean recurse, JspNode node) {
|
||||
this.writer = (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer);
|
||||
this.recurse = recurse;
|
||||
this.visit(node, prefix);
|
||||
try {
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Problem flushing PrintWriter.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(JspNode node, Object data) {
|
||||
dump(node, (String) data);
|
||||
if (recurse) {
|
||||
return super.visit(node, data + " ");
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
private void dump(Node node, String prefix) {
|
||||
//
|
||||
// Dump format is generally composed of the following items...
|
||||
//
|
||||
|
||||
// 1) Dump prefix
|
||||
writer.print(prefix);
|
||||
|
||||
// 2) JJT Name of the Node
|
||||
writer.print(node.toString());
|
||||
|
||||
//
|
||||
// If there are any additional details, then:
|
||||
// 1) A colon
|
||||
// 2) The Node.getImage() if it is non-empty
|
||||
// 3) Extras in parentheses
|
||||
//
|
||||
|
||||
// Standard image handling
|
||||
String image = node.getImage();
|
||||
|
||||
// Extras
|
||||
List<String> extras = new ArrayList<String>();
|
||||
|
||||
// Other extras
|
||||
if (node instanceof ASTAttribute) {
|
||||
extras.add("name=[" + ((ASTAttribute) node).getName() + "]");
|
||||
} else if (node instanceof ASTDeclaration) {
|
||||
extras.add("name=[" + ((ASTDeclaration) node).getName() + "]");
|
||||
} else if (node instanceof ASTDoctypeDeclaration) {
|
||||
extras.add("name=[" + ((ASTDoctypeDeclaration) node).getName() + "]");
|
||||
} else if (node instanceof ASTDoctypeExternalId) {
|
||||
extras.add("uri=[" + ((ASTDoctypeExternalId) node).getUri() + "]");
|
||||
if (((ASTDoctypeExternalId) node).getPublicId().length() > 0) {
|
||||
extras.add("publicId=[" + ((ASTDoctypeExternalId) node).getPublicId() + "]");
|
||||
}
|
||||
} else if (node instanceof ASTElement) {
|
||||
extras.add("name=[" + ((ASTElement) node).getName() + "]");
|
||||
if (((ASTElement) node).isEmpty()) {
|
||||
extras.add("empty");
|
||||
}
|
||||
} else if (node instanceof ASTJspDirective) {
|
||||
extras.add("name=[" + ((ASTJspDirective) node).getName() + "]");
|
||||
} else if (node instanceof ASTJspDirectiveAttribute) {
|
||||
extras.add("name=[" + ((ASTJspDirectiveAttribute) node).getName() + "]");
|
||||
extras.add("value=[" + ((ASTJspDirectiveAttribute) node).getValue() + "]");
|
||||
public void initializeWith(Writer writer, String prefix, boolean recurse, JspNode node) {
|
||||
this.writer = (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer);
|
||||
this.recurse = recurse;
|
||||
this.visit(node, prefix);
|
||||
try {
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Problem flushing PrintWriter.", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Output image and extras
|
||||
if (image != null || !extras.isEmpty()) {
|
||||
writer.print(":");
|
||||
if (image != null) {
|
||||
writer.print(image);
|
||||
}
|
||||
for (String extra : extras) {
|
||||
writer.print("(");
|
||||
writer.print(extra);
|
||||
writer.print(")");
|
||||
}
|
||||
@Override
|
||||
public Object visit(JspNode node, Object data) {
|
||||
dump(node, (String) data);
|
||||
if (recurse) {
|
||||
return super.visit(node, data + " ");
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
writer.println();
|
||||
}
|
||||
private void dump(Node node, String prefix) {
|
||||
//
|
||||
// Dump format is generally composed of the following items...
|
||||
//
|
||||
|
||||
// 1) Dump prefix
|
||||
writer.print(prefix);
|
||||
|
||||
// 2) JJT Name of the Node
|
||||
writer.print(node.toString());
|
||||
|
||||
//
|
||||
// If there are any additional details, then:
|
||||
// 1) A colon
|
||||
// 2) The Node.getImage() if it is non-empty
|
||||
// 3) Extras in parentheses
|
||||
//
|
||||
|
||||
// Standard image handling
|
||||
String image = node.getImage();
|
||||
|
||||
// Extras
|
||||
List<String> extras = new ArrayList<String>();
|
||||
|
||||
// Other extras
|
||||
if (node instanceof ASTAttribute) {
|
||||
extras.add("name=[" + ((ASTAttribute) node).getName() + "]");
|
||||
} else if (node instanceof ASTDeclaration) {
|
||||
extras.add("name=[" + ((ASTDeclaration) node).getName() + "]");
|
||||
} else if (node instanceof ASTDoctypeDeclaration) {
|
||||
extras.add("name=[" + ((ASTDoctypeDeclaration) node).getName() + "]");
|
||||
} else if (node instanceof ASTDoctypeExternalId) {
|
||||
extras.add("uri=[" + ((ASTDoctypeExternalId) node).getUri() + "]");
|
||||
if (((ASTDoctypeExternalId) node).getPublicId().length() > 0) {
|
||||
extras.add("publicId=[" + ((ASTDoctypeExternalId) node).getPublicId() + "]");
|
||||
}
|
||||
} else if (node instanceof ASTElement) {
|
||||
extras.add("name=[" + ((ASTElement) node).getName() + "]");
|
||||
if (((ASTElement) node).isEmpty()) {
|
||||
extras.add("empty");
|
||||
}
|
||||
} else if (node instanceof ASTJspDirective) {
|
||||
extras.add("name=[" + ((ASTJspDirective) node).getName() + "]");
|
||||
} else if (node instanceof ASTJspDirectiveAttribute) {
|
||||
extras.add("name=[" + ((ASTJspDirectiveAttribute) node).getName() + "]");
|
||||
extras.add("value=[" + ((ASTJspDirectiveAttribute) node).getValue() + "]");
|
||||
}
|
||||
|
||||
// Output image and extras
|
||||
if (image != null || !extras.isEmpty()) {
|
||||
writer.print(':');
|
||||
if (image != null) {
|
||||
writer.print(image);
|
||||
}
|
||||
for (String extra : extras) {
|
||||
writer.print('(');
|
||||
writer.print(extra);
|
||||
writer.print(')');
|
||||
}
|
||||
}
|
||||
|
||||
writer.println();
|
||||
}
|
||||
}
|
||||
@@ -8,93 +8,82 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.xpath.Attribute;
|
||||
import net.sourceforge.pmd.util.StringUtil;
|
||||
|
||||
public class DumpFacade {
|
||||
|
||||
private PrintWriter writer;
|
||||
private boolean recurse;
|
||||
private PrintWriter writer;
|
||||
private boolean recurse;
|
||||
|
||||
public void initializeWith(Writer writer, String prefix, boolean recurse, XmlNode node) {
|
||||
this.writer = (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer);
|
||||
this.recurse = recurse;
|
||||
this.dump(node, prefix);
|
||||
try {
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Problem flushing PrintWriter.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Object visit(XmlNode node, Object data) {
|
||||
dump(node, (String) data);
|
||||
if (recurse) {
|
||||
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
|
||||
visit((XmlNode) node.jjtGetChild(i), data + " ");
|
||||
}
|
||||
return data;
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
private void dump(XmlNode node, String prefix) {
|
||||
//
|
||||
// Dump format is generally composed of the following items...
|
||||
//
|
||||
|
||||
// 1) Dump prefix
|
||||
writer.print(prefix);
|
||||
|
||||
// 2) JJT Name of the Node
|
||||
writer.print(node.toString());
|
||||
|
||||
//
|
||||
// If there are any additional details, then:
|
||||
// 1) A colon
|
||||
// 2) The Node.getImage() if it is non-empty
|
||||
// 3) Extras in parentheses
|
||||
//
|
||||
|
||||
// Standard image handling
|
||||
String image = node.getImage();
|
||||
|
||||
// Special image handling (e.g. Nodes with normally null images)
|
||||
|
||||
image = escape(image);
|
||||
|
||||
// Extras
|
||||
List<String> extras = new ArrayList<String>();
|
||||
Iterator<Attribute> iterator = node.getAttributeIterator();
|
||||
while (iterator.hasNext()) {
|
||||
Attribute attribute = iterator.next();
|
||||
extras.add(attribute.getName() + "=" + escape(attribute.getValue()));
|
||||
public void initializeWith(Writer writer, String prefix, boolean recurse, XmlNode node) {
|
||||
this.writer = (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer);
|
||||
this.recurse = recurse;
|
||||
this.dump(node, prefix);
|
||||
try {
|
||||
writer.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Problem flushing PrintWriter.", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Output image and extras
|
||||
if (image != null || !extras.isEmpty()) {
|
||||
writer.print(":");
|
||||
if (image != null) {
|
||||
writer.print(image);
|
||||
}
|
||||
for (String extra : extras) {
|
||||
writer.print("(");
|
||||
writer.print(extra);
|
||||
writer.print(")");
|
||||
}
|
||||
public Object visit(XmlNode node, Object data) {
|
||||
dump(node, (String) data);
|
||||
if (recurse) {
|
||||
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
|
||||
visit((XmlNode) node.jjtGetChild(i), data + " ");
|
||||
}
|
||||
return data;
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
writer.println();
|
||||
}
|
||||
private void dump(XmlNode node, String prefix) {
|
||||
//
|
||||
// Dump format is generally composed of the following items...
|
||||
//
|
||||
|
||||
private static String escape(Object o) {
|
||||
// Replace some whitespace characters so they are visually apparent.
|
||||
if (o == null) {
|
||||
return null;
|
||||
// 1) Dump prefix
|
||||
writer.print(prefix);
|
||||
|
||||
// 2) JJT Name of the Node
|
||||
writer.print(node.toString());
|
||||
|
||||
//
|
||||
// If there are any additional details, then:
|
||||
// 1) A colon
|
||||
// 2) The Node.getImage() if it is non-empty
|
||||
// 3) Extras in parentheses
|
||||
//
|
||||
|
||||
// Standard image handling
|
||||
String image = node.getImage();
|
||||
|
||||
// Special image handling (e.g. Nodes with normally null images)
|
||||
|
||||
image = StringUtil.escapeWhitespace(image);
|
||||
|
||||
// Extras
|
||||
List<String> extras = new ArrayList<String>();
|
||||
Iterator<Attribute> iterator = node.getAttributeIterator();
|
||||
while (iterator.hasNext()) {
|
||||
Attribute attribute = iterator.next();
|
||||
extras.add(attribute.getName() + "=" + StringUtil.escapeWhitespace(attribute.getValue()));
|
||||
}
|
||||
|
||||
// Output image and extras
|
||||
if (image != null || !extras.isEmpty()) {
|
||||
writer.print(':');
|
||||
if (image != null) {
|
||||
writer.print(image);
|
||||
}
|
||||
for (String extra : extras) {
|
||||
writer.print('(');
|
||||
writer.print(extra);
|
||||
writer.print(')');
|
||||
}
|
||||
}
|
||||
|
||||
writer.println();
|
||||
}
|
||||
String s = String.valueOf(o);
|
||||
s = s.replace("\n", "\\n");
|
||||
s = s.replace("\r", "\\r");
|
||||
s = s.replace("\t", "\\t");
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -181,6 +181,24 @@ public final class StringUtil {
|
||||
appendXmlEscaped(buf, src, SUPPORTS_UTF8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace some whitespace characters so they are visually apparent.
|
||||
*
|
||||
* @param o
|
||||
* @return String
|
||||
*/
|
||||
public static String escapeWhitespace(Object o) {
|
||||
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
String s = String.valueOf(o);
|
||||
s = s.replace("\n", "\\n");
|
||||
s = s.replace("\r", "\\r");
|
||||
s = s.replace("\t", "\\t");
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string String
|
||||
|
||||
Reference in new issue
Block a user