package org.w3c.tools.resources ;
import java.io.* ;
public class FileResource extends FramedResource {
protected static int ATTR_FILENAME = -1 ;
protected static int ATTR_FILESTAMP = -1 ;
protected static int ATTR_FILE_LENGTH = -1 ;
protected static int ATTR_FILE_BACKUP = -1 ;
static {
Attribute a = null ;
Class cls = null ;
try {
cls = Class.forName("org.w3c.tools.resources.FileResource") ;
} catch (Exception ex) {
ex.printStackTrace();
System.exit(0);
}
a = new FilenameAttribute("filename"
, null
, Attribute.EDITABLE) ;
ATTR_FILENAME = AttributeRegistry.registerAttribute(cls, a) ;
a = new DateAttribute("file-stamp"
, new Long(-1)
, Attribute.COMPUTED) ;
ATTR_FILESTAMP = AttributeRegistry.registerAttribute(cls, a) ;
a = new IntegerAttribute("file-length"
, null
, Attribute.COMPUTED);
ATTR_FILE_LENGTH = AttributeRegistry.registerAttribute(cls,a);
a = new BooleanAttribute("backup"
, Boolean.FALSE
, Attribute.EDITABLE);
ATTR_FILE_BACKUP = AttributeRegistry.registerAttribute(cls,a);
}
protected File file = null ;
public String getFilename() {
return (String) getValue(ATTR_FILENAME, null);
}
public int getFileLength() {
return ((Integer) getValue(ATTR_FILE_LENGTH,
new Integer(0))).intValue();
}
public long getFileStamp() {
return getLong(ATTR_FILESTAMP, (long) -1) ;
}
public boolean getBackupFlag() {
return getBoolean(ATTR_FILE_BACKUP, false) ;
}
@return
public File getBackupFile() {
File file = getFile() ;
String name = file.getName() ;
return new File(file.getParent(), name+"~") ;
}
@param in@return @exception IOException
public synchronized boolean newContent(InputStream in)
throws IOException
{
File file = getFile() ;
boolean created = (! file.exists() | (file.length() == 0));
String name = file.getName() ;
File temp = new File(file.getParent(), "#"+name+"#") ;
String iomsg = null ;
try {
FileOutputStream fout = new FileOutputStream(temp) ;
byte buf[] = new byte[4096] ;
for (int got = 0 ; (got = in.read(buf)) > 0 ; )
fout.write(buf, 0, got) ;
fout.close() ;
} catch (IOException ex) {
iomsg = ex.getMessage() ;
} finally {
if ( iomsg != null ) {
temp.delete() ;
throw new IOException(iomsg) ;
} else {
if (getBackupFlag()) {
File backup = getBackupFile();
if ( backup.exists() )
backup.delete();
file.renameTo(getBackupFile()) ;
}
temp.renameTo(file) ;
updateFileAttributes() ;
}
}
return created;
}
@return
public long checkContent() {
File file = getFile() ;
long lmt = file.lastModified() ;
long cmt = getFileStamp() ;
if ((cmt < 0) || (cmt < lmt)) {
updateFileAttributes() ;
return getLastModified() ;
} else {
return cmt;
}
}
public synchronized void setValue(int idx, Object value) {
super.setValue(idx, value) ;
if ((idx == ATTR_FILENAME) || (idx == ATTR_IDENTIFIER))
file = null;
}
public synchronized File getFile() {
if ( file == null ) {
String name = getFilename() ;
if ( name == null )
name = getIdentifier() ;
ResourceReference rr = getParent();
ResourceReference rrtemp = null;
Resource p = null;
while ( true ) {
try {
if (rr == null)
return null;
p = rr.lock();
if (p instanceof DirectoryResource) {
file = new File(((DirectoryResource) p)
.getDirectory(), name);
return file;
}
rrtemp = p.getParent();
} catch (InvalidResourceException ex) {
return null;
} finally {
if (rr != null)
rr.unlock();
}
rr = rrtemp;
}
}
return file ;
}
public synchronized boolean verify()
throws MultipleLockException
{
File file = getFile();
if ( ! file.exists() ) {
ResourceReference rr = getParent();
ResourceReference rrtemp = null;
Resource p = null;
while ( true ) {
try {
if (rr == null)
return false;
p = rr.lock();
if (p instanceof DirectoryResource) {
DirectoryResource d = (DirectoryResource) p;
if ( ! d.getExtensibleFlag() )
return false;
else {
String msg = file+
": deleted, removing the FileResource.";
getServer().errlog(this, msg);
delete();
return false;
}
}
rrtemp = p.getParent();
} catch (InvalidResourceException ex) {
return false;
} finally {
if (rr != null)
rr.unlock();
}
rr = rrtemp;
}
} else {
return true;
}
}
public void updateFileAttributes() {
File file = getFile() ;
setValue(ATTR_FILESTAMP, new Long(file.lastModified()));
setValue(ATTR_FILE_LENGTH, new Integer((int)file.length()));
return ;
}
public void updateAttributes() {
long fstamp = getFile().lastModified() ;
long stamp = getLong(ATTR_FILESTAMP, -1) ;
if ((stamp < 0) || (stamp < fstamp))
updateFileAttributes() ;
}
public void initialize(Object values[]) {
super.initialize(values);
disableEvent();
String filename = getFilename();
if ( filename != null ) {
ResourceReference rr = getParent();
try {
Resource parent = rr.lock();
setValue(ATTR_URL, parent.getURLPath()+filename);
} catch (InvalidResourceException ex) {
} finally {
rr.unlock();
}
}
enableEvent();
}
}