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
13 /***
14 * Loads a DOM tree from file, puts into Context as variable.
15 *
16 * @author tomp
17 */
18 public class Load extends FileCommand {
19 /***
20 * Loads a DOM tree from file, puts into Context as variable.
21 *
22 * @param context
23 * DOCUMENT ME!
24 * @return DOCUMENT ME!
25 * @throws Exception
26 * DOCUMENT ME!
27 */
28 public int execute(Context context) throws Exception {
29
30 DocumentBuilder db = context.getDocumentBuilder();
31
32 try {
33 File f = getFile(context, fileName);
34
35 Document doc = db.parse(f);
36
37 context.put(var, doc);
38
39 return done(context, 0);
40
41 } catch (IOException ioe) {
42 return done(context, 1);
43
44 } catch (SAXException se) {
45 return done(context, 2);
46 }
47 }
48
49 /***
50 * @return "LOAD " + var + " <- " + fileName;
51 */
52 public String toString() {
53 return "LOAD " + var + " <- " + fileName;
54 }
55 }
56
57
58
59
60
61
62
63
64
65
66