View Javadoc

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