CSE 143 Programming Assignment #3: HTML Validator solved

$24.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

This program focuses on using Stack and Queue collections. Turn in files named HtmlValidator.java,
mytest.html, and (optionally) HtmlValidatorTest.java from the Homework section of the course web site. You
will need HtmlTag.java and ValidatorMain.java from the web site; place them in the same folder as your program.
Though this assignment relates to web pages and HTML, you do not need to know how to write HTML to complete it.
Background Information About HTML:
Web pages are written in a language called Hypertext Markup Language, or HTML. An HTML file consists of text
surrounded by markings called tags. Tags give information to the text, such as formatting (bold, italic, etc.) or layout
(paragraph, table, list). Some tags specify comments or information about the document (header, title, document type).
A tag consists of a named element between less-than < and greater-than > symbols. For example, the tag for making text
bold uses the element b and is written as . Many tags apply to a range of text, in which case a pair of tags is used: an
opening tag indicating the start of the range and a closing tag indicating the end of the range. A closing tag has a / slash
after its < symbol, such as
. So to make some text bold on a page, one would put the text to be bold between opening
and closing b tags, like this. Tags can be nested to combine effects, bold italic.
Some tags, such as the br tag for inserting a line break or img for inserting an image, do not cover a range of text and are
considered to be “self-closing.” Self-closing tags do not need a closing tag; for a line break, only a tag of
is needed.
Some web developers write self-closing tags with an optional / before the >, such as
.
The distinction between a tag and an element can be confusing. A tag is a complete token surrounded by <> brackets,
which could be either an opening or closing tag, such as

My name is Marty Stepp. I teach at UW.

I have been a teacher here since 2003. Here is a picture of my cat:

In this assignment you will write a class that examines HTML to figure out whether it represents “valid” sequences of
tags. Your validator will use stacks and queues to figure out whether the tags match. Instructor-provided code will read
HTML pages from files and break them apart into tags for you; it’s your job to see whether the tags match correctly.
1 of 5
Implementation Details:
You will write a class named HtmlValidator. You must use Java’s Stack and Queue from java.util. Your class
must have the constructors/methods below. It must be possible to call the methods multiple times in any order and get the
correct results each time. Several methods interact with HtmlTag objects, described later. Unless otherwise specified,
you may not create auxiliary data structures (such as arrays, lists, stacks, queues) to help you solve any method below.
public HtmlValidator()
public HtmlValidator(Queue tags)
Your class should have two constructors. The first should initialize your validator to store an empty queue of HTML
tags. The second should initialize your validator to store a given queue of HTML tags. For example, the queue for the
page shown previously would contain the tags below. Further tags can be added later if the client calls addTag.
front [<!doctype>, ,,,,, ,
,,

, , ,

,

, ,

, , ] back
If the queue passed is null, you should throw an IllegalArgumentException. An empty queue (size 0) is allowed.
The constructors are allowed to construct a queue to store your validator’s tags if necessary.
public void addTag(HtmlTag tag)
In this method you should add the given tag to the end of your validator’s queue.
If the tag passed is null, you should throw an IllegalArgumentException.
public Queue getTags()
In this method you should return your validator’s queue of HTML tags. The queue should contain all tags that were
passed to the constructor (if any) in their proper order, plus/minus any tags added or removed using addTag or
removeAll. If any methods manipulate your queue, you must restore it to its prior state before the method is finished.
public void removeAll(String element)
In this method you should remove from your validator’s queue any tags that match the given element. For example, if
your validator is constructed using the tags from the page shown previously and removeAll(“p”)were called on it, your
queue would be modified to contain the following tags. Notice that all

and

tags have been removed:
front [<!doctype>, ,,,,, ,
,, , , ,,] back
If the element passed does not exactly match any tags (such as an empty string), your queue should not be modified.
You may not use any auxiliary collections such as extra stacks or queues, though you can create simple variables.
If the element passed is null, you should throw an IllegalArgumentException.
public void validate()
In this method you should print an indented text representation of the HTML tags in your queue. Display each tag on its
own line. Every opening tag that requires a closing tag increases the level of indentation of following tags by four spaces
until its closing tag is reached. The output for the HTML file on the first page would be: (continued on next page)
<!doctype>

 

2 of 5
To generate the output, analyze your queue of tags with a Stack. The basic idea of the algorithm is that when you see an
opening tag that is not self-closing, you should push it onto a stack and increase your indentation. When you see a closing
tag, you should pop the top element from the stack and decrease your indentation. You may use a single temporary
Stack (in addition to your validator’s own queue of tags) to help you compute the result. You may not use any other
collections, arrays, etc., though you can create as many simple variables as you like.
Error Handling:
Your validate method should print error messages if you encounter either of the following conditions in the HTML:
• A closing tag that does not match the most recently opened tag (or if there are no open tags at that point).
• Reaching the end of the HTML input with any tags still open that were not properly closed.
For example, the following HTML is valid:

bold text bold and italic text just bold again
more

But the following HTML is not valid, because the appears before the :

bold text bold and italic text just italic neither

The following HTML is also not valid, because thetag is never closed:bold italic normal textSuppose the previous short HTML file were modified to add several errors, as follows: an added unwanted </!doctype>tag, a deleted tag, an added second tag, and a deleted tag:<!doctype html public “-//W3C//DTD HTML 4.01 Transitional//EN”></!doctype>