1 51 package org.apache.fop.configuration; 52 53 import org.xml.sax.helpers.DefaultHandler ; 55 import org.xml.sax.Attributes ; 56 import org.xml.sax.Locator ; 57 58 import java.util.Map ; 60 import java.util.List ; 61 62 import org.apache.fop.messaging.MessageHandler; 64 65 66 70 public class ConfigurationParser extends DefaultHandler { 71 private static final int OUT = 0; 72 private static final int IN_ENTRY = 1; 73 private static final int IN_KEY = 2; 74 private static final int IN_VALUE = 4; 75 private static final int IN_LIST = 8; 76 private static final int IN_SUBENTRY = 16; 77 private static final int IN_SUBKEY = 32; 78 private static final int IN_FONTS = 64; 79 private static final int IN_FONT = 128; 80 81 private static final int STRING = 0; 82 private static final int LIST = 1; 83 private static final int MAP = 2; 84 85 private int status = OUT; 87 private int datatype = -1; 88 89 private static Map configuration; 91 private static Map activeConfiguration; 92 93 private String key = ""; 95 private List keyStack = new java.util.ArrayList (); 96 97 private String value = ""; 99 100 private String subkey = ""; 102 103 private List list = new java.util.ArrayList (15); 105 106 private Map map = new java.util.HashMap (15); 108 109 112 private Locator locator; 113 114 117 private String role = "standard"; 118 119 private List fontList = null; 121 122 private FontInfo fontInfo = null; 124 125 private FontTriplet fontTriplet = null; 127 128 private String fontName, metricsFile, embedFile, kerningAsString; 130 private boolean kerning; 131 private List fontTriplets; 132 133 private String fontTripletName, weight, style; 135 136 public void startDocument() { 137 configuration = Configuration.getConfiguration(); 138 } 139 140 143 public void setDocumentLocator(Locator locator) { 144 this.locator = locator; 145 } 146 147 150 public void startElement(String uri, String localName, String qName, 151 Attributes attributes) { 152 if (localName.equals("key")) { 153 status += IN_KEY; 154 } else if (localName.equals("value")) { 155 status += IN_VALUE; 156 } else if (localName.equals("list")) { 157 status += IN_LIST; 158 } else if (localName.equals("subentry")) { 159 status += IN_SUBENTRY; 160 } else if (localName.equals("entry")) { 161 if (attributes.getLength() == 0) { 163 role = "standard"; 164 } else { 166 role = attributes.getValue("role"); 167 } 168 } else if (localName.equals("configuration")) {} 169 else if (localName.equals("fonts")) { fontList = new java.util.ArrayList (10); 171 } else if (localName.equals("font")) { 172 kerningAsString = attributes.getValue("kerning"); 173 if (kerningAsString.equalsIgnoreCase("yes")) { 174 kerning = true; 175 } else { 176 kerning = false; 177 } 178 metricsFile = attributes.getValue("metrics-file"); 179 embedFile = attributes.getValue("embed-file"); 180 fontName = attributes.getValue("name"); 181 fontTriplets = new java.util.ArrayList (5); 182 } else if (localName.equals("font-triplet")) { 183 fontTripletName = attributes.getValue("name"); 184 weight = attributes.getValue("weight"); 185 style = attributes.getValue("style"); 186 fontTriplet = new FontTriplet(fontTripletName, weight, style); 187 fontTriplets.add(fontTriplet); 188 } else { 189 MessageHandler.errorln("Unknown tag in configuration file: " 191 + localName); 192 } 193 } 195 198 public void endElement(String uri, String localName, String qName) { 199 if (localName.equals("entry")) { 200 switch (datatype) { 201 case STRING: 202 this.store(role, key, value); 203 break; 204 case LIST: 205 this.store(role, key, list); 206 break; 207 case MAP: 208 this.store(role, key, map); 209 } 210 status = OUT; 211 role = "standard"; 212 if (keyStack.size() > 0) { 213 keyStack.remove(keyStack.size() - 1); 214 } 215 if (keyStack.size() > 0) { 216 key = (String )keyStack.get(keyStack.size() - 1); 217 } else { 218 key = ""; 219 } 220 value = ""; 221 } else if (localName.equals("subentry")) { 222 map.put(subkey, value); 223 status -= IN_SUBENTRY; 224 if (keyStack.size() > 0) { 225 keyStack.remove(keyStack.size() - 1); 226 } 227 if (keyStack.size() > 0) { 228 key = (String )keyStack.get(keyStack.size() - 1); 229 } else { 230 key = ""; 231 } 232 value = ""; 233 } else if (localName.equals("key")) { 234 status -= IN_KEY; 235 keyStack.add(key); 236 } else if (localName.equals("list")) { 237 status -= IN_LIST; 238 value = ""; 239 } else if (localName.equals("value")) { 240 status -= IN_VALUE; 241 } else if (localName.equals("fonts")) { 242 this.store("standard", "fonts", fontList); 243 } else if (localName.equals("font")) { 244 fontInfo = new FontInfo(fontName, metricsFile, kerning, 245 fontTriplets, embedFile); 246 fontList.add(fontInfo); 247 fontTriplets = null; 248 metricsFile = null; 249 embedFile = null; 250 fontName = null; 251 kerningAsString = ""; 252 } else if (localName.equals("font-triplet")) {} 253 } 254 255 259 public void characters(char[] ch, int start, int length) { 260 char characters[] = new char[length]; 261 System.arraycopy(ch, start, characters, 0, length); 262 String text = new String (characters); 263 switch (status) { 264 case IN_KEY: 265 key = text; 266 break; 267 case IN_LIST + IN_SUBENTRY + IN_KEY: 268 subkey = text; 269 break; 270 case IN_VALUE: 271 value = text; 272 datatype = STRING; 273 break; 274 case IN_LIST + IN_VALUE: 275 list.add(text); 276 datatype = LIST; 277 break; 278 case IN_LIST + IN_SUBENTRY + IN_VALUE: 279 value = text; 280 datatype = MAP; 281 break; 282 } 283 } 285 292 private void store(String role, String key, Object value) { 293 activeConfiguration = (Map )configuration.get(role); 294 if (activeConfiguration != null) { 295 activeConfiguration.put(key, value); 296 } else { 297 MessageHandler.errorln("Unknown role >" + role 298 + "< for new configuration entry. \n" 299 + "Putting configuration with key:" + key 300 + " into standard configuration."); 301 } 302 } 303 304 } 305 | Popular Tags |