Consider the task of translating a plain text book from Project Gutenberg (http://gutenberg.org) to HTML. For example, here is the start of the first chapter of Tolstoy's Anna Karenina:

Chapter 1

Happy families are all alike; every unhappy family is unhappy in
its own way.

Everything was in confusion in the Oblonskys' house. The wife
had discovered that the husband was carrying on an intrigue with
a French girl, who had been a governess in their family, and she
had announced to her husband that she could not go on living in
the same house with him ...

The equivalent HTML is:

<h1>Chapter 1</h1>
<p>Happy families are all alike; every unhappy family is unhappy in
its own way.</p>
<p>Everything was in confusion in the Oblonskys' house. The wife
had discovered that the husband was carrying on an intrigue with
a French girl, who had been a governess in their family, and she
had announced to her husband that she could not go on living in
the same house with him ...</p>

The HTML conversion can be carried out in two steps. First, the plain text is assembled into segments, blocks of text of the same kind (heading, paragraph, and so on). Then each segment is converted, by surrounding it with the HTML tags and converting special characters.

.

Fetching the text from the Internet and breaking it into segments is a challenging task. Provide an interface and a mock implementation. Combine it with a class that uses the mock implementation to finish the formatting task.

Complete the following files:

MockText.java

/** Produce a document with three sections of type h1, p, and p whose contents is given in the problem statement. */ public class MockText ... { ... }

TextDemo.java

public class TextDemo { public static void main(String[] args) { IText text = new MockText(); while (...) { String type = ...; System.out.println("<" + type + ">" + cleanup(...) + "</" + type + ">"); } } public static String cleanup(String text) { String result = text.replace("&", "&amp;"); result = result.replace("``", "&ldquo;"); result = result.replace("''", "&ldquo;"); result = result.replace("`", "&lsquo;"); result = result.replace("'", "&rsquo;"); result = result.replace("--", "&mdash;"); result = result.replace("<", "&lt;"); result = result.replace(">", "&gt;"); return result; } }

Use the following file:

IText.java

public interface IText
{
   /**
      Checks whether there is another segment
      @return true if there is another segment
   */
   boolean hasMoreSegments();

   /**
      Gets the type of the next segment
      @return the type such as p or h1
   */
   String nextSegmentType();

   /**
      Reads the text of the next segment
      @return the text of the next segment
   */
   String nextSegment();
}