home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2003 October / PCWELT_10_2003.ISO / pcwsoft / CUCKOO.z.exe / cuckoo-xtp.js < prev    next >
Encoding:
JavaScript  |  2001-12-24  |  4.0 KB  |  106 lines

  1. /*
  2.  * cuckoo web authoring
  3.  * cuckoo-xtp.js
  4.  * Convenience tool to build XTP (Resin) files.
  5.  * Version 0.01
  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) {
  27.     // WScript.Echo("xsl:" + xslname + " xmlname:" + xmlname + " to:" + to);
  28.     var fso = new ActiveXObject("Scripting.FileSystemObject");
  29.     var o = fso.CreateTextFile(to, true, false);
  30.     o.Write("<?xml-stylesheet href='" + xslname + "'?>\r\n");
  31.     var i = fso.OpenTextFile(xmlname, 1 /* For reading. */);
  32.     var r = i.ReadLine();
  33.     var cdata = /<!\[CDATA\[/g;
  34.     var ecdata = /\]\]>/g;
  35.     var amp = /&#/g;
  36.     while(!i.AtEndOfStream) {
  37.         r = r.replace(cdata, "").replace(ecdata, "");
  38.         r = r.replace(amp, "&#");
  39.         o.Write(r + "\n");
  40.         r = i.ReadLine();
  41.     }
  42.     r = r.replace(cdata, "").replace(ecdata, "");
  43.     r = r.replace(amp, "&#");
  44.     o.Write(r + "\n");
  45.     i.Close();
  46.     o.Close();
  47. }
  48. /*
  49.  * WSH script.
  50.  * Recommended version of WSH: 5.6 and above for WScript.CurrentDirectory.
  51.  * Usage:
  52.  * cuckoo-gen.js /dir:source-directory|/file:file [/toDir:target-directory|/toFile:target-file] [/xsl:xsl-file]
  53.  * Example:
  54.  * cuckoo-gen.js /dir:D:\cuckoo /toDir:D:\cuckoo /xsl:cuckoo.xsl
  55.  * cuckoo-gen.js /file:D:\cuckoo\myfile.xml /toFile:D:\cuckoo\myfile.xtp /xsl:cuckoo.xsl
  56.  * Note:
  57.  * The xsl file should the path of the xsl file relative to the xtp file on Resin.
  58.  * For instance cuckoo.xsl if cuckoo.xsl is in the same directory as the xtp or
  59.  * ../xsl/cuckoo.xsl if cuckoo.xsl is stored in a xsl directory at the same level as the xtp file or
  60.  * /xsl/cuckoo.xsl. 
  61.  */
  62. var xslfile = null;
  63. var todir = null;
  64. var argsNamed = WScript.Arguments.Named;
  65. if (argsNamed.Exists("xsl"))
  66.     xslfile = argsNamed.Item("xsl");
  67. else
  68.     xslfile = "cuckoo-xtp.xsl";
  69. if (argsNamed.Exists("dir")) {
  70.     var fso = new ActiveXObject("Scripting.FileSystemObject");
  71.     var xmldir = argsNamed.Item("dir");
  72.     if (fso.FolderExists(xmldir)) {
  73.         if (argsNamed.Exists("toDir"))
  74.             todir = argsNamed.Item("toDir");
  75.         else
  76.             toDir = xmldir;
  77.         var fold = fso.GetFolder(xmldir);
  78.         var files = new Enumerator(fold.Files);
  79.         for (; !files.atEnd(); files.moveNext()) {
  80.             var n = files.item().Name;
  81.             var l = n.length - "W.xml".length;
  82.             var s = n.substring(0, l);
  83.             if (n.substring(l) == "W.xml")
  84.                 process(xslfile, xmldir + "\\" + n, todir + "\\" + s + ".xtp");
  85.         }
  86.     }
  87. } else {
  88.     if (argsNamed.Exists("file")) {
  89.         var xmlname = argsNamed.Item("file");
  90.         if (argsNamed.Exists("toFile"))
  91.             process(xslfile, xmlname, argsNamed.Item("toFile"));
  92.         else {
  93.             if (argsNamed.Exists("toDir")) {
  94.                 toDir = argsNamed.Item("toDir");
  95.                 var pos = xmlname.lastIndexOf("\\");
  96.                 var f1 = xmlname.substring(pos);
  97.                 process(xslfile, xmlname, toDir + f1.replace("W.xml", ".xtp"));
  98.             } else
  99.                 process(xslfile, xmlname, xmlname.replace("W.xml", ".xtp"));
  100.         }
  101.     } else
  102.         WScript.Echo(
  103.             "Usage: cuckoo-gen.js /dir:source-directory|/file:file [/toDir:target-directory|"
  104.             + "/toFile:target-file] [/xsl:xsl-file]");
  105. }    
  106.