View Javadoc

1   /*
2    * The SmartConfig Library
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartConfig Library please visit
20   *
21   *                        http://smartconfig.sourceforge.net
22   */
23  package net.smartlab.config;
24  
25  import java.util.Collection;
26  
27  /**
28   * @author rlogiacco
29   */
30  public class Reference extends Element {
31  
32  	/**
33  	 * The name of the element attribute defining a reference to another
34  	 * element.
35  	 */
36  	protected final static String REFERENCE = "refid";
37  
38  	/**
39  	 * The name of the element attribute defining a valid reference target.
40  	 */
41  	protected final static String IDENTIFIER = "id";
42  
43  	/**
44  	 * The referenced node. It will be <code>null</code> until resolved.
45  	 */
46  	protected Node node;
47  
48  	/**
49  	 * The unique identifier for this reference.
50  	 * 
51  	 * @uml.property name="id"
52  	 */
53  	protected String id;
54  
55  
56  	/**
57  	 * Creates a reference contained by the given parent node with a specified
58  	 * name and unique identifier.
59  	 * 
60  	 * @param parent the node containing the element.
61  	 * @param name the element name.
62  	 * @param id the element identifier.
63  	 */
64  	protected Reference(Node parent, String name, String id) {
65  		super(parent, name);
66  		this.id = id;
67  	}
68  
69  	/**
70  	 * @see net.smartlab.config.Element#getId()
71  	 * @uml.property name="id"
72  	 */
73  	public String getId() {
74  		return id;
75  	}
76  
77  	/**
78  	 * @see net.smartlab.config.Element#getElements()
79  	 */
80  	public Collection getElements() throws ConfigurationException {
81  		if (node == null) {
82  			node = parent.resolve(name, id);
83  		}
84  		try {
85  			return node.getElements();
86  		} catch (NullPointerException npe) {
87  			throw new ConfigurationException("Undefined reference to node " + id);
88  		}
89  	}
90  
91  	/**
92  	 * @see net.smartlab.config.Element#getElements(java.lang.String)
93  	 */
94  	public Collection getElements(String name) throws ConfigurationException {
95  		if (node == null) {
96  			node = parent.resolve(this.name, id);
97  		}
98  		try {
99  			return node.getElements(name);
100 		} catch (NullPointerException npe) {
101 			throw new ConfigurationException("Undefined reference to node " + id);
102 		}
103 	}
104 
105 	/**
106 	 * @see net.smartlab.config.Element#getElement(java.lang.String,
107 	 *      java.lang.String, java.lang.String)
108 	 */
109 	public Element getElement(String name, String attribute, String value) throws ConfigurationException {
110 		if (node == null) {
111 			node = parent.resolve(this.name, id);
112 		}
113 		try {
114 			return node.getElement(name, attribute, value);
115 		} catch (NullPointerException npe) {
116 			throw new ConfigurationException("Undefined reference to node " + id);
117 		}
118 	}
119 
120 	/**
121 	 * @see net.smartlab.config.Element#getAttributes()
122 	 */
123 	public Collection getAttributes() throws ConfigurationException {
124 		if (node == null) {
125 			node = parent.resolve(name, id);
126 		}
127 		try {
128 			return node.getAttributes();
129 		} catch (NullPointerException npe) {
130 			throw new ConfigurationException("Undefined reference to node " + id);
131 		}
132 	}
133 
134 	/**
135 	 * @see net.smartlab.config.Element#getAttributeNames()
136 	 */
137 	public Collection getAttributeNames() throws ConfigurationException {
138 		if (node == null) {
139 			node = parent.resolve(name, id);
140 		}
141 		try {
142 			return node.getAttributeNames();
143 		} catch (NullPointerException npe) {
144 			throw new ConfigurationException("Undefined reference to node " + id);
145 		}
146 	}
147 
148 	/**
149 	 * @see net.smartlab.config.Element#getAttribute(java.lang.String)
150 	 */
151 	public String getAttribute(String name) throws ConfigurationException {
152 		if (name.equalsIgnoreCase(REFERENCE)) {
153 			return this.id;
154 		}
155 		if (node == null) {
156 			node = parent.resolve(this.name, id);
157 		}
158 		try {
159 			return node.getAttribute(name);
160 		} catch (NullPointerException npe) {
161 			throw new ConfigurationException("Undefined reference to node " + id);
162 		}
163 	}
164 
165 	/**
166 	 * @see net.smartlab.config.Element#getContent()
167 	 */
168 	public String getContent() throws ConfigurationException {
169 		if (node == null) {
170 			node = parent.resolve(name, id);
171 		}
172 		try {
173 			return node.content;
174 		} catch (NullPointerException npe) {
175 			throw new ConfigurationException("Undefined reference to node " + id);
176 		}
177 	}
178 
179 	/**
180 	 * @see net.smartlab.config.Element#resolve()
181 	 */
182 	public void resolve() throws ConfigurationException {
183 		if (node == null) {
184 			node = parent.resolve(name, id);
185 		}
186 	}
187 }