You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fop.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Licensed to the Apache Software Foundation (ASF) under one or more
  2. // contributor license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright ownership.
  4. // The ASF licenses this file to You under the Apache License, Version 2.0
  5. // (the "License"); you may not use this file except in compliance with
  6. // the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // $Id$ -->
  16. // jscript to run FOP, adapted from the Jakarta-Ant project.
  17. // rpm_mode is irrelevant on Windows
  18. // var rpm_mode=true;
  19. var fop_exec_args = "";
  20. var no_config=false;
  21. var fop_exec_debug=false;
  22. var debug=false;
  23. var keep_open=false;
  24. var show_help=false;
  25. var config_wanted = new Array("FOP_HOME", "CLASSPATH", "FOP_HYPHENATION_PATH", "FOP_OPTS", "JAVA_OPTS", "LOGCHOICE", "LOGLEVEL");
  26. // parse command-line arguments
  27. function read_args() {
  28. var args = WScript.Arguments;
  29. var named = new ActiveXObject("Scripting.Dictionary");
  30. for (i = 0; i < args.length; i++) {
  31. switch(args(i)) {
  32. case "--debug":
  33. debug=true;
  34. break;
  35. case "--execdebug":
  36. fop_exec_debug=true;
  37. break;
  38. case "--keepopen":
  39. keep_open=true;
  40. break;
  41. case "--noconfig":
  42. no_config=true;
  43. break;
  44. case "-h":
  45. case "--help":
  46. case "--h":
  47. case "-help":
  48. show_help=true;
  49. // fop_exec_args=fop_exec_args + " -h";
  50. break;
  51. default:
  52. fop_exec_args=fop_exec_args + " " + args(i);
  53. }
  54. }
  55. if (debug) {
  56. WScript.Echo("debug: " + debug);
  57. WScript.Echo("execdebug: " + fop_exec_debug);
  58. WScript.Echo("keepopen: " + keep_open);
  59. WScript.Echo("noconfig: " + no_config);
  60. WScript.Echo("help: " + show_help);
  61. WScript.Echo("fop arguments: " + fop_exec_args);
  62. }
  63. }
  64. var help_text="Usage:\n"
  65. + WScript.ScriptFullName + " [script options] [FOP options]\n"
  66. + "Script Options:\n"
  67. + " --help, -h print this message and FOP help\n"
  68. + " --debug print debugging information for this launch script\n"
  69. + " --execdebug print FOP exec line generated by this launch script\n"
  70. + " --keepopen keep FOP's command window open\n"
  71. + " --noconfig suppress reading of configuration file and registry";
  72. function read_environment() {
  73. for (i in config_wanted) {
  74. if (!config.Exists(config_wanted[i])) {
  75. var env_var_string = "%" + config_wanted[i] + "%";
  76. var env_var = shell.ExpandEnvironmentStrings(env_var_string);
  77. if (env_var != "" && env_var != env_var_string) {
  78. config.Add(config_wanted[i], env_var);
  79. if (debug) {
  80. WScript.Echo(config_wanted[i] + " env: "
  81. + config.Item(config_wanted[i]));
  82. }
  83. }
  84. }
  85. }
  86. }
  87. function read_desktop(dtname) {
  88. if (fs.FolderExists(dtname)) {
  89. var fopname = fs.GetFolder(dtname).ParentFolder.Path
  90. + "\\Application Data\\Fop";
  91. if (fs.FolderExists(fopname)) {
  92. var fop_conf_name = fs.GetFolder(fopname).Path + "\\fop.conf";
  93. if (fs.FileExists(fop_conf_name)) {
  94. // source fop_conf_file
  95. var conf_file = fs.openTextFile(fs.GetFile(fop_conf_name));
  96. var conf_lines = new ActiveXObject("Scripting.Dictionary");
  97. while (!conf_file.AtEndOfStream) {
  98. var line = conf_file.ReadLine();
  99. var m = line.match(/(.+?)=(.+)/);
  100. if (m != null) {
  101. conf_lines.Add(m[1], m[2]);
  102. }
  103. }
  104. for (j in config_wanted) {
  105. if (!config.Exists(config_wanted[j])
  106. && conf_lines.Exists(config_wanted[j])) {
  107. config.Add(config_wanted[j], conf_lines.Item(config_wanted[j]));
  108. if (debug) {
  109. WScript.Echo(config_wanted[j] + " " + dts[i] + ": "
  110. + config.Item(config_wanted[i]));
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. function read_registry(section) {
  119. for (j in config_wanted) {
  120. if (!config.Exists(config_wanted[j])) {
  121. var reg_var;
  122. try {
  123. reg_var = shell.RegRead(section + "\\Software\\Fop\\"
  124. + config_wanted[j]);
  125. config.Add(config_wanted[j], reg_var);
  126. if (debug) {
  127. WScript.Echo(config_wanted[j] + " " + rks[i] + ": "
  128. + config.Item(config_wanted[j]));
  129. }
  130. } catch(e) {}
  131. }
  132. }
  133. }
  134. // construct FOP_HOME from the script folder
  135. function get_fop_home() {
  136. if (!config.Exists("FOP_HOME")
  137. || !fs.FolderExists(config.Item("FOP_HOME"))) {
  138. var fop_home = WScript.ScriptFullName;
  139. fop_home = fop_home.substring(0, fop_home.length
  140. - WScript.ScriptName.length - 1);
  141. if (config.Exists("FOP_HOME")) {
  142. config.Remove("FOP_HOME");
  143. }
  144. config.Add("FOP_HOME", fop_home);
  145. if (debug) {
  146. WScript.Echo("FOP_HOME dyn: " + config.Item("FOP_HOME"));
  147. }
  148. }
  149. }
  150. function get_java_cmd() {
  151. var java_home = shell.ExpandEnvironmentStrings("%JAVA_HOME%");
  152. javacmd = "java";
  153. if (java_home != "" && typeof(java_home) != "undefined"
  154. && fs.FolderExists(java_home)) {
  155. var javacmd_candidate = java_home + "\\bin\\java.exe";
  156. if (fs.FileExists(javacmd_candidate)) {
  157. javacmd = javacmd_candidate;
  158. }
  159. }
  160. if (debug) {
  161. WScript.Echo("java command: " + javacmd);
  162. }
  163. }
  164. function get_local_classpath() {
  165. if (config.Exists("CLASSPATH")) {
  166. local_classpath = config.Item("CLASSPATH");
  167. if (debug) {
  168. WScript.Echo("local classpath: " + local_classpath);
  169. }
  170. }
  171. // add fop.jar, fop-sandbox and fop-hyph.jar, which reside in $FOP_HOME/build
  172. var lcp = local_classpath;
  173. local_classpath = config.Item("FOP_HOME") + "\\build\\fop.jar;"
  174. + config.Item("FOP_HOME") + "\\build\\fop-sandbox.jar;"
  175. + config.Item("FOP_HOME") + "\\build\\fop-hyph.jar";
  176. if (lcp != "") {
  177. local_classpath += ";" + lcp;
  178. }
  179. if (debug) {
  180. WScript.Echo("local classpath: " + local_classpath);
  181. }
  182. // add in the dependency .jar files, which reside in $FOP_HOME/lib
  183. var libdir_name = config.Item("FOP_HOME") + "\\lib";
  184. var dirlibs;
  185. if (fs.FolderExists(libdir_name)) {
  186. dirlibs = fs.GetFolder(libdir_name).Files;
  187. var e = new Enumerator(dirlibs);
  188. for (; !e.atEnd(); e.moveNext()) {
  189. if (e.item().Name.match("\.jar$")) {
  190. local_classpath = libdir_name + "\\" + e.item().Name + ";" + local_classpath;
  191. }
  192. }
  193. if (debug) {
  194. WScript.Echo("local classpath: " + local_classpath);
  195. }
  196. }
  197. // add in user-defined hyphenation JARs
  198. if (config.Exists("FOP_HYPHENATION_PATH")) {
  199. local_classpath += ";" + config.Item("FOP_HYPHENATION_PATH");
  200. if (debug) {
  201. WScript.Echo("local classpath: " + local_classpath);
  202. }
  203. }
  204. }
  205. // Execute fop via shell.Exec
  206. function fop_exec() {
  207. var fop_exec_command = "\"" + javacmd + "\" "
  208. + (config.Exists("JAVA_OPTS")?config.Item("JAVA_OPTS") + " ":"")
  209. + (config.Exists("LOGCHOICE")?config.Item("LOGCHOICE") + " ":"")
  210. + (config.Exists("LOGLEVEL")?config.Item("LOGLEVEL") + " ":"")
  211. + "-classpath \"" + local_classpath + "\" "
  212. + (config.Exists("FOP_OPTS")?config.Item("FOP_OPTS"):"")
  213. + "org.apache.fop.cli.Main " + fop_exec_args;
  214. if (debug || fop_exec_debug) {
  215. WScript.Echo(fop_exec_command);
  216. }
  217. var fop_run = shell.Exec(fop_exec_command);
  218. while (true) {
  219. while (!fop_run.StdOut.AtEndOfStream) {
  220. WScript.Echo(fop_run.StdOut.ReadLine());
  221. }
  222. while (!fop_run.StdErr.AtEndOfStream) {
  223. WScript.Echo(fop_run.StdErr.ReadLine());
  224. }
  225. if (fop_run.Status == 1) {
  226. break;
  227. }
  228. WScript.Sleep(100);
  229. }
  230. if (debug) {
  231. WScript.Echo("exit status: " + fop_run.ExitCode);
  232. }
  233. }
  234. // Execute fop via shell.Run
  235. function fop_run() {
  236. var fop_exec_command = "cmd /" + (keep_open?"K":"C") + " \""
  237. + "\"" + javacmd + "\" "
  238. + (config.Exists("JAVA_OPTS")?config.Item("JAVA_OPTS") + " ":"")
  239. + (config.Exists("LOGCHOICE")?config.Item("LOGCHOICE") + " ":"")
  240. + (config.Exists("LOGLEVEL")?config.Item("LOGLEVEL") + " ":"")
  241. + "-classpath \"" + local_classpath + "\" "
  242. + (config.Exists("FOP_OPTS")?config.Item("FOP_OPTS") + " ":"")
  243. + "org.apache.fop.cli.Main " + fop_exec_args + "\"";
  244. if (debug || fop_exec_debug) {
  245. WScript.Echo(fop_exec_command);
  246. }
  247. var exit_code = shell.Run(fop_exec_command, 1, 1);
  248. if (debug) {
  249. WScript.Echo("exit status: " + exit_code);
  250. } else {
  251. if (exit_code != 0) {
  252. WScript.Echo("A FOP error occurred (FOP exit status: " + exit_code + ")\n"
  253. + "Use option --keepopen to see FOP's output\n"
  254. + "(that is two dashes)");
  255. }
  256. }
  257. }
  258. function get_log_choice() {
  259. // The default commons logger for JDK1.4 is JDK1.4Logger.
  260. // To use a different logger, uncomment the one desired below
  261. if (!config.Exists("LOGCHOICE")) {
  262. // config.Add("LOGCHOICE","\"-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog\"");
  263. // config.Add("LOGCHOICE","\"-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog\"");
  264. // config.Add("LOGCHOICE","\"-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger\"");
  265. if (debug && config.Exists("LOGCHOICE")) {
  266. WScript.Echo("LOGCHOICE script: " + config.Item("LOGCHOICE"));
  267. }
  268. }
  269. }
  270. function get_log_level() {
  271. // Logging levels
  272. // Below option is only if you are using SimpleLog instead of the default JDK1.4 Logger.
  273. // To set logging levels for JDK 1.4 Logger, edit the %JAVA_HOME%/JRE/LIB/logging.properties
  274. // file instead.
  275. // Possible SimpleLog values: "trace", "debug", "info" (default), "warn", "error", or "fatal".
  276. if (!config.Exists("LOGLEVEL")) {
  277. // config.Add("LOGLEVEL","\"-Dorg.apache.commons.logging.simplelog.defaultlog=INFO\"");
  278. if (debug && config.Exists("LOGLEVEL")) {
  279. WScript.Echo("LOGLEVEL script: " + config.Item("LOGLEVEL"));
  280. }
  281. }
  282. }
  283. var shell = WScript.CreateObject("WScript.Shell");
  284. var fs = WScript.CreateObject("Scripting.FileSystemObject");
  285. // configuration
  286. var config = new ActiveXObject("Scripting.Dictionary");
  287. read_args();
  288. read_environment();
  289. if (!no_config) {
  290. // read user and system-wide fop configurations
  291. var spec = shell.SpecialFolders;
  292. var dts = new Array("Desktop", "AllUsersDesktop");
  293. for (i in dts) {
  294. read_desktop(spec(dts[i]));
  295. }
  296. // read user and system-wide registry
  297. var rks = new Array("HKCU", "HKLM");
  298. for (i in rks) {
  299. read_registry(rks[i]);
  300. }
  301. }
  302. get_fop_home();
  303. get_log_choice();
  304. get_log_level();
  305. var javacmd = "";
  306. get_java_cmd();
  307. var local_classpath = "";
  308. get_local_classpath();
  309. // Show script help if requested
  310. if (show_help) {
  311. // fop_exec_args = "";
  312. keep_open = true;
  313. WScript.Echo(help_text);
  314. }
  315. // fop_exec();
  316. fop_run();