1 17 package org.apache.excalibur.xml.sax; 18 19 import java.util.Enumeration ; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.ContentHandler ; 23 import org.xml.sax.DocumentHandler ; 24 import org.xml.sax.Locator ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.AttributeListImpl ; 27 import org.xml.sax.helpers.NamespaceSupport ; 28 29 36 public class DocumentHandlerAdapter 37 implements ContentHandler 38 { 39 private final static String XMLNS = "xmlns"; 40 private final static String XMLNS_PREFIX = "xmlns:"; 41 private final static String CDATA = "CDATA"; 42 private final DocumentHandler m_documentHandler; 43 private final NamespaceSupport m_support = new NamespaceSupport (); 44 private boolean m_contextPushed = false; 45 46 49 public DocumentHandlerAdapter( final DocumentHandler documentHandler ) 50 { 51 m_documentHandler = documentHandler; 52 } 53 54 57 public void setDocumentLocator( final Locator locator ) 58 { 59 m_documentHandler.setDocumentLocator( locator ); 60 } 61 62 65 public void startDocument() throws SAXException 66 { 67 m_documentHandler.startDocument(); 68 } 69 70 73 public void endDocument() 74 throws SAXException 75 { 76 m_documentHandler.endDocument(); 77 } 78 79 82 public void startPrefixMapping( final String prefix, final String uri ) throws SAXException 83 { 84 if( !m_contextPushed ) 85 { 86 m_support.pushContext(); 87 m_contextPushed = true; 88 } 89 90 m_support.declarePrefix( prefix, uri ); 91 } 92 93 96 public void endPrefixMapping( final String prefix ) throws SAXException 97 { 98 } 100 101 104 public void startElement( final String uri, 105 final String loc, 106 final String raw, 107 final Attributes a ) throws SAXException 108 { 109 if( !m_contextPushed ) 110 { 111 m_support.pushContext(); 112 } 113 m_contextPushed = false; 114 115 final String name = getTagName( loc, raw, uri ); 116 117 final AttributeListImpl attributeList = new AttributeListImpl (); 118 for( int i = 0; i < a.getLength(); i++ ) 119 { 120 String attributeName = a.getQName( i ); 121 if( ( attributeName == null ) || ( attributeName.length() == 0 ) ) 122 { 123 final String attributeNamespaceURI = a.getURI( i ); 124 final String attributeLocalName = a.getLocalName( i ); 125 if( attributeNamespaceURI.length() == 0 ) 126 { 127 attributeName = attributeLocalName; 128 } 129 else 130 { 131 final String prefix = m_support.getPrefix( attributeNamespaceURI ); 132 if( prefix == null ) 133 { 134 throw new SAXException ( "No attribute prefix for namespace URI: " + attributeNamespaceURI ); 135 } 136 attributeName = prefix + ':' + attributeLocalName; 137 } 138 } 139 attributeList.addAttribute( attributeName, a.getType( i ), a.getValue( i ) ); 140 } 141 142 final Enumeration e = m_support.getDeclaredPrefixes(); 143 while( e.hasMoreElements() ) 144 { 145 final String prefix = (String )e.nextElement(); 146 if( prefix.length() == 0 ) 147 { 148 attributeList.addAttribute( XMLNS, CDATA, uri ); 149 } 150 else 151 { 152 attributeList.addAttribute( XMLNS_PREFIX + prefix, CDATA, uri ); 153 } 154 } 155 156 m_documentHandler.startElement( name, attributeList ); 157 } 158 159 162 public void endElement( final String uri, 163 final String loc, 164 final String raw ) throws SAXException 165 { 166 final String name = getTagName( loc, raw, uri ); 167 m_documentHandler.endElement( name ); 168 m_support.popContext(); 169 } 170 171 174 public void characters( final char[] ch, 175 final int start, 176 final int len ) throws SAXException 177 { 178 m_documentHandler.characters( ch, start, len ); 179 } 180 181 184 public void ignorableWhitespace( final char[] ch, 185 final int start, 186 final int len ) throws SAXException 187 { 188 m_documentHandler.ignorableWhitespace( ch, start, len ); 189 } 190 191 194 public void processingInstruction( final String target, 195 final String data ) throws SAXException 196 { 197 m_documentHandler.processingInstruction( target, data ); 198 } 199 200 206 public void skippedEntity( final String name ) throws SAXException 207 { 208 } 210 211 private String getTagName( final String loc, final String raw, final String uri ) throws SAXException 212 { 213 if( raw != null && raw.length() > 0 ) 214 { 215 return raw; 216 } 217 else 218 { 219 final String prefix = getTagPrefix( uri ); 220 return ( ( prefix.length() == 0 ) ? "" : ( prefix + ':' ) ) + loc; 221 } 222 } 223 224 private String getTagPrefix( final String uri ) throws SAXException 225 { 226 if( m_support.getPrefix( uri ) == null ) 227 { 228 if( ( uri == null ) || ( uri.length() < 1 ) ) 229 { 230 return ""; 231 } 232 else 233 { 234 final String defaultURI = m_support.getURI( "" ); 235 if( ( defaultURI != null ) && defaultURI.equals( uri ) ) 236 { 237 return ""; } 239 else 240 { 241 throw new SAXException ( "No element prefix for namespace URI: " + uri ); 242 } 243 } 244 } 245 else 246 { 247 return m_support.getPrefix( uri ); 248 } 249 } 250 } 251 | Popular Tags |