Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with a Java problem please: Create a package called parseXML . Copy

ID: 3844049 • Letter: I

Question

I need help with a Java problem please:

Create a package called parseXML. Copy into it the reference class XMLToken provided for you below. Create in it a program class called ParseXML.

Your ParseXML program prompts the user for the name of a file and then redirects StdIn to that file, as in the previous assignment. It will print to standard output (using StdOut) each token on its own line. While doing this it will also use the algorithm appearing below to parse the file to see whether it is properly structured by the rules mentioned above. If not, it should throw one of three possible exceptions. You will find the exception classes provided for you below.

XMLToken.java:

package parseXML;

public class XMLToken {
   private String token;
     
   public XMLToken(String token)
   {
   this.token=token;
   }
   public boolean isTag()
   {
   if(token.length()>=3){
   if(token.charAt(0)=='<'&&token.charAt(token.length()-1)=='>')
   return true;;
   }
   return false;
   }
   public boolean isOpeningTag()
   {
   if(isTag())
   {
   if(token.charAt(1)!='/')
   return true;
   }
   return false;
   }
  
   public boolean isClosingTag()
   {
   if(isTag())
   {
   if(token.charAt(1)=='/')
   return true;
   }
   return false;
   }

   public String getTagName()
   {
   if(isTag())
   {
   if(isClosingTag())
   return token.substring(2,token.length()-1);
   else
   return token.substring(1,token.length()-1);
   }
   return "";
   }
  

   }

Exception classes:

ParseXMLImproperlyNestedTagsException.java:

package parseXML;

public class ParseXMLImproperlyNestedTagsException extends RuntimeException {
   public ParseXMLImproperlyNestedTagsException(String message) {
       super(message);
   }
}

ParseXMLMissingClosingTagException.java:

package parseXML;

public class ParseXMLMissingClosingTagException extends RuntimeException {
   public ParseXMLMissingClosingTagException(String message) {
       super(message);
   }
}

ParseXMLMissingOpeningTagException.java:

package parseXML;

public class ParseXMLMissingOpeningTagException extends RuntimeException {

   public ParseXMLMissingOpeningTagException(String message) {
       super(message);
   }

}

Specifications Syntactically correct XML A properly formed XML document is one where: Every token is either a tag or a word. The syntax of a tag is described in a previous assignment. Anything that is not a tag is a word. Every opening tag is followed at some point by a closing tag with the same tag name. Opening and closing tags are properly nested. For example, assume the following tags appear in the document:

Explanation / Answer

Here is link for complete project (Using Netbeans 8.0.1 or you can use jGrasp or simply extract zip and run ParseXML.java from command line)

https://drive.google.com/open?id=0By9p2YlwSGU3c0ExZTVNTmpyT0U

You can change the xml data from text file( Included in project data.txt) as below to get diffrent exception output:

For file input as following:

<chapter>
<para>
</para>
</chapter>
<chapter>

Ouput: Improperly Exception

For file input as following:

<chapter>
</para>

Ouput: Missing closing tag exception

For file input as following:

</chapter>
<chapter>

Ouput: Missing opening tag exception

Here is Main Class sample code:

package parsexml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class ParseXML {
  
public static void main(String[] args) {
// TODO code application logic here

ArrayList<String> lsTags = new ArrayList<>();     //used as stack for tag name
  
try {

//As your previous assignment for stdin or stdout is not available I read this file using BufferedReader you can replace with your code

File f = new File("src/parsexml/data.txt");

BufferedReader b = new BufferedReader(new FileReader(f));

String readLine = "";

//This is as per algorithm you provided
  
while ((readLine = b.readLine()) != null) {
  
  
XMLToken token = new XMLToken(readLine);
  
  
if(token.isTag())
{   
if(token.isOpeningTag())
{
String tagName = token.getTagName();
System.out.println(tagName);
lsTags.add(tagName);
  
}
else if(token.isClosingTag())
{
System.out.println(readLine);
  
  
if(lsTags.isEmpty())
throw new ParseXMLMissingOpeningTagException("Found a closing tag with no mathcing opening tag");
  
String lastTagName = lsTags.get(lsTags.size()-1);
lsTags.remove(lsTags.size()-1);
  
String tagName = token.getTagName();
if(!tagName.equals(lastTagName))
throw new ParseXMLMissingClosingTagException("Found a closing tag that does not match its opening tag");
  
}
else
{
System.out.println(readLine);
}
  
}

}

} catch (IOException e) {
e.printStackTrace();
}

if(!lsTags.isEmpty())
throw new ParseXMLImproperlyNestedTagsException("there are opening tags with no with no mathcing closing tags");
else
System.out.println("Erything is good");
  
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote