Help.java
jmud-0.11/src/Help.java · 2.1 KB
From the archive jmud-0_11.tgz
Download the archive « Back to the archive contents
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Vector;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.FileReader;
import util.file.InvalidFileFormatException;
import misc.Separators;
class Help {
private Vector theHelp = null;
Help(String helpFileName)
throws InvalidFileFormatException, FileNotFoundException, IOException {
theHelp = new Vector();
BufferedReader helpFile = null;
helpFile = new BufferedReader(new FileReader(helpFileName));
for ( ; ; ) {
if (!helpFile.ready())
throw new InvalidFileFormatException();
String helpText = helpFile.readLine();
if (helpText.startsWith(Separators.EOF))
break;
if (!helpText.startsWith(Separators.BOR))
throw new InvalidFileFormatException();
helpText = helpText.substring(1);
for ( ; ; ) {
if (!helpFile.ready())
throw new InvalidFileFormatException();
String helpLine = helpFile.readLine();
if (helpLine.startsWith(Separators.BOR))
throw new InvalidFileFormatException();
if (helpLine.startsWith(Separators.EOR))
break;
helpText += Separators.NL + helpLine;
}
theHelp.addElement(helpText);
}
helpFile.close();
}
String findHelpByKey(String keyWord)
throws NoSuchElementException {
String helpText = "";
for (Enumeration help = theHelp.elements(); help.hasMoreElements(); ) {
String currHelp = (String) help.nextElement();
StringTokenizer helpIndex = new StringTokenizer(new StringTokenizer(currHelp, Separators.NL).nextToken());
for ( ; ; ) {
if (helpIndex.hasMoreTokens()) {
if (helpIndex.nextToken().toLowerCase().startsWith(keyWord.toLowerCase())) {
helpText += ((helpText == "") ? currHelp : Separators.NL +
"----------------------------------------------------------------------" +
Separators.NL + currHelp);
break;
}
}
else break;
}
}
if (helpText == "")
throw new NoSuchElementException();
else return helpText;
}
}