1 package org.ejen; 22 23 import org.ejen.util.XSLUtil; 24 import java.util.Properties ; 25 import java.io.File ; 26 import java.io.OutputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.StringWriter ; 29 import javax.xml.transform.stream.StreamResult ; 30 import javax.xml.transform.dom.DOMSource ; 31 import org.w3c.dom.NodeList ; 32 import org.apache.xpath.XPathAPI; 33 import org.apache.xalan.transformer.TransformerImpl; 34 import org.apache.xalan.processor.StylesheetHandler; 35 36 77 public class EjenTemplateNode extends EjenStylesheetNode { 78 protected String _foreach = null; 79 protected String _filepattern = null; 80 protected String _outdated = null; 81 82 86 public String nodeName() { 87 return "template"; 88 } 89 90 94 public Properties getAttributes() { 95 Properties attrs = super.getAttributes(); 96 97 if (_foreach != null) { 98 attrs.setProperty("foreach", _foreach); 99 } 100 if (_filepattern != null) { 101 attrs.setProperty("filepattern", _filepattern); 102 } 103 if (_outdated != null) { 104 attrs.setProperty("outdated", _outdated); 105 } 106 return attrs; 107 } 108 109 159 public void setForeach(String foreach) { 160 _foreach = foreach; 161 } 162 163 181 public void setFilepattern(String filepattern) { 182 _filepattern = filepattern; 183 } 184 185 190 public void setOutdated(String outdate) { 191 _outdated = outdate; 192 } 193 194 198 public void process() { 199 super.process(); 200 TransformerImpl ti = null; 201 DOMSource src = null; 202 203 try { 204 ti = (TransformerImpl) (getFromContext(CTX_TRANSFORMER_IMPL)); 205 src = (DOMSource ) (getFromGlobalContext(CTX_DOM_SOURCE)); 206 } catch (Exception e) { 207 throw new EjenException(this, null, e); 208 } 209 if (ti == null) { 210 throw new EjenException(this, 211 "no '" + CTX_TRANSFORMER_IMPL + "' in context"); 212 } 213 if (src == null) { 214 throw new EjenException(this, 215 "no '" + CTX_DOM_SOURCE + "' in global context"); 216 } 217 if (_foreach != null) { 218 NodeList nl = null; 219 220 try { 221 ti.setParameter("root", src.getNode()); 222 nl = XPathAPI.selectNodeList(src.getNode(), 223 evaluateAVT(ti, _foreach)); 224 } catch (Exception e) { 225 throw new EjenException(this, 226 "invalid 'foreach' attribute: " + _foreach, e); 227 } 228 for (int i = 0; i < nl.getLength(); i++) { 229 DOMSource nodeSrc = null; 230 231 try { 232 nodeSrc = new DOMSource (nl.item(i)); 233 } catch (Exception e) { 234 throw new EjenException(this, 235 "invalid 'foreach' attribute: " + _foreach, e); 236 } 237 processTemplateStreamResult(ti, nodeSrc); 238 } 239 } else { 240 processTemplateStreamResult(ti, src); 241 } 242 } 243 244 250 private void processTemplateStreamResult(TransformerImpl ti, DOMSource src) { 251 StylesheetHandler sh = null; 252 253 try { 254 sh = (StylesheetHandler) (getFromGlobalContext(CTX_STYLESHEET_HANDLER)); 255 } catch (Exception e) { 256 throw new EjenException(this, null, e); 257 } 258 if (sh == null) { 259 throw new EjenException(this, 260 "no '" + CTX_STYLESHEET_HANDLER + "' in context"); 261 } 262 OutputStream outputs = null; 263 264 try { 265 StreamResult sres = null; 266 267 if (_filepattern != null) { 268 String fileName = XSLUtil.evaluateAttribute(sh, 271 ti.getXPathContext(), src.getNode(), _filepattern); 272 boolean flag = false; 273 274 if (_outdated != null && _outdated.equals("true")) { 275 flag = true; 276 } 277 if (!flag || outdated(fileName)) { 278 sendMessageEvent("Creating '" + fileName + "'"); 279 File f = new File (fileName); 280 File pf = f.getParentFile(); 281 282 if (pf != null) { 283 pf.mkdirs(); 284 } 285 outputs = new FileOutputStream (f.getPath()); 286 sres = new StreamResult (outputs); 287 } else { 288 return; 289 } 290 } else { 291 sres = new StreamResult (new StringWriter ()); 292 } 293 ti.transform(src, sres); 294 } catch (Exception e) { 295 throw new EjenException(this, null, e); 296 } 297 finally { 298 if (outputs != null) { 299 try { 300 outputs.close(); 301 } catch (Exception e) {} 302 finally { 303 outputs = null; 304 } 305 } 306 } 307 } 308 309 313 protected boolean outdated(String fileName) { 314 File srcFile = new File ((String ) getFromGlobalContext("DOM_SOURCE_FILE")); 315 File genFile = new File (fileName); 316 317 if (!genFile.exists() || !srcFile.exists()) { return true; 319 } 320 long slm = srcFile.lastModified(); 321 322 if (slm == 0L) { return true; 324 } 325 long glm = genFile.lastModified(); 326 327 if (glm == 0L) { return true; 329 } 330 return slm >= glm; } 332 } 333 | Popular Tags |