package org.ananas.xm;
import java.io.*;
import java.net.*;
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.xml.sax.ext.LexicalHandler;
import org.ananas.util.NotImplementedException;
public class LinkFilter
extends XMLFilterImpl
implements LexicalHandler
{
protected Messenger messenger;
protected MoversSupervisor supervisor;
protected String rootPrefix = null;
protected File directory;
protected LexicalHandler lexicalHandler = null;
protected static final String lexicalHandlerProperty = "http://xml.org/sax/properties/lexical-handler";
public LinkFilter(Messenger messenger,MoversSupervisor supervisor)
{
this.messenger = messenger;
this.supervisor = supervisor;
}
public void setDirectory(File directory,int depth)
{
this.directory = directory;
StringBuffer prefix = new StringBuffer();
for(int i = 0;i < depth;i++)
prefix.append("../");
rootPrefix = prefix.toString();
}
public void startElement(String namespaceURI,
String lName,
String qName,
Attributes attributes)
throws SAXException
{
try
{
int index = -1;
if(qName.equals("a"))
index = attributes.getIndex("href");
else if(qName.equals("img") || qName.equals("input"))
index = attributes.getIndex("src");
if(index != -1)
{
String link = attributes.getValue(index);
if(link.startsWith("!"))
link = rootPrefix + link.substring(1);
File linkFile = null;
linkFile = new File(directory,link);
if(linkFile.exists())
{
String outFile = supervisor.getMover(linkFile).getTargetName(linkFile);
int pos = link.lastIndexOf('/');
if(pos != -1)
link = link.substring(0,pos + 1) + outFile;
else
link = outFile;
}
else if(!(link.startsWith("http:") || link.startsWith("ftp:") ||
link.startsWith("mailto:") || link.startsWith("news:")))
messenger.warning(new XMException(Resources.getString("suspiciouslink"),new Object[] { link }));
if(attributes instanceof AttributesImpl)
((AttributesImpl)attributes).setValue(index,link);
else
throw new NotImplementedException("only works for AttributeListImpl so far");
}
}
catch(XMException e)
{
throw new SAXException(e);
}
super.startElement(namespaceURI,lName,qName,attributes);
}
public void setProperty(String name,Object value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if(name.equals(lexicalHandlerProperty) &&
value instanceof LexicalHandler)
this.lexicalHandler = (LexicalHandler)lexicalHandler;
else
super.setProperty(name,value);
}
public Object getProperty(String name)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if(name.equals(lexicalHandlerProperty))
return lexicalHandler;
else
return super.getProperty(name);
}
public void comment(char[] ch,int start,int length)
throws SAXException
{
if(lexicalHandler != null)
lexicalHandler.comment(ch,start,length);
}
public void endCDATA()
throws SAXException
{
if(lexicalHandler != null)
lexicalHandler.endCDATA();
}
public void endDTD()
throws SAXException
{
if(lexicalHandler != null)
lexicalHandler.endDTD();
}
public void endEntity(String name)
throws SAXException
{
if(lexicalHandler != null)
lexicalHandler.endEntity(name);
}
public void startCDATA()
throws SAXException
{
if(lexicalHandler != null)
lexicalHandler.startCDATA();
}
public void startDTD(String name,String publicId,String systemId)
throws SAXException
{
if(lexicalHandler != null)
lexicalHandler.startDTD(name,publicId,systemId);
}
public void startEntity(String name)
throws SAXException
{
if(lexicalHandler != null)
lexicalHandler.startEntity(name);
}
}
|