home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2003 October / PCWELT_10_2003.ISO / pcwsoft / CUCKOO.z.exe / cuckoo-genForPrinters.js < prev    next >
Encoding:
JavaScript  |  2002-06-15  |  5.2 KB  |  137 lines

  1. /*
  2.  * cuckoo web authoring
  3.  * cuckoo-gen.js
  4.  * Convenience tool to rebuild a delivery
  5.  * Version 0.1.1
  6.  * Copyright (c) 2000-2001 Alexis Grandemange
  7.  * Mail: alexis.grandemange@pagebox.net
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; version
  11.  * 2.1 of the License.
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15.  * GNU Lesser General Public License for more details.
  16.  * A copy of the GNU Lesser General Public License lesser.txt should be
  17.  * included in the distribution.
  18.  */
  19. /*
  20.  * Transforms the xml file
  21.  * Parameters:
  22.  * xslname name of the xsl file
  23.  * xmlname name of the xml source file
  24.  * to name of the target file
  25.  */
  26. function process(xslname, xmlname, to, progid) {
  27.     // WScript.Echo("xsl:" + xslname + " xmlname:" + xmlname + " to:" + to);
  28.     var xmldoc = new ActiveXObject(progid);
  29.     var xsldoc = new ActiveXObject(progid);        
  30.     xmldoc.async = false;
  31.     xsldoc.async = false;
  32.     xmldoc.validateOnParse = false;
  33.     xsldoc.validateOnParse = false;
  34.     xmldoc.load(xmlname);
  35.     xsldoc.load(xslname);            
  36.     var out = new ActiveXObject("Scripting.FileSystemObject");
  37.     var o = out.CreateTextFile(to, true, false)
  38.     if((xmldoc.parseError.errorCode == 0) && (xsldoc.parseError.errorCode == 0)) {
  39.     var str = xmldoc.transformNode(xsldoc);
  40.     // MSXML 3 adds <META http-equiv="Content-Type" content="text/html; charset=UTF-16">
  41.     str = str.replace("; charset=UTF-16", "");
  42.         str = str.replace("content=\"text/html", "content=\"text/html; charset=iso-8859-1");
  43.         str = str.replace(
  44.           "</head>", "<script>\r\nif (is_nav) document.write(\'<link rel=\"stylesheet\" href=\"cuckoo-nav6.css\"" +
  45.           " type=\"text/css\">\');\r\n</script>");
  46.     var a0 = /\u00A0/g;
  47.     var cuckoo = /<cuckoo>/g;
  48.     var ecuckoo = /<\/cuckoo>/g;
  49.     var amp = /!#/g;
  50.     var nbsp = /!nbsp;/g;
  51.     str = str.replace(a0, " ").replace(cuckoo, "").replace(ecuckoo, "").replace(amp, "&#").replace(nbsp, " ");
  52.     var pos = to.lastIndexOf("/");
  53.     if (pos == -1)
  54.         pos = to.lastIndexOf("\\");
  55.     var fn = to.substring(pos + 1);
  56.     var pos = fn.lastIndexOf("ForPrinters.");
  57.     var href = /href=\"\#this/g;
  58.     str = str.replace(href, "href=\"" + fn.substring(0, pos));
  59.     o.write(str);
  60.     }
  61.     o.close();
  62. }
  63. /*
  64.  * WSH script.
  65.  * Recommended version of WSH: 5.6 and above for WScript.CurrentDirectory.
  66.  * Usage:
  67.  * cuckoo-gen.js /dir:source-directory|/file:file [/toDir:target-directory|/toFile:target-file] [/xsl:xsl-file]
  68.  * Example:
  69.  * cuckoo-gen.js /dir:D:\cuckoo /toDir:D:\cuckoo /xsl:D:\cuckoo\cuckoo.xsl
  70.  * cuckoo-gen.js /file:D:\cuckoo\myfile.xml /toFile:D:\cuckoo\myfile.asp /xsl:D:\cuckoo\cuckoo.xsl
  71.  */
  72. var xslfile = null;
  73. var todir = null;
  74. var argsNamed = WScript.Arguments.Named;
  75. if (argsNamed.Exists("xsl"))
  76.     xslfile = argsNamed.Item("xsl");
  77. else
  78.     xslfile = WScript.CurrentDirectory + "\\cuckoo-printer.xsl";
  79. var bKey = null;
  80. var progid = null;
  81. var WshShell = WScript.CreateObject ("WScript.Shell");
  82. try {
  83.     bKey = WshShell.RegRead("HKEY_CLASSES_ROOT\\Msxml2.DOMDocument.4.0\\");
  84. }
  85. catch(e) {}
  86. if (bKey == null) {
  87.     try {
  88.         bKey = WshShell.RegRead("HKEY_CLASSES_ROOT\\Msxml2.DOMDocument.3.0\\");
  89.     }
  90.     catch(e) {}
  91.     if (bKey != null)
  92.         progid = "Msxml2.DOMDocument.3.0";
  93. } else
  94.     progid = "Msxml2.DOMDocument.4.0";
  95. if (progid == null)
  96.     WScript.Echo("You must install MSXML 3 or MSXML 4");
  97. else {
  98.     // WScript.Echo(progid);
  99. if (argsNamed.Exists("dir")) {
  100.     var fso = new ActiveXObject("Scripting.FileSystemObject");
  101.     var xmldir = argsNamed.Item("dir");
  102.     if (fso.FolderExists(xmldir)) {
  103.         if (argsNamed.Exists("toDir"))
  104.             todir = argsNamed.Item("toDir");
  105.         else
  106.             toDir = xmldir;
  107.         var fold = fso.GetFolder(xmldir);
  108.         var files = new Enumerator(fold.Files);
  109.         for (; !files.atEnd(); files.moveNext()) {
  110.             var n = files.item().Name;
  111.             var l = n.length - "W.xml".length;
  112.             var s = n.substring(0, l);
  113.             if (n.substring(l) == "W.xml")
  114.                 process(xslfile, xmldir + "\\" + n, todir + "\\" + s + "ForPrinters.html", progid);
  115.         }
  116.     }
  117. } else {
  118.     if (argsNamed.Exists("file")) {
  119.         var xmlname = argsNamed.Item("file");
  120.         if (argsNamed.Exists("toFile"))
  121.             process(xslfile, xmlname, argsNamed.Item("toFile"), progid);
  122.         else {
  123.             if (argsNamed.Exists("toDir")) {
  124.                 toDir = argsNamed.Item("toDir");
  125.                 var pos = xmlname.lastIndexOf("\\");
  126.                 var f1 = xmlname.substring(pos);
  127.                 process(xslfile, xmlname, toDir + f1.replace("W.xml", "ForPrinters.html"), progid);
  128.             } else
  129.                 process(xslfile, xmlname, xmlname.replace("W.xml", "ForPrinters.html"), progid);
  130.         }
  131.     } else
  132.         WScript.Echo(
  133.             "Usage: cuckoo-gen.js /dir:source-directory|/file:file [/toDir:target-directory|"
  134.             + "/toFile:target-file] [/xsl:xsl-file]");
  135. }    
  136. }
  137.