src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/internalizer/Internalizer.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/internalizer/Internalizer.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,615 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.xjc.reader.internalizer;
    1.30 +
    1.31 +import java.net.MalformedURLException;
    1.32 +import java.net.URL;
    1.33 +import java.util.ArrayList;
    1.34 +import java.util.HashMap;
    1.35 +import java.util.HashSet;
    1.36 +import java.util.List;
    1.37 +import java.util.Map;
    1.38 +import java.util.Set;
    1.39 +import java.text.ParseException;
    1.40 +
    1.41 +import javax.xml.xpath.XPath;
    1.42 +import javax.xml.xpath.XPathConstants;
    1.43 +import javax.xml.xpath.XPathExpressionException;
    1.44 +import javax.xml.xpath.XPathFactory;
    1.45 +
    1.46 +import com.sun.istack.internal.SAXParseException2;
    1.47 +import com.sun.istack.internal.NotNull;
    1.48 +import com.sun.istack.internal.Nullable;
    1.49 +import com.sun.tools.internal.xjc.ErrorReceiver;
    1.50 +import com.sun.tools.internal.xjc.reader.Const;
    1.51 +import com.sun.tools.internal.xjc.util.DOMUtils;
    1.52 +import com.sun.xml.internal.bind.v2.util.EditDistance;
    1.53 +import com.sun.xml.internal.bind.v2.util.XmlFactory;
    1.54 +import com.sun.xml.internal.xsom.SCD;
    1.55 +import java.io.File;
    1.56 +import java.io.IOException;
    1.57 +import java.util.logging.Level;
    1.58 +import java.util.logging.Logger;
    1.59 +import javax.xml.XMLConstants;
    1.60 +
    1.61 +import org.w3c.dom.Attr;
    1.62 +import org.w3c.dom.Document;
    1.63 +import org.w3c.dom.Element;
    1.64 +import org.w3c.dom.NamedNodeMap;
    1.65 +import org.w3c.dom.Node;
    1.66 +import org.w3c.dom.NodeList;
    1.67 +import org.xml.sax.SAXParseException;
    1.68 +
    1.69 +/**
    1.70 + * Internalizes external binding declarations.
    1.71 + *
    1.72 + * <p>
    1.73 + * The {@link #transform(DOMForest,boolean)} method is the entry point.
    1.74 + *
    1.75 + * @author
    1.76 + *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    1.77 + */
    1.78 +class Internalizer {
    1.79 +
    1.80 +    private static final String WSDL_NS = "http://schemas.xmlsoap.org/wsdl/";
    1.81 +
    1.82 +    private final XPath xpath;
    1.83 +
    1.84 +    /**
    1.85 +     * Internalize all &lt;jaxb:bindings> customizations in the given forest.
    1.86 +     *
    1.87 +     * @return
    1.88 +     *      if the SCD support is enabled, the return bindings need to be applied
    1.89 +     *      after schema components are parsed.
    1.90 +     *      If disabled, the returned binding set will be empty.
    1.91 +     *      SCDs are only for XML Schema, and doesn't make any sense for other
    1.92 +     *      schema languages.
    1.93 +     */
    1.94 +    static SCDBasedBindingSet transform( DOMForest forest, boolean enableSCD, boolean disableSecureProcessing ) {
    1.95 +        return new Internalizer(forest, enableSCD, disableSecureProcessing).transform();
    1.96 +    }
    1.97 +
    1.98 +
    1.99 +    private Internalizer(DOMForest forest, boolean enableSCD, boolean disableSecureProcessing) {
   1.100 +        this.errorHandler = forest.getErrorHandler();
   1.101 +        this.forest = forest;
   1.102 +        this.enableSCD = enableSCD;
   1.103 +        xpath = XmlFactory.createXPathFactory(disableSecureProcessing).newXPath();
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * DOMForest object currently being processed.
   1.108 +     */
   1.109 +    private final DOMForest forest;
   1.110 +
   1.111 +    /**
   1.112 +     * All errors found during the transformation is sent to this object.
   1.113 +     */
   1.114 +    private ErrorReceiver errorHandler;
   1.115 +
   1.116 +    /**
   1.117 +     * If true, the SCD-based target selection is supported.
   1.118 +     */
   1.119 +    private boolean enableSCD;
   1.120 +
   1.121 +
   1.122 +    private SCDBasedBindingSet transform() {
   1.123 +
   1.124 +        // either target nodes are conventional DOM nodes (as per spec),
   1.125 +        Map<Element,List<Node>> targetNodes = new HashMap<Element,List<Node>>();
   1.126 +        // ... or it will be schema components by means of SCD (RI extension)
   1.127 +        SCDBasedBindingSet scd = new SCDBasedBindingSet(forest);
   1.128 +
   1.129 +        //
   1.130 +        // identify target nodes for all <jaxb:bindings>
   1.131 +        //
   1.132 +        for (Element jaxbBindings : forest.outerMostBindings) {
   1.133 +            // initially, the inherited context is itself
   1.134 +            buildTargetNodeMap(jaxbBindings, jaxbBindings, null, targetNodes, scd);
   1.135 +        }
   1.136 +
   1.137 +        //
   1.138 +        // then move them to their respective positions.
   1.139 +        //
   1.140 +        for (Element jaxbBindings : forest.outerMostBindings) {
   1.141 +            move(jaxbBindings, targetNodes);
   1.142 +        }
   1.143 +
   1.144 +        return scd;
   1.145 +    }
   1.146 +
   1.147 +    /**
   1.148 +     * Validates attributes of a &lt;jaxb:bindings> element.
   1.149 +     */
   1.150 +    private void validate( Element bindings ) {
   1.151 +        NamedNodeMap atts = bindings.getAttributes();
   1.152 +        for( int i=0; i<atts.getLength(); i++ ) {
   1.153 +            Attr a = (Attr)atts.item(i);
   1.154 +            if( a.getNamespaceURI()!=null )
   1.155 +                continue;   // all foreign namespace OK.
   1.156 +            if( a.getLocalName().equals("node") )
   1.157 +                continue;
   1.158 +            if( a.getLocalName().equals("schemaLocation"))
   1.159 +                continue;
   1.160 +            if( a.getLocalName().equals("scd") )
   1.161 +                continue;
   1.162 +
   1.163 +            // enhancements
   1.164 +            if( a.getLocalName().equals("required") ) //
   1.165 +                continue;
   1.166 +            if( a.getLocalName().equals("multiple") ) //
   1.167 +                continue;
   1.168 +
   1.169 +
   1.170 +            // TODO: flag error for this undefined attribute
   1.171 +        }
   1.172 +    }
   1.173 +
   1.174 +    /**
   1.175 +     * Determines the target node of the "bindings" element
   1.176 +     * by using the inherited target node, then put
   1.177 +     * the result into the "result" map and the "scd" map.
   1.178 +     *
   1.179 +     * @param inheritedTarget
   1.180 +     *      The current target node. This always exists, even if
   1.181 +     *      the user starts specifying targets via SCD (in that case
   1.182 +     *      this inherited target is just not going to be used.)
   1.183 +     * @param inheritedSCD
   1.184 +     *      If the ancestor &lt;bindings> node specifies @scd to
   1.185 +     *      specify the target via SCD, then this parameter represents that context.
   1.186 +     */
   1.187 +    private void buildTargetNodeMap( Element bindings, @NotNull Node inheritedTarget,
   1.188 +                                     @Nullable SCDBasedBindingSet.Target inheritedSCD,
   1.189 +                                     Map<Element,List<Node>> result, SCDBasedBindingSet scdResult ) {
   1.190 +        // start by the inherited target
   1.191 +        Node target = inheritedTarget;
   1.192 +        ArrayList<Node> targetMultiple = null;
   1.193 +
   1.194 +        validate(bindings); // validate this node
   1.195 +
   1.196 +        boolean required = true;
   1.197 +        boolean multiple = false;
   1.198 +
   1.199 +        if(bindings.getAttribute("required") != null) {
   1.200 +            String requiredAttr = bindings.getAttribute("required");
   1.201 +
   1.202 +            if(requiredAttr.equals("no") || requiredAttr.equals("false") || requiredAttr.equals("0"))
   1.203 +                required = false;
   1.204 +        }
   1.205 +
   1.206 +        if(bindings.getAttribute("multiple") != null) {
   1.207 +            String requiredAttr = bindings.getAttribute("multiple");
   1.208 +
   1.209 +            if(requiredAttr.equals("yes") || requiredAttr.equals("true") || requiredAttr.equals("1"))
   1.210 +                multiple = true;
   1.211 +        }
   1.212 +
   1.213 +
   1.214 +        // look for @schemaLocation
   1.215 +        if( bindings.getAttributeNode("schemaLocation")!=null ) {
   1.216 +            String schemaLocation = bindings.getAttribute("schemaLocation");
   1.217 +
   1.218 +            // enhancement - schemaLocation="*" = bind to all schemas..
   1.219 +            if(schemaLocation.equals("*")) {
   1.220 +                for(String systemId : forest.listSystemIDs()) {
   1.221 +                    if (result.get(bindings) == null)
   1.222 +                        result.put(bindings, new ArrayList<Node>());
   1.223 +                    result.get(bindings).add(forest.get(systemId).getDocumentElement());
   1.224 +
   1.225 +                    Element[] children = DOMUtils.getChildElements(bindings, Const.JAXB_NSURI, "bindings");
   1.226 +                    for (Element value : children)
   1.227 +                        buildTargetNodeMap(value, forest.get(systemId).getDocumentElement(), inheritedSCD, result, scdResult);
   1.228 +                }
   1.229 +                return;
   1.230 +            } else {
   1.231 +                try {
   1.232 +                    // TODO: use the URI class
   1.233 +                    // TODO: honor xml:base
   1.234 +                    URL loc = new URL(
   1.235 +                                new URL(forest.getSystemId(bindings.getOwnerDocument())), schemaLocation
   1.236 +                              );
   1.237 +                    schemaLocation = loc.toExternalForm();
   1.238 +                    target = forest.get(schemaLocation);
   1.239 +                    if ((target == null) && (loc.getProtocol().startsWith("file"))) {
   1.240 +                        File f = new File(loc.getFile());
   1.241 +                        schemaLocation = new File(f.getCanonicalPath()).toURI().toString();
   1.242 +                    }
   1.243 +                } catch( MalformedURLException e ) {
   1.244 +                } catch( IOException e ) {
   1.245 +                    Logger.getLogger(Internalizer.class.getName()).log(Level.FINEST, e.getLocalizedMessage());
   1.246 +                }
   1.247 +
   1.248 +                target = forest.get(schemaLocation);
   1.249 +                if(target==null) {
   1.250 +                    reportError( bindings,
   1.251 +                        Messages.format(Messages.ERR_INCORRECT_SCHEMA_REFERENCE,
   1.252 +                            schemaLocation,
   1.253 +                            EditDistance.findNearest(schemaLocation,forest.listSystemIDs())));
   1.254 +
   1.255 +                    return; // abort processing this <jaxb:bindings>
   1.256 +                }
   1.257 +
   1.258 +                target = ((Document)target).getDocumentElement();
   1.259 +            }
   1.260 +        }
   1.261 +
   1.262 +        // look for @node
   1.263 +        if( bindings.getAttributeNode("node")!=null ) {
   1.264 +            String nodeXPath = bindings.getAttribute("node");
   1.265 +
   1.266 +            // evaluate this XPath
   1.267 +            NodeList nlst;
   1.268 +            try {
   1.269 +                xpath.setNamespaceContext(new NamespaceContextImpl(bindings));
   1.270 +                nlst = (NodeList)xpath.evaluate(nodeXPath,target,XPathConstants.NODESET);
   1.271 +            } catch (XPathExpressionException e) {
   1.272 +                if(required) {
   1.273 +                    reportError( bindings,
   1.274 +                        Messages.format(Messages.ERR_XPATH_EVAL,e.getMessage()), e );
   1.275 +                    return; // abort processing this <jaxb:bindings>
   1.276 +                } else {
   1.277 +                    return;
   1.278 +                }
   1.279 +            }
   1.280 +
   1.281 +            if( nlst.getLength()==0 ) {
   1.282 +                if(required)
   1.283 +                    reportError( bindings,
   1.284 +                        Messages.format(Messages.NO_XPATH_EVAL_TO_NO_TARGET, nodeXPath) );
   1.285 +                return; // abort
   1.286 +            }
   1.287 +
   1.288 +            if( nlst.getLength()!=1 ) {
   1.289 +                if(!multiple) {
   1.290 +                    reportError( bindings,
   1.291 +                        Messages.format(Messages.NO_XPATH_EVAL_TOO_MANY_TARGETS, nodeXPath,nlst.getLength()) );
   1.292 +
   1.293 +                    return; // abort
   1.294 +                } else {
   1.295 +                    if(targetMultiple == null) targetMultiple = new ArrayList<Node>();
   1.296 +                    for(int i = 0; i < nlst.getLength(); i++) {
   1.297 +                        targetMultiple.add(nlst.item(i));
   1.298 +                    }
   1.299 +                }
   1.300 +            }
   1.301 +
   1.302 +            // check
   1.303 +            if(!multiple || nlst.getLength() == 1) {
   1.304 +                Node rnode = nlst.item(0);
   1.305 +                if (!(rnode instanceof Element)) {
   1.306 +                    reportError(bindings,
   1.307 +                            Messages.format(Messages.NO_XPATH_EVAL_TO_NON_ELEMENT, nodeXPath));
   1.308 +                    return; // abort
   1.309 +                }
   1.310 +
   1.311 +                if (!forest.logic.checkIfValidTargetNode(forest, bindings, (Element) rnode)) {
   1.312 +                    reportError(bindings,
   1.313 +                            Messages.format(Messages.XPATH_EVAL_TO_NON_SCHEMA_ELEMENT,
   1.314 +                            nodeXPath, rnode.getNodeName()));
   1.315 +                    return; // abort
   1.316 +                }
   1.317 +
   1.318 +                target = rnode;
   1.319 +            } else {
   1.320 +                for(Node rnode : targetMultiple) {
   1.321 +                    if (!(rnode instanceof Element)) {
   1.322 +                        reportError(bindings,
   1.323 +                                Messages.format(Messages.NO_XPATH_EVAL_TO_NON_ELEMENT, nodeXPath));
   1.324 +                        return; // abort
   1.325 +                    }
   1.326 +
   1.327 +                    if (!forest.logic.checkIfValidTargetNode(forest, bindings, (Element) rnode)) {
   1.328 +                        reportError(bindings,
   1.329 +                                Messages.format(Messages.XPATH_EVAL_TO_NON_SCHEMA_ELEMENT,
   1.330 +                                nodeXPath, rnode.getNodeName()));
   1.331 +                        return; // abort
   1.332 +                    }
   1.333 +                }
   1.334 +            }
   1.335 +        }
   1.336 +
   1.337 +        // look for @scd
   1.338 +        if( bindings.getAttributeNode("scd")!=null ) {
   1.339 +            String scdPath = bindings.getAttribute("scd");
   1.340 +            if(!enableSCD) {
   1.341 +                // SCD selector was found, but it's not activated. report an error
   1.342 +                // but recover by handling it anyway. this also avoids repeated error messages.
   1.343 +                reportError(bindings,
   1.344 +                    Messages.format(Messages.SCD_NOT_ENABLED));
   1.345 +                enableSCD = true;
   1.346 +            }
   1.347 +
   1.348 +            try {
   1.349 +                inheritedSCD = scdResult.createNewTarget( inheritedSCD, bindings,
   1.350 +                        SCD.create(scdPath, new NamespaceContextImpl(bindings)) );
   1.351 +            } catch (ParseException e) {
   1.352 +                reportError( bindings, Messages.format(Messages.ERR_SCD_EVAL,e.getMessage()),e );
   1.353 +                return; // abort processing this bindings
   1.354 +            }
   1.355 +        }
   1.356 +
   1.357 +        // update the result map
   1.358 +        if (inheritedSCD != null) {
   1.359 +            inheritedSCD.addBinidng(bindings);
   1.360 +        } else if (!multiple || targetMultiple == null) {
   1.361 +            if (result.get(bindings) == null)
   1.362 +                result.put(bindings, new ArrayList<Node>());
   1.363 +            result.get(bindings).add(target);
   1.364 +        } else {
   1.365 +            for (Node rnode : targetMultiple) {
   1.366 +                if (result.get(bindings) == null)
   1.367 +                    result.put(bindings, new ArrayList<Node>());
   1.368 +
   1.369 +                result.get(bindings).add(rnode);
   1.370 +            }
   1.371 +
   1.372 +        }
   1.373 +
   1.374 +
   1.375 +        // look for child <jaxb:bindings> and process them recursively
   1.376 +        Element[] children = DOMUtils.getChildElements( bindings, Const.JAXB_NSURI, "bindings" );
   1.377 +        for (Element value : children)
   1.378 +            if(!multiple || targetMultiple == null)
   1.379 +                buildTargetNodeMap(value, target, inheritedSCD, result, scdResult);
   1.380 +            else {
   1.381 +                for(Node rnode : targetMultiple) {
   1.382 +                    buildTargetNodeMap(value, rnode, inheritedSCD, result, scdResult);
   1.383 +                }
   1.384 +            }
   1.385 +    }
   1.386 +
   1.387 +    /**
   1.388 +     * Moves JAXB customizations under their respective target nodes.
   1.389 +     */
   1.390 +    private void move(Element bindings, Map<Element, List<Node>> targetNodes) {
   1.391 +        List<Node> nodelist = targetNodes.get(bindings);
   1.392 +
   1.393 +        if(nodelist == null) {
   1.394 +                return; // abort
   1.395 +        }
   1.396 +
   1.397 +        for (Node target : nodelist) {
   1.398 +            if (target == null) // this must be the result of an error on the external binding.
   1.399 +            // recover from the error by ignoring this node
   1.400 +            {
   1.401 +                return;
   1.402 +            }
   1.403 +
   1.404 +            for (Element item : DOMUtils.getChildElements(bindings)) {
   1.405 +                String localName = item.getLocalName();
   1.406 +
   1.407 +                if ("bindings".equals(localName)) {
   1.408 +                    // process child <jaxb:bindings> recursively
   1.409 +                    move(item, targetNodes);
   1.410 +                } else if ("globalBindings".equals(localName)) {
   1.411 +                        // <jaxb:globalBindings> always go to the root of document.
   1.412 +                    Element root = forest.getOneDocument().getDocumentElement();
   1.413 +                    if (root.getNamespaceURI().equals(WSDL_NS)) {
   1.414 +                        NodeList elements = root.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema");
   1.415 +                        if ((elements == null) || (elements.getLength() < 1)) {
   1.416 +                            reportError(item, Messages.format(Messages.ORPHANED_CUSTOMIZATION, item.getNodeName()));
   1.417 +                            return;
   1.418 +                        } else {
   1.419 +                            moveUnder(item, (Element)elements.item(0));
   1.420 +                        }
   1.421 +                    } else {
   1.422 +                        moveUnder(item, root);
   1.423 +                    }
   1.424 +                } else {
   1.425 +                    if (!(target instanceof Element)) {
   1.426 +                        reportError(item,
   1.427 +                                Messages.format(Messages.CONTEXT_NODE_IS_NOT_ELEMENT));
   1.428 +                        return; // abort
   1.429 +                    }
   1.430 +
   1.431 +                    if (!forest.logic.checkIfValidTargetNode(forest, item, (Element) target)) {
   1.432 +                        reportError(item,
   1.433 +                                Messages.format(Messages.ORPHANED_CUSTOMIZATION, item.getNodeName()));
   1.434 +                        return; // abort
   1.435 +                    }
   1.436 +
   1.437 +                    // move this node under the target
   1.438 +                    moveUnder(item, (Element) target);
   1.439 +                }
   1.440 +            }
   1.441 +        }
   1.442 +    }
   1.443 +
   1.444 +    /**
   1.445 +     * Moves the "decl" node under the "target" node.
   1.446 +     *
   1.447 +     * @param decl
   1.448 +     *      A JAXB customization element (e.g., &lt;jaxb:class>)
   1.449 +     *
   1.450 +     * @param target
   1.451 +     *      XML Schema element under which the declaration should move.
   1.452 +     *      For example, &lt;xs:element>
   1.453 +     */
   1.454 +    private void moveUnder( Element decl, Element target ) {
   1.455 +        Element realTarget = forest.logic.refineTarget(target);
   1.456 +
   1.457 +        declExtensionNamespace( decl, target );
   1.458 +
   1.459 +        // copy in-scope namespace declarations of the decl node
   1.460 +        // to the decl node itself so that this move won't change
   1.461 +        // the in-scope namespace bindings.
   1.462 +        Element p = decl;
   1.463 +        Set<String> inscopes = new HashSet<String>();
   1.464 +        while(true) {
   1.465 +            NamedNodeMap atts = p.getAttributes();
   1.466 +            for( int i=0; i<atts.getLength(); i++ ) {
   1.467 +                Attr a = (Attr)atts.item(i);
   1.468 +                if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
   1.469 +                    String prefix;
   1.470 +                    if( a.getName().indexOf(':')==-1 )  prefix = "";
   1.471 +                    else                                prefix = a.getLocalName();
   1.472 +
   1.473 +                    if( inscopes.add(prefix) && p!=decl ) {
   1.474 +                        // if this is the first time we see this namespace bindings,
   1.475 +                        // copy the declaration.
   1.476 +                        // if p==decl, there's no need to. Note that
   1.477 +                        // we want to add prefix to inscopes even if p==Decl
   1.478 +
   1.479 +                        decl.setAttributeNodeNS( (Attr)a.cloneNode(true) );
   1.480 +                    }
   1.481 +                }
   1.482 +            }
   1.483 +
   1.484 +            if( p.getParentNode() instanceof Document )
   1.485 +                break;
   1.486 +
   1.487 +            p = (Element)p.getParentNode();
   1.488 +        }
   1.489 +
   1.490 +        if( !inscopes.contains("") ) {
   1.491 +            // if the default namespace was undeclared in the context of decl,
   1.492 +            // it must be explicitly set to "" since the new environment might
   1.493 +            // have a different default namespace URI.
   1.494 +            decl.setAttributeNS(Const.XMLNS_URI,"xmlns","");
   1.495 +        }
   1.496 +
   1.497 +
   1.498 +        // finally move the declaration to the target node.
   1.499 +        if( realTarget.getOwnerDocument()!=decl.getOwnerDocument() ) {
   1.500 +            // if they belong to different DOM documents, we need to clone them
   1.501 +            Element original = decl;
   1.502 +            decl = (Element)realTarget.getOwnerDocument().importNode(decl,true);
   1.503 +
   1.504 +            // this effectively clones a ndoe,, so we need to copy locators.
   1.505 +            copyLocators( original, decl );
   1.506 +        }
   1.507 +
   1.508 +        realTarget.appendChild( decl );
   1.509 +    }
   1.510 +
   1.511 +    /**
   1.512 +     * Recursively visits sub-elements and declare all used namespaces.
   1.513 +     * TODO: the fact that we recognize all namespaces in the extension
   1.514 +     * is a bad design.
   1.515 +     */
   1.516 +    private void declExtensionNamespace(Element decl, Element target) {
   1.517 +        // if this comes from external namespaces, add the namespace to
   1.518 +        // @extensionBindingPrefixes.
   1.519 +        if( !Const.JAXB_NSURI.equals(decl.getNamespaceURI()) )
   1.520 +            declareExtensionNamespace( target, decl.getNamespaceURI() );
   1.521 +
   1.522 +        NodeList lst = decl.getChildNodes();
   1.523 +        for( int i=0; i<lst.getLength(); i++ ) {
   1.524 +            Node n = lst.item(i);
   1.525 +            if( n instanceof Element )
   1.526 +                declExtensionNamespace( (Element)n, target );
   1.527 +        }
   1.528 +    }
   1.529 +
   1.530 +
   1.531 +    /** Attribute name. */
   1.532 +    private static final String EXTENSION_PREFIXES = "extensionBindingPrefixes";
   1.533 +
   1.534 +    /**
   1.535 +     * Adds the specified namespace URI to the jaxb:extensionBindingPrefixes
   1.536 +     * attribute of the target document.
   1.537 +     */
   1.538 +    private void declareExtensionNamespace( Element target, String nsUri ) {
   1.539 +        // look for the attribute
   1.540 +        Element root = target.getOwnerDocument().getDocumentElement();
   1.541 +        Attr att = root.getAttributeNodeNS(Const.JAXB_NSURI,EXTENSION_PREFIXES);
   1.542 +        if( att==null ) {
   1.543 +            String jaxbPrefix = allocatePrefix(root,Const.JAXB_NSURI);
   1.544 +            // no such attribute. Create one.
   1.545 +            att = target.getOwnerDocument().createAttributeNS(
   1.546 +                Const.JAXB_NSURI,jaxbPrefix+':'+EXTENSION_PREFIXES);
   1.547 +            root.setAttributeNodeNS(att);
   1.548 +        }
   1.549 +
   1.550 +        String prefix = allocatePrefix(root,nsUri);
   1.551 +        if( att.getValue().indexOf(prefix)==-1 )
   1.552 +            // avoid redeclaring the same namespace twice.
   1.553 +            att.setValue( att.getValue()+' '+prefix);
   1.554 +    }
   1.555 +
   1.556 +    /**
   1.557 +     * Declares a new prefix on the given element and associates it
   1.558 +     * with the specified namespace URI.
   1.559 +     * <p>
   1.560 +     * Note that this method doesn't use the default namespace
   1.561 +     * even if it can.
   1.562 +     */
   1.563 +    private String allocatePrefix( Element e, String nsUri ) {
   1.564 +        // look for existing namespaces.
   1.565 +        NamedNodeMap atts = e.getAttributes();
   1.566 +        for( int i=0; i<atts.getLength(); i++ ) {
   1.567 +            Attr a = (Attr)atts.item(i);
   1.568 +            if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
   1.569 +                if( a.getName().indexOf(':')==-1 )  continue;
   1.570 +
   1.571 +                if( a.getValue().equals(nsUri) )
   1.572 +                    return a.getLocalName();    // found one
   1.573 +            }
   1.574 +        }
   1.575 +
   1.576 +        // none found. allocate new.
   1.577 +        while(true) {
   1.578 +            String prefix = "p"+(int)(Math.random()*1000000)+'_';
   1.579 +            if(e.getAttributeNodeNS(Const.XMLNS_URI,prefix)!=null)
   1.580 +                continue;   // this prefix is already allocated.
   1.581 +
   1.582 +            e.setAttributeNS(Const.XMLNS_URI,"xmlns:"+prefix,nsUri);
   1.583 +            return prefix;
   1.584 +        }
   1.585 +    }
   1.586 +
   1.587 +
   1.588 +    /**
   1.589 +     * Copies location information attached to the "src" node to the "dst" node.
   1.590 +     */
   1.591 +    private void copyLocators( Element src, Element dst ) {
   1.592 +        forest.locatorTable.storeStartLocation(
   1.593 +            dst, forest.locatorTable.getStartLocation(src) );
   1.594 +        forest.locatorTable.storeEndLocation(
   1.595 +            dst, forest.locatorTable.getEndLocation(src) );
   1.596 +
   1.597 +        // recursively process child elements
   1.598 +        Element[] srcChilds = DOMUtils.getChildElements(src);
   1.599 +        Element[] dstChilds = DOMUtils.getChildElements(dst);
   1.600 +
   1.601 +        for( int i=0; i<srcChilds.length; i++ )
   1.602 +            copyLocators( srcChilds[i], dstChilds[i] );
   1.603 +    }
   1.604 +
   1.605 +
   1.606 +    private void reportError( Element errorSource, String formattedMsg ) {
   1.607 +        reportError( errorSource, formattedMsg, null );
   1.608 +    }
   1.609 +
   1.610 +    private void reportError( Element errorSource,
   1.611 +        String formattedMsg, Exception nestedException ) {
   1.612 +
   1.613 +        SAXParseException e = new SAXParseException2( formattedMsg,
   1.614 +            forest.locatorTable.getStartLocation(errorSource),
   1.615 +            nestedException );
   1.616 +        errorHandler.error(e);
   1.617 +    }
   1.618 +}

mercurial