KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > publisher > impl > util > XMLPropertiesHelper


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.books.publisher.impl.util;
17
18 import org.xml.sax.helpers.DefaultHandler JavaDoc;
19 import org.xml.sax.helpers.AttributesImpl JavaDoc;
20 import org.xml.sax.Attributes JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.InputSource JavaDoc;
23 import org.xml.sax.ContentHandler JavaDoc;
24 import org.apache.commons.collections.map.ListOrderedMap;
25 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
26
27 import javax.xml.parsers.SAXParser JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.io.InputStream JavaDoc;
31
32 /**
33  * Reads properties from a Java 1.5 style XML properties file.
34  */

35 public class XMLPropertiesHelper {
36
37     public static Map load(InputStream JavaDoc is) throws Exception JavaDoc {
38         return load(is, (Map)null);
39     }
40
41     public static Map load(InputStream JavaDoc is, Map defaults) throws Exception JavaDoc {
42         return load(is, defaults, "properties");
43     }
44
45     public static Map load(InputStream JavaDoc is, String JavaDoc rootElement) throws Exception JavaDoc {
46         return load(is, null, rootElement);
47     }
48
49     public static Map load(InputStream JavaDoc is, Map defaults, String JavaDoc rootElement) throws Exception JavaDoc {
50         return load(new InputSource JavaDoc(is), defaults, rootElement);
51     }
52
53     public static Map load(InputSource JavaDoc is, Map defaults, String JavaDoc rootElement) throws Exception JavaDoc {
54         SAXParser JavaDoc 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 JavaDoc contentHandler) throws SAXException JavaDoc {
65         generateSaxFragment(map, "properties", contentHandler);
66     }
67
68     public static void generateSaxFragment(Map map, String JavaDoc rootElement, ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
69         contentHandler.startElement("", rootElement, rootElement, new AttributesImpl JavaDoc());
70
71         Iterator JavaDoc entryIt = map.entrySet().iterator();
72         while (entryIt.hasNext()) {
73             Map.Entry entry = (Map.Entry)entryIt.next();
74             String JavaDoc key = (String JavaDoc)entry.getKey();
75             String JavaDoc value = (String JavaDoc)entry.getValue();
76             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
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 JavaDoc {
87         private String JavaDoc rootElementName;
88         private final Map properties;
89         private int nesting = 0;
90         private String JavaDoc key = null;
91         private StringBuffer JavaDoc entry = new StringBuffer JavaDoc();
92
93         public PropertiesHandler(Map properties, String JavaDoc rootElementName) {
94             this.rootElementName = rootElementName;
95             this.properties = properties;
96         }
97
98         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
99             nesting++;
100             if (nesting == 1 && !(uri.equals("") && localName.equals(rootElementName))) {
101                 throw new SAXException JavaDoc("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 JavaDoc("Missing or empty key attribute on <entry> element.");
107                 }
108             }
109         }
110
111         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
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 JavaDoc {
120             if (key != null)
121                 entry.append(ch, start, length);
122         }
123     }
124 }
125
Popular Tags