1 /*
2 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26 package com.sun.tools.internal.xjc.reader.internalizer;
27
28 import javax.xml.XMLConstants;
29
30 import org.xml.sax.Attributes;
31 import org.xml.sax.SAXException;
32 import org.xml.sax.SAXNotRecognizedException;
33 import org.xml.sax.SAXNotSupportedException;
34 import org.xml.sax.XMLReader;
35 import org.xml.sax.helpers.AttributesImpl;
36 import org.xml.sax.helpers.XMLFilterImpl;
37
38 /**
39 * {@link XMLReader} filter for supporting
40 * <tt>http://xml.org/sax/features/namespace-prefixes</tt> feature.
41 *
42 * @author Kohsuke Kawaguchi
43 */
44 final class ContentHandlerNamespacePrefixAdapter extends XMLFilterImpl {
45 /**
46 * True if <tt>http://xml.org/sax/features/namespace-prefixes</tt> is set to true.
47 */
48 private boolean namespacePrefixes = false;
49
50 private String[] nsBinding = new String[8];
51 private int len;
52
53 public ContentHandlerNamespacePrefixAdapter() {
54 }
55
56 public ContentHandlerNamespacePrefixAdapter(XMLReader parent) {
57 setParent(parent);
58 }
59
60 public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
61 if(name.equals(PREFIX_FEATURE))
62 return namespacePrefixes;
63 return super.getFeature(name);
64 }
65
66 public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
67 if(name.equals(PREFIX_FEATURE)) {
68 this.namespacePrefixes = value;
69 return;
70 }
71 if(name.equals(NAMESPACE_FEATURE) && value)
72 return;
73 super.setFeature(name, value);
74 }
75
76
77 public void startPrefixMapping(String prefix, String uri) throws SAXException {
78 if(len==nsBinding.length) {
79 // reallocate
80 String[] buf = new String[nsBinding.length*2];
81 System.arraycopy(nsBinding,0,buf,0,nsBinding.length);
82 nsBinding = buf;
83 }
84 nsBinding[len++] = prefix;
85 nsBinding[len++] = uri;
86 super.startPrefixMapping(prefix,uri);
87 }
88
89 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
90 if(namespacePrefixes) {
91 this.atts.setAttributes(atts);
92 // add namespace bindings back as attributes
93 for( int i=0; i<len; i+=2 ) {
94 String prefix = nsBinding[i];
95 if(prefix.length()==0)
96 this.atts.addAttribute(XMLConstants.XML_NS_URI,"xmlns","xmlns","CDATA",nsBinding[i+1]);
97 else
98 this.atts.addAttribute(XMLConstants.XML_NS_URI,prefix,"xmlns:"+prefix,"CDATA",nsBinding[i+1]);
99 }
100 atts = this.atts;
101 }
102 len=0;
103 super.startElement(uri, localName, qName, atts);
104 }
105
106 private final AttributesImpl atts = new AttributesImpl();
107
108 private static final String PREFIX_FEATURE = "http://xml.org/sax/features/namespace-prefixes";
109 private static final String NAMESPACE_FEATURE = "http://xml.org/sax/features/namespaces";
110 }