KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > config > element > GenericConfigElement


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.element;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.alfresco.config.ConfigElement;
23
24 /**
25  * Implementation of a generic configuration element. This class can handle the
26  * representation of any config element in a generic manner.
27  *
28  * @author gavinc
29  */

30 public class GenericConfigElement extends ConfigElementAdapter
31 {
32     /**
33      * Default constructor
34      *
35      * @param name Name of the config element
36      */

37     public GenericConfigElement(String JavaDoc name)
38     {
39         super(name);
40     }
41     
42     /**
43      * @see org.alfresco.web.config.ConfigElement#combine(org.alfresco.web.config.ConfigElement)
44      */

45     public ConfigElement combine(ConfigElement configElement)
46     {
47         GenericConfigElement combined = new GenericConfigElement(this.name);
48         combined.setValue(configElement.getValue());
49
50         // add the existing attributes to the new instance
51
if (this.attributes != null)
52         {
53             Iterator JavaDoc<String JavaDoc> attrs = this.getAttributes().keySet().iterator();
54             while (attrs.hasNext())
55             {
56                 String JavaDoc attrName = attrs.next();
57                 String JavaDoc attrValue = configElement.getAttribute(attrName);
58                 combined.addAttribute(attrName, attrValue);
59             }
60         }
61
62         // add/combine the attributes from the given instance
63
if (configElement.getAttributes() != null)
64         {
65             Iterator JavaDoc<String JavaDoc> attrs = configElement.getAttributes().keySet().iterator();
66             while (attrs.hasNext())
67             {
68                 String JavaDoc attrName = attrs.next();
69                 String JavaDoc attrValue = configElement.getAttribute(attrName);
70                 combined.addAttribute(attrName, attrValue);
71             }
72         }
73
74         // add the existing children to the new instance
75
List JavaDoc<ConfigElement> kids = this.getChildren();
76         if (kids != null)
77         {
78             for (int x = 0; x < kids.size(); x++)
79             {
80                 ConfigElement ce = kids.get(x);
81                 combined.addChild(ce);
82             }
83         }
84
85         // add the children from the given instance
86
kids = configElement.getChildren();
87         if (kids != null)
88         {
89             for (int x = 0; x < kids.size(); x++)
90             {
91                 ConfigElement ce = kids.get(x);
92                 combined.addChild(ce);
93             }
94         }
95
96         return combined;
97     }
98
99     /**
100      * Adds the attribute with the given name and value
101      *
102      * @param name
103      * Name of the attribute
104      * @param value
105      * Value of the attribute
106      */

107     public void addAttribute(String JavaDoc name, String JavaDoc value)
108     {
109         this.attributes.put(name, value);
110     }
111
112     /**
113      * Adds the given config element as a child of this element
114      *
115      * @param configElement
116      * The child config element
117      */

118     public void addChild(ConfigElement configElement)
119     {
120         this.children.add(configElement);
121     }
122 }
123
Popular Tags