View Javadoc

1   /*
2    * Copyright 2001-2005 (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   
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      // Helper methods
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         // remove the last space
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         /* get the quote */
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  * Redistribution and use of this software and associated documentation
202  * ("Software"), with or without modification, are permitted provided that the
203  * following conditions are met:
204  * 
205  * 1. Redistributions of source code must retain copyright statements and
206  * notices. Redistributions must also contain a copy of this document.
207  * 
208  * 2. Redistributions in binary form must reproduce the above copyright notice,
209  * this list of conditions and the following disclaimer in the documentation
210  * and/or other materials provided with the distribution.
211  * 
212  * 3. The name "DOM4J" must not be used to endorse or promote products derived
213  * from this Software without prior written permission of MetaStuff, Ltd. For
214  * written permission, please contact dom4j-info@metastuff.com.
215  * 
216  * 4. Products derived from this Software may not be called "DOM4J" nor may
217  * "DOM4J" appear in their names without prior written permission of MetaStuff,
218  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
219  * 
220  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
221  * 
222  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
223  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
226  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
227  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
228  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
229  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
230  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
231  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
232  * POSSIBILITY OF SUCH DAMAGE.
233  * 
234  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
235  */