KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.cocoon.ProcessingException;
24 import org.apache.cocoon.caching.CacheableProcessingComponent;
25 import org.apache.cocoon.environment.SourceResolver;
26 import org.apache.cocoon.transformation.AbstractTransformer;
27 import org.apache.cocoon.xml.XMLUtils;
28 import org.apache.commons.lang.BooleanUtils;
29 import org.apache.excalibur.source.SourceValidity;
30 import org.apache.excalibur.source.impl.validity.NOPValidity;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * This transformer is an utility transformer for dealing with (x)html content.
36  * It has two operating modes:
37  *
38  * Add Mode (default): The transformer simply adds an html and a body element
39  * around the sax stream.
40  *
41  * Remove Mode: The transformer removes all surrounding elements like html and body
42  * and only passes everything on to the next pipeline component that's contained
43  * in a body element.
44  *
45  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
46  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
47  *
48  * @version CVS $Id: HTMLRootTransformer.java 123788 2004-12-31 12:43:00Z antonio $
49  */

50 public class HTMLRootTransformer
51     extends AbstractTransformer
52     implements CacheableProcessingComponent{
53
54     /** the operating mode: true means adding the root elements, false means removing them */
55     protected boolean addMode;
56         
57     /** do we remove the root tag? */
58     protected boolean ignoreRootElement;
59     protected int ignoreRootElementCount;
60
61     protected boolean insideBodyTag;
62     
63     /* (non-Javadoc)
64      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
65      */

66     public void setup(SourceResolver resolver,
67                       Map JavaDoc objectModel,
68                       String JavaDoc src,
69                       Parameters par)
70     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
71         this.addMode = par.getParameterAsBoolean("add-mode", true);
72         this.ignoreRootElement = par.getParameterAsBoolean("ignore-root", false);
73         this.ignoreRootElementCount = 0;
74         this.insideBodyTag = false;
75     }
76
77     /* (non-Javadoc)
78      * @see org.xml.sax.ContentHandler#endDocument()
79      */

80     public void endDocument() throws SAXException JavaDoc {
81         if ( this.addMode ) {
82             XMLUtils.endElement(this.xmlConsumer, "body");
83             XMLUtils.endElement(this.xmlConsumer, "html");
84         }
85         super.endDocument();
86     }
87
88     /* (non-Javadoc)
89      * @see org.xml.sax.ContentHandler#startDocument()
90      */

91     public void startDocument() throws SAXException JavaDoc {
92         super.startDocument();
93         if ( this.addMode ) {
94             XMLUtils.startElement(this.xmlConsumer, "html");
95             XMLUtils.startElement(this.xmlConsumer, "body");
96         }
97     }
98
99     /* (non-Javadoc)
100      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
101      */

102     public void startElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qName, Attributes JavaDoc attr) throws SAXException JavaDoc {
103         if ( !this.ignoreRootElement || this.ignoreRootElementCount > 0) {
104             if ( this.addMode || this.insideBodyTag ) {
105                 this.contentHandler.startElement(uri,local,qName,attr);
106             }
107         }
108         if ( "body".equals(local) ) {
109             this.insideBodyTag = true;
110         }
111         this.ignoreRootElementCount++;
112     }
113
114     /* (non-Javadoc)
115      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
116      */

117     public void endElement(String JavaDoc uri, String JavaDoc local, String JavaDoc qName) throws SAXException JavaDoc {
118         if ( "body".equals(local) ) {
119             this.insideBodyTag = false;
120         }
121         this.ignoreRootElementCount--;
122         if (!this.ignoreRootElement || this.ignoreRootElementCount > 0) {
123             if ( this.addMode || this.insideBodyTag ) {
124                 this.contentHandler.endElement(uri, local, qName);
125             }
126         }
127     }
128     
129     /* (non-Javadoc)
130      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
131      */

132     public void characters(char[] c, int start, int len) throws SAXException JavaDoc {
133         if ( this.addMode || this.insideBodyTag ) {
134             super.characters(c, start, len);
135         }
136     }
137     
138     /* (non-Javadoc)
139      * @see org.apache.cocoon.caching.CacheableProcessingComponent#getKey()
140      */

141     public Serializable JavaDoc getKey() {
142         return new Boolean JavaDoc[] { BooleanUtils.toBooleanObject(this.addMode), BooleanUtils.toBooleanObject(this.ignoreRootElement)};
143     }
144
145     /* (non-Javadoc)
146      * @see org.apache.cocoon.caching.CacheableProcessingComponent#getValidity()
147      */

148     public SourceValidity getValidity() {
149         return NOPValidity.SHARED_INSTANCE;
150     }
151 }
152
Popular Tags