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

developerWorks 中国  >  XML  >

Working XML: Processing instructions and parameters

Adding support for multiple style sheets

developerWorks

Return to article.


Listing 5: Adapted StylingMover
        
public File move(File sourceFile,File targetDir,int depth)
   throws XMException
{
   try
   {
      File targetFile = new File(targetDir,getTargetName(sourceFile));
      if(!build && targetFile.exists())
         if(targetFile.lastModified() >= sourceFile.lastModified())
            return null;
      serializer.setOutputStream(new FileOutputStream(targetFile));
      linkFilter.setDirectory(sourceFile.getParentFile(),depth);
      linkFilter.setContentHandler(serializer.asContentHandler());
      // the lexical handler is required for CDATA sections
      if(serializer instanceof LexicalHandler)
         linkFilter.setProperty("http://xml.org/sax/properties/lexical-handler",
			                       serializer);
      else
         throw new NotImplementedException("LexicalHandler interface required");
      SAXResult result = new SAXResult(linkFilter);
      XMLReader reader = JAXPHelper.createXMLReader(false);
      ProcessingInstructionHandler handler = new ProcessingInstructionHandler();
      reader.setContentHandler(handler);
      
        try
      {
         reader.parse(JAXPHelper.toInputSource(sourceFile));
      }
      catch(SAXException e)
      {
         if(!handler.isStopException(e))
            throw e;
      }
      String href = handler.getHRef();
      Transformer transformer = templates.getTransformer(href,sourceFile);
      Iterator iterator = handler.getParamsIterator();
      while(iterator.hasNext())
      {
         Map.Entry entry = (Map.Entry)iterator.next();
         transformer.setParameter((String)entry.getKey(),(String)entry.getValue());
      }
      transformer.transform(new StreamSource(sourceFile),result);
      return targetFile;
   }
   catch(SAXException e)
   {
      messenger.error(new XMException(e));
   }
   catch(IOException e)
   {
      messenger.error(new XMException(e));
   }
   catch(TransformerException e)
   {
      messenger.error(new XMException(e));
   }
   return null;
}
      

Return to article.

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