// Return a vector of Tags, given a URL name
//This class will be used by the class KVMFile
import java.net.*;
import java.io.*;
import java.util.*;
//See the Java documentation for Vector
public class TagsVector extends Vector
{
TagsVector(URL context, String URLName) {
//The try{} catch{} blocks look for and deal with exceptions. Basically,
//if anything goes wrong in a try{} block, the catch{} block has a chance
//to fix it or print an error message
try {
URL url = new URL(context, URLName);
try {
//This opens a DataInputStream from the StickFigure file
DataInputStream stream = new DataInputStream(url.openStream());
try {
//While you can still readLine()s, parse() the read string
for (String line ; (line = stream.readLine()) != null ; )
parseLine(line);
} catch (Exception e) {System.out.println("reading: " + e);}
//Add anything that may be left in text after parseLine()
addItem(text);
} catch (Exception e) {System.out.println(e);}
} catch (MalformedURLException e) {System.out.println(e);}
}
//addItem() can add either a single character or a string
private void addItem(String item) { // ADD ITEM ONLY IF IT
for (int i = 0 ; i < item.length() ; i++) // CONTAINS NONSPACE
if (item.charAt(i) != ' ') { // CHARACTERS
addElement(item);
return;
}
}
//Click to read about Strings
private String text = "";
//Creates two variables: untagged and quoted
private boolean untagged = true, quoted;
private void parseLine(String line) {
for (int i = 0 ; i < line.length() ; i++)
// Start A Tag
if (line.charAt(i) == '<') {
//flush text
addItem(text);
text = "";
addItem("<");
untagged = false;
}
// Add To Untagged Text
else if (untagged)
text += line.substring(i, i+1);
// Parse Tagged Text
else {
// Skip Over Space or "="
for ( ; i < line.length() ; i++)
if (line.charAt(i) != ' ' && line.charAt(i) != '=')
break;
if (i < line.length())
// End The Tag (process inner ( ) first, then evaluate untagged)
if (untagged = (line.charAt(i) == '>'))
addItem(">");
// Get Next Tag Item
else {
//Advance past the quote
if (quoted = (line.charAt(i) == '"')) i++; //End of if statement
int j = i;
for ( ; j < line.length() ; j++) {
int c = line.charAt(j);
//Keep advancing untill one of these stopping points
if (c == '"' || (!quoted && (c==' ' || c=='=' || c=='>')))
break;
}
//add everything covered by the loop
addItem(line.substring(i, j));
//Translated: If (quoted = true) then (i = j) else (i = j-1)
i = quoted ? j : j-1;
}
}
}
}