KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > portal > portlets > XMLPortlet


1 /*
2  * Copyright 2000-2001,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
17 package org.apache.jetspeed.portal.portlets;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21
22 import org.apache.ecs.html.Comment;
23 import org.apache.ecs.ConcreteElement;
24 import org.apache.ecs.StringElement;
25 import org.apache.ecs.ElementContainer;
26
27 import org.apache.jetspeed.cache.disk.JetspeedDiskCache;
28 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
29 import org.apache.jetspeed.services.logging.JetspeedLogger;
30 import org.apache.jetspeed.util.SAXPIFilter;
31
32 import org.apache.turbine.util.RunData;
33
34
35 /**
36 Portlet to output an XML well-formed document without any rendering.
37 Strips all PIs and XML declaration before outputting content to allow
38 for inclusion in a wider XML document.
39
40 @author <A HREF="mailto:raphael@apache.org">RaphaŽl Luta</A>
41 @version $Id: XMLPortlet.java,v 1.21 2004/02/23 04:03:34 jford Exp $
42 */

43 public class XMLPortlet extends AbstractPortlet
44 {
45     /**
46      * Static initialization of the logger for this class
47      */

48     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(XMLPortlet.class.getName());
49     
50     /**
51     Return the PI-stripped XML document wrapped in an ECS ElementContainer
52     */

53     public ConcreteElement getContent( RunData rundata ) {
54
55         String JavaDoc myContent = null;
56
57         ElementContainer content = new ElementContainer();
58         
59         // Strip all PIs and XML declaration from the input XML content URL
60
try {
61
62             ByteArrayOutputStream JavaDoc bos= new ByteArrayOutputStream JavaDoc();
63
64             String JavaDoc url = JetspeedDiskCache.getInstance().getEntry( getPortletConfig().getURL() ).getURL();
65
66             new SAXPIFilter(new PrintWriter JavaDoc(bos),true).print( url );
67             myContent = bos.toString();
68
69         } catch (Exception JavaDoc e) {
70
71             
72             logger.error( "Could not parse the following URL: " + this.getPortletConfig().getURL(), e );
73             return content;
74         }
75
76         // If parsing is OK, wraps content in ECS element
77
if (myContent!=null) {
78             content = new ElementContainer();
79
80             content.addElement( new Comment( "BEGIN PORTLET" ) );
81             content.addElement( new StringElement( myContent ) );
82             content.addElement( new Comment( "END PORTLET" ) );
83         }
84
85         return content;
86     }
87
88     /**
89     Return the PI-stripped XML document wrapped in an ECS ElementContainer.
90     Currently same as getContent()
91     */

92     public ConcreteElement getContent(Object JavaDoc params, RunData rundata) {
93         return getContent( rundata );
94     }
95
96
97 }
98
Popular Tags