A document holds paragraphs, headings and images. Three separate reports run over the same document: one counts words, one exports HTML, one exports plain text. Each report treats each element type differently.
The document nodes and DocumentExporter are already provided. Implement only these visitor components:
DocVisitor declares one visit method for each node type.WordCountVisitor counts words in paragraphs and headings while ignoring images.HtmlExportVisitor produces the HTML line for each node type.PlainTextVisitor produces the plain text line for each node type.
The provided DocumentExporter exposes this behavior:
DocumentExporter() creates an empty document.boolean addParagraph(String text) appends a paragraph and returns true. It returns false once 20 elements are stored.boolean addHeading(String text, int level) appends a heading and returns true. A level below 1 or above 6 changes nothing and returns false.boolean addImage(String url, String altText) appends an image and returns true.int elementCount() returns how many elements are stored.int wordCount() returns the total number of words in the document.String[] exportHtml() returns one HTML line per element, in insertion order.String[] exportPlainText() returns one plain text line per element, in insertion order.
A word is a run of characters with no spaces in it. Paragraph text and heading text contribute words. An image contributes none.
The HTML lines are <p>text</p> for a paragraph, <h3>text</h3> for a heading at level 3, and <img src="url" alt="altText" /> for an image.
The plain text lines are the paragraph text unchanged, the heading text in capitals, and the image alt text wrapped in square brackets.
An empty document counts zero words and exports no lines.
DocumentExporter is the only type the tests call. Complete the visitor interface and visitor classes so each pre-implemented element can hand itself to the report rather than being examined by it.
Example 1:
Input:
Output:
Explanation: The document holds a heading, a paragraph and an image. The word count reads the heading and the paragraph and skips the image, so 2 words plus 8 words gives 10. The HTML export walks the same three elements and produces a different line shape for each one.
Example 2:
Input:
Output:
Explanation: The plain text export reads the same three element types and answers differently from the HTML export. The heading comes back in capitals, the paragraph comes back unchanged, and the image comes back as its alt text in square brackets.
Constraints
0 <= text.length <= 100- Text, urls and alt text contain letters, digits, spaces and punctuation only.
- At most
20 elements are stored in one document. - At most
100 calls in total are made across all methods.