You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigurationParser.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.configuration;
  8. import org.apache.fop.render.pdf.EmbedFontInfo;
  9. import org.apache.fop.render.pdf.FontTriplet;
  10. // sax
  11. import org.xml.sax.helpers.DefaultHandler;
  12. import org.xml.sax.Attributes;
  13. import org.xml.sax.Locator;
  14. // java
  15. import java.util.HashMap;
  16. import java.util.ArrayList;
  17. /**
  18. * SAX2 Handler which retrieves the configuration information and stores them in Configuration.
  19. * Normally this class doesn't need to be accessed directly.
  20. */
  21. public class ConfigurationParser extends DefaultHandler {
  22. private final int OUT = 0;
  23. private final int IN_ENTRY = 1;
  24. private final int IN_KEY = 2;
  25. private final int IN_VALUE = 4;
  26. private final int IN_LIST = 8;
  27. private final int IN_SUBENTRY = 16;
  28. private final int IN_SUBKEY = 32;
  29. private final int IN_FONTS = 64;
  30. private final int IN_FONT = 128;
  31. private final int STRING = 0;
  32. private final int LIST = 1;
  33. private final int MAP = 2;
  34. // state of parser
  35. private int status = OUT;
  36. private int datatype = -1;
  37. // store the result configuration
  38. private static HashMap configuration;
  39. private static HashMap activeConfiguration;
  40. // stores key for new config entry
  41. private String key = "";
  42. private ArrayList keyStack = new ArrayList();
  43. // stores string value
  44. private String value = "";
  45. // stores key for new config entry
  46. private String subkey = "";
  47. // stores list value
  48. private ArrayList list = new ArrayList(15);
  49. // stores hashtable value
  50. private HashMap map = new HashMap(15);
  51. /**
  52. * locator for line number information
  53. */
  54. private Locator locator;
  55. /**
  56. * determines role / target of configuration information, default is standard
  57. */
  58. private String role = "standard";
  59. // stores fonts
  60. private ArrayList fontList = null;
  61. // stores information on one font
  62. private EmbedFontInfo fontInfo = null;
  63. // stores information on a font triplet
  64. private FontTriplet fontTriplet = null;
  65. // information on a font
  66. private String fontName, metricsFile, embedFile, kerningAsString;
  67. private boolean kerning;
  68. private ArrayList fontTriplets;
  69. // information on a font triplet
  70. private String fontTripletName, weight, style;
  71. public void startDocument() {
  72. configuration = Configuration.getConfiguration();
  73. }
  74. /**
  75. * get locator for position information
  76. */
  77. public void setDocumentLocator(Locator locator) {
  78. this.locator = locator;
  79. }
  80. /**
  81. * extracts the element and attribute name and sets the fitting status and datatype values
  82. */
  83. public void startElement(String uri, String localName, String qName,
  84. Attributes attributes) {
  85. if (localName.equals("key")) {
  86. status += IN_KEY;
  87. } else if (localName.equals("value")) {
  88. status += IN_VALUE;
  89. } else if (localName.equals("list")) {
  90. status += IN_LIST;
  91. } else if (localName.equals("subentry")) {
  92. status += IN_SUBENTRY;
  93. } else if (localName.equals("entry")) {
  94. // role=standard as default
  95. if (attributes.getLength() == 0) {
  96. role = "standard";
  97. // retrieve attribute value for "role" which determines configuration target
  98. } else {
  99. role = attributes.getValue("role");
  100. }
  101. } else if (localName.equals("configuration")) {}
  102. else if (localName.equals("fonts")) { // list of fonts starts
  103. fontList = new ArrayList(10);
  104. } else if (localName.equals("font")) {
  105. kerningAsString = attributes.getValue("kerning");
  106. if (kerningAsString.equalsIgnoreCase("yes")) {
  107. kerning = true;
  108. } else {
  109. kerning = false;
  110. }
  111. metricsFile = attributes.getValue("metrics-file");
  112. embedFile = attributes.getValue("embed-file");
  113. fontName = attributes.getValue("name");
  114. fontTriplets = new ArrayList(5);
  115. } else if (localName.equals("font-triplet")) {
  116. fontTripletName = attributes.getValue("name");
  117. weight = attributes.getValue("weight");
  118. style = attributes.getValue("style");
  119. fontTriplet = new FontTriplet(fontTripletName, weight, style);
  120. fontTriplets.add(fontTriplet);
  121. } else {
  122. // to make sure that user knows about false tag
  123. //log.error("Unknown tag in configuration file: "
  124. // + localName);
  125. }
  126. } // end startElement
  127. /**
  128. * stores subentries or entries into their hashes (map for subentries, configuration for entry)
  129. */
  130. public void endElement(String uri, String localName, String qName) {
  131. if (localName.equals("entry")) {
  132. switch (datatype) {
  133. case STRING:
  134. this.store(role, key, value);
  135. break;
  136. case LIST:
  137. this.store(role, key, list);
  138. break;
  139. case MAP:
  140. this.store(role, key, map);
  141. }
  142. status = OUT;
  143. role = "standard";
  144. if (keyStack.size() > 0) {
  145. keyStack.remove(keyStack.size() - 1);
  146. }
  147. if (keyStack.size() > 0) {
  148. key = (String)keyStack.get(keyStack.size() - 1);
  149. } else {
  150. key = "";
  151. }
  152. value = "";
  153. } else if (localName.equals("subentry")) {
  154. map.put(subkey, value);
  155. status -= IN_SUBENTRY;
  156. if (keyStack.size() > 0) {
  157. keyStack.remove(keyStack.size() - 1);
  158. }
  159. if (keyStack.size() > 0) {
  160. key = (String)keyStack.get(keyStack.size() - 1);
  161. } else {
  162. key = "";
  163. }
  164. value = "";
  165. } else if (localName.equals("key")) {
  166. status -= IN_KEY;
  167. keyStack.add(key);
  168. } else if (localName.equals("list")) {
  169. status -= IN_LIST;
  170. value = "";
  171. } else if (localName.equals("value")) {
  172. status -= IN_VALUE;
  173. } else if (localName.equals("fonts")) {
  174. this.store("standard", "fonts", fontList);
  175. } else if (localName.equals("font")) {
  176. fontInfo = new EmbedFontInfo(fontName, metricsFile, kerning,
  177. fontTriplets, embedFile);
  178. fontList.add(fontInfo);
  179. fontTriplets = null;
  180. metricsFile = null;
  181. embedFile = null;
  182. fontName = null;
  183. kerningAsString = "";
  184. } else if (localName.equals("font-triplet")) {}
  185. }
  186. /**
  187. * extracts characters from text nodes and puts them into their respective
  188. * variables
  189. */
  190. public void characters(char[] ch, int start, int length) {
  191. char characters[] = new char[length];
  192. System.arraycopy(ch, start, characters, 0, length);
  193. String text = new String(characters);
  194. switch (status) {
  195. case IN_KEY:
  196. key = text;
  197. break;
  198. case IN_LIST + IN_SUBENTRY + IN_KEY:
  199. subkey = text;
  200. break;
  201. case IN_VALUE:
  202. value = text;
  203. datatype = STRING;
  204. break;
  205. case IN_LIST + IN_VALUE:
  206. list.add(text);
  207. datatype = LIST;
  208. break;
  209. case IN_LIST + IN_SUBENTRY + IN_VALUE:
  210. value = text;
  211. datatype = MAP;
  212. break;
  213. }
  214. } // end characters
  215. /**
  216. * stores configuration entry into configuration hashtable according to the role
  217. *
  218. * @param role a string containing the role / target for this configuration information
  219. * @param key a string containing the key value for the configuration
  220. * @param value a string containing the value for the configuration
  221. */
  222. private void store(String role, String key, Object value) {
  223. activeConfiguration = (HashMap)configuration.get(role);
  224. if (activeConfiguration != null) {
  225. activeConfiguration.put(key, value);
  226. } else {
  227. //log.error("Unknown role >" + role
  228. // + "< for new configuration entry. \n"
  229. // + "Putting configuration with key:" + key
  230. // + " into standard configuration.");
  231. }
  232. }
  233. }