]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/document/PlainTextElement.java
af632e99829f0c994a0e10c49d0746398f4aa016
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / document / PlainTextElement.java
1 /*
2  * Copyright (c) 2001-2007 Sun Microsystems, Inc.  All rights reserved.
3  *  
4  *  The Sun Project JXTA(TM) Software License
5  *  
6  *  Redistribution and use in source and binary forms, with or without 
7  *  modification, are permitted provided that the following conditions are met:
8  *  
9  *  1. Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *  
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.
15  *  
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.
21  *  
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.
26  *  
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.
29  *  
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.
40  *  
41  *  JXTA is a registered trademark of Sun Microsystems, Inc. in the United 
42  *  States and other countries.
43  *  
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.
47  *  
48  *  ====================================================================
49  *  
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.
53  *  
54  *  This license is based on the BSD license adopted by the Apache Foundation. 
55  */
56
57 package net.jxta.impl.document;
58
59
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;
68 import java.util.Map;
69
70 import java.io.IOException;
71
72 import net.jxta.document.Element;
73 import net.jxta.document.StructuredDocument;
74 import net.jxta.document.StructuredTextDocument;
75 import net.jxta.document.TextElement;
76
77 import net.jxta.document.Attributable;
78 import net.jxta.document.Attribute;
79
80
81 /**
82  * This class is an implementation of the StructuredDocument interface using
83  * simple text
84  */
85 public class PlainTextElement implements TextElement<PlainTextElement>, Attributable {
86     protected PlainTextDocument doc;
87
88     protected PlainTextElement parent;
89
90     protected final String name;
91
92     protected final String val;
93
94     private List children = new Vector();
95
96     private Map attributes = new HashMap();
97
98     /**
99      * Creates new PlainTextElement
100      */
101     protected PlainTextElement(PlainTextDocument doc, String name) {
102         this(doc, name, null);
103     }
104
105     /**
106      * Creates new PlainTextElement
107      */
108     protected PlainTextElement(PlainTextDocument doc, String name, String val) {
109         this.doc = doc;
110         this.name = name;
111         this.val = val;
112     }
113
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public boolean equals(Object element) {
119         if (this == element) {
120             return true;
121         }
122
123         if (!(element instanceof PlainTextElement)) {
124             return false;
125         }
126
127         PlainTextElement textElement = (PlainTextElement) element;
128
129         if (doc != textElement.doc) {
130             return false;
131         }
132
133         if (!getName().equals(textElement.getName())) {
134             return false;
135         }
136
137         String val1 = getTextValue();
138         String val2 = textElement.getTextValue();
139
140         if ((null == val1) && (null == val2)) {
141             return true;
142         }
143
144         if ((null == val1) || (null == val2)) {
145             return false;
146         }
147
148         return val1.equals(val2);
149     }
150
151     /**
152      * {@inheritDoc}
153      */
154     public StructuredTextDocument getRoot() {
155         return (StructuredTextDocument) doc;
156     }
157
158     /**
159      * Get the name associated with an element.
160      *
161      * @return A string containing the key of this element.
162      */
163     public String getKey() {
164         return getName();
165     }
166
167     /**
168      * Get the value (if any) associated with an element.
169      *
170      * @return A string containing the value of this element, if any, otherwise null.
171      */
172     public String getValue() {
173         return getTextValue();
174     }
175
176     /**
177      * {@inheritDoc}
178      */
179     public PlainTextElement getParent() {
180         return parent;
181     }
182
183     /**
184      * {@inheritDoc}
185      */
186     public Enumeration getChildren() {
187         return Collections.enumeration(children);
188     }
189
190     /**
191      * {@inheritDoc}
192      */
193     public String getName() {
194         return name;
195     }
196
197     /**
198      * {@inheritDoc}
199      */
200     public String getTextValue() {
201         return val;
202     }
203
204     /**
205      * {@inheritDoc}
206      */
207     public void appendChild(PlainTextElement element) {
208         if (element.doc != this.doc) {
209             throw new IllegalArgumentException("Wrong Document");
210         }
211
212         element.parent = this;
213         children.add(element);
214     }
215
216     /**
217      * {@inheritDoc}
218      */
219     public Enumeration<PlainTextElement> getChildren(Object key) {
220         if (key instanceof String)
221             return getChildren((String) key);
222         else
223             throw new ClassCastException(key.getClass().getName() + " not supported by getChildren.");
224     }
225
226     /**
227      * {@inheritDoc}
228      */
229     public Enumeration<PlainTextElement> getChildren(String name) {
230         List result = new ArrayList();
231
232         for (Iterator eachChild = children.iterator(); eachChild.hasNext();) {
233             TextElement aChild = (TextElement) eachChild.next();
234
235             if (name.equals(aChild.getName())) {
236                 result.add(aChild);
237             }
238         }
239
240         return Collections.enumeration(result);
241     }
242
243     /**
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
246      * be indented
247      *
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
250      *                line.
251      * @param recurse if true then also print the children of this element.
252      */
253     protected void printNice(Writer into, int indent, boolean recurse) throws IOException {
254
255         // do indent
256         for (int eachTab = 0; eachTab < indent; eachTab++) {
257             into.write("\t");
258         }
259
260         // print node name
261         into.write(name);
262
263         // print attributes
264         Enumeration attributes = getAttributes();
265
266         if (attributes.hasMoreElements()) {
267             into.write(" ( ");
268
269             while (attributes.hasMoreElements()) {
270                 Attribute anAttr = (Attribute) attributes.nextElement();
271
272                 into.write(anAttr.getName() + "=\"" + anAttr.getValue() + "\" ");
273             }
274             into.write(")");
275         }
276
277         into.write(" : ");
278         // print node value
279         if (null != val) {
280             into.write(val + "\n");
281         } else {
282             into.write("\n");
283         }
284
285         // recurse as needed
286         if (recurse) {
287             for (Enumeration childrens = getChildren(); childrens.hasMoreElements();) {
288                 ((PlainTextElement) childrens.nextElement()).printNice(into, indent + 1, recurse);
289             }
290         }
291     }
292
293     // Attributable methods
294
295     /**
296      * {@inheritDoc}
297      */
298     public String addAttribute(String name, String value) {
299
300         String oldAttrValue = (String) attributes.remove(name);
301
302         attributes.put(name, value);
303
304         return oldAttrValue;
305     }
306
307     /**
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
312      * is returned.
313      *
314      * @param newAttrib new attribute.
315      * @return String  containing previous value for this name if the value
316      *         is being replaced otherwise null.
317      */
318     public String addAttribute(Attribute newAttrib) {
319         return addAttribute(newAttrib.getName(), newAttrib.getValue());
320     }
321
322     /**
323      * {@inheritDoc}
324      */
325     public Enumeration getAttributes() {
326
327         Vector attrs = new Vector();
328
329         for (Iterator eachAttr = attributes.entrySet().iterator(); eachAttr.hasNext();) {
330             Map.Entry anAttr = (Map.Entry) eachAttr.next();
331
332             Attribute attr = new Attribute(this, (String) anAttr.getKey(), (String) anAttr.getValue());
333
334             attrs.addElement(attr);
335         }
336
337         return attrs.elements();
338     }
339
340     /**
341      * {@inheritDoc}
342      */
343     public Attribute getAttribute(String name) {
344         String value = (String) attributes.get(name);
345
346         if (null == value) {
347             return null;
348         }
349
350         // build the object
351         return new Attribute(this, name, value);
352     }
353 }