1   
2   
3   
4   
5   
6   
7   
8   package org.dom4j.tree;
9   
10  import java.io.IOException;
11  import java.io.Writer;
12  import java.util.HashMap;
13  import java.util.Iterator;
14  import java.util.Map;
15  import java.util.StringTokenizer;
16  
17  import org.dom4j.Element;
18  import org.dom4j.ProcessingInstruction;
19  import org.dom4j.Visitor;
20  
21  /***
22   * <p>
23   * <code>AbstractProcessingInstruction</code> is an abstract base class for
24   * tree implementors to use for implementation inheritence.
25   * </p>
26   * 
27   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
28   * @version $Revision: 1.17 $
29   */
30  public abstract class AbstractProcessingInstruction extends AbstractNode
31          implements ProcessingInstruction {
32      public AbstractProcessingInstruction() {
33      }
34  
35      public short getNodeType() {
36          return PROCESSING_INSTRUCTION_NODE;
37      }
38  
39      public String getPath(Element context) {
40          Element parent = getParent();
41  
42          return ((parent != null) && (parent != context)) ? (parent
43                  .getPath(context) + "/processing-instruction()")
44                  : "processing-instruction()";
45      }
46  
47      public String getUniquePath(Element context) {
48          Element parent = getParent();
49  
50          return ((parent != null) && (parent != context)) ? (parent
51                  .getUniquePath(context) + "/processing-instruction()")
52                  : "processing-instruction()";
53      }
54  
55      public String toString() {
56          return super.toString() + " [ProcessingInstruction: &" + getName()
57                  + ";]";
58      }
59  
60      public String asXML() {
61          return "<?" + getName() + " " + getText() + "?>";
62      }
63  
64      public void write(Writer writer) throws IOException {
65          writer.write("<?");
66          writer.write(getName());
67          writer.write(" ");
68          writer.write(getText());
69          writer.write("?>");
70      }
71  
72      public void accept(Visitor visitor) {
73          visitor.visit(this);
74      }
75  
76      public void setValue(String name, String value) {
77          throw new UnsupportedOperationException("This PI is read-only and "
78                  + "cannot be modified");
79      }
80  
81      public void setValues(Map data) {
82          throw new UnsupportedOperationException("This PI is read-only and "
83                  + "cannot be modified");
84      }
85  
86      public String getName() {
87          return getTarget();
88      }
89  
90      public void setName(String name) {
91          setTarget(name);
92      }
93  
94      public boolean removeValue(String name) {
95          return false;
96      }
97  
98      
99  
100     /***
101      * <p>
102      * This will convert the Map to a string representation.
103      * </p>
104      * 
105      * @param values
106      *            is a <code>Map</code> of PI data to convert
107      * 
108      * @return DOCUMENT ME!
109      */
110     protected String toString(Map values) {
111         StringBuffer buffer = new StringBuffer();
112 
113         for (Iterator iter = values.entrySet().iterator(); iter.hasNext();) {
114             Map.Entry entry = (Map.Entry) iter.next();
115             String name = (String) entry.getKey();
116             String value = (String) entry.getValue();
117 
118             buffer.append(name);
119             buffer.append("=\"");
120             buffer.append(value);
121             buffer.append("\" ");
122         }
123 
124         
125         buffer.setLength(buffer.length() - 1);
126 
127         return buffer.toString();
128     }
129 
130     /***
131      * <p>
132      * Parses the raw data of PI as a <code>Map</code>.
133      * </p>
134      * 
135      * @param text
136      *            <code>String</code> PI data to parse
137      * 
138      * @return DOCUMENT ME!
139      */
140     protected Map parseValues(String text) {
141         Map data = new HashMap();
142 
143         StringTokenizer s = new StringTokenizer(text, " =\'\"", true);
144 
145         while (s.hasMoreTokens()) {
146             String name = getName(s);
147 
148             if (s.hasMoreTokens()) {
149                 String value = getValue(s);
150                 data.put(name, value);
151             }
152         }
153 
154         return data;
155     }
156 
157     private String getName(StringTokenizer tokenizer) {
158         String token = tokenizer.nextToken();
159         StringBuffer name = new StringBuffer(token);
160 
161         while (tokenizer.hasMoreTokens()) {
162             token = tokenizer.nextToken();
163 
164             if (!token.equals("=")) {
165                 name.append(token);
166             } else {
167                 break;
168             }
169         }
170 
171         return name.toString().trim();
172     }
173 
174     private String getValue(StringTokenizer tokenizer) {
175         String token = tokenizer.nextToken();
176         StringBuffer value = new StringBuffer();
177 
178         
179         while (tokenizer.hasMoreTokens() && !token.equals("\'")
180                 && !token.equals("\"")) {
181             token = tokenizer.nextToken();
182         }
183 
184         String quote = token;
185 
186         while (tokenizer.hasMoreTokens()) {
187             token = tokenizer.nextToken();
188 
189             if (!quote.equals(token)) {
190                 value.append(token);
191             } else {
192                 break;
193             }
194         }
195 
196         return value.toString();
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 
227 
228 
229 
230 
231 
232 
233 
234 
235