1 package org.ejen; 22 23 import java.util.Properties ; 24 import java.io.File ; 25 import java.io.OutputStream ; 26 import java.io.FileOutputStream ; 27 import javax.xml.transform.Transformer ; 28 import javax.xml.transform.dom.DOMSource ; 29 import javax.xml.transform.stream.StreamResult ; 30 import javax.xml.transform.OutputKeys ; 31 import org.apache.xalan.processor.TransformerFactoryImpl; 32 import org.apache.xalan.templates.OutputProperties; 33 34 75 public class EjenSaveNode extends EjenChildNode { 76 protected String _file = null; 77 protected String _encoding = "iso-8859-1"; 78 protected String _indent = "yes"; 79 protected String _amount = "2"; 80 81 85 public String nodeName() { 86 return "save"; 87 } 88 89 93 public Properties getAttributes() { 94 Properties attrs = super.getAttributes(); 95 96 if (_file != null) { 97 attrs.setProperty("file", _file); 98 } 99 if (_encoding != null) { 100 attrs.setProperty("encoding", _encoding); 101 } 102 if (_indent != null) { 103 attrs.setProperty("indent", _indent); 104 } 105 if (_amount != null) { 106 attrs.setProperty("amount", _amount); 107 } 108 return attrs; 109 } 110 111 115 public void setFile(String file) { 116 _file = file; 117 } 118 119 123 public void setEncoding(String encoding) { 124 _encoding = encoding; 125 } 126 127 131 public void setIndent(String indent) { 132 _indent = indent; 133 } 134 135 139 public void setAmount(String amount) { 140 _amount = amount; 141 } 142 143 147 public void check() { 148 super.check(); 149 if (_file == null) { 150 throw new EjenException(this, "No 'file' attribute"); 151 } 152 } 153 154 158 public void process() { 159 super.process(); 160 TransformerFactoryImpl tfi = null; 161 DOMSource src = null; 162 163 try { 164 tfi = (TransformerFactoryImpl) (getFromGlobalContext(CTX_TRANSFORMER_FACTORY_IMPL)); 165 src = (DOMSource ) (getFromGlobalContext(CTX_DOM_SOURCE)); 166 } catch (Exception e) { 167 throw new EjenException(this, null, e); 168 } 169 if (tfi == null) { 170 throw new EjenException(this, 171 "no '" + CTX_TRANSFORMER_FACTORY_IMPL 172 + "' in global context"); 173 } 174 if (src == null) { 175 throw new EjenException(this, 176 "no '" + CTX_DOM_SOURCE + "' in global context"); 177 } 178 OutputStream outputs = null; 179 180 try { 181 File f = new File (evaluateAVT(_file)); 182 File pf = f.getParentFile(); 183 184 if (pf != null) { 185 pf.mkdirs(); 186 } 187 outputs = new FileOutputStream (f.getPath()); 188 Transformer serializer = tfi.newTransformer(); 189 190 serializer.setOutputProperty(OutputKeys.INDENT, evaluateAVT(_indent)); 191 serializer.setOutputProperty(OutputKeys.METHOD, "xml"); 192 serializer.setOutputProperty(OutputKeys.ENCODING, 193 evaluateAVT(_encoding)); 194 serializer.setOutputProperty(OutputProperties.S_KEY_INDENT_AMOUNT, 195 evaluateAVT(_amount)); 196 serializer.transform(src, new StreamResult (outputs)); 197 } catch (Exception e) { 198 throw new EjenException(this, _file, e); 199 } 200 finally { 201 if (outputs != null) { 202 try { 203 outputs.close(); 204 } catch (Exception e) {} 205 finally { 206 outputs = null; 207 } 208 } 209 } 210 } 211 } 212 | Popular Tags |