001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.hivemind.impl;
016
017 import java.util.ArrayList;
018 import java.util.Collections;
019 import java.util.HashMap;
020 import java.util.List;
021 import java.util.Map;
022
023 import org.apache.hivemind.Attribute;
024 import org.apache.hivemind.Element;
025 import org.apache.hivemind.util.ToStringBuilder;
026
027 /**
028 * Implementation of {@link org.apache.hivemind.Element}.
029 *
030 * @author Howard Lewis Ship
031 */
032 public final class ElementImpl extends BaseLocatable implements Element
033 {
034 private String _elementName;
035 private String _content;
036 private List _elements;
037 private List _safeElements;
038 private List _attributes;
039 private Map _attributesMap;
040 private List _safeAttributes;
041
042 public void setElementName(String elementName)
043 {
044 _elementName = elementName;
045 }
046
047 public String getElementName()
048 {
049 return _elementName;
050 }
051
052 public void addAttribute(Attribute attribute)
053 {
054 if (_attributes == null)
055 {
056 _attributes = new ArrayList();
057 _attributesMap = new HashMap();
058 }
059
060 _attributes.add(attribute);
061 _attributesMap.put(attribute.getName(), attribute);
062 }
063
064 public void addElement(Element element)
065 {
066 if (_elements == null)
067 _elements = new ArrayList();
068
069 _elements.add(element);
070 }
071
072 public synchronized List getAttributes()
073 {
074 if (_attributes == null)
075 return Collections.EMPTY_LIST;
076
077 if (_safeAttributes == null)
078 _safeAttributes = Collections.unmodifiableList(_attributes);
079
080 return _safeAttributes;
081 }
082
083 public String getContent()
084 {
085 if (_content == null)
086 return "";
087
088 return _content;
089 }
090
091 public synchronized List getElements()
092 {
093 if (_elements == null)
094 return Collections.EMPTY_LIST;
095
096 if (_safeElements == null)
097 _safeElements = Collections.unmodifiableList(_elements);
098
099 return _safeElements;
100 }
101
102 public String getAttributeValue(String attributeName)
103 {
104 if (_attributesMap == null)
105 return null;
106
107 Attribute a = (Attribute) _attributesMap.get(attributeName);
108
109 if (a == null)
110 return null;
111
112 return a.getValue();
113 }
114
115 public boolean isEmpty()
116 {
117 return _elements == null || _elements.size() == 0;
118 }
119
120 public void setContent(String string)
121 {
122 _content = string;
123 }
124
125 public String toString()
126 {
127 ToStringBuilder builder = new ToStringBuilder(this);
128
129 builder.append("elementName", _elementName);
130 builder.append("attributes", _attributes);
131 builder.append("elements", _elements);
132 builder.append("content", _content);
133
134 return builder.toString();
135 }
136 }