org.ejen.ext
Class FileUtil

java.lang.Object
  |
  +--org.ejen.ext.FileUtil

public class FileUtil
extends Object

File utility (static methods).

Usage (XSL stylesheet)

  <?xml version="1.0" encoding="iso-8859-1"?>

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  ...
                  xmlns:fiu="org.ejen.ext.FileUtil"
                  exclude-result-prefixes="fiu ..."
                  version="1.0">

    <xsl:output method="xml" encoding="iso-8859-1"/>

    <xsl:template match="ejen">

      <xsl:copy-of select="fiu:properties($file-name)"/>

      <xsl:copy-of select="fiu:listFiles('../../src')"/>
      <xsl:copy-of select="fiu:listFiles('../../src','\.java$|\.jjt$')"/>
      <xsl:copy-of select="fiu:listFiles('../../src','\.java$|\.jjt$',true())"/>

    </xsl:template>

  </xsl:stylesheet>
 

Version:
1.0
Author:
F. Wolff

Field Summary
static String S_FALSE
           
static String S_TRUE
           
 
Constructor Summary
protected FileUtil()
          Protected constructor (prevents instanciation).
 
Method Summary
protected static void appendFiles(Document doc, NodeSet ns, File[] files, FileFilter ff, boolean rec)
          Creates "file" Nodes and adds them to a NodeSet.
static NodeSet listFiles(ExpressionContext context, String path)
          Returns a NodeSet that contains files list for a given directory.
static NodeSet listFiles(ExpressionContext context, String path, String filter)
          Returns a NodeSet that contains a filtered files list for a given directory.
static NodeSet listFiles(ExpressionContext context, String path, String filter, boolean rec)
          Returns a NodeSet that contains a filtered files list for a given directory and (optionally) for its sub-directories.
static NodeSet properties(ExpressionContext context, String fileName)
          Returns a NodeSet that contains informations about the fileName file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

S_TRUE

public static final String S_TRUE
See Also:
Constant Field Values

S_FALSE

public static final String S_FALSE
See Also:
Constant Field Values
Constructor Detail

FileUtil

protected FileUtil()
Protected constructor (prevents instanciation).

Method Detail

properties

public static NodeSet properties(ExpressionContext context,
                                 String fileName)
Returns a NodeSet that contains informations about the fileName file.


  <xsl:value-of select="fiu:properties('$file-name')"/>
 

The returned NodeSet will have the following structure:


  <raw><![CDATA[../../ejb-1.1/bmp-jboss-hsqldb/out/test/src/org/ejb/test/EJB_DOUBLE_UNIQUE_PK.java]]></raw>
  <name><![CDATA[EJB_DOUBLE_UNIQUE_PK.java]]></name>
  <name-no-ext><![CDATA[EJB_DOUBLE_UNIQUE_PK]]></name-no-ext>
  <parent><![CDATA[../../ejb-1.1/bmp-jboss-hsqldb/out/test/src/org/ejb/test]]></parent>
  <path><![CDATA[../../ejb-1.1/bmp-jboss-hsqldb/out/test/src/org/ejb/test/EJB_DOUBLE_UNIQUE_PK.java]]></path>
  <is-absolute><![CDATA[false]]></is-absolute>
  <absolute-path><![CDATA[/devs/java/ejen-1.0-pre2/examples/simples/file/../../ejb-1.1/bmp-jboss-hsqldb/out/test/src/org/ejb/test/EJB_DOUBLE_UNIQUE_PK.java]]></absolute-path>
  <canonical-path><![CDATA[/devs/java/ejen-1.0-pre2/examples/ejb-1.1/bmp-jboss-hsqldb/out/test/src/org/ejb/test/EJB_DOUBLE_UNIQUE_PK.java]]></canonical-path>
  <url><![CDATA[file:/devs/java/ejen-1.0-pre2/examples/simples/file/../../ejb-1.1/bmp-jboss-hsqldb/out/test/src/org/ejb/test/EJB_DOUBLE_UNIQUE_PK.java]]></url>
  <can-read><![CDATA[true]]></can-read>
  <can-write><![CDATA[true]]></can-write>
  <exists><![CDATA[true]]></exists>
  <is-directory><![CDATA[false]]></is-directory>
  <is-hidden><![CDATA[false]]></is-hidden>
  <last-modified><![CDATA[1016763382000]]></last-modified>
  <length><![CDATA[2380]]></length>
 

