import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; public class AllJs4EnrichedContentDotJsFileMaker { private static String JsDirectory = "."; private static String xslFileNames[] = { "novelist-similar-authors.xsl", "novelist-similar-titles.xsl", "sd-hit-list-summary.xsl", "sd-novelist-article.xsl", "sd-related-content.xsl", "sd-show-all-content.xsl", // "sd-syndetics-blink.xsl", "syndetics-author-notes.xsl", "syndetics-booklist-review.xsl", "syndetics-choice-review.xsl", "syndetics-item-summary.xsl", "syndetics-library-journal-review.xsl", "syndetics-publishers-weekly-review.xsl", "syndetics-school-library-journal-review.xsl", "syndetics-summary.xsl", "syndetics-toc.xsl", "tree.xsl" }; private static String xmlFileNames[] = { "sd-detail-chili.xml", "sd-detail-item-tab.xml", "sd-detail-rating.xml", // "sd-ec-blink-content.xml", "sd-enriched-content.xml", "sd-hit-list-content.xml", "sd-hit-list-covers.xml", "sd-hit-list-links.xml", "sd-hit-list-rating.xml", "sd-hit-list-teaser.xml", "sd-related-content.xml" }; private static String JavaScriptFileNames [] = { "sd-ec-version.js", "sd-ec-timer.js", "sd-common.js", "sd-ec-events.js", "sd-ec-progress.js", "sd-ec-stats.js", "getcontent.js", "sd-ec-xdoc-loader.js", "sd-ec-ajax-loader.js", "sd-ec-response-handler.js", "sd-xml-data.js" , "sd-xsl-data.js" , "sd-ec-values.js", "sd-ec-conditions.js", "sd-ec-region-def.js", "sd-ec-specifics.js", "sd-ec-engine.js", "sd-ec-page-interface.js", "sd-ec-xml.js", "sd-ec-html-with-in-xml.js", "sd-ec-response-handler-for-html.js", "sd-ec-tree.js", // "sd-ec-blink-2.js", "sd-ec-on-demand.js", "sd-ec-syndetics.js", "sd-ec-hit-list-covers.js", "sd-ec-tabs.js", "sd-ec-hit-list-similar-entities.js", "sd-ec-chili-4.js" }; public static void main (String[] args) { String FileSeparatorLineForJs = "// ***************************************************************************"; String FileSeparatorLineForXml = "// ==========================================================================="; String FileSeparatorLineForXsl = "// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"; consolidateXslFiles( xslFileNames, FileSeparatorLineForXsl, "sd-xsl-data.js"); consolidateXmlFiles( xmlFileNames, FileSeparatorLineForXml, "sd-xml-data.js"); consolidateJsFiles (JavaScriptFileNames, FileSeparatorLineForJs , "all-js-4-enriched-content.js"); } private static void consolidateXmlFiles (String srcFileNames[], String FileSeparatorLineForXml, String dstFileName) { List dstLines = new ArrayList(); for (String srcFileName : srcFileNames) { String srcFileSpec = JsDirectory + '/' + srcFileName; List srcLines = getLines(srcFileSpec); if (srcLines == null) throw new Error("cannot open '" + srcFileSpec + "'"); dstLines.add(""); dstLines.add(FileSeparatorLineForXml); dstLines.add("// file: " + srcFileName); dstLines.add(FileSeparatorLineForXml); dstLines.add(""); // $_XmlData['/Js/sd-detail-item-tab.xml'] = dstLines.add("$_XmlData['/Js/" + srcFileName + "'] ="); dstLines.add(""); int count = srcLines.size(); int last = count -1; int prevSize = 0; for (int k = 0; k < count; k++) { String srcLine = srcLines.get(k); String outLine = srcLine; // .trim() -- does this fail at runtime because trailing space is needed? int thisSize = outLine.trim().length(); if (k == 0) { outLine = " '" + outLine + "'"; } else if (k == last) { outLine = "+ '" + outLine + "';"; } else if (thisSize <= 0) { if (prevSize <= 0) continue; } else { outLine = "+ '" + outLine + "'"; } prevSize = thisSize; // System.out.println(outLine); if (srcLine.contains("'")) throw new RuntimeException("' in text"); if (srcLine.contains("*")) throw new RuntimeException("* in text"); dstLines.add(outLine); } } String dstFileSpec = JsDirectory + '/' + dstFileName; System.out.println("writing: " + dstFileSpec); putLines(dstFileSpec, HeaderForSdXmlDataDotJsFile, dstLines); } private static String[] HeaderForSdXslDataDotJsFile = { "var SdXslData = {}; // public global identifiers", "var $_XslData = {}; // private global identifiers", "", "// ---------------------------------------------------------------------------", "", "SdXslData.getXslAsHardCodedString = function (xslFileName)", "{", " if (xslFileName && xslFileName.indexOf(\"/Js/\") != 0)", " {", " xslFileName = \"/Js\" + xslFileName;", " }", "// SdCommon.lnk(\"unable to load hardcoded: \", xslFileName);", // "// return null; // $_XslData[xslFileName];", " var xslTxt = $_XslData[xslFileName];", " if (xslTxt != null)", " {", " xslTxt = $_EcXdocLoader.translate(xslTxt);", " }", " return xslTxt;", "}", "", "// ---------------------------------------------------------------------------", "// Fast Loading XSL data", "// ---------------------------------------------------------------------------", }; private static String[] HeaderForSdXmlDataDotJsFile = { "var SdXmlData = {}; // public global identifiers", "var $_XmlData = {}; // private global identifiers", "", "// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -", "", "SdXmlData.fetchXMLDoc = function (xsl)", "{", " if (xsl && xsl.length > 0 && xsl.charAt(0) != '/') xsl = \"/\" + xsl;", "", " var xslTxt;", " try {", " xslTxt = SdXmlData.getXmlAsHardCodedString(xsl);", " } catch (e) {", " xslTxt = null;", " }", "", " return xslTxt;", "}", "", "SdXmlData.getXmlAsHardCodedString = function (xmlFileName)", "{", "if (xmlFileName && xmlFileName.indexOf(\"/Js/\") != 0)", " {", " xmlFileName = \"/Js\" + xmlFileName;", " }", "", " return $_XmlData[xmlFileName];", "}" }; private static void consolidateXslFiles (String srcFileNames[], String FileSeparatorLineForXsl, String dstFileName) { List dstLines = new ArrayList(); for (String srcFileName : srcFileNames) { String srcFileSpec = JsDirectory + '/' + srcFileName; List srcLines = getLines(srcFileSpec); if (srcLines == null) throw new Error("cannot open '" + srcFileSpec + "'"); dstLines.add(""); // dstLines.add(Stars79); // dstLines.add("// file: " + srcFileName); // dstLines.add(Stars79); dstLines.add(""); dstLines.add("$_XslData['/Js/" + srcFileName + "'] ="); dstLines.add(""); int count = srcLines.size(); int last = count -1; int prevSize = 0; for (int k = 0; k < count; k++) { String srcLine = srcLines.get(k); srcLine = escape (srcLine,'\''); srcLine = isolate(srcLine,'*'); String outLine = srcLine; // .trim() -- does this fail at runtime because trailing space is needed? int thisSize = outLine.trim().length(); if (k == 0) { outLine = " '" + outLine + "'"; } else if (k == last) { outLine = "+ '" + outLine + "';"; } else if (thisSize <= 0) { if (prevSize <= 0) continue; } else { outLine = "+ '" + outLine + "'"; } prevSize = thisSize; // if (srcLine.contains("'")) throw new RuntimeException("' in text -- " + outLine); // if (srcLine.contains("*")) throw new RuntimeException("* in text -- " + outLine); dstLines.add(outLine); } dstLines.add(FileSeparatorLineForXsl); } String dstFileSpec = JsDirectory + '/' + dstFileName; System.out.println("writing: " + dstFileSpec); putLines(dstFileSpec, HeaderForSdXslDataDotJsFile, dstLines); } private static void consolidateJsFiles (String srcFileNames[], String FileSeparatorLineForJs, String dstFileName) { List dstLines = new ArrayList(); for (String srcFileName : srcFileNames) { String srcFileSpec = JsDirectory + '/' + srcFileName; List srcLines = getLines(srcFileSpec); if (srcLines == null) throw new Error("cannot open '" + srcFileSpec + "'"); dstLines.add(""); dstLines.add(FileSeparatorLineForJs); dstLines.add("// file: " + srcFileName); dstLines.add(FileSeparatorLineForJs); dstLines.add(""); for (String srcLine : srcLines) { dstLines.add(srcLine); } } String dstFileSpec = JsDirectory + '/' + dstFileName; System.out.println("writing: " + dstFileSpec); putLines(dstFileSpec, null, dstLines); } public static String isolate (String text, char special) { int index = text.indexOf(special); while (index >= 0) { String prefix = text.substring(0,index); String suffix = text.substring(index+1); text = prefix + "' + '" + special + "' + '" + suffix; // hi*y*u // 01234567890 // hi' + '*' + 'y*u // ^ ^ // 2 7 index = text.indexOf(special, index+6); } return text; } public static String escape (String text, char special) { int index = text.indexOf(special); while (index >= 0) { String prefix = text.substring(0,index); String suffix = text.substring(index+1); text = prefix + "\\" + special + suffix; // hi'y'u // 01234567890 // hi\'y\'u // ^^ // 23 index = text.indexOf(special, index+2); } return text; } private static List getLines (String fileName) { List list = new ArrayList(); try { LineNumberReader reader; { FileInputStream fis = new FileInputStream(fileName); reader = new LineNumberReader( new InputStreamReader(fis) ); } for (;;) { String line = reader.readLine(); if (line == null) break; list.add(line); } } catch (Exception e) { return null; } return list; } private static String putLines (String fileName, String[] header, List lines) { try { PrintStream ps = new PrintStream( new FileOutputStream(fileName) ); if (header != null) { for (String line : header) { if (line == null) line = ""; ps.println(line); } } for (String line : lines) { if (line == null) line = ""; ps.println(line); } ps.close(); } catch (FileNotFoundException e) { return e.getMessage(); } return null; } }