1 17 package org.apache.excalibur.xmlizer; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.apache.avalon.framework.component.Component; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.logger.AbstractLogEnabled; 29 import org.apache.avalon.framework.service.ServiceException; 30 import org.apache.avalon.framework.service.ServiceManager; 31 import org.apache.avalon.framework.service.Serviceable; 32 import org.apache.avalon.framework.thread.ThreadSafe; 33 import org.apache.excalibur.xml.sax.SAXParser; 34 import org.xml.sax.ContentHandler ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 38 48 public final class DefaultXMLizer extends AbstractLogEnabled 49 implements XMLizer, Serviceable, Configurable, ThreadSafe, Component 50 { 51 private ServiceManager m_serviceManager; 52 private Map m_mimeTypes = new HashMap (); 53 54 public void service( ServiceManager serviceManager ) throws ServiceException 55 { 56 m_serviceManager = serviceManager; 57 } 58 59 public void configure( Configuration configuration ) throws ConfigurationException 60 { 61 final Configuration[] parsers = configuration.getChildren("parser"); 62 for ( int i = 0; i < parsers.length; i++ ) 63 { 64 final Configuration parser = parsers[i]; 65 final String mimeType = parser.getAttribute("mime-type"); 66 final String role = parser.getAttribute("role"); 67 m_mimeTypes.put(mimeType, role); 68 if ( getLogger().isDebugEnabled() ) 69 { 70 getLogger().debug("XMLizer: Registering parser '"+role+"' for mime-type '"+mimeType+"'."); 71 } 72 } 73 if ( getLogger().isDebugEnabled() ) 74 { 75 getLogger().debug("XMLizer: Default parser is '"+SAXParser.ROLE+"'."); 76 } 77 } 78 79 85 public void toSAX( final InputStream stream, 86 final String mimeType, 87 final String systemID, 88 final ContentHandler handler ) 89 throws SAXException , IOException 90 { 91 if ( null == stream ) 92 { 93 throw new NullPointerException ( "stream" ); 94 } 95 if ( null == handler ) 96 { 97 throw new NullPointerException ( "handler" ); 98 } 99 100 final String parserRole; 101 if ( m_mimeTypes.containsKey(mimeType) ) 102 { 103 parserRole = (String ) m_mimeTypes.get(mimeType); 104 } 105 else 106 { 107 if ( getLogger().isDebugEnabled() ) 108 { 109 final String message = "No mime-type for xmlizing " + systemID + 110 ", guessing text/xml"; 111 getLogger().debug( message ); 112 } 113 parserRole = SAXParser.ROLE; 114 } 115 116 SAXParser parser = null; 117 try 118 { 119 parser = (SAXParser) m_serviceManager.lookup( parserRole ); 120 121 final InputSource inputSource = new InputSource ( stream ); 122 inputSource.setSystemId( systemID ); 123 parser.parse( inputSource, handler, null ); 124 } 125 catch ( ServiceException e ) 126 { 127 throw new SAXException ( "Cannot parse content of type " + mimeType, e ); 128 } 129 finally 130 { 131 m_serviceManager.release(parser); 132 } 133 } 134 } 135 136 | Popular Tags |