My goal in the method is to write a simple clustering program that determines if a clustering document is valid based on these rules:
- All clusterings start with
<
and end with>
. Every start clustering<something>
must be followed (eventually) by an end clustering</something>
. - Clusterings must be properly nested as follows
<a><b>data</b></a>
. This is not allowed:.<a><b>data</a></b>
- The first clustering of the document is the root clustering. The document can have only one root clustering (and corresponding end clustering). Everything else must be enclosed inside the root's start and end clusterings. In the example above,
<songdata>
is the root. - All clusterings are case-sensitive. This means
<song>
is not the same as<Song>
, which is not the same as<SONG>
, etc. Start and end clusterings do not have to occur on the same line, but each clustering can not be broken between two lines. So you can't do this:
<dura tion>
The clustering name can contain only letters.
- Everything that is not a clustering is considered document data.
- Extra white space is ignored. Thus,
<a> <b>data</b> </a>
is the same as<a><b>data</b></a>
.
This is the code that I'm using to accomplish my task:
data=in.readLine();
++line;
nextDataValue=data.trim();//removes whitespace from both ends
if(nextDataValue.charAt(0)!='<') error("No enclosing root pillow.", line);//Everything else must be enclosed inside the root's start and end pillows.
StringTokenizer tokenizer1=new StringTokenizer(nextDataValue, ">");
while(tokenizer1.hasMoreTokens())
{
nextDataValue=tokenizer1.nextToken();//extract each data element of the string
if (nextDataValue.indexOf('<') == -1) break;// no < in line, so get next
pillow=nextDataValue.substring(nextDataValue.indexOf('<')+1, nextDataValue.length());// pillow=get < to > exclusive in nextDataValue
if(pillow.charAt(0)!='/')
{
stack.push(pillow);//start
start_pillow_pushed=true;
}
else if(pillow.charAt(0)=='/')//end
{
start_pillow_pushed=false;
if(!stack.isEmpty())
{
if(!nextDataValue.equals(stack.pop())) error("Mismatched start pillow "+nextDataValue+">", line);// For each pillow you find, pop the stack to see if the matching start pillow is there.
}
else error("Missing start pillow "+nextDataValue+">", line);//empty stack
}
}
for(; (data=in.readLine())!=null;)
{
++line;
nextDataValue=data.trim();//removes whitespace from both ends
if(nextDataValue.charAt(0)!='<') error("No enclosing root pillow.", line);//Everything else must be enclosed inside the root's start and end pillows.
StringTokenizer tokenizer2=new StringTokenizer(nextDataValue, ">");
while(tokenizer2.hasMoreTokens())
{
nextDataValue.trim();
nextDataValue=tokenizer2.nextToken();//extract each data element of the string
if (nextDataValue.indexOf('<') == -1) break;// no < in line, so get next
pillow=nextDataValue.substring(nextDataValue.indexOf('<')+1, nextDataValue.length());// pillow=get < to > exclusive in nextDataValue
if(pillow.charAt(0)!='/')
{
stack.push(pillow);//start
start_pillow_pushed=true;
}
else if(pillow.charAt(0)=='/')//end
{
start_pillow_pushed=false;
if(!stack.isEmpty())
{
if(!nextDataValue.equals(stack.pop())) error("Mismatched start pillow "+nextDataValue+">", line);// For each pillow you find, pop the stack to see if the matching start pillow is there.
}
else error("Missing start pillow "+nextDataValue+">", line);//empty stack
}
}
}
if(stack.isEmpty()) System.out.println("Input clustering document is valid.");//reports the document is valid based on the clustering rules above
else error("Missing end pillow for "+nextDataValue+">", line);
Please indicate what can be improved in this.