boolean bAutoDetectEncoding = true;
TreeSet<String> oFiles = new TreeSet<String>();
TreeSet<String> oEntries = new TreeSet<String>();
Perl5Matcher oMatcher = new Perl5Matcher();
Perl5Matcher oReplacer = new Perl5Matcher();
Perl5Compiler oCompiler = new Perl5Compiler();
try {
String sHtml = readfilestr(sBasePath+sFilePath,"ASCII");
if (null==sHtml) {
if (DebugFile.trace) {
DebugFile.writeln("Could not read file "+sBasePath+sFilePath);
DebugFile.decIdent();
throw new IOException("Could not read file "+sBasePath+sFilePath);
}
}
if (DebugFile.trace) {
DebugFile.writeln(String.valueOf(sHtml.length())+" characters readed from file "+sBasePath+sFilePath);
}
if (oMatcher.contains(sHtml, oCompiler.compile("<meta\\x20+http-equiv=(\"|')?Content-Type(\"|')?\\x20+content=(\"|')?text/html;\\x20+charset=(\\w|-){3,32}(\"|')?>",Perl5Compiler.CASE_INSENSITIVE_MASK))) {
if (DebugFile.trace) DebugFile.writeln("<meta http-equiv> tag found");
String sHttpEquiv = oMatcher.getMatch().toString();
int iCharset = Gadgets.indexOfIgnoreCase(sHttpEquiv,"charset=");
if (iCharset>0) {
int iQuoute = sHttpEquiv.indexOf('"', iCharset);
if (iQuoute<0) iQuoute = sHttpEquiv.indexOf((char)39, iCharset);
if (iQuoute<0) {
bAutoDetectEncoding = true;
} else {
sEncoding = sHttpEquiv.substring(iCharset+8, iQuoute);
if (DebugFile.trace) DebugFile.writeln("setting charset encoding to "+sEncoding);
bAutoDetectEncoding = false;
try {
byte[] aTest = new String("Test").getBytes(sEncoding);
} catch (UnsupportedEncodingException uex) {
bAutoDetectEncoding = true;
}
}
} else {
bAutoDetectEncoding = true;
}
} else {
bAutoDetectEncoding = true;
}
if (bAutoDetectEncoding) {
if (DebugFile.trace) DebugFile.writeln("Autodetecting encoding");
ByteArrayInputStream oHtmlStrm = new ByteArrayInputStream(sHtml.getBytes("ASCII"));
sEncoding = new CharacterSetDetector().detect(oHtmlStrm,"ASCII");
oHtmlStrm.close();
if (DebugFile.trace) DebugFile.writeln("Encoding set to "+sEncoding);
}
Pattern oPattern = oCompiler.compile("<base(\\x20)+href=(\"|')?([^'\"\\r\\n]+)(\"|')?(\\x20)*/?>", Perl5Compiler.CASE_INSENSITIVE_MASK);
if (oMatcher.contains(sHtml, oPattern)) {
sBaseHref = Gadgets.chomp(oMatcher.getMatch().group(3),"/");
if (DebugFile.trace) DebugFile.writeln("<base href="+sBaseHref+">");
}
PatternMatcherInput oMatchInput = new PatternMatcherInput(sHtml);
oPattern = oCompiler.compile("\\x20(src=|background=|background-image:url\\x28)(\"|')?([^'\"\\r\\n]+)(\"|')?(\\x20|\\x29|/|>)", Perl5Compiler.CASE_INSENSITIVE_MASK);
StringSubstitution oSrcSubs = new StringSubstitution();
int nMatches = 0;
while (oMatcher.contains(oMatchInput, oPattern)) {
nMatches++;
String sMatch = oMatcher.getMatch().toString();
String sAttr = oMatcher.getMatch().group(1);
String sQuo = oMatcher.getMatch().group(2);
if (sQuo==null) sQuo = "";
String sSrc = oMatcher.getMatch().group(3);
if (DebugFile.trace) DebugFile.writeln("Source file found at "+sSrc);
String sEnd = oMatcher.getMatch().group(5);
if (!oFiles.contains(sSrc)) oFiles.add(sSrc);
String sFilename = sSrc.substring(sSrc.replace('\\','/').lastIndexOf('/')+1);
if (DebugFile.trace)
DebugFile.writeln("StringSubstitution.setSubstitution("+sMatch+" replace with "+sMatch.substring(0,sAttr.length()+1)+sQuo+sFilename+sQuo+sEnd+")");
oSrcSubs.setSubstitution(sMatch.substring(0,sAttr.length()+1)+sQuo+sFilename+sQuo+sEnd);
sHtml = Util.substitute(oReplacer, oCompiler.compile(sMatch), oSrcSubs, sHtml, Util.SUBSTITUTE_ALL);
} //wend
oMatchInput = new PatternMatcherInput(sHtml);
oPattern = oCompiler.compile("<link\\x20+(rel=(\"|')?stylesheet(\"|')?\\x20+)?(type=(\"|')?text/css(\"|')?\\x20+)?href=(\"|')?([^'\"\\r\\n]+)(\"|')?");
while (oMatcher.contains(oMatchInput, oPattern)) {
nMatches++;
String sMatch = oMatcher.getMatch().toString();
String sSrc = oMatcher.getMatch().group(8);
String sFilename = sSrc.substring(sSrc.replace('\\','/').lastIndexOf('/')+1);
if (!oFiles.contains(sSrc)) oFiles.add(sSrc);
if (DebugFile.trace)
DebugFile.writeln("StringSubstitution.setSubstitution("+sMatch+" replace with "+Gadgets.replace(sMatch, sSrc, sFilename)+")");
oSrcSubs.setSubstitution(Gadgets.replace(sMatch, sSrc, sFilename));
sHtml = Util.substitute(oReplacer, oCompiler.compile(sMatch), oSrcSubs, sHtml);
} // wend
if (DebugFile.trace) {
DebugFile.writeln(String.valueOf(nMatches)+" matches found");
DebugFile.write("\n"+sHtml+"\n");