KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xmlizer > DefaultXMLizer


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xmlizer;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
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 JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  * Converter for transforming any input stream with a given mime-type
40  * into SAX events.
41  * This component acts like a selector. All XMLizer can "register"
42  * themselfes for a given mime-type and this component forwards
43  * the transformation to the registered on.
44  *
45  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
46  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:34 $
47  */

48 public final class DefaultXMLizer extends AbstractLogEnabled
49         implements XMLizer, Serviceable, Configurable, ThreadSafe, Component
50 {
51     private ServiceManager m_serviceManager;
52     private Map JavaDoc m_mimeTypes = new HashMap JavaDoc();
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 JavaDoc mimeType = parser.getAttribute("mime-type");
66             final String JavaDoc 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     /**
80      * Generates SAX events from the given input stream
81      * @param stream the data
82      * @param mimeType the mime-type for the data
83      * @param systemID the URI defining the data (this is optional and can be null)
84      */

85     public void toSAX( final InputStream JavaDoc stream,
86                        final String JavaDoc mimeType,
87                        final String JavaDoc systemID,
88                        final ContentHandler JavaDoc handler )
89             throws SAXException JavaDoc, IOException JavaDoc
90     {
91         if ( null == stream )
92         {
93             throw new NullPointerException JavaDoc( "stream" );
94         }
95         if ( null == handler )
96         {
97             throw new NullPointerException JavaDoc( "handler" );
98         }
99
100         final String JavaDoc parserRole;
101         if ( m_mimeTypes.containsKey(mimeType) )
102         {
103             parserRole = (String JavaDoc) m_mimeTypes.get(mimeType);
104         }
105         else
106         {
107             if ( getLogger().isDebugEnabled() )
108             {
109                 final String JavaDoc 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 JavaDoc inputSource = new InputSource JavaDoc( stream );
122             inputSource.setSystemId( systemID );
123             parser.parse( inputSource, handler, null );
124         }
125         catch ( ServiceException e )
126         {
127             throw new SAXException JavaDoc( "Cannot parse content of type " + mimeType, e );
128         }
129         finally
130         {
131             m_serviceManager.release(parser);
132         }
133     }
134 }
135
136
Popular Tags