X359: ArrayBasedStack markup validation

Write the validateMarkup() standalone method to simulate the behavior of real-world tools used to confirm the validity of web pages.

HTML is the standard markup language for creating web pages. Creating a web page involves using HTML tags and elements to define and organize the structure and composition of the page so as to produce a valid or well-formed document.

The markup for a webpage is considered valid or well-formed if it adheres to the syntax rules defined for the markup language.

The validateMarkup() method receives a String array of tags representing the HTML markup for a web page. When invoked the method parses the String array to determine if the HTML markup is valid markup. If the document is valid the method must return the boolean value true, if not valid the method must return false. For the purpose of this exercise you are to focus on the rule that every start (open) tag must have a matching end (or closing) tag.

The method will use a stack and helper methods to determine validity. Your method must parse each item in the tag array. When parsing an individual tag your method may use the openTag and closingTag methods to determine the type of tag being parsed. The openTag method returns true when the tag passed to the method is a start or open tag, false otherwise. The closingTag method returns true when the tag passed to the method is an end or closing tag, false otherwise. Further specifics regarding the implementation of each method has been provided below.

When a start (open) tag is encountered your method must push the tag on to the stack. When an end (close) tag is found the validateMarkup() method must pop the stack then check the result popped against the end (close) tag to determine if they match. The matchedTags() method returns true if they match, false otherwise. If the tags match then the pair will be considered as valid, if they do not then the pair will be considered as invalid and the validateMarkup() should return false. The method will continue to parse the remaining tags in the tags array until all tags in the array have been processed, returning true if no tag pairs were found to be invalid.

public boolean openTag(String tag){ 
    String openTag = "<([a-zA-Z_0-9])+>"; 
    Pattern p = Pattern.compile(openTag); 
    Matcher m = p.matcher(tag); 
    return m.matches(); 
}

public boolean closingTag(String tag){
    String openTag = "<\/([a-zA-Z_0-9])+>"; 
    Pattern p = Pattern.compile(openTag); 
    Matcher m = p.matcher(tag); 
    return m.matches(); 
}

public boolean matchedTags(String open, String closed) { 
    if (open.substring(1).equals(closed.substring(2))) { 
            return true; 
    } 
    return false; 
}

n

A sample web page has been provided below, end (closing) tags are those beginning with </, for example '' is the start (open) tag (signaling the start or root element of the HTML page) and '' is the matching closing tag (signaling the end of the HTML page):

 <html> 
    <title>Hi, I am a web page</title> 
    <body>This is the body</body>
 </html>

Examples:

Test validMarkup() on a simple valid web page.
Test validMarkup() on a simple invalid web page.
Reset

Practice a different Java exercise