Source Code Cross Referenced for OverrideCombiner.java in  » Library » Apache-commons-configuration-1.4-src » org » apache » commons » configuration » tree » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons configuration 1.4 src » org.apache.commons.configuration.tree 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.configuration.tree;
018:
019:        import java.util.Iterator;
020:
021:        /**
022:         * <p>
023:         * A concrete combiner implementation that is able to construct an override
024:         * combination.
025:         * </p>
026:         * <p>
027:         * An <em>override combination</em> means that nodes in the first node
028:         * structure take precedence over nodes in the second, or - in other words -
029:         * nodes of the second structure are only added to the resulting structure if
030:         * they do not occure in the first one. This is especially suitable for dealing
031:         * with the properties of configurations that are defined in an
032:         * <code>override</code> section of a configuration definition file (hence the
033:         * name).
034:         * </p>
035:         * <p>
036:         * This combiner will iterate over the second node hierarchy and find all nodes
037:         * that are not contained in the first hierarchy; these are added to the result.
038:         * If a node can be found in both structures, it is checked whether a
039:         * combination (in a recursive way) can be constructed for the two, which will
040:         * then be added. Per default, nodes are combined, which occur only once in both
041:         * structures. This test is implemented in the <code>canCombine()</code>
042:         * method.
043:         * </p>
044:         * <p>
045:         * As is true for the <code>{@link UnionCombiner}</code>, for this combiner
046:         * list nodes are important. The <code>addListNode()</code> can be called to
047:         * declare certain nodes as list nodes. This has the effect that these nodes
048:         * will never be combined.
049:         * </p>
050:         *
051:         * @author <a
052:         * href="http://jakarta.apache.org/commons/configuration/team-list.html">Commons
053:         * Configuration team</a>
054:         * @version $Id: OverrideCombiner.java 439648 2006-09-02 20:42:10Z oheger $
055:         * @since 1.3
056:         */
057:        public class OverrideCombiner extends NodeCombiner {
058:            /**
059:             * Constructs an override combination for the passed in node structures.
060:             *
061:             * @param node1 the first node
062:             * @param node2 the second node
063:             * @return the resulting combined node structure
064:             */
065:            public ConfigurationNode combine(ConfigurationNode node1,
066:                    ConfigurationNode node2) {
067:                ViewNode result = createViewNode();
068:                result.setName(node1.getName());
069:
070:                // Process nodes from the first structure, which override the second
071:                for (Iterator it = node1.getChildren().iterator(); it.hasNext();) {
072:                    ConfigurationNode child = (ConfigurationNode) it.next();
073:                    ConfigurationNode child2 = canCombine(node1, node2, child);
074:                    if (child2 != null) {
075:                        result.addChild(combine(child, child2));
076:                    } else {
077:                        result.addChild(child);
078:                    }
079:                }
080:
081:                // Process nodes from the second structure, which are not contained
082:                // in the first structure
083:                for (Iterator it = node2.getChildren().iterator(); it.hasNext();) {
084:                    ConfigurationNode child = (ConfigurationNode) it.next();
085:                    if (node1.getChildrenCount(child.getName()) < 1) {
086:                        result.addChild(child);
087:                    }
088:                }
089:
090:                // Handle attributes and value
091:                addAttributes(result, node1, node2);
092:                result.setValue((node1.getValue() != null) ? node1.getValue()
093:                        : node2.getValue());
094:
095:                return result;
096:            }
097:
098:            /**
099:             * Handles the attributes during a combination process. First all attributes
100:             * of the first node will be added to the result. Then all attributes of the
101:             * second node, which are not contained in the first node, will also be
102:             * added.
103:             *
104:             * @param result the resulting node
105:             * @param node1 the first node
106:             * @param node2 the second node
107:             */
108:            protected void addAttributes(ViewNode result,
109:                    ConfigurationNode node1, ConfigurationNode node2) {
110:                result.appendAttributes(node1);
111:                for (Iterator it = node2.getAttributes().iterator(); it
112:                        .hasNext();) {
113:                    ConfigurationNode attr = (ConfigurationNode) it.next();
114:                    if (node1.getAttributeCount(attr.getName()) == 0) {
115:                        result.addAttribute(attr);
116:                    }
117:                }
118:            }
119:
120:            /**
121:             * Tests if a child node of the second node can be combined with the given
122:             * child node of the first node. If this is the case, the corresponding node
123:             * will be returned, otherwise <b>null</b>. This implementation checks
124:             * whether the child node occurs only once in both hierarchies and is no
125:             * known list node.
126:             *
127:             * @param node1 the first node
128:             * @param node2 the second node
129:             * @param child the child node (of the first node)
130:             * @return a child of the second node, with which a combination is possible
131:             */
132:            protected ConfigurationNode canCombine(ConfigurationNode node1,
133:                    ConfigurationNode node2, ConfigurationNode child) {
134:                if (node2.getChildrenCount(child.getName()) == 1
135:                        && node1.getChildrenCount(child.getName()) == 1
136:                        && !isListNode(child)) {
137:                    return (ConfigurationNode) node2.getChildren(
138:                            child.getName()).get(0);
139:                } else {
140:                    return null;
141:                }
142:            }
143:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.