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

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