1 package com.icl.saxon.output; 2 3 import com.icl.saxon.*; 4 import com.icl.saxon.om.Namespace; 5 import java.util.*; 6 import java.io.Writer ; 7 import org.xml.sax.Attributes ; 8 import javax.xml.transform.OutputKeys ; 9 import javax.xml.transform.TransformerException ; 10 11 15 16 public class UncommittedEmitter extends ProxyEmitter { 17 18 boolean committed = false; 19 boolean initialNewline = false; 20 boolean initialEscaping = true; 21 StringBuffer pendingCharacters; 22 23 public void startDocument() throws TransformerException { 24 committed = false; 25 } 26 27 30 31 public void endDocument() throws TransformerException { 32 if (!committed) { 34 switchToXML(); 35 } 36 super.endDocument(); 37 } 38 39 40 41 44 45 public void characters(char ch[], int start, int length) throws TransformerException { 46 if (!committed) { 47 boolean allWhite = true; 48 if (pendingCharacters==null) { 49 pendingCharacters=new StringBuffer (); 50 } 51 for (int i=start; i<start+length; i++) { 52 char c = ch[i]; 53 if (!Character.isWhitespace(c)) { 54 allWhite = false; 55 } 56 if (initialEscaping) { 57 if (c=='<') { 58 pendingCharacters.append("<"); 59 } else if (c=='>') { 60 pendingCharacters.append(">"); 61 } else if (c=='&') { 62 pendingCharacters.append("&"); 63 } else { 64 pendingCharacters.append(c); 65 } 66 } else { 67 pendingCharacters.append(c); 68 } 69 } 70 if (!allWhite) { 71 switchToXML(); 72 } 73 } else { 74 super.characters(ch, start, length); 75 } 76 } 77 78 81 82 public void processingInstruction(String target, String data) throws TransformerException { 83 if (!committed) { 84 if (pendingCharacters==null) { 85 pendingCharacters=new StringBuffer (); 86 } 87 pendingCharacters.append("<?" + target + " " + data + "?>"); 88 } else { 89 super.processingInstruction(target, data); 90 } 91 } 92 93 96 97 public void comment (char ch[], int start, int length) throws TransformerException { 98 if (!committed) { 99 if (pendingCharacters==null) { 100 pendingCharacters=new StringBuffer (); 101 } 102 pendingCharacters.append("<!--" + new String (ch, start, length) + "-->"); 103 } else { 104 super.comment(ch, start, length); 105 } 106 } 107 108 114 115 public void startElement(int nameCode, Attributes attributes, 116 int[] namespaces, int nscount) throws TransformerException { 117 if (!committed) { 118 String name = namePool.getLocalName(nameCode); 119 short uriCode = namePool.getURICode(nameCode); 120 if (name.equalsIgnoreCase("html") && uriCode==Namespace.NULL_CODE) { 121 switchToHTML(); 122 } else { 123 switchToXML(); 124 } 125 } 126 super.startElement(nameCode, attributes, namespaces, nscount); 127 } 128 129 132 133 private void switchToXML() throws TransformerException { 134 Emitter e = new XMLEmitter(); 135 String indent = outputProperties.getProperty(OutputKeys.INDENT); 136 if (indent!=null && indent.equals("yes")) { 137 XMLIndenter in = new XMLIndenter(); 138 in.setUnderlyingEmitter(e); 139 e = in; 140 } 141 String cdata = outputProperties.getProperty(OutputKeys.CDATA_SECTION_ELEMENTS); 142 if (cdata!=null && cdata.length()>0) { 143 CDATAFilter filter = new CDATAFilter(); 144 filter.setUnderlyingEmitter(e); 145 e=filter; 146 } 147 switchTo(e); 148 } 149 150 153 154 private void switchToHTML() throws TransformerException { 155 Emitter e = new HTMLEmitter(); 156 String indent = outputProperties.getProperty(OutputKeys.INDENT); 157 if (indent==null || indent.equals("yes")) { 158 HTMLIndenter in = new HTMLIndenter(); 159 in.setUnderlyingEmitter(e); 160 e = in; 161 } 162 switchTo(e); 163 } 164 165 170 171 public void setEscaping(boolean escaping) throws TransformerException { 172 if (!committed) { 173 initialEscaping = escaping; 174 } 175 super.setEscaping(escaping); 176 } 177 178 181 182 private void switchTo(Emitter emitter) throws TransformerException { 183 setUnderlyingEmitter(emitter); 184 committed = true; 185 emitter.setWriter(writer); 186 emitter.setOutputProperties(outputProperties); 187 emitter.startDocument(); 188 if (pendingCharacters!=null) { 189 emitter.setEscaping(false); 190 int len = pendingCharacters.length(); 191 char[] chars = new char[len]; 192 pendingCharacters.getChars(0, len, chars, 0); 193 emitter.characters(chars, 0, len); 194 } 195 emitter.setEscaping(initialEscaping); 196 } 197 198 } 199 200 | Popular Tags |