public void read(File dir,File toMark)
throws XMException
{
try
{
if(toMark != null)
toMark = toMark.getCanonicalFile();
if(contentHandler == null)
return;
if(!dir.isDirectory())
messenger.fatal(new XMException(Resources.getString("notdirectory"),new Object[] { dir.getPath() }));
if(!embedded)
contentHandler.startDocument();
contentHandler.startPrefixMapping("xm",namespaceURI);
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute("","xm","xmlns:xm","CDATA",namespaceURI);
contentHandler.startElement(namespaceURI,"Directory","xm:Directory",attributes);
attributes.clear();
File[] files = dir.listFiles();
File file = null;
char[] chars = null;
for(int i = 0;i < files.length;i++)
{
file = files[i];
attributes.addAttribute("","isDirectory","isDirectory","CDATA",String.valueOf(file.isDirectory()));
attributes.addAttribute("","isFile","isFile","CDATA",String.valueOf(file.isFile()));
attributes.addAttribute("","isHidden","isHidden","CDATA",String.valueOf(file.isHidden()));
if(toMark != null)
{
// use the canonical file for comparison: the two files
// have been constructed differently and Java may report
// them as different even though they point to the same file
File canonicalFile = file.getCanonicalFile();
attributes.addAttribute("","isMarked","isMarked","CDATA",String.valueOf(canonicalFile.equals(toMark)));
}
else
attributes.addAttribute("","isMarked","isMarked","CDATA","true");
attributes.addAttribute("","canRead","canRead","CDATA",String.valueOf(file.canRead()));
attributes.addAttribute("","canWrite","canWrite","CDATA",String.valueOf(file.canWrite()));
attributes.addAttribute("","length","length","CDATA",String.valueOf(file.length()));
attributes.addAttribute("","lastModified","lastModified","CDATA",dateTimeFormat.format(new Date(file.lastModified())));
contentHandler.startElement(namespaceURI,"File","xm:File",attributes);
attributes.clear();
chars = file.getName().toCharArray();
contentHandler.characters(chars,0,chars.length);
contentHandler.endElement(namespaceURI,"File","xm:File");
}
contentHandler.endElement(namespaceURI,"Directory","xm:Directory");
contentHandler.endPrefixMapping("xm");
if(!embedded)
contentHandler.endDocument();
}
catch(IOException e)
{
throw new XMException(e);
}
catch(SAXException e)
{
throw new XMException(e);
}
}
|