KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom4j > o3impl > AbstractProcessingInstruction


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: AbstractProcessingInstruction.java,v 1.2 2003/06/10 16:18:36 per_nyfelt Exp $
8  */

9
10 package org.ozoneDB.xml.dom4j.o3impl;
11
12 import org.dom4j.Element;
13 import org.dom4j.ProcessingInstruction;
14 import org.dom4j.Visitor;
15
16 import java.io.IOException JavaDoc;
17 import java.io.Writer JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 /** <p><code>AbstractProcessingInstruction</code> is an abstract base class for
24  * tree implementors to use for implementation inheritence.</p>
25  *
26  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
27  * @version $Revision: 1.2 $
28  */

29 public abstract class AbstractProcessingInstruction extends AbstractNode implements ProcessingInstruction {
30
31     public AbstractProcessingInstruction() {
32     }
33
34     public short getNodeType() {
35         return PROCESSING_INSTRUCTION_NODE;
36     }
37
38     public String JavaDoc getPath(Element context) {
39         Element parent = getParent();
40         return (parent != null && parent != context)
41                 ? parent.getPath(context) + "/processing-instruction()"
42                 : "processing-instruction()";
43     }
44
45     public String JavaDoc getUniquePath(Element context) {
46         Element parent = getParent();
47         return (parent != null && parent != context)
48                 ? parent.getUniquePath(context) + "/processing-instruction()"
49                 : "processing-instruction()";
50     }
51
52     public String JavaDoc toString() {
53         return super.toString() + " [ProcessingInstruction: &" + getName() + ";]";
54     }
55
56     public String JavaDoc asXML() {
57         return "<?" + getName() + " " + getText() + "?>";
58     }
59
60     public void write(Writer JavaDoc writer) throws IOException JavaDoc {
61         writer.write("<?");
62         writer.write(getName());
63         writer.write(" ");
64         writer.write(getText());
65         writer.write("?>");
66     }
67
68     public void accept(Visitor visitor) {
69         visitor.visit(this);
70     }
71
72     public void setValue(String JavaDoc name, String JavaDoc value) {
73         throw new UnsupportedOperationException JavaDoc(
74                 "This PI is read-only and cannot be modified"
75         );
76     }
77
78     public void setValues(Map JavaDoc data) {
79         throw new UnsupportedOperationException JavaDoc(
80                 "This PI is read-only and cannot be modified"
81         );
82     }
83
84     public String JavaDoc getName() {
85         return getTarget();
86     }
87
88     public void setName(String JavaDoc name) {
89         setTarget(name);
90     }
91
92     public boolean removeValue(String JavaDoc name) {
93         return false;
94     }
95
96
97     // Helper methods
98

99     /** <p>This will convert the Map to a string representation.</p>
100      *
101      * @param values is a <code>Map</code> of PI data to convert
102      */

103     protected String JavaDoc toString(Map JavaDoc values) {
104         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
105
106         for (Iterator JavaDoc iter = values.entrySet().iterator(); iter.hasNext();) {
107             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
108             String JavaDoc name = (String JavaDoc) entry.getKey();
109             String JavaDoc value = (String JavaDoc) entry.getValue();
110
111             buffer.append(name);
112             buffer.append("=\"");
113             buffer.append(value);
114             buffer.append("\" ");
115         }
116         // remove the last space
117
buffer.setLength(buffer.length() - 1);
118         return buffer.toString();
119     }
120
121     /**<p>Parses the raw data of PI as a <code>Map</code>.</p>
122      *
123      * @param text <code>String</code> PI data to parse
124      */

125     protected Map JavaDoc parseValues(String JavaDoc text) {
126         Map JavaDoc data = new HashMap JavaDoc();
127
128         // Break up name/value pairs
129
StringTokenizer JavaDoc s =
130                 new StringTokenizer JavaDoc(text);
131
132         // Iterate through the pairs
133
while (s.hasMoreTokens()) {
134             // Now break up pair on the = and (" or ') characters
135
StringTokenizer JavaDoc t =
136                     new StringTokenizer JavaDoc(s.nextToken(), "='\"");
137
138             if (t.countTokens() >= 2) {
139                 String JavaDoc name = t.nextToken();
140                 String JavaDoc value = t.nextToken();
141
142                 data.put(name, value);
143             }
144         }
145         return data;
146     }
147 }
148
149
150 /*
151  * Redistribution and use of this software and associated documentation
152  * ("Software"), with or without modification, are permitted provided
153  * that the following conditions are met:
154  *
155  * 1. Redistributions of source code must retain copyright
156  * statements and notices. Redistributions must also contain a
157  * copy of this document.
158  *
159  * 2. Redistributions in binary form must reproduce the
160  * above copyright notice, this list of conditions and the
161  * following disclaimer in the documentation and/or other
162  * materials provided with the distribution.
163  *
164  * 3. The name "DOM4J" must not be used to endorse or promote
165  * products derived from this Software without prior written
166  * permission of MetaStuff, Ltd. For written permission,
167  * please contact dom4j-info@metastuff.com.
168  *
169  * 4. Products derived from this Software may not be called "DOM4J"
170  * nor may "DOM4J" appear in their names without prior written
171  * permission of MetaStuff, Ltd. DOM4J is a registered
172  * trademark of MetaStuff, Ltd.
173  *
174  * 5. Due credit should be given to the DOM4J Project
175  * (http://dom4j.org/).
176  *
177  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
178  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
179  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
180  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
181  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
182  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
183  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
184  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
185  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
186  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
187  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
188  * OF THE POSSIBILITY OF SUCH DAMAGE.
189  *
190  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
191  *
192  * $Id: AbstractProcessingInstruction.java,v 1.2 2003/06/10 16:18:36 per_nyfelt Exp $
193  */

194
Popular Tags