001: /*
002: * $Id: XmlDefinitionsSet.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.tiles.xmlDefinition;
023:
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import org.apache.struts.tiles.NoSuchDefinitionException;
029:
030: /**
031: * A set of definitions read from XML definitions file.
032: */
033: public class XmlDefinitionsSet {
034: /** Defined definitions. */
035: protected Map definitions;
036:
037: /**
038: * Constructor.
039: */
040: public XmlDefinitionsSet() {
041: definitions = new HashMap();
042: }
043:
044: /**
045: * Put definition in set.
046: * @param definition Definition to add.
047: */
048: public void putDefinition(XmlDefinition definition) {
049: definitions.put(definition.getName(), definition);
050: }
051:
052: /**
053: * Get requested definition.
054: * @param name Definition name.
055: */
056: public XmlDefinition getDefinition(String name) {
057: return (XmlDefinition) definitions.get(name);
058: }
059:
060: /**
061: * Get definitions map.
062: */
063: public Map getDefinitions() {
064: return definitions;
065: }
066:
067: /**
068: * Resolve extended instances.
069: */
070: public void resolveInheritances() throws NoSuchDefinitionException {
071: // Walk through all definitions and resolve individual inheritance
072: Iterator i = definitions.values().iterator();
073: while (i.hasNext()) {
074: XmlDefinition definition = (XmlDefinition) i.next();
075: definition.resolveInheritance(this );
076: } // end loop
077: }
078:
079: /**
080: * Add definitions from specified child definitions set.
081: * For each definition in child, look if it already exists in this set.
082: * If not, add it, if yes, overload parent's definition with child definition.
083: * @param child Definition used to overload this object.
084: */
085: public void extend(XmlDefinitionsSet child) {
086: if (child == null)
087: return;
088: Iterator i = child.getDefinitions().values().iterator();
089: while (i.hasNext()) {
090: XmlDefinition childInstance = (XmlDefinition) i.next();
091: XmlDefinition parentInstance = getDefinition(childInstance
092: .getName());
093: if (parentInstance != null) {
094: parentInstance.overload(childInstance);
095: } else
096: putDefinition(childInstance);
097: } // end loop
098: }
099:
100: /**
101: * Get String representation.
102: */
103: public String toString() {
104: return "definitions=" + definitions.toString();
105: }
106:
107: }
|