View Javadoc

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