Thu, 12 Oct 2017 19:44:07 +0800
merge
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 */
26 package com.sun.xml.internal.bind.v2.runtime;
28 import javax.xml.namespace.NamespaceContext;
29 import javax.xml.stream.XMLEventWriter;
30 import javax.xml.stream.XMLStreamWriter;
32 /**
33 * Post-init action for {@link MarshallerImpl} that incorporate the in-scope namespace bindings
34 * from a StAX writer.
35 *
36 * <p>
37 * It's always used either with {@link XMLStreamWriter}, {@link XMLEventWriter}, or bare
38 * {@link NamespaceContext},
39 * but to reduce the # of classes in the runtime I wrote only one class that handles both.
40 *
41 * @author Kohsuke Kawaguchi
42 */
43 final class StAXPostInitAction implements Runnable {
44 private final XMLStreamWriter xsw;
45 private final XMLEventWriter xew;
46 private final NamespaceContext nsc;
47 private final XMLSerializer serializer;
49 StAXPostInitAction(XMLStreamWriter xsw,XMLSerializer serializer) {
50 this.xsw = xsw;
51 this.xew = null;
52 this.nsc = null;
53 this.serializer = serializer;
54 }
56 StAXPostInitAction(XMLEventWriter xew,XMLSerializer serializer) {
57 this.xsw = null;
58 this.xew = xew;
59 this.nsc = null;
60 this.serializer = serializer;
61 }
63 StAXPostInitAction(NamespaceContext nsc,XMLSerializer serializer) {
64 this.xsw = null;
65 this.xew = null;
66 this.nsc = nsc;
67 this.serializer = serializer;
68 }
70 public void run() {
71 NamespaceContext ns = nsc;
72 if(xsw!=null) ns = xsw.getNamespaceContext();
73 if(xew!=null) ns = xew.getNamespaceContext();
75 // StAX javadoc isn't very clear on the behavior,
76 // so work defensively in anticipation of broken implementations.
77 if(ns==null)
78 return;
80 // we can't enumerate all the in-scope namespace bindings in StAX,
81 // so we only look for the known static namespace URIs.
82 // this is less than ideal, but better than nothing.
83 for( String nsUri : serializer.grammar.nameList.namespaceURIs ) {
84 String p = ns.getPrefix(nsUri);
85 if(p!=null)
86 serializer.addInscopeBinding(nsUri,p);
87 }
88 }
89 }