1 16 package org.apache.cocoon.xml; 17 18 import javax.xml.transform.Transformer ; 19 import javax.xml.transform.TransformerFactory ; 20 import javax.xml.transform.TransformerException ; 21 import javax.xml.transform.TransformerConfigurationException ; 22 import javax.xml.transform.dom.DOMSource ; 23 import javax.xml.transform.sax.SAXResult ; 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.ContentHandler ; 26 import org.xml.sax.Locator ; 27 import org.xml.sax.SAXException ; 28 import org.xml.sax.ext.LexicalHandler ; 29 import org.w3c.dom.Node ; 30 31 51 public class IncludeXMLConsumer implements XMLConsumer { 52 53 54 private static final TransformerFactory FACTORY = TransformerFactory.newInstance(); 55 56 private final ContentHandler contentHandler; 57 private final LexicalHandler lexicalHandler; 58 59 private boolean ignoreEmptyCharacters; 60 private boolean ignoreRootElement; 61 private int ignoreRootElementCount; 62 private boolean inDTD; 63 64 67 public IncludeXMLConsumer (XMLConsumer consumer) { 68 this.contentHandler = consumer; 69 this.lexicalHandler = consumer; 70 } 71 72 75 public IncludeXMLConsumer (ContentHandler contentHandler, LexicalHandler lexicalHandler) { 76 this.contentHandler = contentHandler; 77 this.lexicalHandler = lexicalHandler; 78 } 79 80 83 public IncludeXMLConsumer (ContentHandler contentHandler) { 84 this.contentHandler = contentHandler; 85 this.lexicalHandler = contentHandler instanceof LexicalHandler ? (LexicalHandler )contentHandler : null; 86 } 87 88 96 public static void includeNode(Node node, 97 ContentHandler contentHandler, 98 LexicalHandler lexicalHandler) 99 throws SAXException { 100 if (node != null) { 101 if (node.getNodeType() == Node.TEXT_NODE){ 102 String value = node.getNodeValue(); 103 contentHandler.characters(value.toCharArray(), 0, value.length()); 104 } else { 105 try { 106 IncludeXMLConsumer filter = new IncludeXMLConsumer(contentHandler, lexicalHandler); 107 Transformer transformer = FACTORY.newTransformer(); 108 DOMSource source = new DOMSource (node); 109 SAXResult result = new SAXResult (filter); 110 result.setLexicalHandler(filter); 111 transformer.transform(source, result); 112 } catch (TransformerConfigurationException e) { 113 throw new SAXException ("TransformerConfigurationException", e); 114 } catch (TransformerException e) { 115 throw new SAXException ("TransformerException", e); 116 } 117 } 118 } 119 } 120 121 126 public void setIgnoreEmptyCharacters(boolean value) { 127 this.ignoreEmptyCharacters = value; 128 } 129 130 135 public void setIgnoreRootElement(boolean value) { 136 this.ignoreRootElement = value; 137 this.ignoreRootElementCount = 0; 138 } 139 140 144 public void setDocumentLocator(Locator loc) { 145 this.contentHandler.setDocumentLocator(loc); 146 } 147 148 public void startDocument() throws SAXException { 149 } 151 152 public void endDocument() throws SAXException { 153 } 155 156 public void startPrefixMapping(String prefix, String uri) throws SAXException { 157 this.contentHandler.startPrefixMapping(prefix, uri); 158 } 159 160 public void endPrefixMapping(String prefix) throws SAXException { 161 this.contentHandler.endPrefixMapping(prefix); 162 } 163 164 public void startElement(String uri, String local, String qName, Attributes attr) throws SAXException { 165 if (this.ignoreRootElement == false || 166 this.ignoreRootElementCount > 0) { 167 this.contentHandler.startElement(uri,local,qName,attr); 168 } 169 this.ignoreRootElementCount++; 170 } 171 172 public void endElement(String uri, String local, String qName) throws SAXException { 173 this.ignoreRootElementCount--; 174 if (!this.ignoreRootElement || this.ignoreRootElementCount > 0) { 175 this.contentHandler.endElement(uri, local, qName); 176 } 177 } 178 179 public void characters(char[] ch, int start, int end) throws SAXException { 180 if (this.ignoreEmptyCharacters) { 181 String text = new String (ch, start, end).trim(); 182 if (text.length() > 0) { 183 this.contentHandler.characters(text.toCharArray(), 0, text.length()); 184 } 185 } else { 186 this.contentHandler.characters(ch, start, end); 187 } 188 } 189 190 public void ignorableWhitespace(char[] ch, int start, int end) throws SAXException { 191 if (!this.ignoreEmptyCharacters) { 192 this.contentHandler.ignorableWhitespace(ch, start, end); 193 } 194 } 195 196 public void processingInstruction(String name, String value) throws SAXException { 197 this.contentHandler.processingInstruction(name, value); 198 } 199 200 public void skippedEntity(String ent) throws SAXException { 201 this.contentHandler.skippedEntity(ent); 202 } 203 204 208 public void startDTD(String name, String public_id, String system_id) 209 throws SAXException { 210 this.inDTD = true; 212 } 213 214 public void endDTD() throws SAXException { 215 this.inDTD = false; 217 } 218 219 public void startEntity(String name) throws SAXException { 220 if (lexicalHandler != null) { 221 lexicalHandler.startEntity(name); 222 } 223 } 224 225 public void endEntity(String name) throws SAXException { 226 if (lexicalHandler != null) { 227 lexicalHandler.endEntity(name); 228 } 229 } 230 231 public void startCDATA() throws SAXException { 232 if (lexicalHandler != null) { 233 lexicalHandler.startCDATA(); 234 } 235 } 236 237 public void endCDATA() throws SAXException { 238 if (lexicalHandler != null) { 239 lexicalHandler.endCDATA(); 240 } 241 } 242 243 public void comment(char ary[], int start, int length) throws SAXException { 244 if (!inDTD && lexicalHandler != null) { 245 lexicalHandler.comment(ary,start,length); 246 } 247 } 248 } 249 | Popular Tags |