1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.io.IOException;
13 import java.io.Writer;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.Map;
17 import java.util.StringTokenizer;
18
19 import org.dom4j.Element;
20 import org.dom4j.ProcessingInstruction;
21 import org.dom4j.Visitor;
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.15 $
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 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 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 toString() {
53 return super.toString() + " [ProcessingInstruction: &" + getName() + ";]";
54 }
55
56 public String asXML() {
57 return "<?" + getName() + " " + getText() + "?>";
58 }
59
60 public void write(Writer writer) throws IOException {
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 name, String value) {
73 throw new UnsupportedOperationException(
74 "This PI is read-only and cannot be modified"
75 );
76 }
77
78 public void setValues(Map data) {
79 throw new UnsupportedOperationException(
80 "This PI is read-only and cannot be modified"
81 );
82 }
83
84 public String getName() {
85 return getTarget();
86 }
87
88 public void setName(String name) {
89 setTarget(name);
90 }
91
92 public boolean removeValue(String name) {
93 return false;
94 }
95
96
97
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 toString(Map values) {
104 StringBuffer buffer = new StringBuffer();
105
106 for ( Iterator iter = values.entrySet().iterator(); iter.hasNext(); ) {
107 Map.Entry entry = (Map.Entry) iter.next();
108 String name = (String) entry.getKey();
109 String value = (String) entry.getValue();
110
111 buffer.append(name);
112 buffer.append("=\"");
113 buffer.append(value);
114 buffer.append("\" ");
115 }
116
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 parseValues(String text) {
126 Map data = new HashMap();
127
128 StringTokenizer s = new StringTokenizer(text, " =\'\"", true);
129 while (s.hasMoreTokens()) {
130 String name = getName(s);
131 if (s.hasMoreTokens()) {
132 String value = getValue(s);
133 data.put(name, value);
134 }
135 }
136
137 return data;
138 }
139
140 private String getName(StringTokenizer tokenizer) {
141 String token = tokenizer.nextToken();
142 StringBuffer name = new StringBuffer(token);
143 while (tokenizer.hasMoreTokens()) {
144 token = tokenizer.nextToken();
145 if (!token.equals("=")) {
146 name.append(token);
147 } else {
148 break;
149 }
150 }
151
152 return name.toString().trim();
153 }
154
155 private String getValue(StringTokenizer tokenizer) {
156 String token = tokenizer.nextToken();
157 StringBuffer value = new StringBuffer();
158
159
160 while (tokenizer.hasMoreTokens() && !token.equals("\'") && !token.equals("\"")) {
161 token = tokenizer.nextToken();
162 }
163
164 String quote = token;
165 while (tokenizer.hasMoreTokens()) {
166 token = tokenizer.nextToken();
167 if (!quote.equals(token)) {
168 value.append(token);
169 } else {
170 break;
171 }
172 }
173
174 return value.toString();
175 }
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226