KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > xml > TagMap


1 /*
2  * $Id: TagMap.java 2478 2006-11-23 11:42:17Z 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.xml;
52
53 import java.io.FileInputStream JavaDoc;
54 import java.io.FileNotFoundException JavaDoc;
55 import java.io.InputStream JavaDoc;
56 import java.util.HashMap JavaDoc;
57
58 import javax.xml.parsers.SAXParser JavaDoc;
59 import javax.xml.parsers.SAXParserFactory JavaDoc;
60
61 import org.xml.sax.Attributes JavaDoc;
62 import org.xml.sax.InputSource JavaDoc;
63 import org.xml.sax.helpers.DefaultHandler JavaDoc;
64
65 import com.lowagie.text.ExceptionConverter;
66
67 /**
68  * The <CODE>Tags</CODE>-class maps several XHTML-tags to iText-objects.
69  */

70
71 public class TagMap extends HashMap JavaDoc {
72
73     private static final long serialVersionUID = -6809383366554350820L;
74
75     class AttributeHandler extends DefaultHandler JavaDoc {
76         
77 /** This is a tag */
78         public static final String JavaDoc TAG = "tag";
79         
80 /** This is a tag */
81         public static final String JavaDoc ATTRIBUTE = "attribute";
82         
83 /** This is an attribute */
84         public static final String JavaDoc NAME = "name";
85         
86 /** This is an attribute */
87         public static final String JavaDoc ALIAS = "alias";
88         
89 /** This is an attribute */
90         public static final String JavaDoc VALUE = "value";
91         
92 /** This is an attribute */
93         public static final String JavaDoc CONTENT = "content";
94         
95 /** This is the tagmap using the AttributeHandler */
96         private HashMap JavaDoc tagMap;
97         
98 /** This is the current peer. */
99         private XmlPeer currentPeer;
100         
101 /**
102  * Constructs a new SAXiTextHandler that will translate all the events
103  * triggered by the parser to actions on the <CODE>Document</CODE>-object.
104  *
105  * @param tagMap A Hashmap containing XmlPeer-objects
106  */

107         
108         public AttributeHandler(HashMap JavaDoc tagMap) {
109             super();
110             this.tagMap = tagMap;
111         }
112         
113 /**
114  * This method gets called when a start tag is encountered.
115  *
116  * @param uri the Uniform Resource Identifier
117  * @param lname the local name (without prefix), or the empty string if Namespace processing is not being performed.
118  * @param tag the name of the tag that is encountered
119  * @param attrs the list of attributes
120  */

121         
122         public void startElement(String JavaDoc uri, String JavaDoc lname, String JavaDoc tag, Attributes JavaDoc attrs) {
123             String JavaDoc name = attrs.getValue(NAME);
124             String JavaDoc alias = attrs.getValue(ALIAS);
125             String JavaDoc value = attrs.getValue(VALUE);
126             if (name != null) {
127                 if(TAG.equals(tag)) {
128                     currentPeer = new XmlPeer(name, alias);
129                 }
130                 else if (ATTRIBUTE.equals(tag)) {
131                     if (alias != null) {
132                         currentPeer.addAlias(name, alias);
133                     }
134                     if (value != null) {
135                         currentPeer.addValue(name, value);
136                     }
137                 }
138             }
139             value = attrs.getValue(CONTENT);
140             if (value != null) {
141                 currentPeer.setContent(value);
142             }
143         }
144         
145 /**
146  * This method gets called when ignorable white space encountered.
147  *
148  * @param ch an array of characters
149  * @param start the start position in the array
150  * @param length the number of characters to read from the array
151  */

152         
153         public void ignorableWhitespace(char[] ch, int start, int length) {
154             // do nothing
155
}
156         
157 /**
158  * This method gets called when characters are encountered.
159  *
160  * @param ch an array of characters
161  * @param start the start position in the array
162  * @param length the number of characters to read from the array
163  */

164         
165         public void characters(char[] ch, int start, int length) {
166             // do nothing
167
}
168         
169 /**
170  * This method gets called when an end tag is encountered.
171  *
172  * @param uri the Uniform Resource Identifier
173  * @param lname the local name (without prefix), or the empty string if Namespace processing is not being performed.
174  * @param tag the name of the tag that ends
175  */

176         
177         public void endElement(String JavaDoc uri, String JavaDoc lname, String JavaDoc tag) {
178             if (TAG.equals(tag))
179                 tagMap.put(currentPeer.getAlias(), currentPeer);
180         }
181     }
182     
183     /**
184      * Constructs a TagMap
185      * @param tagfile the path to an XML file with the tagmap
186      */

187     public TagMap(String JavaDoc tagfile) {
188         super();
189         try {
190             init(TagMap.class.getClassLoader().getResourceAsStream(tagfile));
191         }catch(Exception JavaDoc e) {
192             try {
193                 init(new FileInputStream JavaDoc(tagfile));
194             } catch (FileNotFoundException JavaDoc fnfe) {
195                 throw new ExceptionConverter(fnfe);
196             }
197         }
198     }
199
200     /**
201      * Constructs a TagMap.
202      * @param in An InputStream with the tagmap xml
203      */

204     public TagMap(InputStream JavaDoc in) {
205         super();
206         init(in);
207     }
208
209     protected void init(InputStream JavaDoc in) {
210         try {
211             SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
212             parser.parse(new InputSource JavaDoc(in), new AttributeHandler(this));
213         }
214         catch(Exception JavaDoc e) {
215             throw new ExceptionConverter(e);
216         }
217     }
218
219
220 }
221
Popular Tags