package nu.fw.jeti.plugins.xhtml;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import nu.fw.jeti.jabber.elements.Extension;
import nu.fw.jeti.jabber.handlers.ExtensionHandler;
import nu.fw.jeti.plugins.Word;
import nu.fw.jeti.util.Log;
import org.xml.sax.Attributes;
/**
* @author E.S. de Boer
*
*
*/
public class XHTMLHandler extends ExtensionHandler
{
private List wordList = new ArrayList();
private SimpleAttributeSet currentAttributes;
private StringBuffer body;
public void startHandling(Attributes attr)
{
reset();
body = new StringBuffer(); //attr.getValue("from");
}
private void reset()
{
body = null;
wordList.clear();
currentAttributes = new SimpleAttributeSet();
}
public void startElement(String name, Attributes attr)
{
addToWordList(getUntrimmedText());
//create xhtml string to print in log window
body.append(getText() + "<" + name + " ");
for (int i = 0; i < attr.getLength(); i++)
{
body.append(attr.getQName(i) + "=\"" + attr.getValue(i) + "\"");
}
body.append(">");
SimpleAttributeSet sas = new SimpleAttributeSet();
sas.setResolveParent(currentAttributes);
if(name.equals("body"))
{
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("blockquote"))
{//TODO indent does not work
StyleConstants.setLeftIndent(sas, 10f);
StyleConstants.setRightIndent(sas, 10f);
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
wordList.add(new Word("\n",sas));
}
else if(name.equals("q"))
{//quote cite=url attribute not implemented???
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
wordList.add(new Word("'",sas));
}
else if(name.equals("pre"))
{
StyleConstants.setFontFamily(sas,"courier");
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("li") || name.equals("ol") || name.equals("ul"))
{//TODO implement lists
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("h1"))
{//todo h1 =?
StyleConstants.setFontSize(sas,32);
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("h2"))
{
StyleConstants.setFontSize(sas,24);
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("h3"))
{
StyleConstants.setFontSize(sas,18);
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("a"))
{//TODO link
StyleConstants.setFontFamily(sas,"courier");
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("img"))
{//TODO img
StyleConstants.setFontFamily(sas,"courier");
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("span") || name.equals("div") || name.equals("p") )
{
parseStyle(attr.getValue("style"),sas);
currentAttributes = sas;
}
else if(name.equals("em")|| name.equals("cite") )
{
StyleConstants.setItalic(sas,true);
currentAttributes = sas;
}
else if(name.equals("strong"))
{
StyleConstants.setBold(sas,true);
currentAttributes = sas;
}
else if(name.equals("code"))
{//render in monospace font (courier)
StyleConstants.setFontFamily(sas,"courier");
currentAttributes = sas;
}
else
{
StringBuffer temp = new StringBuffer();
temp.append("<" + name + " ");
for (int i = 0; i < attr.getLength(); i++)
{
temp.append(attr.getQName(i) + "=\"" + attr.getValue(i) + "\"");
}
temp.append(">");
Log.notParsedXML(temp.toString());
}
clearCurrentChars();
}
private void parseStyle(String styleAttributes,SimpleAttributeSet sas)
{
if(styleAttributes==null)return;
String[] styles = styleAttributes.split(";");
for(int i=0;i");
if(name.equals("a") || name.equals("body") || name.equals("cite")
|| name.equals("code") || name.equals("div") || name.equals("em") || name.equals("h1")
|| name.equals("h2")|| name.equals("h3") || name.equals("img") || name.equals("li")
|| name.equals("ol")|| name.equals("p")|| name.equals("pre")|| name.equals("ul")
|| name.equals("strong") || name.equals("span"))
{
currentAttributes =(SimpleAttributeSet)currentAttributes.getResolveParent();
}
else if(name.equals("br"))
{
wordList.add(new Word("\n"));
}
else if(name.equals("q"))
{//quote cite=url attribute not implemented???
wordList.add(new Word("'",currentAttributes));
currentAttributes =(SimpleAttributeSet)currentAttributes.getResolveParent();
}
else if(name.equals("blockquote"))
{
wordList.add(new Word("\n",null));
currentAttributes =(SimpleAttributeSet)currentAttributes.getResolveParent();
}
clearCurrentChars();
}
private void addToWordList(StringBuffer text)
{
if(text.length()<1)return;
StringBuffer temp = new StringBuffer();
for(int i = 0;i0)wordList.add(new Word(temp,currentAttributes));
}
public Extension build()
{
addToWordList(getUntrimmedText());
body.append(getText());
clearCurrentChars();
Extension e = new XHTML(body.toString(),new ArrayList(wordList));
reset();
return e;
}
}
/*
* Overrides for emacs
* Local variables:
* tab-width: 4
* End:
*/