KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > util > NSPrefixDecorator


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.util;
24
25 import java.util.Iterator;
26
27 import org.xml.sax.*;
28 import org.xml.sax.helpers.AttributesImpl;
29
30 /**
31  * A ContentHandler and LexicalHandler implementation used to perform qualified
32  * SAX names (rawnames) reconstruction from prefix mapping events, intended
33  * repository only support the default values. It also performs xmlns attributes
34  * reconstruction.
35  */

36 public class NSPrefixDecorator extends NamespaceContextHandler implements SAXConstants
37 {
38     private static final String RCSRevision = "$Revision: 1.1 $";
39     private static final String RCSName = "$Name: $";
40     
41     private boolean enabled = false;
42     private AttributesImpl attributes = null;
43     
44     public final static String NS_PREFIX = "xmlns";
45     public final static String XML_PREFIX = "xml";
46     
47     public NSPrefixDecorator()
48     {}
49     
50     private String getQualifiedName(String uri, String localName, String qName) throws SAXException
51     {
52         if (localName == null || localName.length() == 0)
53             return qName;
54         String prefix = getPrefix(uri); // ATTENTION : No guaranty it is the last prefix.
55
if (prefix == null || prefix.length() == 0)
56             return localName;
57         else
58             return prefix + ":" + localName;
59     }
60     
61     public final void startElement(String namespaceURI, String localName, String qName, Attributes atts)
62     throws SAXException
63     {
64         if (enabled)
65         {
66             attributes.clear();
67             
68             /* add xmlns attributes */
69             Iterator enum = getDeclaredPrefixes().iterator();
70             String uri;
71             String prefix;
72             while (enum.hasNext())
73             {
74                 prefix = (String)enum.next();
75                 if (!prefix.equals(XML_PREFIX))
76                 {
77                     uri = getNamespaceURI(prefix);
78                     attributes.addAttribute(XMLNS_URI, prefix, prefix.length() == 0 ? NS_PREFIX : NS_PREFIX+":"+prefix, "CDATA", uri);
79                 }
80             }
81             
82             int len = atts.getLength();
83             for (int i=0; i<len; i++)
84             {
85                 attributes.addAttribute(
86                 atts.getURI(i),
87                 atts.getLocalName(i),
88                 getQualifiedName(atts.getURI(i), atts.getLocalName(i), atts.getQName(i)), // browsing must be performed for this reason
89
atts.getType(i),
90                 atts.getValue(i));
91             }
92             
93             super.startElement(namespaceURI, localName, getQualifiedName(namespaceURI, localName, qName), attributes);
94         }
95         else
96             super.startElement(namespaceURI, localName, qName, atts);
97     }
98     
99     public final void endElement(String namespaceURI, String localName, String qName)
100     throws SAXException
101     {
102         if (enabled)
103             super.endElement(namespaceURI, localName, getQualifiedName(namespaceURI, localName, qName));
104         else
105             super.endElement(namespaceURI, localName, qName);
106     }
107     /**
108      * True if raw names reconstruction is enabled.
109      * @return
110      */

111     public boolean isEnabled() {
112         return enabled;
113     }
114
115     /**
116      * Enables raw names reconstruction.
117      * @param enabled
118      */

119     public void setEnabled(boolean enabled) {
120         if (enabled && attributes == null)
121             attributes = new AttributesImpl();
122         this.enabled = enabled;
123     }
124
125 }
126
Popular Tags