1
2
3
4
5
6
7
8
9
10 package org.dom4j.tree;
11
12 import java.util.Collections;
13 import java.util.Map;
14
15 import org.dom4j.Element;
16 import org.dom4j.Node;
17
18 /*** <p><code>FlyweightProcessingInstruction</code> is a Flyweight pattern implementation
19 * of a singly linked, read-only XML Processing Instruction.</p>
20 *
21 * <p>This node could be shared across documents and elements though
22 * it does not support the parent relationship.</p>
23 *
24 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
25 * @version $Revision: 1.5 $
26 */
27 public class FlyweightProcessingInstruction extends AbstractProcessingInstruction {
28
29 /*** The target of the PI */
30 protected String target;
31
32 /*** The values for the PI as a String */
33 protected String text;
34
35 /*** The values for the PI in name/value pairs */
36 protected Map values;
37
38 /*** A default constructor for implementors to use.
39 */
40 public FlyweightProcessingInstruction() {
41 }
42
43 /*** <p>This will create a new PI with the given target and values</p>
44 *
45 * @param target is the name of the PI
46 * @param values is the <code>Map</code> of the values for the PI
47 */
48 public FlyweightProcessingInstruction(String target,Map values) {
49 this.target = target;
50 this.values = values;
51 this.text = toString(values);
52 }
53
54 /*** <p>This will create a new PI with the given target and values</p>
55 *
56 * @param target is the name of the PI
57 * @param text is the values for the PI as text
58 */
59 public FlyweightProcessingInstruction(String target,String text) {
60 this.target = target;
61 this.text = text;
62 this.values = parseValues(text);
63 }
64
65 public String getTarget() {
66 return target;
67 }
68
69 public void setTarget(String target) {
70 throw new UnsupportedOperationException( "This PI is read-only and cannot be modified" );
71 }
72
73 public String getText() {
74 return text;
75 }
76
77 public String getValue(String name) {
78 String answer = (String) values.get(name);
79 if (answer == null) {
80 return "";
81 }
82 return answer;
83 }
84
85 public Map getValues() {
86 return Collections.unmodifiableMap( values );
87 }
88
89 protected Node createXPathResult(Element parent) {
90 return new DefaultProcessingInstruction( parent, getTarget(), getText() );
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142