1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/internalizer/ContentHandlerNamespacePrefixAdapter.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,115 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.xjc.reader.internalizer; 1.30 + 1.31 +import javax.xml.XMLConstants; 1.32 + 1.33 +import org.xml.sax.Attributes; 1.34 +import org.xml.sax.SAXException; 1.35 +import org.xml.sax.SAXNotRecognizedException; 1.36 +import org.xml.sax.SAXNotSupportedException; 1.37 +import org.xml.sax.XMLReader; 1.38 +import org.xml.sax.helpers.AttributesImpl; 1.39 +import org.xml.sax.helpers.XMLFilterImpl; 1.40 + 1.41 +/** 1.42 + * {@link XMLReader} filter for supporting 1.43 + * <tt>http://xml.org/sax/features/namespace-prefixes</tt> feature. 1.44 + * 1.45 + * @author Kohsuke Kawaguchi 1.46 + */ 1.47 +final class ContentHandlerNamespacePrefixAdapter extends XMLFilterImpl { 1.48 + /** 1.49 + * True if <tt>http://xml.org/sax/features/namespace-prefixes</tt> is set to true. 1.50 + */ 1.51 + private boolean namespacePrefixes = false; 1.52 + 1.53 + private String[] nsBinding = new String[8]; 1.54 + private int len; 1.55 + 1.56 + public ContentHandlerNamespacePrefixAdapter() { 1.57 + } 1.58 + 1.59 + public ContentHandlerNamespacePrefixAdapter(XMLReader parent) { 1.60 + setParent(parent); 1.61 + } 1.62 + 1.63 + @Override 1.64 + public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { 1.65 + if(name.equals(PREFIX_FEATURE)) 1.66 + return namespacePrefixes; 1.67 + return super.getFeature(name); 1.68 + } 1.69 + 1.70 + @Override 1.71 + public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { 1.72 + if(name.equals(PREFIX_FEATURE)) { 1.73 + this.namespacePrefixes = value; 1.74 + return; 1.75 + } 1.76 + if(name.equals(NAMESPACE_FEATURE) && value) 1.77 + return; 1.78 + super.setFeature(name, value); 1.79 + } 1.80 + 1.81 + 1.82 + @Override 1.83 + public void startPrefixMapping(String prefix, String uri) throws SAXException { 1.84 + if (XMLConstants.XML_NS_URI.equals(uri)) return; //xml prefix shall not be declared based on jdk api javadoc 1.85 + if(len==nsBinding.length) { 1.86 + // reallocate 1.87 + String[] buf = new String[nsBinding.length*2]; 1.88 + System.arraycopy(nsBinding,0,buf,0,nsBinding.length); 1.89 + nsBinding = buf; 1.90 + } 1.91 + nsBinding[len++] = prefix; 1.92 + nsBinding[len++] = uri; 1.93 + super.startPrefixMapping(prefix,uri); 1.94 + } 1.95 + 1.96 + @Override 1.97 + public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 1.98 + if(namespacePrefixes) { 1.99 + this.atts.setAttributes(atts); 1.100 + // add namespace bindings back as attributes 1.101 + for( int i=0; i<len; i+=2 ) { 1.102 + String prefix = nsBinding[i]; 1.103 + if(prefix.length()==0) 1.104 + this.atts.addAttribute(XMLConstants.XML_NS_URI,"xmlns","xmlns","CDATA",nsBinding[i+1]); 1.105 + else 1.106 + this.atts.addAttribute(XMLConstants.XML_NS_URI,prefix,"xmlns:"+prefix,"CDATA",nsBinding[i+1]); 1.107 + } 1.108 + atts = this.atts; 1.109 + } 1.110 + len=0; 1.111 + super.startElement(uri, localName, qName, atts); 1.112 + } 1.113 + 1.114 + private final AttributesImpl atts = new AttributesImpl(); 1.115 + 1.116 + private static final String PREFIX_FEATURE = "http://xml.org/sax/features/namespace-prefixes"; 1.117 + private static final String NAMESPACE_FEATURE = "http://xml.org/sax/features/namespaces"; 1.118 +}