import java.io.*; import java.util.*; public class SHTMLReader { // ------------------------------------------------------------------------- // Variables // ------------------------------------------------------------------------- private BufferedReader shtmlReader; private BufferedReader includeReader; private File shtmlFile; private String line; private String includeLine; private String afterInclude; private String rootDir; private boolean inInclude; // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- public SHTMLReader(File shtmlFile) throws IOException { this.shtmlFile = shtmlFile; inInclude = false; rootDir = ""; FileReader fileReader = new FileReader(shtmlFile); shtmlReader = new BufferedReader(fileReader); } // ------------------------------------------------------------------------- // Methods // ------------------------------------------------------------------------- public String readLine() throws IOException { line = ""; if (!inInclude) { readSHTML(); } else { readInclude(); } if (line != null) { replaceIncludes(); replaceLinks(); } return line; } private void readSHTML() throws IOException { line = shtmlReader.readLine(); } private void readInclude() throws IOException { if (includeLine == null) { includeLine = includeReader.readLine(); } if (includeLine != null) { line += includeLine; includeLine = includeReader.readLine(); } if (includeLine == null) { inInclude = false; includeReader.close(); includeReader = null; line += afterInclude; } } private void replaceIncludes() throws IOException { int index = line.indexOf(""); if (index != -1 && endIndex > index) { int slashIndex = line.lastIndexOf("./", endIndex); if (slashIndex != -1) rootDir = line.substring(index + 19, slashIndex + 2); else rootDir = ""; } while (index != -1) { inInclude = true; File includeFile = new File(shtmlFile.getAbsoluteFile().getParent(), line.substring(index + 19, endIndex)); FileReader fileReader = new FileReader(includeFile); includeReader = new BufferedReader(fileReader); afterInclude = (line.substring(endIndex + 4, line.length())); line = (line.substring(0, index)); readInclude(); index = line.indexOf(""); } index = line.indexOf(""); while (index != -1) { line = line.substring(0, index) + rootDir + line.substring(index + 15, line.length()); index = line.indexOf(""); } index = line.indexOf(""); while (index != -1) { line = line.substring(0, index) + new Date(shtmlFile.lastModified()) + line.substring(index + 16, line.length()); index = line.indexOf(""); } index = line.indexOf(""); while (index != -1) { line = line.substring(0, index) + (Math.round(shtmlFile.length() / 10.24) / 100.0) + " KB"+ line.substring(index + 13, line.length()); index = line.indexOf(""); } } private void replaceLinks() throws IOException { int index = line.indexOf(".shtm"); while (index != -1) { line = line.substring(0, index) + ".htm" + line.substring(index + 5, line.length()); index = line.indexOf(".shtm"); } } public void close() throws IOException { shtmlReader.close(); } }