1 | /* | |
2 | * $Id: TagMap.java 3373 2008-05-12 16:21:24Z xlv $ | |
3 | * | |
4 | * Copyright 2001, 2002 by Bruno Lowagie. | |
5 | * | |
6 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
7 | * (the "License"); you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
9 | * | |
10 | * Software distributed under the License is distributed on an "AS IS" basis, | |
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 | * for the specific language governing rights and limitations under the License. | |
13 | * | |
14 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
15 | * | |
16 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
17 | * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
18 | * All Rights Reserved. | |
19 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
20 | * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. | |
21 | * | |
22 | * Contributor(s): all the names of the contributors are added in the source code | |
23 | * where applicable. | |
24 | * | |
25 | * Alternatively, the contents of this file may be used under the terms of the | |
26 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
27 | * provisions of LGPL are applicable instead of those above. If you wish to | |
28 | * allow use of your version of this file only under the terms of the LGPL | |
29 | * License and not to allow others to use your version of this file under | |
30 | * the MPL, indicate your decision by deleting the provisions above and | |
31 | * replace them with the notice and other provisions required by the LGPL. | |
32 | * If you do not delete the provisions above, a recipient may use your version | |
33 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
34 | * | |
35 | * This library is free software; you can redistribute it and/or modify it | |
36 | * under the terms of the MPL as stated above or under the terms of the GNU | |
37 | * Library General Public License as published by the Free Software Foundation; | |
38 | * either version 2 of the License, or any later version. | |
39 | * | |
40 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
41 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
42 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
43 | * details. | |
44 | * | |
45 | * If you didn't download this code from the following link, you should check if | |
46 | * you aren't using an obsolete version: | |
47 | * http://www.lowagie.com/iText/ | |
48 | */ | |
49 | ||
50 | package com.lowagie.text.xml; | |
51 | ||
52 | ||
53 | import com.lowagie.text.ExceptionConverter; | |
54 | import org.xml.sax.Attributes; | |
55 | import org.xml.sax.InputSource; | |
56 | import org.xml.sax.helpers.DefaultHandler; | |
57 | ||
58 | import javax.xml.parsers.SAXParser; | |
59 | import javax.xml.parsers.SAXParserFactory; | |
60 | import java.io.FileInputStream; | |
61 | import java.io.FileNotFoundException; | |
62 | import java.io.InputStream; | |
63 | import java.util.HashMap; | |
64 | import java.util.Map; | |
65 | ||
66 | /** | |
67 | * The <CODE>Tags</CODE>-class maps several XHTML-tags to iText-objects. | |
68 | */ | |
69 | ||
70 | public class TagMap extends HashMap<String, XmlPeer> { | |
71 | ||
72 | private static final long serialVersionUID = -6809383366554350820L; | |
73 | ||
74 | /** | |
75 | * Constructs a TagMap | |
76 | * | |
77 | * @param tagfile the path to an XML file with the tagmap | |
78 | */ | |
79 | public TagMap(String tagfile) { | |
80 | try { | |
81 |
1
1. |
init(TagMap.class.getClassLoader().getResourceAsStream(tagfile)); |
82 | } catch (Exception e) { | |
83 | try { | |
84 |
1
1. |
init(new FileInputStream(tagfile)); |
85 | } catch (FileNotFoundException fnfe) { | |
86 | throw new ExceptionConverter(fnfe); | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | /** | |
92 | * Constructs a TagMap. | |
93 | * | |
94 | * @param in An InputStream with the tagmap xml | |
95 | */ | |
96 | public TagMap(InputStream in) { | |
97 |
1
1. |
init(in); |
98 | } | |
99 | ||
100 | protected void init(InputStream in) { | |
101 | try { | |
102 | SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); | |
103 |
1
1. init : removed call to javax/xml/parsers/SAXParser::parse → NO_COVERAGE |
parser.parse(new InputSource(in), new AttributeHandler(this)); |
104 | } catch (Exception e) { | |
105 | throw new ExceptionConverter(e); | |
106 | } | |
107 | } | |
108 | ||
109 | class AttributeHandler extends DefaultHandler { | |
110 | ||
111 | /** | |
112 | * This is a tag | |
113 | */ | |
114 | public static final String TAG = "tag"; | |
115 | ||
116 | /** | |
117 | * This is a tag | |
118 | */ | |
119 | public static final String ATTRIBUTE = "attribute"; | |
120 | ||
121 | /** | |
122 | * This is an attribute | |
123 | */ | |
124 | public static final String NAME = "name"; | |
125 | ||
126 | /** | |
127 | * This is an attribute | |
128 | */ | |
129 | public static final String ALIAS = "alias"; | |
130 | ||
131 | /** | |
132 | * This is an attribute | |
133 | */ | |
134 | public static final String VALUE = "value"; | |
135 | ||
136 | /** | |
137 | * This is an attribute | |
138 | */ | |
139 | public static final String CONTENT = "content"; | |
140 | ||
141 | /** | |
142 | * This is the tagmap using the AttributeHandler | |
143 | */ | |
144 | private Map<String, XmlPeer> tagMap; | |
145 | ||
146 | /** | |
147 | * This is the current peer. | |
148 | */ | |
149 | private XmlPeer currentPeer; | |
150 | ||
151 | /** | |
152 | * Constructs a new SAXiTextHandler that will translate all the events | |
153 | * triggered by the parser to actions on the <CODE>Document</CODE>-object. | |
154 | * | |
155 | * @param tagMap A Hashmap containing XmlPeer-objects | |
156 | */ | |
157 | ||
158 | public AttributeHandler(Map<String, XmlPeer> tagMap) { | |
159 | this.tagMap = tagMap; | |
160 | } | |
161 | ||
162 | /** | |
163 | * This method gets called when a start tag is encountered. | |
164 | * | |
165 | * @param uri the Uniform Resource Identifier | |
166 | * @param lname the local name (without prefix), or the empty string if Namespace processing is not being | |
167 | * performed. | |
168 | * @param tag the name of the tag that is encountered | |
169 | * @param attrs the list of attributes | |
170 | */ | |
171 | ||
172 | public void startElement(String uri, String lname, String tag, Attributes attrs) { | |
173 | String name = attrs.getValue(NAME); | |
174 | String alias = attrs.getValue(ALIAS); | |
175 | String value = attrs.getValue(VALUE); | |
176 |
1
1. startElement : negated conditional → NO_COVERAGE |
if (name != null) { |
177 |
1
1. startElement : negated conditional → NO_COVERAGE |
if (TAG.equals(tag)) { |
178 | currentPeer = new XmlPeer(name, alias); | |
179 |
1
1. startElement : negated conditional → NO_COVERAGE |
} else if (ATTRIBUTE.equals(tag)) { |
180 |
1
1. startElement : negated conditional → NO_COVERAGE |
if (alias != null) { |
181 |
1
1. startElement : removed call to com/lowagie/text/xml/XmlPeer::addAlias → NO_COVERAGE |
currentPeer.addAlias(name, alias); |
182 | } | |
183 |
1
1. startElement : negated conditional → NO_COVERAGE |
if (value != null) { |
184 |
1
1. startElement : removed call to com/lowagie/text/xml/XmlPeer::addValue → NO_COVERAGE |
currentPeer.addValue(name, value); |
185 | } | |
186 | } | |
187 | } | |
188 | value = attrs.getValue(CONTENT); | |
189 |
1
1. startElement : negated conditional → NO_COVERAGE |
if (value != null) { |
190 |
1
1. startElement : removed call to com/lowagie/text/xml/XmlPeer::setContent → NO_COVERAGE |
currentPeer.setContent(value); |
191 | } | |
192 | } | |
193 | ||
194 | /** | |
195 | * This method gets called when ignorable white space encountered. | |
196 | * | |
197 | * @param ch an array of characters | |
198 | * @param start the start position in the array | |
199 | * @param length the number of characters to read from the array | |
200 | */ | |
201 | ||
202 | public void ignorableWhitespace(char[] ch, int start, int length) { | |
203 | // do nothing | |
204 | } | |
205 | ||
206 | /** | |
207 | * This method gets called when characters are encountered. | |
208 | * | |
209 | * @param ch an array of characters | |
210 | * @param start the start position in the array | |
211 | * @param length the number of characters to read from the array | |
212 | */ | |
213 | ||
214 | public void characters(char[] ch, int start, int length) { | |
215 | // do nothing | |
216 | } | |
217 | ||
218 | /** | |
219 | * This method gets called when an end tag is encountered. | |
220 | * | |
221 | * @param uri the Uniform Resource Identifier | |
222 | * @param lname the local name (without prefix), or the empty string if Namespace processing is not being | |
223 | * performed. | |
224 | * @param tag the name of the tag that ends | |
225 | */ | |
226 | ||
227 | public void endElement(String uri, String lname, String tag) { | |
228 |
1
1. endElement : negated conditional → NO_COVERAGE |
if (TAG.equals(tag)) { |
229 | tagMap.put(currentPeer.getAlias(), currentPeer); | |
230 | } | |
231 | } | |
232 | } | |
233 | ||
234 | ||
235 | } | |
Mutations | ||
81 |
1.1 |
|
84 |
1.1 |
|
97 |
1.1 |
|
103 |
1.1 |
|
176 |
1.1 |
|
177 |
1.1 |
|
179 |
1.1 |
|
180 |
1.1 |
|
181 |
1.1 |
|
183 |
1.1 |
|
184 |
1.1 |
|
189 |
1.1 |
|
190 |
1.1 |
|
228 |
1.1 |