author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 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.xmlschema.bindinfo; |
|
27 |
||
28 |
import com.sun.xml.internal.bind.v2.WellKnownNamespace; |
|
29 |
import java.util.ArrayList; |
|
30 |
||
31 |
import org.xml.sax.Attributes; |
|
32 |
import org.xml.sax.ContentHandler; |
|
33 |
import org.xml.sax.Locator; |
|
34 |
import org.xml.sax.SAXException; |
|
35 |
import org.xml.sax.XMLFilter; |
|
36 |
import org.xml.sax.helpers.XMLFilterImpl; |
|
37 |
||
38 |
/** |
|
39 |
* {@link XMLFilter} that can fork an event to another {@link ContentHandler} |
|
40 |
* in the middle. |
|
41 |
* |
|
42 |
* <p> |
|
43 |
* The side handler receives SAX events before the next handler in the filter chain does. |
|
44 |
* |
|
45 |
* @author Kohsuke Kawaguchi |
|
46 |
*/ |
|
47 |
public class ForkingFilter extends XMLFilterImpl { |
|
48 |
||
49 |
/** |
|
50 |
* Non-null if we are also forking events to this handler. |
|
51 |
*/ |
|
52 |
private ContentHandler side; |
|
53 |
||
54 |
/** |
|
55 |
* The depth of the current element that the {@link #side} handler |
|
56 |
* is seeing. |
|
57 |
*/ |
|
58 |
private int depth; |
|
59 |
||
60 |
/** |
|
61 |
* In-scope namespace mapping. |
|
62 |
* namespaces[2n ] := prefix |
|
63 |
* namespaces[2n+1] := namespace URI |
|
64 |
*/ |
|
65 |
private final ArrayList<String> namespaces = new ArrayList<String>(); |
|
66 |
||
67 |
private Locator loc; |
|
68 |
||
69 |
public ForkingFilter() { |
|
70 |
} |
|
71 |
||
72 |
public ForkingFilter(ContentHandler next) { |
|
73 |
setContentHandler(next); |
|
74 |
} |
|
75 |
||
76 |
public ContentHandler getSideHandler() { |
|
77 |
return side; |
|
78 |
} |
|
79 |
||
80 |
@Override |
|
81 |
public void setDocumentLocator(Locator locator) { |
|
82 |
super.setDocumentLocator(locator); |
|
83 |
this.loc = locator; |
|
84 |
} |
|
85 |
||
86 |
public Locator getDocumentLocator() { |
|
87 |
return loc; |
|
88 |
} |
|
89 |
||
90 |
@Override |
|
91 |
public void startDocument() throws SAXException { |
|
92 |
reset(); |
|
93 |
super.startDocument(); |
|
94 |
} |
|
95 |
||
96 |
private void reset() { |
|
97 |
namespaces.clear(); |
|
98 |
side = null; |
|
99 |
depth = 0; |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public void endDocument() throws SAXException { |
|
104 |
loc = null; |
|
105 |
reset(); |
|
106 |
super.endDocument(); |
|
107 |
} |
|
108 |
||
109 |
@Override |
|
110 |
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
|
111 |
if (WellKnownNamespace.XML_NAMESPACE_URI.equals(uri)) return; //xml prefix shall not be declared based on jdk api javadoc |
|
112 |
if(side!=null) |
|
113 |
side.startPrefixMapping(prefix,uri); |
|
114 |
namespaces.add(prefix); |
|
115 |
namespaces.add(uri); |
|
116 |
super.startPrefixMapping(prefix,uri); |
|
117 |
} |
|
118 |
||
119 |
@Override |
|
120 |
public void endPrefixMapping(String prefix) throws SAXException { |
|
121 |
if ("xml".equals(prefix)) return; //xml prefix shall not be declared based on jdk api javadoc |
|
122 |
if(side!=null) |
|
123 |
side.endPrefixMapping(prefix); |
|
124 |
super.endPrefixMapping(prefix); |
|
125 |
namespaces.remove(namespaces.size()-1); |
|
126 |
namespaces.remove(namespaces.size()-1); |
|
127 |
} |
|
128 |
||
129 |
@Override |
|
130 |
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { |
|
131 |
if(side!=null) { |
|
132 |
side.startElement(uri,localName,qName,atts); |
|
133 |
depth++; |
|
134 |
} |
|
135 |
super.startElement(uri, localName, qName, atts); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Starts the event forking. |
|
140 |
*/ |
|
141 |
public void startForking(String uri, String localName, String qName, Attributes atts, ContentHandler side) throws SAXException { |
|
142 |
if(this.side!=null) throw new IllegalStateException(); // can't fork to two handlers |
|
143 |
||
144 |
this.side = side; |
|
145 |
depth = 1; |
|
146 |
side.setDocumentLocator(loc); |
|
147 |
side.startDocument(); |
|
148 |
for( int i=0; i<namespaces.size(); i+=2 ) |
|
149 |
side.startPrefixMapping(namespaces.get(i),namespaces.get(i+1)); |
|
150 |
side.startElement(uri,localName,qName,atts); |
|
151 |
} |
|
152 |
||
153 |
@Override |
|
154 |
public void endElement(String uri, String localName, String qName) throws SAXException { |
|
155 |
if(side!=null) { |
|
156 |
side.endElement(uri,localName,qName); |
|
157 |
depth--; |
|
158 |
if(depth==0) { |
|
159 |
for( int i=namespaces.size()-2; i>=0; i-=2 ) |
|
160 |
side.endPrefixMapping(namespaces.get(i)); |
|
161 |
side.endDocument(); |
|
162 |
side = null; |
|
163 |
} |
|
164 |
} |
|
165 |
super.endElement(uri, localName, qName); |
|
166 |
} |
|
167 |
||
168 |
@Override |
|
169 |
public void characters(char ch[], int start, int length) throws SAXException { |
|
170 |
if(side!=null) |
|
171 |
side.characters(ch, start, length); |
|
172 |
super.characters(ch, start, length); |
|
173 |
} |
|
174 |
||
175 |
@Override |
|
176 |
public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { |
|
177 |
if(side!=null) |
|
178 |
side.ignorableWhitespace(ch, start, length); |
|
179 |
super.ignorableWhitespace(ch, start, length); |
|
180 |
} |
|
181 |
} |