Groovy Documentation

utils.gate.util
Class FileUtils

java.lang.Object
  utils.gate.util.FileUtils

class FileUtils

Some file utilities.

author:
Hamish Cunningham


Property Summary
static Pattern META_ENCODING_PATTERN

A regex pattern used to find the encoding from a meta tag such as

static Pattern XML_ENCODING_PATTERN

A regex pattern used to find the encoding from an XML declaration such as

static Logger log

Logger

 
Constructor Summary
FileUtils()

 
Method Summary
static String decodeHtmlFile(File htmlFile)

Return the String contents of an html file, decoded according to the encodings given in any xml declaration or meta tag.

static List dirList(File dir)

List all the entries in a directory, with dot files last.

static File findFileInTree(String fileName, File topDir, File startDir)

Find the first occurence of a file above a location in a directory tree up to and including a particular top directory.

static String getBody(InputStream htmlFile)

Get the body of an HTML file.

static String getBody(File htmlFile)

Get the body of an HTML file.

static String getBody(String text)

Get the body of an HTML file.

static boolean isValidFileName(String path)

Do some basic validity checks on a file name.

static void putBody(File htmlFile, String text)

Take the html body given by a String text, and place it in the body of the html File htmlFile.

static String replaceFileSuffix(File file, String newSuffix)

Replace the suffix of a file name.

static String replaceFileSuffix(String path, String newSuffix)

Replace the suffix of a file name.

static String setBodyDiv(String pageString, String divId, String text)

Set the value (content) of a DIV occuring in the BODY string of an HTML file according to its ID.

 
Methods inherited from class Object
wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 

Property Detail

META_ENCODING_PATTERN

static Pattern META_ENCODING_PATTERN
A regex pattern used to find the encoding from a meta tag such as


XML_ENCODING_PATTERN

static Pattern XML_ENCODING_PATTERN
A regex pattern used to find the encoding from an XML declaration such as


log

static Logger log
Logger


 
Constructor Detail

FileUtils

FileUtils()


 
Method Detail

decodeHtmlFile

public static String decodeHtmlFile(File htmlFile)
Return the String contents of an html file, decoded according to the encodings given in any xml declaration or meta tag. In order to overcome the chicken and egg situation of reading the encoding out of a file when we don't know the encoding, the method looks at the first four bytes to see if it can work out the encoding. This auto encoding is then used to check for an explicit encoding. The auto encoding byte check assumes an xml declaration at the start of the file - it looks for the first four bytes of that declaration. If there is no xml declaration, it falls back on UTF8 to then look for an explicit encoding. The method could be extended to examine an initial html element tag.
see:
http://www.w3.org/TR/REC-xml/#sec-guessing-no-ext-info
see:
http://www.w3.org/TR/REC-xml/#charencoding


dirList

public static List dirList(File dir)
List all the entries in a directory, with dot files last. Excludes .svn and *.yam entries.


findFileInTree

public static File findFileInTree(String fileName, File topDir, File startDir)
Find the first occurence of a file above a location in a directory tree up to and including a particular top directory.


getBody

public static String getBody(InputStream htmlFile)
Get the body of an HTML file. Tries to use Sitemesh (HTMLPageParser); if that fails uses some imperfect string matching on body tags.


getBody

public static String getBody(File htmlFile)
Get the body of an HTML file. Tries to use Sitemesh (HTMLPageParser); if that fails uses some imperfect string matching on body tags. Try to work out a correct encoding for the file.


getBody

public static String getBody(String text)
Get the body of an HTML file. Tries to use Sitemesh (HTMLPageParser); if that fails uses some imperfect string matching on body tags.


isValidFileName

public static boolean isValidFileName(String path)
Do some basic validity checks on a file name.


putBody

public static void putBody(File htmlFile, String text)
Take the html body given by a String text, and place it in the body of the html File htmlFile. TODO: this appears not to have worked, as it used to be called by PS.overwrite(Map,File,String) but didn't preserve the original HTML header during edits...


replaceFileSuffix

public static String replaceFileSuffix(File file, String newSuffix)
Replace the suffix of a file name. For example: on xyz.html replaces the html part; on xyz does nothing.


replaceFileSuffix

public static String replaceFileSuffix(String path, String newSuffix)
Replace the suffix of a file name. For example: on xyz.html replaces the html part; on xyz does nothing.


setBodyDiv

public static String setBodyDiv(String pageString, String divId, String text)
Set the value (content) of a DIV occuring in the BODY string of an HTML file according to its ID.


 

Groovy Documentation