1 17 package org.apache.excalibur.xml.sax; 18 19 import java.util.Enumeration ; 20 21 import org.xml.sax.AttributeList ; 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.AttributesImpl ; 27 import org.xml.sax.helpers.NamespaceSupport ; 28 29 36 37 public class ContentHandlerAdapter 38 implements DocumentHandler 39 { 40 private final static String XMLNS = "xmlns"; 41 private final static String XMLNS_PREFIX = "xmlns:"; 42 43 private final ContentHandler m_handler; 44 private final NamespaceSupport m_support = new NamespaceSupport (); 45 46 public ContentHandlerAdapter( final ContentHandler handler ) 47 { 48 m_handler = handler; 49 } 50 51 public void setDocumentLocator( final Locator locator ) 52 { 53 m_handler.setDocumentLocator( locator ); 54 } 55 56 public void startDocument() throws SAXException 57 { 58 m_handler.startDocument(); 59 } 60 61 public void endDocument() throws SAXException 62 { 63 m_handler.endDocument(); 64 } 65 66 public void characters( final char ch[], 67 final int start, 68 final int length ) throws SAXException 69 { 70 m_handler.characters( ch, start, length ); 71 } 72 73 public void ignorableWhitespace( final char ch[], 74 final int start, 75 final int length ) throws SAXException 76 { 77 m_handler.ignorableWhitespace( ch, start, length ); 78 } 79 80 public void processingInstruction( final String target, 81 final String data ) throws SAXException 82 { 83 m_handler.processingInstruction( target, data ); 84 } 85 86 public void startElement( final String name, 87 final AttributeList atts ) throws SAXException 88 { 89 m_support.pushContext(); 90 91 for( int i = 0; i < atts.getLength(); i++ ) 92 { 93 final String attributeName = atts.getName( i ); 94 if( attributeName.startsWith( XMLNS_PREFIX ) ) 95 { 96 m_support.declarePrefix( attributeName.substring( 6 ), atts.getValue( i ) ); 97 } 98 else if( attributeName.equals( XMLNS ) ) 99 { 100 m_support.declarePrefix( "", atts.getValue( i ) ); 101 } 102 } 103 104 final AttributesImpl attributes = new AttributesImpl (); 105 for( int i = 0; i < atts.getLength(); i++ ) 106 { 107 final String attributeName = atts.getName( i ); 108 if( !attributeName.startsWith( XMLNS_PREFIX ) && !attributeName.equals( XMLNS ) ) 109 { 110 final String [] parts = m_support.processName( attributeName, new String [ 3 ], true ); 111 attributes.addAttribute( parts[ 0 ], parts[ 1 ], parts[ 2 ], atts.getType( i ), atts.getValue( i ) ); 112 } 113 } 114 115 final Enumeration e = m_support.getDeclaredPrefixes(); 116 while( e.hasMoreElements() ) 117 { 118 final String prefix = (String )e.nextElement(); 119 m_handler.startPrefixMapping( prefix, m_support.getURI( prefix ) ); 120 } 121 122 final String [] parts = m_support.processName( name, new String [ 3 ], false ); 123 m_handler.startElement( parts[ 0 ], parts[ 1 ], parts[ 2 ], attributes ); 124 } 125 126 public void endElement( final String name ) throws SAXException 127 { 128 final String [] parts = m_support.processName( name, new String [ 3 ], false ); 129 m_handler.endElement( parts[ 0 ], parts[ 1 ], parts[ 2 ] ); 130 131 final Enumeration e = m_support.getDeclaredPrefixes(); 132 while( e.hasMoreElements() ) 133 { 134 final String prefix = (String )e.nextElement(); 135 m_handler.endPrefixMapping( prefix ); 136 } 137 138 m_support.popContext(); 139 } 140 } 141 | Popular Tags |