home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2003 October / PCWELT_10_2003.ISO / pcwsoft / CUCKOO.z.exe / form-php.js < prev    next >
Encoding:
JavaScript  |  2002-06-09  |  4.5 KB  |  123 lines

  1. /*
  2.  * cuckoo web authoring
  3.  * form-php.js
  4.  * Convenience tool to generate a PHP processing the request of a Cuckoo document
  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.     str = str.replace('<?xml version="1.0" encoding="UTF-16"?>', "");
  41.     var a0 = /\u00A0/g;    // For Opera
  42.     var lt = /</g;
  43.     var gt = />/g;
  44.     var amp = /&#/g;
  45.     str = str.replace(a0, " ").replace(lt, "<").replace(gt, ">").replace(amp, "&#");
  46.     o.write(str);
  47.     }
  48.     o.close();
  49. }
  50. /*
  51.  * WSH script.
  52.  * Recommended version of WSH: 5.6 and above for WScript.CurrentDirectory.
  53.  * Usage:
  54.  * form-php.js /dir:source-directory|/file:file [/toDir:target-directory|/toFile:target-file] [/xsl:xsl-file]
  55.  * Example:
  56.  * form-php.js /dir:D:\cuckoo /toDir:D:\cuckoo /xsl:D:\cuckoo\cuckoo.xsl
  57.  * form-php.js /file:D:\cuckoo\myfile-phpF.xml /toFile:D:\cuckoo\myfile.php /xsl:D:\cuckoo\form-php.xsl
  58.  */
  59. var xslfile = null;
  60. var todir = null;
  61. var argsNamed = WScript.Arguments.Named;
  62. if (argsNamed.Exists("xsl"))
  63.     xslfile = argsNamed.Item("xsl");
  64. else
  65.     xslfile = WScript.CurrentDirectory + "\\form-php.xsl";
  66. var bKey = null;
  67. var progid = null;
  68. var WshShell = WScript.CreateObject ("WScript.Shell");
  69. try {
  70.     bKey = WshShell.RegRead("HKEY_CLASSES_ROOT\\Msxml2.DOMDocument.4.0\\");
  71. }
  72. catch(e) {}
  73. if (bKey == null) {
  74.     try {
  75.         bKey = WshShell.RegRead("HKEY_CLASSES_ROOT\\Msxml2.DOMDocument.3.0\\");
  76.     }
  77.     catch(e) {}
  78.     if (bKey != null)
  79.         progid = "Msxml2.DOMDocument.3.0";
  80. } else
  81.     progid = "Msxml2.DOMDocument.4.0";
  82. if (progid == null)
  83.     WScript.Echo("You must install MSXML 3 or MSXML 4");
  84. else {
  85. if (argsNamed.Exists("dir")) {
  86.     var fso = new ActiveXObject("Scripting.FileSystemObject");
  87.     var xmldir = argsNamed.Item("dir");
  88.     if (fso.FolderExists(xmldir)) {
  89.         if (argsNamed.Exists("toDir"))
  90.             todir = argsNamed.Item("toDir");
  91.         else
  92.             toDir = xmldir;
  93.         var fold = fso.GetFolder(xmldir);
  94.         var files = new Enumerator(fold.Files);
  95.         for (; !files.atEnd(); files.moveNext()) {
  96.             var n = files.item().Name;
  97.             var l = n.length - "-phpF.xml".length;
  98.             var s = n.substring(0, l);
  99.             if (n.substring(l) == "-phpF.xml")
  100.                 process(xslfile, xmldir + "\\" + n, todir + "\\" + s + ".php", progid);
  101.         }
  102.     }
  103. } else {
  104.     if (argsNamed.Exists("file")) {
  105.         var xmlname = argsNamed.Item("file");
  106.         if (argsNamed.Exists("toFile"))
  107.             process(xslfile, xmlname, argsNamed.Item("toFile"), progid);
  108.         else {
  109.             if (argsNamed.Exists("toDir")) {
  110.                 toDir = argsNamed.Item("toDir");
  111.                 var pos = xmlname.lastIndexOf("\\");
  112.                 var f1 = xmlname.substring(pos);
  113.                 process(xslfile, xmlname, toDir + f1.replace("-phpF.xml", ".php"), progid);
  114.             } else
  115.                 process(xslfile, xmlname, xmlname.replace("-phpF.xml", ".php"), progid);
  116.         }
  117.     } else
  118.         WScript.Echo(
  119.             "Usage: form-php.js /dir:source-directory|/file:file [/toDir:target-directory|"
  120.             + "/toFile:target-file] [/xsl:xsl-file]");
  121. }    
  122. }
  123.