Java Magazine, Sept/Oct 2018
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 72 design patterns public void visitTextNode TextNode textNode wordCount wordCount textNode getText @ Override public void visitImageNode ImageNode imageNode You might say theres nothing to do but lets try this if imageNode caption null visitTextNode imageNode caption Simplistic implementation of word counting private int wordCount String text Replace all nonspace chars with nothing add one because hello word has one space but it is two words return text trim replaceAll s length 1 This code is plugged into main in a similar fashion Visitor wordCountVisitor new WordCountVisitor for Node n nodes n accept wordCountVisitor System out printf The document has about d words n WordCountVisitor wordCountVisitor getWordCount
You must have JavaScript enabled to view digital editions.