View Javadoc

1   package net.sf.tomp.xtcl.command;
2   
3   import net.sf.tomp.xtcl.Context;
4   
5   import org.w3c.dom.Document;
6   import org.xml.sax.SAXException;
7   
8   import javax.xml.parsers.DocumentBuilder;
9   
10  import java.io.File;
11  import java.io.IOException;
12  import java.io.PrintStream;
13  
14  /***
15   * DOCUMENT ME!
16   * 
17   * @author tomp
18   */
19  public class Load extends FileCommand {
20      /***
21       * DOCUMENT ME!
22       * 
23       * @param context DOCUMENT ME!
24       * @return DOCUMENT ME!
25       * @throws Exception DOCUMENT ME!
26       */
27      public int execute(Context context) throws Exception {
28          DocumentBuilder db = context.getDocumentBuilder();
29          PrintStream out = context.getOut();
30  
31          try {
32              File f = null;
33  
34              if (fileName.startsWith("$")) {
35                  f = context.refToFile(fileName);
36              } else {
37                  f = context.getFile(fileName);
38              }
39  
40              Document doc = db.parse(f);
41  
42              context.put(var, doc);
43  
44              if (out != null) {
45                  out.print("OK " + this);
46              }
47  
48              return 0;
49          } catch (IOException ioe) {
50              if (out != null) {
51                  out.print("ERR " + this + ": " + ioe);
52              }
53  
54              return 1;
55          } catch (SAXException se) {
56              if (out != null) {
57                  out.print("ERR " + this + ": " + se);
58              }
59  
60              return 2;
61          }
62      }
63  
64      /***
65       * DOCUMENT ME!
66       * 
67       * @return DOCUMENT ME!
68       */
69      public String toString() {
70          return "LOAD " + var + " <- " + fileName;
71      }
72  }
73  
74  /*
75   * The contents of this file are subject to the Mozilla Public License Version
76   * 1.1 (the "License"); you may not use this file except in compliance with the
77   * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
78   * Software distributed under the License is distributed on an "AS IS" basis,
79   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
80   * the specific language governing rights and limitations under the License. The
81   * Original Code is: all this file. The Initial Developer of the Original Code
82   * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
83   */