1 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 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); 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 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)), 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 111 public boolean isEnabled() { 112 return enabled; 113 } 114 115 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 |