001: /*
002: * $Id: TagMap.java 2478 2006-11-23 11:42:17Z blowagie $
003: * $Name$
004: *
005: * Copyright 2001, 2002 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.xml;
052:
053: import java.io.FileInputStream;
054: import java.io.FileNotFoundException;
055: import java.io.InputStream;
056: import java.util.HashMap;
057:
058: import javax.xml.parsers.SAXParser;
059: import javax.xml.parsers.SAXParserFactory;
060:
061: import org.xml.sax.Attributes;
062: import org.xml.sax.InputSource;
063: import org.xml.sax.helpers.DefaultHandler;
064:
065: import com.lowagie.text.ExceptionConverter;
066:
067: /**
068: * The <CODE>Tags</CODE>-class maps several XHTML-tags to iText-objects.
069: */
070:
071: public class TagMap extends HashMap {
072:
073: private static final long serialVersionUID = -6809383366554350820L;
074:
075: class AttributeHandler extends DefaultHandler {
076:
077: /** This is a tag */
078: public static final String TAG = "tag";
079:
080: /** This is a tag */
081: public static final String ATTRIBUTE = "attribute";
082:
083: /** This is an attribute */
084: public static final String NAME = "name";
085:
086: /** This is an attribute */
087: public static final String ALIAS = "alias";
088:
089: /** This is an attribute */
090: public static final String VALUE = "value";
091:
092: /** This is an attribute */
093: public static final String CONTENT = "content";
094:
095: /** This is the tagmap using the AttributeHandler */
096: private HashMap tagMap;
097:
098: /** This is the current peer. */
099: 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 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 uri, String lname, String tag,
123: Attributes attrs) {
124: String name = attrs.getValue(NAME);
125: String alias = attrs.getValue(ALIAS);
126: String value = attrs.getValue(VALUE);
127: if (name != null) {
128: if (TAG.equals(tag)) {
129: currentPeer = new XmlPeer(name, alias);
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 uri, String lname, String 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 tagfile) {
188: super ();
189: try {
190: init(TagMap.class.getClassLoader().getResourceAsStream(
191: tagfile));
192: } catch (Exception e) {
193: try {
194: init(new FileInputStream(tagfile));
195: } catch (FileNotFoundException fnfe) {
196: throw new ExceptionConverter(fnfe);
197: }
198: }
199: }
200:
201: /**
202: * Constructs a TagMap.
203: * @param in An InputStream with the tagmap xml
204: */
205: public TagMap(InputStream in) {
206: super ();
207: init(in);
208: }
209:
210: protected void init(InputStream in) {
211: try {
212: SAXParser parser = SAXParserFactory.newInstance()
213: .newSAXParser();
214: parser.parse(new InputSource(in),
215: new AttributeHandler(this ));
216: } catch (Exception e) {
217: throw new ExceptionConverter(e);
218: }
219: }
220:
221: }
|