1 16 package org.apache.cocoon.xml; 17 18 import org.apache.avalon.framework.logger.LogEnabled; 19 import org.apache.avalon.framework.logger.Logger; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.DocumentHandler ; 23 import org.xml.sax.Locator ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.helpers.AttributeListImpl ; 26 27 import java.util.Vector ; 28 29 43 public class DocumentHandlerWrapper extends AbstractXMLConsumer implements LogEnabled { 44 45 protected Logger log; 46 47 48 private NamespacesTable namespaces=new NamespacesTable(); 49 50 private Vector undecl=new Vector (); 51 52 53 protected DocumentHandler documentHandler=null; 54 55 58 public DocumentHandlerWrapper() { 59 super(); 60 } 61 62 65 public DocumentHandlerWrapper(DocumentHandler document) { 66 this(); 67 this.setDocumentHandler(document); 68 } 69 70 75 public void enableLogging(Logger logger) { 76 if (this.log == null) { 77 this.log = logger; 78 } 79 } 80 81 84 public void recycle() { 85 this.documentHandler = null; 86 } 87 88 94 public void setDocumentHandler(DocumentHandler document) 95 throws IllegalStateException { 96 if (this.documentHandler!=null) throw new IllegalStateException (); 97 this.documentHandler=document; 98 } 99 100 103 public void setDocumentLocator (Locator locator) { 104 if (this.documentHandler==null) return; 105 else this.documentHandler.setDocumentLocator(locator); 106 } 107 108 111 public void startDocument () 112 throws SAXException { 113 if (this.documentHandler==null) 114 throw new SAXException ("DocumentHandler not set"); 115 this.documentHandler.startDocument(); 116 } 117 118 121 public void endDocument () 122 throws SAXException { 123 if (this.documentHandler==null) 124 throw new SAXException ("DocumentHandler not set"); 125 this.documentHandler.endDocument(); 126 } 127 128 131 public void startPrefixMapping(String prefix, String uri) 132 throws SAXException { 133 this.undecl.addElement(this.namespaces.addDeclaration(prefix,uri)); 134 } 135 136 139 public void endPrefixMapping(String prefix) 140 throws SAXException { 141 if (namespaces.removeDeclaration(prefix)==null) 142 throw new SAXException ("Namespace prefix \""+prefix+ 143 "\" never declared"); 144 } 145 146 149 public void startElement(String uri, String loc, String raw, Attributes a) 150 throws SAXException { 151 if (this.documentHandler==null) 152 throw new SAXException ("DocumentHandler not set"); 153 NamespacesTable.Name name=this.namespaces.resolve(uri,raw,null,loc); 154 AttributeListImpl a2=new AttributeListImpl (); 156 if (this.undecl.size()>0) { 158 for (int x=0; x<this.undecl.size(); x++) { 159 NamespacesTable.Declaration dec=null; 160 dec=(NamespacesTable.Declaration)this.undecl.elementAt(x); 161 String aname="xmlns"; 162 if (dec.getPrefix().length()>0) aname="xmlns:"+dec.getPrefix(); 163 a2.addAttribute(aname,"CDATA",dec.getUri()); 164 } 165 this.undecl.clear(); 166 } 167 for (int x=0; x<a.getLength(); x++) { 169 NamespacesTable.Name aname=namespaces.resolve(a.getURI(x), 170 a.getQName(x), 171 null, 172 a.getLocalName(x)); 173 a2.addAttribute(aname.getQName(),a.getType(x),a.getValue(x)); 174 } 175 this.documentHandler.startElement(name.getQName(),a2); 177 } 178 179 180 183 public void endElement(String uri, String loc, String raw) 184 throws SAXException { 185 if (this.documentHandler==null) 186 throw new SAXException ("DocumentHandler not set"); 187 NamespacesTable.Name name=this.namespaces.resolve(uri,raw,null,loc); 188 this.documentHandler.endElement(name.getQName()); 189 } 190 191 194 public void characters(char ch[], int start, int len) 195 throws SAXException { 196 if (this.documentHandler==null) 197 throw new SAXException ("DocumentHandler not set"); 198 this.documentHandler.characters(ch,start,len); 199 } 200 201 204 public void ignorableWhitespace(char ch[], int start, int len) 205 throws SAXException { 206 if (this.documentHandler==null) 207 throw new SAXException ("DocumentHandler not set"); 208 this.documentHandler.ignorableWhitespace(ch,start,len); 209 } 210 211 214 public void processingInstruction(String target, String data) 215 throws SAXException { 216 if (this.documentHandler==null) 217 throw new SAXException ("DocumentHandler not set"); 218 this.documentHandler.processingInstruction(target,data); 219 } 220 } 221 | Popular Tags |