1 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 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 100 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 buffer.setLength(buffer.length() - 1); 126 127 return buffer.toString(); 128 } 129 130 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 236 | Popular Tags |