"raw" node contains simply the fileName parameter value. Some of those Nodes may not be available (MalformedURLException [url], IOException [canonical-path], or SecurityException [can-read, can-write, ..., length]).

See File.

XSLT parameters:
[Mandatory/AVT] name of the file.

Parameters:
context - automatically passed by the xalan extension mechanism.
fileName - name of the file.
Returns:
file properties.
Throws:
WrappedRuntimeException - DOM error.

listFiles

public static NodeSet listFiles(ExpressionContext context,
                                String path)
Returns a NodeSet that contains files list for a given directory.

This method does not filter anything but directories (only regular file names are returned) and is not recursive (only file names in the given directory are listed).


  <xsl:value-of select="fiu:listFiles('../../src/org/ejen/ext')"/>
 

The returned NodeSet will have the following structure:


  <file>
    <path><![CDATA[/devs/java/ejen-1.0-pre3/src/org/ejen/ext]]></path>
    <sep><![CDATA[/]]></sep>
    <name><![CDATA[XMLInclude.java]]></name>
  </file>
  <file>
    <path><![CDATA[/devs/java/ejen-1.0-pre3/src/org/ejen/ext]]></path>
    <sep><![CDATA[/]]></sep>
    <name><![CDATA[FileUtil.java]]></name>
  </file>
  ...
 

"path" Nodes contain "canonical" paths (absolute, without symbolic links, etc.). Because this method is not recursive, "path" Nodes will be always equal to the canonical representation of the "path" argument.

XSLT parameters:
[Mandatory/AVT] directory path.

Parameters:
context - automatically passed by the xalan extension mechanism.
path - directory path.
Returns:
a NodeSet that contains "file" Nodes.
Throws:
WrappedRuntimeException - DOM or IO errors...

listFiles

public static NodeSet listFiles(ExpressionContext context,
                                String path,
                                String filter)
Returns a NodeSet that contains a filtered files list for a given directory.

This method is not recursive (only file names in the given directory are listed). The "filter" argument must be a regular expression (see RE) and only refers to regular file names (neither directory names nor full path file names).

Tips: "\.java$" means "accept file names that end with '.java'". "\.java$|\.jjt$" means "accept file names that end with '.java' or '.jjt'".


  <xsl:value-of select="fiu:listFiles('../../src','\.java$|\.jjt$')"/>
 

See listFiles(ExpressionContext, String).

XSLT parameters:
[Mandatory/AVT] directory path.
[Mandatory/AVT] file name filter.

Parameters:
context - automatically passed by the xalan extension mechanism.
path - directory path.
filter - file name filter.
Returns:
a NodeSet that contains "file" Nodes.
Throws:
WrappedRuntimeException - DOM or IO errors...

listFiles

public static NodeSet listFiles(ExpressionContext context,
                                String path,
                                String filter,
                                boolean rec)
Returns a NodeSet that contains a filtered files list for a given directory and (optionally) for its sub-directories.

This method is recursive if the "rec" argument is true. The "filter" argument must be a regular expression (see listFiles(ExpressionContext, String,String)).


  <xsl:value-of select="fiu:listFiles('../../src','\.java$|\.jjt$',true())"/>
 

XSLT parameters:
[Mandatory/AVT] directory path.
[Mandatory/AVT] file name filter.
[Mandatory] true() or false(): list recursively or not.

Parameters:
context - automatically passed by the xalan extension mechanism.
path - directory path.
filter - file name filter.
rec - true (recursive) or false.
Returns:
a NodeSet that contains "file" Nodes.
Throws:
WrappedRuntimeException - DOM or IO errors...

appendFiles

protected static void appendFiles(Document doc,
                                  NodeSet ns,
                                  File[] files,
                                  FileFilter ff,
                                  boolean rec)
                           throws Exception
Creates "file" Nodes and adds them to a NodeSet.

Parameters:
doc - Document to be used for Node creation.
ns - NodeSet (where Nodes have to be added).
files - files list.
ff - FileFilter to use.
rec - true (recursive) or false.
Throws:
WrappedRuntimeException - DOM or IO errors...
Exception