1 16 package org.outerj.daisy.books.publisher.impl.util; 17 18 import org.xml.sax.helpers.DefaultHandler ; 19 import org.xml.sax.helpers.AttributesImpl ; 20 import org.xml.sax.Attributes ; 21 import org.xml.sax.SAXException ; 22 import org.xml.sax.InputSource ; 23 import org.xml.sax.ContentHandler ; 24 import org.apache.commons.collections.map.ListOrderedMap; 25 import org.outerj.daisy.xmlutil.LocalSAXParserFactory; 26 27 import javax.xml.parsers.SAXParser ; 28 import java.util.Map ; 29 import java.util.Iterator ; 30 import java.io.InputStream ; 31 32 35 public class XMLPropertiesHelper { 36 37 public static Map load(InputStream is) throws Exception { 38 return load(is, (Map)null); 39 } 40 41 public static Map load(InputStream is, Map defaults) throws Exception { 42 return load(is, defaults, "properties"); 43 } 44 45 public static Map load(InputStream is, String rootElement) throws Exception { 46 return load(is, null, rootElement); 47 } 48 49 public static Map load(InputStream is, Map defaults, String rootElement) throws Exception { 50 return load(new InputSource (is), defaults, rootElement); 51 } 52 53 public static Map load(InputSource is, Map defaults, String rootElement) throws Exception { 54 SAXParser parser = LocalSAXParserFactory.getSAXParserFactory().newSAXParser(); 55 Map properties = new ListOrderedMap(); 56 if (defaults != null) 57 properties.putAll(defaults); 58 PropertiesHandler propertiesHandler = new PropertiesHandler(properties, rootElement); 59 parser.getXMLReader().setContentHandler(propertiesHandler); 60 parser.getXMLReader().parse(is); 61 return properties; 62 } 63 64 public static void generateSaxFragment(Map map, ContentHandler contentHandler) throws SAXException { 65 generateSaxFragment(map, "properties", contentHandler); 66 } 67 68 public static void generateSaxFragment(Map map, String rootElement, ContentHandler contentHandler) throws SAXException { 69 contentHandler.startElement("", rootElement, rootElement, new AttributesImpl ()); 70 71 Iterator entryIt = map.entrySet().iterator(); 72 while (entryIt.hasNext()) { 73 Map.Entry entry = (Map.Entry)entryIt.next(); 74 String key = (String )entry.getKey(); 75 String value = (String )entry.getValue(); 76 AttributesImpl attrs = new AttributesImpl (); 77 attrs.addAttribute("", "key", "key", "CDATA", key); 78 contentHandler.startElement("", "entry", "entry", attrs); 79 contentHandler.characters(value.toCharArray(), 0, value.length()); 80 contentHandler.endElement("", "entry", "entry"); 81 } 82 83 contentHandler.endElement("", rootElement, rootElement); 84 } 85 86 private static class PropertiesHandler extends DefaultHandler { 87 private String rootElementName; 88 private final Map properties; 89 private int nesting = 0; 90 private String key = null; 91 private StringBuffer entry = new StringBuffer (); 92 93 public PropertiesHandler(Map properties, String rootElementName) { 94 this.rootElementName = rootElementName; 95 this.properties = properties; 96 } 97 98 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 99 nesting++; 100 if (nesting == 1 && !(uri.equals("") && localName.equals(rootElementName))) { 101 throw new SAXException ("Expected <" + rootElementName + "> root element."); 102 } else if (nesting == 2 && uri.equals("") && localName.equals("entry")) { 103 entry.setLength(0); 104 key = attributes.getValue("key"); 105 if (key == null || key.trim().equals("")) { 106 throw new SAXException ("Missing or empty key attribute on <entry> element."); 107 } 108 } 109 } 110 111 public void endElement(String uri, String localName, String qName) throws SAXException { 112 if (nesting == 2 && key != null) { 113 properties.put(key, entry.toString()); 114 key = null; 115 } 116 nesting--; 117 } 118 119 public void characters(char ch[], int start, int length) throws SAXException { 120 if (key != null) 121 entry.append(ch, start, length); 122 } 123 } 124 } 125 | Popular Tags |