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  
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   * The contents of this file are subject to the Mozilla Public License Version
59   * 1.1 (the "License"); you may not use this file except in compliance with the
60   * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
61   * Software distributed under the License is distributed on an "AS IS" basis,
62   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
63   * the specific language governing rights and limitations under the License. The
64   * Original Code is: all this file. The Initial Developer of the Original Code
65   * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
66   */