View Javadoc

1   package gov.noaa.mptools;
2   
3   /***
4    * Title: MPRunner.java
5    * Description:  runs mp, giving the results and file outputs as strings.
6    * Copyright:    Copyright (c) 2001
7    * Company:      NGDC
8    * @author  Travis
9    * @version 1.0
10   */
11  
12  import java.util.Date;
13  import java.io.*;
14  
15  /*** A java wrapper around "Metadata Parser" also known as mp written by Peter Sweitzer.
16   */
17  public class MPRunner {
18  
19      /*** Where to store the temporary files generated by this class.
20       */    
21    String tempDir = "/tmp";
22    /*** The specific command.  Under linux this command is mp.lnx.  It could be renamed to mp.
23     * Whatever....
24     */  
25    String mpCommand = "mp";
26    /*** The conf file for mp wich defines an extensions and hints file
27     * amongst other things.
28     */  
29    String configFile = null;
30    /*** The alias file to use.  If null, it is ignored.
31     */  
32    String aliasFile = null;
33  
34    /*** Creates a new instance of MPRunner
35     */  
36    public MPRunner() {
37    }
38  
39    /*** Set the directory to place the temporary files generated by this class.
40     * @param tDir The relative or specific path.
41     */  
42    public void setTempDir(String tDir){
43      tempDir=tDir;
44      }
45  
46    /*** Sets the location and the name of the mp command.
47     * @param str The complete path to mp and the mp command.  Or the relative path and the mp
48     * command.
49     */  
50    public void setMPCommand(String str){
51      this.mpCommand=str;
52      }
53  
54  
55    /*** Sets the location and name of the conrfig file to use with mp.  (The -c option)
56     * @param conf The config file.
57     */  
58    public void setConfigFile(String conf){
59      configFile=conf;
60      }
61  
62    /*** Sets the location and name of the alias file.
63     * @param alias the alias file.
64     */  
65    public void setAliasFile(String alias){
66      aliasFile=alias;
67      }
68  
69    /*** This will run mp against the specified file.
70     * @return the output from mp
71     * @param mpFile full path to the xmlFile to be parsed
72     * @throws MPException If something goes wrong.
73     */
74    public String runMPWithFileName(String mpFile) throws MPException{
75      //first we make our command line
76      StringBuffer sbCommand = new StringBuffer(mpCommand);
77      if (configFile != null){
78         sbCommand.append(" -c ").append(configFile);
79         }
80      if (aliasFile != null){
81        sbCommand.append(" -a ").append(aliasFile);
82        }
83      sbCommand.append(" ").append(mpFile);
84  
85      Runtime runtime = Runtime.getRuntime();
86      Process process;
87      try {
88        process = runtime.exec(sbCommand.toString());
89        }
90      catch (IOException e){
91        throw new MPException ("MP DID NOT RUN, IOEXCEPTION " + e.toString());
92        }
93      int status;
94  
95      InputStream pin = process.getErrorStream();
96      InputStreamReader cin = new InputStreamReader(pin);
97      BufferedReader in = new BufferedReader(cin);
98      StringBuffer sb = new StringBuffer();
99      String line=null;
100     try {
101        while ((line = in.readLine())!=null){
102           //hack to work around results in mp to ignore
103 //          if (line.indexOf("(unknown)")<0 && line.indexOf("UTF-8")<0){
104             sb.append(line).append("\n");
105 //          }
106  
107           
108           }
109         in.close();
110         }
111     catch (IOException e){
112       throw new MPException("MP DID NOT RUN, IO EXCEPTION "
113           + "Command:" + sbCommand.toString() + "\n" + e.toString());
114       }
115 
116     try {
117       status = process.waitFor();
118       }
119     catch (InterruptedException e){
120       throw new MPException ("MP DID NOT RUN, INERRUPTED WHILE WAITING " 
121            + "command:" + sbCommand.toString() + "\n" + e.toString());
122       }
123 
124     if (status != 0){
125        throw new MPException ("MP DID NOT RUN, ABNORMAL TERMINATION status:"+status+"\n"
126 		+ "Command:" + sbCommand.toString());
127        }
128 
129     //mp created an error file, this is what we want to return is string form
130     //it is created in the same directory as mpFile, but it has a .err extension
131 
132 
133     return sb.toString();
134     }
135 
136 
137   /*** this creates a file containing the specified string which is then parsed by mp
138    * @return error output from mp
139    * @param xml The xml of the record to run against mp.
140    * @throws MPException If something goes wrong.
141    *
142    */
143   public String runMPWithXMLString(String xml) throws MPException {
144     //we will create a file with the xml representation
145     Date now = new Date();
146     StringBuffer sbFile = new StringBuffer();
147     sbFile.append(tempDir).append("/");
148     sbFile.append((now.toString()).replace(' ','_'));
149     sbFile.append(".xml");
150 
151     //need to somehow threadsafe this file
152     File xmlFile = new File (sbFile.toString());
153     FileOutputStream fo;
154     try {
155       FileWriter fWriter = new FileWriter(xmlFile);
156       fWriter.write(xml);
157       fWriter.write("\n");
158       fWriter.close();
159       }
160     catch (IOException e){
161       throw new MPException ("MP DID NOT RUN, IOEXCEPTION"+e.toString());
162       }
163 
164 
165 
166 
167       String returnString = runMPWithFileName(sbFile.toString());
168       xmlFile.delete();
169     return returnString;
170     }
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 }