private void readRecursive(final Node target, final org.w3c.dom.Node source) { readAttributes(target, source); final NodeList sourceChildren = source.getChildNodes(); if (sourceChildren != null) { for (int i = 0; i < sourceChildren.getLength(); i++) { final org.w3c.dom.Node child = sourceChildren.item(i); if (child.getNodeType() != org.w3c.dom.Node.TEXT_NODE) { // Create our representation of the Node final Node newTarget = target.createChild(child.getNodeName()); if (onlyTextChildren(child)) { // See if we're dealing with a comment and mark specifically if (child.getNodeType() == org.w3c.dom.Node.COMMENT_NODE) { newTarget.setComment(true); } // Set text newTarget.text(child.getTextContent()); readAttributes(newTarget, child); } else { readRecursive(newTarget, child); } } else { target.text(child.getTextContent()); } } } }
private void readRecursive(final Node target, final org.w3c.dom.Node source) { readAttributes(target, source); final NodeList sourceChildren = source.getChildNodes(); if (sourceChildren != null) { for (int i = 0; i < sourceChildren.getLength(); i++) { final org.w3c.dom.Node child = sourceChildren.item(i); if (child.getNodeType() != org.w3c.dom.Node.TEXT_NODE) { // Create our representation of the Node final Node newTarget = target.createChild(child.getNodeName()); if (onlyTextChildren(child)) { // See if we're dealing with a comment and mark specifically if (child.getNodeType() == org.w3c.dom.Node.COMMENT_NODE) { newTarget.setComment(true); } // Set text newTarget.text(child.getTextContent()); readAttributes(newTarget, child); } else { readRecursive(newTarget, child); } } else { target.text(child.getTextContent()); } } } }
import japa.parser.JavaParser; import japa.parser.ParseException; import japa.parser.ast.CompilationUnit; import japa.parser.ast.Node; import java.io.File; import java.io.IOException; public class RemoveAllComments { static void removeComments(Node node) { for (Node child : node.getChildrenNodes()) { child.setComment(null); removeComments(child); } } public static void main(String[] args) throws ParseException, IOException { File sourceFile = new File("Test.java"); CompilationUnit cu = JavaParser.parse(sourceFile); removeComments(cu); System.out.println(cu.toString()); } }