blob: ff4e4281a4c4010fe2a9f23967a68053836a9b08 [file] [log] [blame]
Richard S. Hall7fa14152006-06-14 15:22:03 +00001/*
2 * Copyright 2006 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.felix.ipojo.metadata;
18
19/**
20 * Element.
Richard S. Hallbe279fc2006-06-22 12:56:53 +000021 * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
Richard S. Hall7fa14152006-06-14 15:22:03 +000022 */
23public class Element {
24
25 /**
26 * Name of the element.
27 */
28 private String m_name;
29
30 /**
31 * Name space of the element.
32 */
33 private String m_nameSpace;
34
35 /**
36 * List of the attributes of the element.
37 */
38 private Attribute[] m_attributes = new Attribute[0];
39
40 /**
41 * List of the subelement of the element.
42 */
43 private Element[] m_elements = new Element[0];
44
45 /**
46 * Constructor.
47 * @param name : the name of the element
48 * @pram ns : the namespace of the element
49 */
50 public Element(String name, String ns) {
51 m_name = name.toLowerCase();
52 m_nameSpace = ns;
53 }
54
55 /**
56 * @return the sub elements
57 */
58 public Element[] getElements() { return m_elements; }
59
60 /**
61 * @return the attributes
62 */
63 public Attribute[] getAttributes() { return m_attributes; }
64
65 /**
66 * @return the name of the element
67 */
68 public String getName() { return m_name; }
69
70 /**
71 * @return the namespace of the element
72 */
73 public String getNameSpace() { return m_nameSpace; }
74
75 /**
76 * Return the value of the attribute given in parameter.
77 * @param name : the name of the searched attribute
78 * @return the value of the attrbitue given in parameter, null if the attribute does not exist
79 */
80 public String getAttribute(String name) {
81 name = name.toLowerCase();
82 for (int i = 0; i < m_attributes.length; i++) {
83 if (m_attributes[i].getName().equals(name)) {
84 return m_attributes[i].getValue();
85 }
86 }
87 System.err.println("[Error in Metadata] The attribute " + name + " does not exist in " + m_name + " [" + m_nameSpace + "]");
88 return null;
89 }
90
91 /**
92 * Return the value of the attrbitue "name" of the namespace "ns".
93 * @param name : name of the attribute to find
94 * @param ns : namespace of the attribute to find
95 * @return the String value of the attribute, or null if the attribute is not found.
96 */
97 public String getAttribute(String name, String ns) {
98 name = name.toLowerCase();
99 for (int i = 0; i < m_attributes.length; i++) {
100 if (m_attributes[i].getName().equals(name) && m_attributes[i].getNameSpace().equals(ns)) {
101 return m_attributes[i].getValue();
102 }
103 }
104 System.err.println("[Error in Metadata] The attribute " + name + "[" + ns + "] does not exist in " + m_name + " [" + m_nameSpace + "]");
105 return null;
106 }
107
108 /**
109 * Add a subelement.
110 * @param elem : the element to add
111 */
112 public void addElement(Element elem) {
113 for (int i = 0; (m_elements != null) && (i < m_elements.length); i++) {
114 if (m_elements[i] == elem) { return; }
115 }
116
117 if (m_elements != null) {
118 Element[] newElementsList = new Element[m_elements.length + 1];
119 System.arraycopy(m_elements, 0, newElementsList, 0, m_elements.length);
120 newElementsList[m_elements.length] = elem;
121 m_elements = newElementsList;
122 }
123 else { m_elements = new Element[] {elem}; }
124 }
125
126 private static Element[] addElement(Element[] list, Element elem) {
127 if (list != null) {
128 Element[] newElementsList = new Element[list.length + 1];
129 System.arraycopy(list, 0, newElementsList, 0, list.length);
130 newElementsList[list.length] = elem;
131 return newElementsList;
132 }
133 else { return new Element[] {elem}; }
134 }
135
136 /**
137 * Remove a subelement.
138 * @param elem : the element to remove
139 */
140 public void removeElement(Element elem) {
141 int idx = -1;
142 for (int i = 0; i < m_elements.length; i++) {
143 if (m_elements[i] == elem) { idx = i; break; }
144 }
145
146 if (idx >= 0) {
147 if ((m_elements.length - 1) == 0) { m_elements = new Element[0]; }
148 else {
149 Element[] newElementsList = new Element[m_elements.length - 1];
150 System.arraycopy(m_elements, 0, newElementsList, 0, idx);
151 if (idx < newElementsList.length) {
152 System.arraycopy(m_elements, idx + 1, newElementsList, idx, newElementsList.length - idx); }
153 m_elements = newElementsList;
154 }
155 }
156 }
157
158 /**
159 * Add a attribute.
160 * @param att : the attribute to add
161 */
162 public void addAttribute(Attribute att) {
163 for (int i = 0; (m_attributes != null) && (i < m_attributes.length); i++) {
164 if (m_attributes[i] == att) { return; }
165 }
166
167 if (m_attributes != null) {
168 Attribute[] newAttributesList = new Attribute[m_attributes.length + 1];
169 System.arraycopy(m_attributes, 0, newAttributesList, 0, m_attributes.length);
170 newAttributesList[m_attributes.length] = att;
171 m_attributes = newAttributesList;
172 }
173 else { m_attributes = new Attribute[] {att}; }
174 }
175
176 /**
177 * Remove an attribute.
178 * @param att : the attribute to remove
179 */
180 public void removeAttribute(Attribute att) {
181 int idx = -1;
182 for (int i = 0; i < m_attributes.length; i++) {
183 if (m_attributes[i] == att) { idx = i; break; }
184 }
185
186 if (idx >= 0) {
187 if ((m_attributes.length - 1) == 0) { m_attributes = new Attribute[0]; }
188 else {
189 Attribute[] newAttributesList = new Attribute[m_attributes.length - 1];
190 System.arraycopy(m_attributes, 0, newAttributesList, 0, idx);
191 if (idx < newAttributesList.length) {
192 System.arraycopy(m_attributes, idx + 1, newAttributesList, idx, newAttributesList.length - idx); }
193 m_attributes = newAttributesList;
194 }
195 }
196 }
197
198 /**
199 * Get the elements array of the element type given in parameter.
200 * This method look for an empty namespace.
201 * @param name : the type of the element to find (element name)
202 * @return the resulting element array (empty if the search failed)
203 */
204 public Element[] getElements(String name) {
205 name = name.toLowerCase();
206 Element[] list = new Element[0];
207 for (int i = 0; i < m_elements.length; i++) {
208 if (m_elements[i].getName().equals(name) && m_elements[i].getNameSpace().equals("")) {
209 list = Element.addElement(list, m_elements[i]);
210 }
211 }
212 return list;
213 }
214
215 /**
216 * Get the elements array of the element type given in parameter.
217 * @param name : the type of the element to find (element name)
218 * @param ns : the namespace of the element
219 * @return the resulting element array (empty if the search failed)
220 */
221 public Element[] getElements(String name, String ns) {
222 name = name.toLowerCase();
223 Element[] list = new Element[0];
224 for (int i = 0; i < m_elements.length; i++) {
225 if (m_elements[i].getName().equals(name) && m_elements[i].getNameSpace().equals(ns)) {
226 list = Element.addElement(list, m_elements[i]);
227 }
228 }
229 return list;
230 }
231
232 /**
233 * Is the element contains a subelement of the type given in parameter.
234 * This method does not manage the namespace
235 * @param name : type of the element to check.
236 * @return true if the element contains an element of the type "name"
237 */
238 public boolean containsElement(String name) {
239 name = name.toLowerCase();
240 for (int i = 0; i < m_elements.length; i++) {
241 if (m_elements[i].getName().equals(name)) { return true; }
242 }
243 return false;
244 }
245
246 /**
247 * Is the element contains a subelement of the type given in parameter.
248 * This method does not manage the namespace
249 * @param name : type of the element to check.
250 * @return true if the element contains an element of the type "name"
251 */
252 public boolean containsElement(String name, String ns) {
253 name = name.toLowerCase();
254 ns = ns.toLowerCase();
255 for (int i = 0; i < m_elements.length; i++) {
256 if (m_elements[i].getName().equals(name) && m_elements[i].getNameSpace().equals(ns)) { return true; }
257 }
258 return false;
259 }
260
261 /**
262 * Is the element contains an attribute of the name given in parameter.
263 * @param name : name of the element
264 * @return true if the element contains an attribute of the type "name"
265 */
266 public boolean containsAttribute(String name) {
267 name = name.toLowerCase();
268 for (int i = 0; i < m_attributes.length; i++) {
269 if (m_attributes[i].getName().equals(name)) { return true; }
270 }
271 return false;
272 }
273
274 /**
275 * @return the first-order namespaces list of the current element. First-order namespace are namespace of the element attribute and namespaces of its direct sub-element.
276 */
277 public String[] getNamespaces() {
278 String[] ns = new String[0];
279
280 // Look for each direct sub-element
281 for (int i = 0; i < m_elements.length; i++) {
282 boolean found = false;
283 for (int j = 0; !found && j < ns.length; j++) {
284 if (ns[j].equals(m_elements[i].getNameSpace())) { found = true; }
285 }
286 if (!found) {
287 String[] newNSList = new String[ns.length + 1];
288 System.arraycopy(ns, 0, newNSList, 0, ns.length);
289 newNSList[ns.length] = m_elements[i].getNameSpace();
290 ns = newNSList;
291 }
292 }
293
294 // Look for each attribute
295 for (int i = 0; i < m_attributes.length; i++) {
296 boolean found = false;
297 for (int j = 0; !found && j < ns.length; j++) {
298 if (ns[j].equals(m_attributes[i].getNameSpace())) { found = true; }
299 }
300 if (!found) {
301 String[] newNSList = new String[ns.length + 1];
302 System.arraycopy(ns, 0, newNSList, 0, ns.length);
303 newNSList[ns.length] = m_attributes[i].getNameSpace();
304 ns = newNSList;
305 }
306 }
307
308 return ns;
309 }
310
311}