KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > transformation > RSSTransformer


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

16 package org.apache.cocoon.portal.transformation;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.cocoon.ProcessingException;
27 import org.apache.cocoon.caching.CacheableProcessingComponent;
28 import org.apache.cocoon.components.sax.XMLDeserializer;
29 import org.apache.cocoon.components.sax.XMLSerializer;
30 import org.apache.cocoon.environment.SourceResolver;
31 import org.apache.cocoon.transformation.AbstractSAXTransformer;
32 import org.apache.cocoon.xml.IncludeXMLConsumer;
33 import org.apache.cocoon.xml.XMLConsumer;
34 import org.apache.excalibur.source.SourceValidity;
35 import org.apache.excalibur.source.impl.validity.NOPValidity;
36 import org.apache.excalibur.xmlizer.XMLizer;
37 import org.xml.sax.Attributes JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39
40 /**
41  * This transformer records the content of all description elements
42  * and tries to interpret them as valid XML.
43  * It's actually a quick hack...
44  *
45  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
46  * @version CVS $Id: RSSTransformer.java 124685 2005-01-08 22:20:56Z antonio $
47  */

48 public final class RSSTransformer
49 extends AbstractSAXTransformer
50 implements CacheableProcessingComponent {
51
52     /** The xmlizer for converting html to xml */
53     protected XMLizer xmlizer;
54     
55     /** The xml deserializer */
56     protected XMLDeserializer deserializer;
57     
58     /** The filter */
59     protected XMLConsumer filter;
60     
61     /**
62      * receive notification of start element event.
63      **/

64     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attributes)
65     throws SAXException JavaDoc {
66         super.startElement(uri,name,raw,attributes);
67         if ("description".equals(name)) {
68             this.startTextRecording();
69         }
70     }
71
72     /**
73      * receive notification of end element event.
74      */

75     public void endElement(String JavaDoc uri,String JavaDoc name,String JavaDoc raw)
76     throws SAXException JavaDoc {
77         if ("description".equals(name)) {
78             final String JavaDoc text = this.endTextRecording();
79             final String JavaDoc html = "<html><body>"+text+"</body></html>";
80             
81             Object JavaDoc parsed = null;
82             XMLSerializer serializer = null;
83             try {
84                 serializer = (XMLSerializer)this.manager.lookup(XMLSerializer.ROLE);
85                 InputStream JavaDoc inputStream = new ByteArrayInputStream JavaDoc(html.getBytes());
86                 this.xmlizer.toSAX(inputStream,
87                                     "text/html",
88                                     null,
89                                     serializer);
90                 // if no exception occurs, everything is fine!
91
parsed = serializer.getSAXFragment();
92             } catch (Exception JavaDoc ignore) {
93                 // just ignore all exceptions
94
} finally {
95                 this.manager.release( serializer );
96             }
97             if ( parsed != null ) {
98                 this.deserializer.setConsumer( this.filter );
99                 this.deserializer.deserialize( parsed );
100             } else {
101                 this.sendTextEvent(text);
102             }
103         }
104         super.endElement(uri,name,raw);
105     }
106
107     /* (non-Javadoc)
108      * @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
109      */

110     public void recycle() {
111         this.manager.release( this.xmlizer );
112         this.manager.release( this.deserializer );
113         this.xmlizer = null;
114         this.deserializer = null;
115         this.filter = null;
116         super.recycle();
117     }
118
119     /* (non-Javadoc)
120      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
121      */

122     public void setup(SourceResolver resolver,
123                        Map JavaDoc objectModel,
124                        String JavaDoc src,
125                        Parameters par)
126     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
127         super.setup(resolver, objectModel, src, par);
128         try {
129             this.xmlizer = (XMLizer)this.manager.lookup(XMLizer.ROLE);
130             this.deserializer = (XMLDeserializer)this.manager.lookup(XMLDeserializer.ROLE);
131         } catch (ServiceException ce) {
132             throw new ProcessingException("Unable to lookup component.", ce);
133         }
134     }
135
136    static class HTMLFilter extends IncludeXMLConsumer {
137        
138        int bodyCount = 0;
139        
140        public HTMLFilter(XMLConsumer consumer) {
141            super(consumer);
142        }
143
144        public void startElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qName, Attributes JavaDoc attr) throws SAXException JavaDoc {
145            if (bodyCount > 0 ) {
146                super.startElement(uri, local, qName, attr);
147            }
148            if ("body".equalsIgnoreCase(local)) {
149                bodyCount++;
150            }
151        }
152
153        public void endElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qName) throws SAXException JavaDoc {
154            if ("body".equalsIgnoreCase(local)) {
155                bodyCount--;
156            }
157            if (bodyCount > 0 ) {
158                super.endElement(uri, local, qName );
159            }
160        }
161
162    }
163
164     /* (non-Javadoc)
165      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#setupTransforming()
166      */

167     public void setupTransforming()
168         throws IOException JavaDoc, ProcessingException, SAXException JavaDoc {
169         super.setupTransforming();
170         this.filter = new HTMLFilter( this.xmlConsumer );
171     }
172
173     /* (non-Javadoc)
174      * @see org.apache.cocoon.caching.CacheableProcessingComponent#getKey()
175      */

176     public Serializable JavaDoc getKey() {
177         return "1";
178     }
179
180     /* (non-Javadoc)
181      * @see org.apache.cocoon.caching.CacheableProcessingComponent#getValidity()
182      */

183     public SourceValidity getValidity() {
184         return NOPValidity.SHARED_INSTANCE;
185     }
186
187 }
188
Popular Tags