01: /*
02: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
03: *
04: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
05: *
06: * The contents of this file are subject to the terms of either the GNU
07: * General Public License Version 2 only ("GPL") or the Common Development
08: * and Distribution License("CDDL") (collectively, the "License"). You
09: * may not use this file except in compliance with the License. You can obtain
10: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
12: * language governing permissions and limitations under the License.
13: *
14: * When distributing the software, include this License Header Notice in each
15: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16: * Sun designates this particular file as subject to the "Classpath" exception
17: * as provided by Sun in the GPL Version 2 section of the License file that
18: * accompanied this code. If applicable, add the following below the License
19: * Header, with the fields enclosed by brackets [] replaced by your own
20: * identifying information: "Portions Copyrighted [year]
21: * [name of copyright owner]"
22: *
23: * Contributor(s):
24: *
25: * If you wish your version of this file to be governed by only the CDDL or
26: * only the GPL Version 2, indicate your decision by adding "[Contributor]
27: * elects to include this software in this distribution under the [CDDL or GPL
28: * Version 2] license." If you don't indicate a single choice of license, a
29: * recipient has the option to distribute your version of this file under
30: * either the CDDL, the GPL Version 2 or to extend the choice of license to
31: * its licensees as provided above. However, if you add GPL Version 2 code
32: * and therefore, elected the GPL Version 2 license, then the option applies
33: * only if the new code is made subject to such option by the copyright
34: * holder.
35: */
36:
37: package com.sun.xml.bind.v2.runtime;
38:
39: import java.util.HashSet;
40: import java.util.Set;
41:
42: import javax.xml.XMLConstants;
43:
44: import org.w3c.dom.Attr;
45: import org.w3c.dom.NamedNodeMap;
46: import org.w3c.dom.Node;
47:
48: /**
49: * Post-init action for {@link MarshallerImpl} that incorporate the in-scope namespace bindings
50: * from a DOM node.
51: *
52: * TODO: do we really need this? think about a better way to put this logic back into marshaller.
53: *
54: * @author Kohsuke Kawaguchi
55: */
56: final class DomPostInitAction implements Runnable {
57:
58: private final Node node;
59: private final XMLSerializer serializer;
60:
61: DomPostInitAction(Node node, XMLSerializer serializer) {
62: this .node = node;
63: this .serializer = serializer;
64: }
65:
66: // declare the currently in-scope namespace bindings
67: public void run() {
68: Set<String> declaredPrefixes = new HashSet<String>();
69: for (Node n = node; n != null
70: && n.getNodeType() == Node.ELEMENT_NODE; n = n
71: .getParentNode()) {
72: NamedNodeMap atts = n.getAttributes();
73: if (atts == null)
74: continue; // broken DOM. but be graceful.
75: for (int i = 0; i < atts.getLength(); i++) {
76: Attr a = (Attr) atts.item(i);
77: String nsUri = a.getNamespaceURI();
78: if (nsUri == null
79: || !nsUri
80: .equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
81: continue; // not a namespace declaration
82: String prefix = a.getLocalName();
83: if (prefix == null)
84: continue; // broken DOM. skip to be safe
85: if (prefix.equals("xmlns")) {
86: prefix = "";
87: }
88: String value = a.getValue();
89: if (value == null)
90: continue; // broken DOM. skip to be safe
91: if (declaredPrefixes.add(prefix)) {
92: serializer.addInscopeBinding(value, prefix);
93: }
94: }
95: }
96: }
97: }
|