1 16 package org.apache.cocoon.components.xscript; 17 18 import org.xml.sax.Attributes ; 19 import org.xml.sax.SAXException ; 20 import org.xml.sax.helpers.DefaultHandler ; 21 22 import java.util.Stack ; 23 24 25 34 public class StringBufferContentHandler extends DefaultHandler { 35 private static Object marker = new Object (); 36 37 private Stack namespaces = new Stack (); 38 private StringBuffer stringBuffer; 39 40 public StringBufferContentHandler(StringBuffer stringBuffer) { 41 this.stringBuffer = stringBuffer; 42 } 43 44 public void startPrefixMapping(String prefix, String uri) 45 throws SAXException { 46 namespaces.push(new NPU(prefix, uri)); 47 } 48 49 public void endPrefixMapping(String prefix) 50 throws SAXException { 51 namespaces.pop(); 52 } 53 54 public void startElement(String uri, String loc, String qName, Attributes a) 55 throws SAXException { 56 int lastNamespaceIndex = 0; 57 58 lastNamespaceIndex = namespaces.size() - 1; 59 while (lastNamespaceIndex >= 0 60 && namespaces.elementAt(lastNamespaceIndex) != marker) { 61 lastNamespaceIndex--; 62 } 63 64 if (lastNamespaceIndex < 0) { 65 lastNamespaceIndex = 0; 66 } else if (namespaces.elementAt(lastNamespaceIndex) == marker) { 67 lastNamespaceIndex++; 68 } 69 70 namespaces.push(marker); 71 stringBuffer.append("<").append(qName); 72 73 for (int i = 0, len = a.getLength(); i < len; i++) { 74 String attrName = a.getQName(i); 79 if (attrName.startsWith("xmlns:")) { 80 String name = a.getLocalName(i); 82 83 boolean found = false; 85 for (int j = namespaces.size() - 1; 86 j >= lastNamespaceIndex; 87 j--) { 88 Object obj = namespaces.elementAt(j); 89 if (obj == marker) { 90 continue; 91 } 92 NPU npu = (NPU) obj; 93 94 if (name.equals(npu.prefix)) { 95 found = true; 96 break; 97 } 98 } 99 100 if (!found) { 101 namespaces.push(new NPU(name, a.getValue(i))); 102 } 103 } else { 104 stringBuffer.append(" ").append(a.getQName(i)).append("=\""); 106 escape(stringBuffer, a.getValue(i)); 107 stringBuffer.append("\""); 108 } 109 } 110 111 if (namespaces.size() != 0) { 112 for (int i = namespaces.size() - 1; i >= lastNamespaceIndex; i--) { 113 Object obj = namespaces.elementAt(i); 114 if (obj == marker) { 115 continue; 116 } 117 NPU npu = (NPU) obj; 118 if ("".equals(npu.prefix)) { 119 stringBuffer.append(" xmlns").append("=\"").append(npu.uri).append("\""); 121 } else { 122 stringBuffer.append(" xmlns:").append(npu.prefix).append("=\"").append(npu.uri).append("\""); 123 } 124 } 125 } 126 127 stringBuffer.append(">"); 128 } 129 130 public void endElement(String uri, String loc, String qName) 131 throws SAXException { 132 stringBuffer.append("</").append(qName).append(">"); 133 134 Object obj; 135 do { 136 obj = namespaces.pop(); 137 } while (obj != marker); 138 } 139 140 public void characters(char ch[], int start, int len) 141 throws SAXException { 142 escape(stringBuffer, ch, start, len); 143 } 144 145 146 151 private static void escape(StringBuffer buffer, String s) { 152 char[] ch = s.toCharArray(); 153 escape(buffer, ch, 0, ch.length); 154 } 155 156 161 private static void escape(StringBuffer buffer, char ch[], int start, int len) { 162 for (int i = start; i < start + len; i++) { 163 switch (ch[i]) { 164 case '<': 165 buffer.append("<"); 166 break; 167 case '&': 168 buffer.append("&"); 169 break; 170 case '>': 171 buffer.append(">"); 172 break; 173 default: 174 buffer.append(ch[i]); 175 break; 176 } 177 } 178 } 179 } 180 181 class NPU { 182 public String prefix; 183 public String uri; 184 185 NPU(String prefix, String uri) { 186 this.prefix = prefix; 187 this.uri = uri; 188 } 189 190 public String toString() { 191 return this.prefix + "=" + this.uri; 192 } 193 } 194 | Popular Tags |