/* $Id: FileAccess.java,v 1.4 2001/02/26 23:41:01 agarcia3 Exp $
webEditor. The new way in content management
Copyright (C) 2001 Alfredo Garcia
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
package webEditor.util;
import java.io.*;
import java.util.*;
import org.apache.regexp.RE;
/**
* Encapsulates certain file access operations
*
* @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
*/
public class FileAccess
{
/**
* Read the content of a file in a single string
* @param file File to read
* @return String Content of the file
*/
public String readFile (String file)
{
RandomAccessFile fileDesc;
String buffer = null;
String line = null;
try {
fileDesc = new RandomAccessFile (file,"r");
while ( (line = fileDesc.readLine() ) != null ) {
if ( buffer == null ) {
buffer = line + "\n";
}
else {
buffer = buffer + line + "\n";
}
}
} catch (Exception e) {
e.printStackTrace();
}
return (buffer);
}
/**
* Create the directory where the data file is stored
* @param docName doc ID (usually the relative path)
* @param path Absolute path from the document
* @return void
*/
public void createDocDir (String docName, String path)
{
try {
// We use this regular expresion in order to match xml files
// if the docName string doesn�t include the "/" string is because
// is not a normal document
RE r = new RE("/");
if ( r.match (docName) ) {
StringTokenizer token = new StringTokenizer (docName,"/");
// We have strings with the format /yyyy/mm/dd/file.xml
String yearDir = path + token.nextToken();
File fileDesc = new File (yearDir);
if (! fileDesc.exists() ) {
fileDesc.mkdir ();
}
String monthDir = yearDir + "/" + token.nextToken();
fileDesc = new File (monthDir);
if (! fileDesc.exists() ) {
fileDesc.mkdir ();
}
String dayDir = monthDir + "/" + token.nextToken();
fileDesc = new File (dayDir);
if (! fileDesc.exists() ) {
fileDesc.mkdir ();
}
}
}
catch (Exception e) {}
}
}