KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > tools > ConfigurationUtil


1 /*
2  * $Id: ConfigurationUtil.java,v 1.7 2003/10/29 07:34:09 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.tools;
59
60 import org.apache.avalon.framework.configuration.Configuration;
61 import org.apache.avalon.framework.configuration.DefaultConfiguration;
62 import org.w3c.dom.Attr JavaDoc;
63 import org.w3c.dom.Document JavaDoc;
64 import org.w3c.dom.DocumentFragment JavaDoc;
65 import org.w3c.dom.Element JavaDoc;
66 import org.w3c.dom.NamedNodeMap JavaDoc;
67 import org.w3c.dom.Node JavaDoc;
68 import org.w3c.dom.Text JavaDoc;
69
70 /**
71  * This utility class provides helper methods for Avalon Configuration objects.
72  *
73  * @author Jeremias Maerki
74  */

75 public class ConfigurationUtil {
76
77     /**
78      * Utility class: Constructor prevents instantiating when subclassed.
79      */

80     protected ConfigurationUtil() {
81         throw new UnsupportedOperationException JavaDoc();
82     }
83     
84     /**
85      * Builds a Configuration object from a DOM node.
86      * @param node the DOM node
87      * @return the Configuration object
88      */

89     public static Configuration buildConfiguration(Node JavaDoc node) {
90         return processNode(node);
91     }
92     
93     private static Element JavaDoc findDocumentElement(Document JavaDoc document) {
94         //return document.getDocumentElement(); Xalan-bug, doesn't work (2.4.1)
95
Node JavaDoc nd = null;
96         for (int i = 0; i < document.getChildNodes().getLength(); i++) {
97             nd = document.getChildNodes().item(i);
98             if (nd.getNodeType() == Node.ELEMENT_NODE) {
99                 return (Element JavaDoc)nd;
100             }
101         }
102         return null;
103     }
104     
105     private static DefaultConfiguration processNode(Node JavaDoc node) {
106         if (node.getNodeType() == Node.ELEMENT_NODE) {
107             return processElement((Element JavaDoc)node);
108         } else if (node.getNodeType() == Node.DOCUMENT_NODE) {
109             return processElement(findDocumentElement((Document JavaDoc)node));
110         } else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) {
111             DocumentFragment JavaDoc df = (DocumentFragment JavaDoc)node;
112             return processNode(df.getFirstChild());
113         } else {
114             return null;
115         }
116     }
117     
118     private static DefaultConfiguration processElement(Element JavaDoc el) {
119         DefaultConfiguration cfg = new DefaultConfiguration(el.getLocalName());
120         NamedNodeMap JavaDoc atts = el.getAttributes();
121         for (int i = 0; i < atts.getLength(); i++) {
122             Attr JavaDoc attr = (Attr JavaDoc)atts.item(i);
123             cfg.setAttribute(attr.getName(), attr.getValue());
124         }
125         for (int i = 0; i < el.getChildNodes().getLength(); i++) {
126             Node JavaDoc node = el.getChildNodes().item(i);
127             if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
128                 Attr JavaDoc attr = (Attr JavaDoc)node;
129                 cfg.setAttribute(attr.getName(), attr.getNodeValue());
130             } else if (node.getNodeType() == Node.ELEMENT_NODE) {
131                 cfg.addChild(processElement((Element JavaDoc)node));
132             } else if (node.getNodeType() == Node.TEXT_NODE) {
133                 String JavaDoc s = cfg.getValue("") + ((Text JavaDoc)node).getData();
134                 cfg.setValue(s.trim());
135             } else {
136                 //ignore
137
}
138         }
139         return cfg;
140     }
141
142 }
143
Popular Tags