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