|
1 /* |
|
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * 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 @Override |
|
61 public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { |
|
62 if(name.equals(PREFIX_FEATURE)) |
|
63 return namespacePrefixes; |
|
64 return super.getFeature(name); |
|
65 } |
|
66 |
|
67 @Override |
|
68 public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { |
|
69 if(name.equals(PREFIX_FEATURE)) { |
|
70 this.namespacePrefixes = value; |
|
71 return; |
|
72 } |
|
73 if(name.equals(NAMESPACE_FEATURE) && value) |
|
74 return; |
|
75 super.setFeature(name, value); |
|
76 } |
|
77 |
|
78 |
|
79 @Override |
|
80 public void startPrefixMapping(String prefix, String uri) throws SAXException { |
|
81 if (XMLConstants.XML_NS_URI.equals(uri)) return; //xml prefix shall not be declared based on jdk api javadoc |
|
82 if(len==nsBinding.length) { |
|
83 // reallocate |
|
84 String[] buf = new String[nsBinding.length*2]; |
|
85 System.arraycopy(nsBinding,0,buf,0,nsBinding.length); |
|
86 nsBinding = buf; |
|
87 } |
|
88 nsBinding[len++] = prefix; |
|
89 nsBinding[len++] = uri; |
|
90 super.startPrefixMapping(prefix,uri); |
|
91 } |
|
92 |
|
93 @Override |
|
94 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
|
95 if(namespacePrefixes) { |
|
96 this.atts.setAttributes(atts); |
|
97 // add namespace bindings back as attributes |
|
98 for( int i=0; i<len; i+=2 ) { |
|
99 String prefix = nsBinding[i]; |
|
100 if(prefix.length()==0) |
|
101 this.atts.addAttribute(XMLConstants.XML_NS_URI,"xmlns","xmlns","CDATA",nsBinding[i+1]); |
|
102 else |
|
103 this.atts.addAttribute(XMLConstants.XML_NS_URI,prefix,"xmlns:"+prefix,"CDATA",nsBinding[i+1]); |
|
104 } |
|
105 atts = this.atts; |
|
106 } |
|
107 len=0; |
|
108 super.startElement(uri, localName, qName, atts); |
|
109 } |
|
110 |
|
111 private final AttributesImpl atts = new AttributesImpl(); |
|
112 |
|
113 private static final String PREFIX_FEATURE = "http://xml.org/sax/features/namespace-prefixes"; |
|
114 private static final String NAMESPACE_FEATURE = "http://xml.org/sax/features/namespaces"; |
|
115 } |