001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: AbstractProcessingInstruction.java,v 1.10 2001/07/25 10:51:11 jstrachan Exp $ 008 */ 009 010package org.dom4j.tree; 011 012import java.io.IOException; 013import java.io.Writer; 014 015import java.util.Map; 016import java.util.HashMap; 017import java.util.Iterator; 018import java.util.StringTokenizer; 019 020import org.dom4j.Element; 021import org.dom4j.ProcessingInstruction; 022import org.dom4j.Visitor; 023 024/** <p><code>AbstractProcessingInstruction</code> is an abstract base class for 025 * tree implementors to use for implementation inheritence.</p> 026 * 027 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 028 * @version $Revision: 1.10 $ 029 */ 030public abstract class AbstractProcessingInstruction extends AbstractNode implements ProcessingInstruction { 031 032 public AbstractProcessingInstruction() { 033 } 034 035 public short getNodeType() { 036 return PROCESSING_INSTRUCTION_NODE; 037 } 038 039 public String getPath(Element context) { 040 Element parent = getParent(); 041 return ( parent != null && parent != context ) 042 ? parent.getPath( context ) + "/processing-instruction()" 043 : "processing-instruction()"; 044 } 045 046 public String getUniquePath(Element context) { 047 Element parent = getParent(); 048 return ( parent != null && parent != context ) 049 ? parent.getUniquePath( context ) + "/processing-instruction()" 050 : "processing-instruction()"; 051 } 052 053 public String toString() { 054 return super.toString() + " [ProcessingInstruction: &" + getName() + ";]"; 055 } 056 057 public String asXML() { 058 return "<?" + getName() + " " + getText() + "?>"; 059 } 060 061 public void write(Writer writer) throws IOException { 062 writer.write( "<?" ); 063 writer.write( getName() ); 064 writer.write( " " ); 065 writer.write( getText() ); 066 writer.write( "?>" ); 067 } 068 069 public void accept(Visitor visitor) { 070 visitor.visit(this); 071 } 072 073 public void setValue(String name, String value) { 074 throw new UnsupportedOperationException( 075 "This PI is read-only and cannot be modified" 076 ); 077 } 078 079 public void setValues(Map data) { 080 throw new UnsupportedOperationException( 081 "This PI is read-only and cannot be modified" 082 ); 083 } 084 085 public String getName() { 086 return getTarget(); 087 } 088 089 public void setName(String name) { 090 setTarget(name); 091 } 092 093 public boolean removeValue(String name) { 094 return false; 095 } 096 097 098 // Helper methods 099 100 /** <p>This will convert the Map to a string representation.</p> 101 * 102 * @param values is a <code>Map</code> of PI data to convert 103 */ 104 protected String toString(Map values) { 105 StringBuffer buffer = new StringBuffer(); 106 107 for ( Iterator iter = values.entrySet().iterator(); iter.hasNext(); ) { 108 Map.Entry entry = (Map.Entry) iter.next(); 109 String name = (String) entry.getKey(); 110 String value = (String) entry.getValue(); 111 112 buffer.append(name); 113 buffer.append("=\""); 114 buffer.append(value); 115 buffer.append("\" "); 116 } 117 // remove the last space 118 buffer.setLength(buffer.length() - 1); 119 return buffer.toString(); 120 } 121 122 /**<p>Parses the raw data of PI as a <code>Map</code>.</p> 123 * 124 * @param text <code>String</code> PI data to parse 125 */ 126 protected Map parseValues(String text) { 127 Map data = new HashMap(); 128 129 // Break up name/value pairs 130 StringTokenizer s = 131 new StringTokenizer(text); 132 133 // Iterate through the pairs 134 while (s.hasMoreTokens()) { 135 // Now break up pair on the = and (" or ') characters 136 StringTokenizer t = 137 new StringTokenizer(s.nextToken(), "='\""); 138 139 if (t.countTokens() >= 2) { 140 String name = t.nextToken(); 141 String value = t.nextToken(); 142 143 data.put(name, value); 144 } 145 } 146 return data; 147 } 148} 149 150 151 152 153 154 155/* 156 * Redistribution and use of this software and associated documentation 157 * ("Software"), with or without modification, are permitted provided 158 * that the following conditions are met: 159 * 160 * 1. Redistributions of source code must retain copyright 161 * statements and notices. Redistributions must also contain a 162 * copy of this document. 163 * 164 * 2. Redistributions in binary form must reproduce the 165 * above copyright notice, this list of conditions and the 166 * following disclaimer in the documentation and/or other 167 * materials provided with the distribution. 168 * 169 * 3. The name "DOM4J" must not be used to endorse or promote 170 * products derived from this Software without prior written 171 * permission of MetaStuff, Ltd. For written permission, 172 * please contact dom4j-info@metastuff.com. 173 * 174 * 4. Products derived from this Software may not be called "DOM4J" 175 * nor may "DOM4J" appear in their names without prior written 176 * permission of MetaStuff, Ltd. DOM4J is a registered 177 * trademark of MetaStuff, Ltd. 178 * 179 * 5. Due credit should be given to the DOM4J Project 180 * (http://dom4j.org/). 181 * 182 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 183 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 184 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 185 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 186 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 187 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 188 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 189 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 190 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 191 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 192 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 193 * OF THE POSSIBILITY OF SUCH DAMAGE. 194 * 195 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 196 * 197 * $Id: AbstractProcessingInstruction.java,v 1.10 2001/07/25 10:51:11 jstrachan Exp $ 198 */