1 50 51 package com.lowagie.text.xml; 52 53 import java.io.FileInputStream ; 54 import java.io.FileNotFoundException ; 55 import java.io.InputStream ; 56 import java.util.HashMap ; 57 58 import javax.xml.parsers.SAXParser ; 59 import javax.xml.parsers.SAXParserFactory ; 60 61 import org.xml.sax.Attributes ; 62 import org.xml.sax.InputSource ; 63 import org.xml.sax.helpers.DefaultHandler ; 64 65 import com.lowagie.text.ExceptionConverter; 66 67 70 71 public class TagMap extends HashMap { 72 73 private static final long serialVersionUID = -6809383366554350820L; 74 75 class AttributeHandler extends DefaultHandler { 76 77 78 public static final String TAG = "tag"; 79 80 81 public static final String ATTRIBUTE = "attribute"; 82 83 84 public static final String NAME = "name"; 85 86 87 public static final String ALIAS = "alias"; 88 89 90 public static final String VALUE = "value"; 91 92 93 public static final String CONTENT = "content"; 94 95 96 private HashMap tagMap; 97 98 99 private XmlPeer currentPeer; 100 101 107 108 public AttributeHandler(HashMap tagMap) { 109 super(); 110 this.tagMap = tagMap; 111 } 112 113 121 122 public void startElement(String uri, String lname, String tag, Attributes attrs) { 123 String name = attrs.getValue(NAME); 124 String alias = attrs.getValue(ALIAS); 125 String 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 152 153 public void ignorableWhitespace(char[] ch, int start, int length) { 154 } 156 157 164 165 public void characters(char[] ch, int start, int length) { 166 } 168 169 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 187 public TagMap(String tagfile) { 188 super(); 189 try { 190 init(TagMap.class.getClassLoader().getResourceAsStream(tagfile)); 191 }catch(Exception e) { 192 try { 193 init(new FileInputStream (tagfile)); 194 } catch (FileNotFoundException fnfe) { 195 throw new ExceptionConverter(fnfe); 196 } 197 } 198 } 199 200 204 public TagMap(InputStream in) { 205 super(); 206 init(in); 207 } 208 209 protected void init(InputStream in) { 210 try { 211 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 212 parser.parse(new InputSource (in), new AttributeHandler(this)); 213 } 214 catch(Exception e) { 215 throw new ExceptionConverter(e); 216 } 217 } 218 219 220 } 221 | Popular Tags |