Question: In this lab, you will write a program to validate HTML files using JAVA : You can use StackInterface and ArrayBoundedStack class for this program.

In this lab, you will write a program to validate HTML files using JAVA:

You can use StackInterface and ArrayBoundedStack class for this program. You will read from HTML file and output the complete and missing tags. For instance, a tag begins and ends like this: ...... In HTML, tags are element names enclosed in angle brackets . Most tags have start and end tags that enclose some document element(s) and possibly other tags. End tags have the same name as the corresponding start tag, preceded by a '/':

Arrays are objects, which means that when we declare an array, the name of the array is a reference

.

Start tags can contain attributes which provide additional information about the element, such as or

. Some tags do not surround any content. These "empty elements" have start tags but no end tags.

Restrictions for the Validator: there are multiple versions of html with different rules. We are going to simplify a bit for the purposes of this lab. We are going to use the following rules for valid html:

  • tags are case insensitive
  • tags must start and end within the same line
  • the following tags are empty, and so have no corresponding close tag: , , , , , ,
    , ,, , , , , , ,
  • all other tags must have a close tag
  • tags must be nested correctly, so

    . . .

    is valid but

    . . .

    is not.

REQUIREMENTS:

  1. Prompt for the name of the file to be validated. Create a Scanner for the file and pass it to your validator.
  2. Your validator should use a stack to check for correct nesting of tags. The algorithm is similar to the algorithm for checking for balanced parentheses in our textbook. Open tags are pushed onto the stack. Each close tag is compared with the top of the stack to check for a match.
  3. Don't forget that some tags have no close tag.
  4. Create a class for tags. This class should not have set or get methods. The Tag class should contain the list of tags with no end tag and a method to test whether a tag has no end tag. Your tag class should also have a method to test whether a pair of tags are matching start and end tags. You will probably need some additional methods in the Tag class.
  5. You need to extract the tags from the input file. Attributes are not stacked. My advice is to write a method similar to the "split" method in the String class. The method I'm thinking of will take a String containing a line of text read from the input file and return an array or ArrayList of the tags (modified to remove attributes) in that String. A good place for this method is to make it a static method in the Tag class.

The output could be like this:

  • each time a pair of tags is matched, print a message such as: "matched:

    and

    "
  • each time a tag with no end tag is processed, print a message such as "no match needed: "
  • each time a start tag has no matching end tag, print a message such as: "missing end tag:
  • "
  • each time there is no start tag to match an end tag, print a message such as "missing start tag: "
  • if there are no errors in the file, print a message saying that the file is valid; if there are any errors in the file, print a message saying that the file is invalid

Classes you can use:

public class ArrayBoundedStack implements StackInterface { protected final int DEFCAP = 100; // default capacity protected T[] elements; // holds stack elements protected int topIndex = -1; // index of top element in stack

public ArrayBoundedStack() { elements = (T[]) new Object[DEFCAP]; }

public ArrayBoundedStack(int maxSize) { elements = (T[]) new Object[maxSize]; }

public void push(T element) { if (isFull()) throw new StackOverflowException("Push attempted on a full stack."); else { topIndex++; elements[topIndex] = element; } }

public void pop() { if (isEmpty()) throw new StackUnderflowException("Pop attempted on an empty stack."); else { elements[topIndex] = null; topIndex--; } }

public T top() { T topOfStack = null; if (isEmpty()) throw new StackUnderflowException("Top attempted on an empty stack."); else topOfStack = elements[topIndex]; return topOfStack; }

public boolean isEmpty() { return (topIndex == -1); }

public boolean isFull() { return (topIndex == (elements.length - 1)); } }

public interface StackInterface { void push(T element) throws StackOverflowException;

void pop() throws StackUnderflowException;

T top() throws StackUnderflowException; boolean isEmpty();

boolean isFull(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!