KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > html > SAXmyHtmlHandler


1 /*
2  * $Id: SAXmyHtmlHandler.java 2748 2007-05-12 15:11:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 2001, 2002 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.html;
52
53 import java.util.HashMap JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import org.xml.sax.Attributes JavaDoc;
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 /**
68  * The <CODE>Tags</CODE>-class maps several XHTML-tags to iText-objects.
69  */

70
71 public class SAXmyHtmlHandler extends SAXiTextHandler // SAXmyHandler
72
{
73
74     /** These are the properties of the body section. */
75     private Properties JavaDoc bodyAttributes = new Properties JavaDoc();
76
77     /** This is the status of the table border. */
78     private boolean tableBorder = false;
79
80     /**
81      * Constructs a new SAXiTextHandler that will translate all the events
82      * triggered by the parser to actions on the <CODE>Document</CODE>-object.
83      *
84      * @param document
85      * this is the document on which events must be triggered
86      */

87
88     public SAXmyHtmlHandler(DocListener document) {
89         super(document, new HtmlTagMap());
90     }
91     /**
92      * Constructs a new SAXiTextHandler that will translate all the events
93      * triggered by the parser to actions on the <CODE>Document</CODE>-object.
94      *
95      * @param document
96      * this is the document on which events must be triggered
97      * @param bf
98      */

99
100     public SAXmyHtmlHandler(DocListener document, BaseFont bf) {
101         super(document, new HtmlTagMap(), bf);
102     }
103
104     /**
105      * Constructs a new SAXiTextHandler that will translate all the events
106      * triggered by the parser to actions on the <CODE>Document</CODE>-object.
107      *
108      * @param document
109      * this is the document on which events must be triggered
110      * @param htmlTags
111      * a tagmap translating HTML tags to iText tags
112      */

113
114     public SAXmyHtmlHandler(DocListener document, HashMap JavaDoc htmlTags) {
115         super(document, htmlTags);
116     }
117
118     /**
119      * This method gets called when a start tag is encountered.
120      *
121      * @param uri
122      * the Uniform Resource Identifier
123      * @param lname
124      * the local name (without prefix), or the empty string if
125      * Namespace processing is not being performed.
126      * @param name
127      * the name of the tag that is encountered
128      * @param attrs
129      * the list of attributes
130      */

131
132     public void startElement(String JavaDoc uri, String JavaDoc lname, String JavaDoc name,
133             Attributes JavaDoc attrs) {
134         // System.err.println("Start: " + name);
135

136         // super.handleStartingTags is replaced with handleStartingTags
137
// suggestion by Vu Ngoc Tan/Hop
138
name = name.toLowerCase();
139         if (((HtmlTagMap) myTags).isHtml(name)) {
140             // we do nothing
141
return;
142         }
143         if (((HtmlTagMap) myTags).isHead(name)) {
144             // we do nothing
145
return;
146         }
147         if (((HtmlTagMap) myTags).isTitle(name)) {
148             // we do nothing
149
return;
150         }
151         if (((HtmlTagMap) myTags).isMeta(name)) {
152             // we look if we can change the body attributes
153
String JavaDoc meta = null;
154             String JavaDoc content = null;
155             if (attrs != null) {
156                 for (int i = 0; i < attrs.getLength(); i++) {
157                     String JavaDoc 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             // we do nothing for the moment, in a later version we could extract
171
// the style sheet
172
return;
173         }
174         if (((HtmlTagMap) myTags).isBody(name)) {
175             // maybe we could extract some info about the document: color,
176
// margins,...
177
// but that's for a later version...
178
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 JavaDoc p = peer.getAttributes(attrs);
191                 String JavaDoc 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 JavaDoc attributes = new Properties JavaDoc();
211         if (attrs != null) {
212             for (int i = 0; i < attrs.getLength(); i++) {
213                 String JavaDoc attribute = attrs.getQName(i).toLowerCase();
214                 attributes.setProperty(attribute, attrs.getValue(i).toLowerCase());
215             }
216         }
217         handleStartingTags(name, attributes);
218     }
219
220     /**
221      * This method gets called when an end tag is encountered.
222      *
223      * @param uri
224      * the Uniform Resource Identifier
225      * @param lname
226      * the local name (without prefix), or the empty string if
227      * Namespace processing is not being performed.
228      * @param name
229      * the name of the tag that ends
230      */

231
232     public void endElement(String JavaDoc uri, String JavaDoc lname, String JavaDoc name) {
233         // System.err.println("End: " + name);
234
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             // we do nothing
245
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             // we do nothing
255
return;
256         }
257         if (((HtmlTagMap) myTags).isLink(name)) {
258             // we do nothing
259
return;
260         }
261         if (((HtmlTagMap) myTags).isBody(name)) {
262             // we do nothing
263
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         // super.handleEndingTags is replaced with handleEndingTags
274
// suggestion by Ken Auer
275
handleEndingTags(name);
276     }
277 }
Popular Tags