KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > visual > ConvertUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: ConvertUtils.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.visual;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.apache.commons.logging.Log;
26
27 /**
28  * Utilities for converting files with external converters.
29  */

30 public class ConvertUtils {
31
32     /**
33      * Calls an external converter application (GhostScript, for example).
34      * @param cmd the full command
35      * @param envp array of strings, each element of which has environment variable settings
36      * in format name=value.
37      * @param workDir the working directory of the subprocess, or null if the subprocess should
38      * inherit the working directory of the current process.
39      * @param log the logger to log output by the external application to
40      * @throws IOException in case the external call fails
41      */

42     public static void convert(String JavaDoc cmd, String JavaDoc[] envp, File JavaDoc workDir, final Log log)
43                 throws IOException JavaDoc {
44         log.debug(cmd);
45
46         Process JavaDoc process = null;
47         try {
48             process = Runtime.getRuntime().exec(cmd, envp, null);
49
50             //Redirect stderr output
51
RedirectorLineHandler errorHandler = new AbstractRedirectorLineHandler() {
52                 public void handleLine(String JavaDoc line) {
53                     log.error("ERR> " + line);
54                 }
55             };
56             StreamRedirector errorRedirector
57                 = new StreamRedirector(process.getErrorStream(), errorHandler);
58
59             //Redirect stdout output
60
RedirectorLineHandler outputHandler = new AbstractRedirectorLineHandler() {
61                 public void handleLine(String JavaDoc line) {
62                     log.debug("OUT> " + line);
63                 }
64             };
65             StreamRedirector outputRedirector
66                 = new StreamRedirector(process.getInputStream(), outputHandler);
67             new Thread JavaDoc(errorRedirector).start();
68             new Thread JavaDoc(outputRedirector).start();
69                 
70             process.waitFor();
71         } catch (java.lang.InterruptedException JavaDoc ie) {
72             throw new IOException JavaDoc("The call to the external converter failed: " + ie.getMessage());
73         } catch (java.io.IOException JavaDoc ioe) {
74             throw new IOException JavaDoc("The call to the external converter failed: " + ioe.getMessage());
75         }
76
77         int exitValue = process.exitValue();
78         if (exitValue != 0) {
79             throw new IOException JavaDoc("The call to the external converter failed. Result: "
80                     + exitValue);
81         }
82
83     }
84     
85     
86 }
87
Popular Tags