IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  XML  >

Working XML: Wrapping up XM version 1

Managing a list of links and the table of contents

developerWorks

Return to article.


Listing 2. The read() method produces the XML document
        
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);
   }
}
      

Return to article.

    关于 IBM 隐私条约 联系 IBM 使用条款