1 package org.ejen.ext; 22 23 import org.ejen.util.XSLUtil; 24 import org.ejen.util.DOMUtil; 25 import java.io.File ; 26 import java.io.FileFilter ; 27 import java.io.IOException ; 28 import java.net.MalformedURLException ; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.apache.regexp.RE; 32 import org.apache.xpath.NodeSet; 33 import org.apache.xml.utils.WrappedRuntimeException; 34 import org.apache.xalan.extensions.ExpressionContext; 35 36 68 public class FileUtil { 69 public static final String S_TRUE = "true"; 70 public static final String S_FALSE = "false"; 71 72 75 protected FileUtil() {} 76 77 122 public static NodeSet properties(ExpressionContext context, String fileName) { 123 fileName = XSLUtil.evaluate(context, fileName); 124 Document doc = XSLUtil.getContextDocument(context); 125 126 try { 127 File f = new File (fileName); 128 NodeSet ns = new NodeSet(); 129 130 ns.addElement(DOMUtil.createCDATANode(doc, "raw", fileName)); 131 String s = f.getName(); 132 133 ns.addElement(DOMUtil.createCDATANode(doc, "name", s)); 134 int i = s.indexOf('.'); 135 136 ns.addElement(DOMUtil.createCDATANode(doc, "name-no-ext", 137 ((i != -1) ? s.substring(0, i) : s))); 138 s = f.getParent(); 139 ns.addElement(DOMUtil.createCDATANode(doc, "parent", 140 ((s != null) ? s : ""))); 141 ns.addElement(DOMUtil.createCDATANode(doc, "path", f.getPath())); 142 ns.addElement(DOMUtil.createCDATANode(doc, "is-absolute", 143 (f.isAbsolute() ? S_TRUE : S_FALSE))); 144 ns.addElement(DOMUtil.createCDATANode(doc, "absolute-path", 145 f.getAbsolutePath())); 146 try { 147 ns.addElement(DOMUtil.createCDATANode(doc, "canonical-path", 148 f.getCanonicalPath())); 149 } catch (IOException e) {} 150 try { 151 ns.addElement(DOMUtil.createCDATANode(doc, "url", 152 f.toURL().toString())); 153 } catch (MalformedURLException e) {} 154 try { 155 ns.addElement(DOMUtil.createCDATANode(doc, "can-read", 156 (f.canRead() ? S_TRUE : S_FALSE))); 157 } catch (SecurityException e) {} 158 try { 159 ns.addElement(DOMUtil.createCDATANode(doc, "can-write", 160 (f.canWrite() ? S_TRUE : S_FALSE))); 161 } catch (SecurityException e) {} 162 try { 163 ns.addElement(DOMUtil.createCDATANode(doc, "exists", 164 (f.exists() ? S_TRUE : S_FALSE))); 165 } catch (SecurityException e) {} 166 try { 167 ns.addElement(DOMUtil.createCDATANode(doc, "is-directory", 168 (f.isDirectory() ? S_TRUE : S_FALSE))); 169 } catch (SecurityException e) {} 170 try { 171 ns.addElement(DOMUtil.createCDATANode(doc, "is-hidden", 172 (f.isHidden() ? S_TRUE : S_FALSE))); 173 } catch (SecurityException e) {} 174 try { 175 ns.addElement(DOMUtil.createCDATANode(doc, "last-modified", 176 Long.toString(f.lastModified()))); 177 } catch (SecurityException e) {} 178 try { 179 ns.addElement(DOMUtil.createCDATANode(doc, "length", 180 Long.toString(f.length()))); 181 } catch (SecurityException e) {} 182 183 return ns; 184 } catch (WrappedRuntimeException e) { 185 throw e; 186 } catch (Exception e) { 187 throw new WrappedRuntimeException(e); 188 } 189 } 190 191 233 public static NodeSet listFiles(ExpressionContext context, String path) { 234 return listFiles(context, path, null, false); 235 } 236 237 267 public static NodeSet listFiles(ExpressionContext context, String path, 268 String filter) { 269 return listFiles(context, path, filter, false); 270 } 271 272 297 public static NodeSet listFiles(ExpressionContext context, String path, 298 String filter, boolean rec) { 299 path = XSLUtil.evaluate(context, path); 300 final String ffilter = (filter != null) 301 ? XSLUtil.evaluate(context, filter) 302 : null; 303 Document doc = XSLUtil.getContextDocument(context); 304 305 try { 306 File f = new File (path); 307 308 if (!f.isDirectory()) { 309 throw new IllegalArgumentException (path + " is not a directory."); 310 } 311 FileFilter ff = null; 312 313 if (ffilter != null) { 314 ff = new FileFilter () { 315 RE _re = new RE(ffilter); 316 public boolean accept(File f) { 317 return f.isDirectory() || _re.match(f.getName()); 318 } 319 320 public String getDescription() { 321 return ffilter; 322 } 323 }; 324 } 325 File [] files = f.listFiles(ff); 326 NodeSet ns = new NodeSet(); 327 328 appendFiles(doc, ns, files, ff, rec); 329 return ns; 330 } catch (Exception e) { 331 throw new WrappedRuntimeException(e); 332 } 333 } 334 335 345 protected static void appendFiles(Document doc, NodeSet ns, File [] files, 346 FileFilter ff, boolean rec) throws Exception { 347 for (int i = 0; i < files.length; i++) { 348 File f = files[i]; 349 350 if (!f.isDirectory()) { 351 Element file = doc.createElement("file"); 352 Element elt = doc.createElement("path"); 353 String path = f.getCanonicalFile().getParent(); 354 355 elt.appendChild(doc.createCDATASection((path != null) 356 ? path 357 : "")); 358 file.appendChild(elt); 359 elt = doc.createElement("sep"); 360 elt.appendChild(doc.createCDATASection(f.separator)); 361 file.appendChild(elt); 362 elt = doc.createElement("name"); 363 elt.appendChild(doc.createCDATASection(f.getName())); 364 file.appendChild(elt); 365 ns.addElement(file); 366 } else if (rec) { 367 appendFiles(doc, ns, f.listFiles(ff), ff, rec); 368 } 369 } 370 } 371 } 372
| Popular Tags
|