1 16 package org.apache.cocoon.xml; 17 18 import java.util.Enumeration ; 19 import java.util.Hashtable ; 20 import java.util.Vector ; 21 22 import org.xml.sax.AttributeList ; 23 import org.xml.sax.ContentHandler ; 24 import org.xml.sax.DocumentHandler ; 25 import org.xml.sax.Locator ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.helpers.AttributesImpl ; 28 29 42 public class DocumentHandlerAdapter extends AbstractXMLProducer 43 implements DocumentHandler { 44 45 46 private Hashtable stackedNS=new Hashtable (); 47 48 private NamespacesTable namespaces=new NamespacesTable(); 49 50 private int stack=0; 51 52 55 public DocumentHandlerAdapter() { 56 super(); 57 } 58 59 62 public DocumentHandlerAdapter(XMLConsumer consumer) { 63 this(); 64 super.setConsumer(consumer); 65 } 66 67 70 public DocumentHandlerAdapter(ContentHandler content) { 71 this(); 72 super.setContentHandler(content); 73 } 74 75 78 public void setDocumentLocator (Locator locator) { 79 if (super.contentHandler==null) return; 80 else super.contentHandler.setDocumentLocator(locator); 81 } 82 83 86 public void startDocument () 87 throws SAXException { 88 if (super.contentHandler==null) 89 throw new SAXException ("ContentHandler not set"); 90 super.contentHandler.startDocument(); 91 } 92 93 96 public void endDocument () 97 throws SAXException { 98 if (super.contentHandler==null) 99 throw new SAXException ("ContentHandler not set"); 100 super.contentHandler.endDocument(); 101 } 102 103 106 public void startElement (String name, AttributeList a) 107 throws SAXException { 108 if (super.contentHandler==null) 109 throw new SAXException ("ContentHandler not set"); 110 AttributesImpl a2=new AttributesImpl(); 113 Vector nslist=new Vector (); 114 for (int x=0; x<a.getLength(); x++) { 115 String att=a.getName(x); 116 String uri=a.getValue(x); 117 if (att.equals("xmlns") || att.startsWith("xmlns:")) { 118 String pre=""; 119 if (att.length()>5) pre=att.substring(6); 120 this.namespaces.addDeclaration(pre,uri); 121 nslist.addElement(pre); 122 super.contentHandler.startPrefixMapping(pre,uri); 123 } 124 } 125 if (nslist.size()>0) this.stackedNS.put(new Integer (this.stack),nslist); 126 NamespacesTable.Name w=this.namespaces.resolve(null,name,null,null); 128 for (int x=0; x<a.getLength(); x++) { 130 String att=a.getName(x); 131 if (att.equals("xmlns") || att.startsWith("xmlns:")) continue; 132 NamespacesTable.Name k=this.namespaces.resolve(null,att,null,null); 134 String val=a.getValue(x); 135 String typ=a.getType(x); 136 String uri=k.getPrefix().length()==0?"":k.getUri(); 137 a2.addAttribute(uri,k.getLocalName(),k.getQName(),typ,val); 138 } 139 super.contentHandler.startElement(w.getUri(),w.getLocalName(), 141 w.getQName(),a2); 142 this.stack++; 144 } 145 146 149 public void endElement (String name) 150 throws SAXException { 151 if (super.contentHandler==null) 152 throw new SAXException ("ContentHandler not set"); 153 this.stack--; 155 NamespacesTable.Name w=this.namespaces.resolve(null,name,null,null); 157 super.contentHandler.endElement(w.getUri(),w.getLocalName(), 158 w.getQName()); 159 Vector nslist=(Vector )this.stackedNS.remove(new Integer (this.stack)); 161 if (nslist==null) return; 162 if (nslist.size()==0) return; 163 Enumeration e=nslist.elements(); 164 while (e.hasMoreElements()) { 165 String prefix=(String )e.nextElement(); 166 NamespacesTable.Declaration d=namespaces.removeDeclaration(prefix); 167 super.contentHandler.endPrefixMapping(d.getPrefix()); 168 } 169 } 170 171 172 175 public void characters (char ch[], int start, int len) 176 throws SAXException { 177 if (super.contentHandler==null) 178 throw new SAXException ("ContentHandler not set"); 179 super.contentHandler.characters(ch,start,len); 180 } 181 182 183 186 public void ignorableWhitespace (char ch[], int start, int len) 187 throws SAXException { 188 if (super.contentHandler==null) 189 throw new SAXException ("ContentHandler not set"); 190 super.contentHandler.ignorableWhitespace(ch,start,len); 191 } 192 193 194 197 public void processingInstruction (String target, String data) 198 throws SAXException { 199 if (super.contentHandler==null) 200 throw new SAXException ("ContentHandler not set"); 201 super.contentHandler.processingInstruction(target,data); 202 } 203 } 204 | Popular Tags |