KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > config > xml > elementreader > GenericElementReader


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.config.xml.elementreader;
18
19 import java.util.Iterator JavaDoc;
20
21 import org.dom4j.Attribute;
22 import org.dom4j.Element;
23
24 import org.alfresco.config.ConfigElement;
25 import org.alfresco.config.element.GenericConfigElement;
26
27 /**
28  * Implementation of a generic element reader. This class can be used to
29  * convert any config element into a GenericConfigElement.
30  *
31  * @author gavinc
32  */

33 public class GenericElementReader implements ConfigElementReader
34 {
35    /**
36     * @see org.alfresco.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element)
37     */

38    public ConfigElement parse(Element element)
39    {
40       GenericConfigElement configElement = null;
41       
42       if (element != null)
43       {
44          configElement = createConfigElement(element);
45          
46          // process any children there may be
47
processChildren(element, configElement);
48       }
49       
50       return configElement;
51    }
52
53    /**
54     * Recursively processes the children creating the required config element
55     * objects as it goes
56     *
57     * @param element
58     * @param parentConfig
59     */

60    private void processChildren(Element element, GenericConfigElement parentConfig)
61    {
62       // get the list of children for the given element
63
Iterator JavaDoc<Element> children = element.elementIterator();
64       while (children.hasNext())
65       {
66          Element child = children.next();
67          GenericConfigElement childConfigElement = createConfigElement(child);
68          parentConfig.addChild(childConfigElement);
69          
70          // recurse down the children
71
processChildren(child, childConfigElement);
72       }
73    }
74    
75    /**
76     * Creates a ConfigElementImpl object from the given element.
77     *
78     * @param element
79     * @return
80     */

81    private GenericConfigElement createConfigElement(Element element)
82    {
83       // get the name and value of the given element
84
String JavaDoc name = element.getName();
85       
86       // create the config element object and populate with value
87
// and attributes
88
GenericConfigElement configElement = new GenericConfigElement(name);
89       if ((element.hasContent()) && (element.hasMixedContent() == false))
90       {
91          String JavaDoc value = element.getTextTrim();
92          if (value != null && value.length() > 0)
93          {
94             configElement.setValue(value);
95          }
96       }
97       
98       Iterator JavaDoc<Attribute> attrs = element.attributeIterator();
99       while (attrs.hasNext())
100       {
101          Attribute attr = attrs.next();
102          String JavaDoc attrName = attr.getName();
103          String JavaDoc attrValue = attr.getValue();
104          
105          configElement.addAttribute(attrName, attrValue);
106       }
107       
108       return configElement;
109    }
110 }
111
Popular Tags