1 50 51 package com.lowagie.text.html; 52 53 import java.util.HashMap ; 54 import java.util.Properties ; 55 56 import org.xml.sax.Attributes ; 57 58 import com.lowagie.text.DocListener; 59 import com.lowagie.text.DocumentException; 60 import com.lowagie.text.Element; 61 import com.lowagie.text.ElementTags; 62 import com.lowagie.text.ExceptionConverter; 63 import com.lowagie.text.pdf.BaseFont; 64 import com.lowagie.text.xml.SAXiTextHandler; 65 import com.lowagie.text.xml.XmlPeer; 66 67 70 71 public class SAXmyHtmlHandler extends SAXiTextHandler { 73 74 75 private Properties bodyAttributes = new Properties (); 76 77 78 private boolean tableBorder = false; 79 80 87 88 public SAXmyHtmlHandler(DocListener document) { 89 super(document, new HtmlTagMap()); 90 } 91 99 100 public SAXmyHtmlHandler(DocListener document, BaseFont bf) { 101 super(document, new HtmlTagMap(), bf); 102 } 103 104 113 114 public SAXmyHtmlHandler(DocListener document, HashMap htmlTags) { 115 super(document, htmlTags); 116 } 117 118 131 132 public void startElement(String uri, String lname, String name, 133 Attributes attrs) { 134 136 name = name.toLowerCase(); 139 if (((HtmlTagMap) myTags).isHtml(name)) { 140 return; 142 } 143 if (((HtmlTagMap) myTags).isHead(name)) { 144 return; 146 } 147 if (((HtmlTagMap) myTags).isTitle(name)) { 148 return; 150 } 151 if (((HtmlTagMap) myTags).isMeta(name)) { 152 String meta = null; 154 String content = null; 155 if (attrs != null) { 156 for (int i = 0; i < attrs.getLength(); i++) { 157 String attribute = attrs.getQName(i); 158 if (attribute.equalsIgnoreCase(HtmlTags.CONTENT)) 159 content = attrs.getValue(i); 160 else if (attribute.equalsIgnoreCase(HtmlTags.NAME)) 161 meta = attrs.getValue(i); 162 } 163 } 164 if (meta != null && content != null) { 165 bodyAttributes.put(meta, content); 166 } 167 return; 168 } 169 if (((HtmlTagMap) myTags).isLink(name)) { 170 return; 173 } 174 if (((HtmlTagMap) myTags).isBody(name)) { 175 XmlPeer peer = new XmlPeer(ElementTags.ITEXT, name); 179 peer.addAlias(ElementTags.TOP, HtmlTags.TOPMARGIN); 180 peer.addAlias(ElementTags.BOTTOM, HtmlTags.BOTTOMMARGIN); 181 peer.addAlias(ElementTags.RIGHT, HtmlTags.RIGHTMARGIN); 182 peer.addAlias(ElementTags.LEFT, HtmlTags.LEFTMARGIN); 183 bodyAttributes.putAll(peer.getAttributes(attrs)); 184 handleStartingTags(peer.getTag(), bodyAttributes); 185 return; 186 } 187 if (myTags.containsKey(name)) { 188 XmlPeer peer = (XmlPeer) myTags.get(name); 189 if (ElementTags.TABLE.equals(peer.getTag()) || ElementTags.CELL.equals(peer.getTag())) { 190 Properties p = peer.getAttributes(attrs); 191 String value; 192 if (ElementTags.TABLE.equals(peer.getTag()) 193 && (value = p.getProperty(ElementTags.BORDERWIDTH)) != null) { 194 if (Float.parseFloat(value + "f") > 0) { 195 tableBorder = true; 196 } 197 } 198 if (tableBorder) { 199 p.put(ElementTags.LEFT, String.valueOf(true)); 200 p.put(ElementTags.RIGHT, String.valueOf(true)); 201 p.put(ElementTags.TOP, String.valueOf(true)); 202 p.put(ElementTags.BOTTOM, String.valueOf(true)); 203 } 204 handleStartingTags(peer.getTag(), p); 205 return; 206 } 207 handleStartingTags(peer.getTag(), peer.getAttributes(attrs)); 208 return; 209 } 210 Properties attributes = new Properties (); 211 if (attrs != null) { 212 for (int i = 0; i < attrs.getLength(); i++) { 213 String attribute = attrs.getQName(i).toLowerCase(); 214 attributes.setProperty(attribute, attrs.getValue(i).toLowerCase()); 215 } 216 } 217 handleStartingTags(name, attributes); 218 } 219 220 231 232 public void endElement(String uri, String lname, String name) { 233 name = name.toLowerCase(); 235 if (ElementTags.PARAGRAPH.equals(name)) { 236 try { 237 document.add((Element) stack.pop()); 238 return; 239 } catch (DocumentException e) { 240 throw new ExceptionConverter(e); 241 } 242 } 243 if (((HtmlTagMap) myTags).isHead(name)) { 244 return; 246 } 247 if (((HtmlTagMap) myTags).isTitle(name)) { 248 if (currentChunk != null) { 249 bodyAttributes.put(ElementTags.TITLE, currentChunk.getContent()); 250 } 251 return; 252 } 253 if (((HtmlTagMap) myTags).isMeta(name)) { 254 return; 256 } 257 if (((HtmlTagMap) myTags).isLink(name)) { 258 return; 260 } 261 if (((HtmlTagMap) myTags).isBody(name)) { 262 return; 264 } 265 if (myTags.containsKey(name)) { 266 XmlPeer peer = (XmlPeer) myTags.get(name); 267 if (ElementTags.TABLE.equals(peer.getTag())) { 268 tableBorder = false; 269 } 270 super.handleEndingTags(peer.getTag()); 271 return; 272 } 273 handleEndingTags(name); 276 } 277 } | Popular Tags |