 |
返回原文..
Benoit Marchal (
bmarchal@pineapplesoft.com)
Consultant, Pineapplesoft
August 2001
Listing 3. StylingMover.java
package org.ananas.xm;
import java.io.*;
import java.util.*;
import org.xml.sax.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import org.apache.xalan.serialize.*;
import org.xml.sax.ext.LexicalHandler;
import org.ananas.util.NotImplementedException;
import org.apache.xalan.templates.OutputProperties;
public class StylingMover
extends DefaultMoverImpl
{
protected Templates templates;
protected long lastModified;
protected String extension;
protected LinkFilter linkFilter;
protected Serializer serializer;
public StylingMover(Messenger messenger,MoversSupervisor supervisor,File stylesheetFile)
throws XMException
{
super(messenger);
try
{
linkFilter = new LinkFilter(messenger,supervisor);
lastModified = stylesheetFile.lastModified();
templates = TransformerFactory.newInstance().newTemplates(new StreamSource(stylesheetFile));
// right now limited to HTML formatting
Properties properties = templates.getOutputProperties();
extension = '.' + properties.getProperty(OutputKeys.METHOD);
serializer = SerializerFactory.getSerializer(properties);
}
catch(TransformerConfigurationException e)
{
messenger.fatal(new XMException(e));
}
}
public String getTargetName(File file)
{
return getDisplayName(file) + extension;
}
public File move(File sourceFile,File targetDir,int depth)
throws XMException
{
try
{
File targetFile = new File(targetDir,getTargetName(sourceFile));
if(targetFile.exists())
{
if(targetFile.lastModified() >= sourceFile.lastModified() &
targetFile.lastModified() >= 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("The serializer has to implements the LexicalHandler interface");
SAXResult result = new SAXResult(linkFilter);
Transformer transformer = templates.newTransformer();
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;
}
}
|
返回原文.
|  |
|