2 * Copyright (c) 2001-2007 Sun Microsystems, Inc. All rights reserved.
4 * The Sun Project JXTA(TM) Software License
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment: "This product includes software
18 * developed by Sun Microsystems, Inc. for JXTA(TM) technology."
19 * Alternately, this acknowledgment may appear in the software itself, if
20 * and wherever such third-party acknowledgments normally appear.
22 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must
23 * not be used to endorse or promote products derived from this software
24 * without prior written permission. For written permission, please contact
25 * Project JXTA at http://www.jxta.org.
27 * 5. Products derived from this software may not be called "JXTA", nor may
28 * "JXTA" appear in their name, without prior written permission of Sun.
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN
33 * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
36 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
39 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * JXTA is a registered trademark of Sun Microsystems, Inc. in the United
42 * States and other countries.
44 * Please see the license information page at :
45 * <http://www.jxta.org/project/www/license.html> for instructions on use of
46 * the license in source files.
48 * ====================================================================
50 * This software consists of voluntary contributions made by many individuals
51 * on behalf of Project JXTA. For more information on Project JXTA, please see
52 * http://www.jxta.org.
54 * This license is based on the BSD license adopted by the Apache Foundation.
57 package net.jxta.impl.document;
60 import java.io.Writer;
61 import java.util.ArrayList;
62 import java.util.Collections;
63 import java.util.Enumeration;
64 import java.util.List;
65 import java.util.Vector;
66 import java.util.HashMap;
67 import java.util.Iterator;
70 import java.io.IOException;
72 import net.jxta.document.Element;
73 import net.jxta.document.StructuredDocument;
74 import net.jxta.document.StructuredTextDocument;
75 import net.jxta.document.TextElement;
77 import net.jxta.document.Attributable;
78 import net.jxta.document.Attribute;
82 * This class is an implementation of the StructuredDocument interface using
85 public class PlainTextElement implements TextElement<PlainTextElement>, Attributable {
86 protected PlainTextDocument doc;
88 protected PlainTextElement parent;
90 protected final String name;
92 protected final String val;
94 private List children = new Vector();
96 private Map attributes = new HashMap();
99 * Creates new PlainTextElement
101 protected PlainTextElement(PlainTextDocument doc, String name) {
102 this(doc, name, null);
106 * Creates new PlainTextElement
108 protected PlainTextElement(PlainTextDocument doc, String name, String val) {
118 public boolean equals(Object element) {
119 if (this == element) {
123 if (!(element instanceof PlainTextElement)) {
127 PlainTextElement textElement = (PlainTextElement) element;
129 if (doc != textElement.doc) {
133 if (!getName().equals(textElement.getName())) {
137 String val1 = getTextValue();
138 String val2 = textElement.getTextValue();
140 if ((null == val1) && (null == val2)) {
144 if ((null == val1) || (null == val2)) {
148 return val1.equals(val2);
154 public StructuredTextDocument getRoot() {
155 return (StructuredTextDocument) doc;
159 * Get the name associated with an element.
161 * @return A string containing the key of this element.
163 public String getKey() {
168 * Get the value (if any) associated with an element.
170 * @return A string containing the value of this element, if any, otherwise null.
172 public String getValue() {
173 return getTextValue();
179 public PlainTextElement getParent() {
186 public Enumeration getChildren() {
187 return Collections.enumeration(children);
193 public String getName() {
200 public String getTextValue() {
207 public void appendChild(PlainTextElement element) {
208 if (element.doc != this.doc) {
209 throw new IllegalArgumentException("Wrong Document");
212 element.parent = this;
213 children.add(element);
219 public Enumeration<PlainTextElement> getChildren(Object key) {
220 if (key instanceof String)
221 return getChildren((String) key);
223 throw new ClassCastException(key.getClass().getName() + " not supported by getChildren.");
229 public Enumeration<PlainTextElement> getChildren(String name) {
230 List result = new ArrayList();
232 for (Iterator eachChild = children.iterator(); eachChild.hasNext();) {
233 TextElement aChild = (TextElement) eachChild.next();
235 if (name.equals(aChild.getName())) {
240 return Collections.enumeration(result);
244 * Write the contents of this element and optionally its children. The
245 * writing is done to a provided java.io.Writer. The writing can optionally
248 * @param into The java.io.Writer that the output will be sent to.
249 * @param indent the number of tabs which will be inserted before each
251 * @param recurse if true then also print the children of this element.
253 protected void printNice(Writer into, int indent, boolean recurse) throws IOException {
256 for (int eachTab = 0; eachTab < indent; eachTab++) {
264 Enumeration attributes = getAttributes();
266 if (attributes.hasMoreElements()) {
269 while (attributes.hasMoreElements()) {
270 Attribute anAttr = (Attribute) attributes.nextElement();
272 into.write(anAttr.getName() + "=\"" + anAttr.getValue() + "\" ");
280 into.write(val + "\n");
287 for (Enumeration childrens = getChildren(); childrens.hasMoreElements();) {
288 ((PlainTextElement) childrens.nextElement()).printNice(into, indent + 1, recurse);
293 // Attributable methods
298 public String addAttribute(String name, String value) {
300 String oldAttrValue = (String) attributes.remove(name);
302 attributes.put(name, value);
308 * Adds an attribute with the given name and value. Some implementations
309 * may support only a single value for each distinct name. Others may
310 * support multiple values for each name. If the value being provided
311 * replaces some other value then that value is returned otherwise null
314 * @param newAttrib new attribute.
315 * @return String containing previous value for this name if the value
316 * is being replaced otherwise null.
318 public String addAttribute(Attribute newAttrib) {
319 return addAttribute(newAttrib.getName(), newAttrib.getValue());
325 public Enumeration getAttributes() {
327 Vector attrs = new Vector();
329 for (Iterator eachAttr = attributes.entrySet().iterator(); eachAttr.hasNext();) {
330 Map.Entry anAttr = (Map.Entry) eachAttr.next();
332 Attribute attr = new Attribute(this, (String) anAttr.getKey(), (String) anAttr.getValue());
334 attrs.addElement(attr);
337 return attrs.elements();
343 public Attribute getAttribute(String name) {
344 String value = (String) attributes.get(name);
351 return new Attribute(this, name, value);