KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > RedundantNamespacesFilter


1 /*
2  * Copyright 1999-2005 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.xml;
17
18 import org.xml.sax.Attributes JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20
21 /**
22  * A SAX filter that strips out redundant namespace declarations.
23  *
24  * <p>
25  * It handles both duplicate declarations (i.e. a namespace already declared by a
26  * parent element) and empty namespaces scopes (i.e. start/stopPrefixMapping with
27  * no element inbetween) that can be produced by some components (e.g. JXTG or
28  * BrowserUpdateTransformer). Such empty scopes confuse the Xalan serializer which
29  * then produces weird namespace declarations (<code>xmlns:%@$#^@#="%@$#^@#"</code>).
30  *
31  * <p>
32  * This is a the most simple use of {@link NamespacesTable}.
33  *
34  * @version CVS $Id: RedundantNamespacesFilter.java 231482 2005-08-11 17:04:13Z sylvain $
35  */

36 public class RedundantNamespacesFilter extends AbstractXMLPipe {
37
38     /** Layered storage for all namespace declarations */
39     private NamespacesTable ns = new NamespacesTable();
40
41     /**
42      * No-arg constructor. Requires an explicit call to
43      * <code>setConsumer()</code>.
44      */

45     public RedundantNamespacesFilter() {
46         // Nothing
47
}
48
49     /**
50      * Creates a filter directly linked to its consumer
51      *
52      * @param consumer
53      * the SAX stream consumer
54      */

55     public RedundantNamespacesFilter(XMLConsumer consumer) {
56         setConsumer(consumer);
57     }
58
59     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
60         // Just declare it: duplicate declarations are ignorede by NamespacesTable
61
ns.addDeclaration(prefix, uri);
62     }
63
64     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a) throws SAXException JavaDoc {
65         // Declare namespaces for this scope, if any
66
ns.enterScope(this.contentHandler);
67         super.startElement(uri, loc, raw, a);
68     }
69
70     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
71         super.endElement(uri, loc, raw);
72         ns.leaveScope(this.contentHandler);
73     }
74
75     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
76         ns.removeDeclaration(prefix);
77     }
78 }
79
Popular Tags