1 /*
   2  * Copyright (c) 1997, 2012, 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 java.net.MalformedURLException;
  29 import java.net.URL;
  30 import java.util.ArrayList;
  31 import java.util.HashMap;
  32 import java.util.HashSet;
  33 import java.util.List;
  34 import java.util.Map;
  35 import java.util.Set;
  36 import java.text.ParseException;
  37 
  38 import javax.xml.xpath.XPath;
  39 import javax.xml.xpath.XPathConstants;
  40 import javax.xml.xpath.XPathExpressionException;
  41 import javax.xml.xpath.XPathFactory;
  42 
  43 import com.sun.istack.internal.SAXParseException2;
  44 import com.sun.istack.internal.NotNull;
  45 import com.sun.istack.internal.Nullable;
  46 import com.sun.tools.internal.xjc.ErrorReceiver;
  47 import com.sun.tools.internal.xjc.reader.Const;
  48 import com.sun.tools.internal.xjc.util.DOMUtils;
  49 import com.sun.xml.internal.bind.v2.util.EditDistance;
  50 import com.sun.xml.internal.bind.v2.util.XmlFactory;
  51 import com.sun.xml.internal.xsom.SCD;
  52 import java.io.File;
  53 import java.io.IOException;
  54 import java.util.logging.Level;
  55 import java.util.logging.Logger;
  56 import javax.xml.XMLConstants;
  57 
  58 import org.w3c.dom.Attr;
  59 import org.w3c.dom.Document;
  60 import org.w3c.dom.Element;
  61 import org.w3c.dom.NamedNodeMap;
  62 import org.w3c.dom.Node;
  63 import org.w3c.dom.NodeList;
  64 import org.xml.sax.SAXParseException;
  65 
  66 /**
  67  * Internalizes external binding declarations.
  68  *
  69  * <p>
  70  * The {@link #transform(DOMForest,boolean)} method is the entry point.
  71  *
  72  * @author
  73  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  74  */
  75 class Internalizer {
  76 
  77     private static final String WSDL_NS = "http://schemas.xmlsoap.org/wsdl/";
  78 
  79     private static XPathFactory xpf = null;
  80 
  81     private final XPath xpath;
  82 
  83     /**
  84      * Internalize all &lt;jaxb:bindings> customizations in the given forest.
  85      *
  86      * @return
  87      *      if the SCD support is enabled, the return bindings need to be applied
  88      *      after schema components are parsed.
  89      *      If disabled, the returned binding set will be empty.
  90      *      SCDs are only for XML Schema, and doesn't make any sense for other
  91      *      schema languages.
  92      */
  93     static SCDBasedBindingSet transform( DOMForest forest, boolean enableSCD, boolean disableSecureProcessing ) {
  94         return new Internalizer(forest, enableSCD, disableSecureProcessing).transform();
  95     }
  96 
  97 
  98     private Internalizer(DOMForest forest, boolean enableSCD, boolean disableSecureProcessing) {
  99         this.errorHandler = forest.getErrorHandler();
 100         this.forest = forest;
 101         this.enableSCD = enableSCD;
 102         synchronized (this) {
 103             if (xpf == null) {
 104                 xpf = XmlFactory.createXPathFactory(disableSecureProcessing);
 105             }
 106         }
 107         xpath = xpf.newXPath();
 108     }
 109 
 110     /**
 111      * DOMForest object currently being processed.
 112      */
 113     private final DOMForest forest;
 114 
 115     /**
 116      * All errors found during the transformation is sent to this object.
 117      */
 118     private ErrorReceiver errorHandler;
 119 
 120     /**
 121      * If true, the SCD-based target selection is supported.
 122      */
 123     private boolean enableSCD;
 124 
 125 
 126     private SCDBasedBindingSet transform() {
 127 
 128         // either target nodes are conventional DOM nodes (as per spec),
 129         Map<Element,List<Node>> targetNodes = new HashMap<Element,List<Node>>();
 130         // ... or it will be schema components by means of SCD (RI extension)
 131         SCDBasedBindingSet scd = new SCDBasedBindingSet(forest);
 132 
 133         //
 134         // identify target nodes for all <jaxb:bindings>
 135         //
 136         for (Element jaxbBindings : forest.outerMostBindings) {
 137             // initially, the inherited context is itself
 138             buildTargetNodeMap(jaxbBindings, jaxbBindings, null, targetNodes, scd);
 139         }
 140 
 141         //
 142         // then move them to their respective positions.
 143         //
 144         for (Element jaxbBindings : forest.outerMostBindings) {
 145             move(jaxbBindings, targetNodes);
 146         }
 147 
 148         return scd;
 149     }
 150 
 151     /**
 152      * Validates attributes of a &lt;jaxb:bindings> element.
 153      */
 154     private void validate( Element bindings ) {
 155         NamedNodeMap atts = bindings.getAttributes();
 156         for( int i=0; i<atts.getLength(); i++ ) {
 157             Attr a = (Attr)atts.item(i);
 158             if( a.getNamespaceURI()!=null )
 159                 continue;   // all foreign namespace OK.
 160             if( a.getLocalName().equals("node") )
 161                 continue;
 162             if( a.getLocalName().equals("schemaLocation"))
 163                 continue;
 164             if( a.getLocalName().equals("scd") )
 165                 continue;
 166 
 167             // enhancements
 168             if( a.getLocalName().equals("required") ) //
 169                 continue;
 170             if( a.getLocalName().equals("multiple") ) //
 171                 continue;
 172 
 173 
 174             // TODO: flag error for this undefined attribute
 175         }
 176     }
 177 
 178     /**
 179      * Determines the target node of the "bindings" element
 180      * by using the inherited target node, then put
 181      * the result into the "result" map and the "scd" map.
 182      *
 183      * @param inheritedTarget
 184      *      The current target node. This always exists, even if
 185      *      the user starts specifying targets via SCD (in that case
 186      *      this inherited target is just not going to be used.)
 187      * @param inheritedSCD
 188      *      If the ancestor &lt;bindings> node specifies @scd to
 189      *      specify the target via SCD, then this parameter represents that context.
 190      */
 191     private void buildTargetNodeMap( Element bindings, @NotNull Node inheritedTarget,
 192                                      @Nullable SCDBasedBindingSet.Target inheritedSCD,
 193                                      Map<Element,List<Node>> result, SCDBasedBindingSet scdResult ) {
 194         // start by the inherited target
 195         Node target = inheritedTarget;
 196         ArrayList<Node> targetMultiple = null;
 197 
 198         validate(bindings); // validate this node
 199 
 200         boolean required = true;
 201         boolean multiple = false;
 202 
 203         if(bindings.getAttribute("required") != null) {
 204             String requiredAttr = bindings.getAttribute("required");
 205 
 206             if(requiredAttr.equals("no") || requiredAttr.equals("false") || requiredAttr.equals("0"))
 207                 required = false;
 208         }
 209 
 210         if(bindings.getAttribute("multiple") != null) {
 211             String requiredAttr = bindings.getAttribute("multiple");
 212 
 213             if(requiredAttr.equals("yes") || requiredAttr.equals("true") || requiredAttr.equals("1"))
 214                 multiple = true;
 215         }
 216 
 217 
 218         // look for @schemaLocation
 219         if( bindings.getAttributeNode("schemaLocation")!=null ) {
 220             String schemaLocation = bindings.getAttribute("schemaLocation");
 221 
 222             // enhancement - schemaLocation="*" = bind to all schemas..
 223             if(schemaLocation.equals("*")) {
 224                 for(String systemId : forest.listSystemIDs()) {
 225                     if (result.get(bindings) == null)
 226                         result.put(bindings, new ArrayList<Node>());
 227                     result.get(bindings).add(forest.get(systemId).getDocumentElement());
 228 
 229                     Element[] children = DOMUtils.getChildElements(bindings, Const.JAXB_NSURI, "bindings");
 230                     for (Element value : children)
 231                         buildTargetNodeMap(value, forest.get(systemId).getDocumentElement(), inheritedSCD, result, scdResult);
 232                 }
 233                 return;
 234             } else {
 235                 try {
 236                     // TODO: use the URI class
 237                     // TODO: honor xml:base
 238                     URL loc = new URL(
 239                                 new URL(forest.getSystemId(bindings.getOwnerDocument())), schemaLocation
 240                               );
 241                     schemaLocation = loc.toExternalForm();
 242                     target = forest.get(schemaLocation);
 243                     if ((target == null) && (loc.getProtocol().startsWith("file"))) {
 244                         File f = new File(loc.getFile());
 245                         schemaLocation = new File(f.getCanonicalPath()).toURI().toString();
 246                     }
 247                 } catch( MalformedURLException e ) {
 248                 } catch( IOException e ) {
 249                     Logger.getLogger(Internalizer.class.getName()).log(Level.FINEST, e.getLocalizedMessage());
 250                 }
 251 
 252                 target = forest.get(schemaLocation);
 253                 if(target==null) {
 254                     reportError( bindings,
 255                         Messages.format(Messages.ERR_INCORRECT_SCHEMA_REFERENCE,
 256                             schemaLocation,
 257                             EditDistance.findNearest(schemaLocation,forest.listSystemIDs())));
 258 
 259                     return; // abort processing this <jaxb:bindings>
 260                 }
 261 
 262                 target = ((Document)target).getDocumentElement();
 263             }
 264         }
 265 
 266         // look for @node
 267         if( bindings.getAttributeNode("node")!=null ) {
 268             String nodeXPath = bindings.getAttribute("node");
 269 
 270             // evaluate this XPath
 271             NodeList nlst;
 272             try {
 273                 xpath.setNamespaceContext(new NamespaceContextImpl(bindings));
 274                 nlst = (NodeList)xpath.evaluate(nodeXPath,target,XPathConstants.NODESET);
 275             } catch (XPathExpressionException e) {
 276                 if(required) {
 277                     reportError( bindings,
 278                         Messages.format(Messages.ERR_XPATH_EVAL,e.getMessage()), e );
 279                     return; // abort processing this <jaxb:bindings>
 280                 } else {
 281                     return;
 282                 }
 283             }
 284 
 285             if( nlst.getLength()==0 ) {
 286                 if(required)
 287                     reportError( bindings,
 288                         Messages.format(Messages.NO_XPATH_EVAL_TO_NO_TARGET, nodeXPath) );
 289                 return; // abort
 290             }
 291 
 292             if( nlst.getLength()!=1 ) {
 293                 if(!multiple) {
 294                     reportError( bindings,
 295                         Messages.format(Messages.NO_XPATH_EVAL_TOO_MANY_TARGETS, nodeXPath,nlst.getLength()) );
 296 
 297                     return; // abort
 298                 } else {
 299                     if(targetMultiple == null) targetMultiple = new ArrayList<Node>();
 300                     for(int i = 0; i < nlst.getLength(); i++) {
 301                         targetMultiple.add(nlst.item(i));
 302                     }
 303                 }
 304             }
 305 
 306             // check
 307             if(!multiple || nlst.getLength() == 1) {
 308                 Node rnode = nlst.item(0);
 309                 if (!(rnode instanceof Element)) {
 310                     reportError(bindings,
 311                             Messages.format(Messages.NO_XPATH_EVAL_TO_NON_ELEMENT, nodeXPath));
 312                     return; // abort
 313                 }
 314 
 315                 if (!forest.logic.checkIfValidTargetNode(forest, bindings, (Element) rnode)) {
 316                     reportError(bindings,
 317                             Messages.format(Messages.XPATH_EVAL_TO_NON_SCHEMA_ELEMENT,
 318                             nodeXPath, rnode.getNodeName()));
 319                     return; // abort
 320                 }
 321 
 322                 target = rnode;
 323             } else {
 324                 for(Node rnode : targetMultiple) {
 325                     if (!(rnode instanceof Element)) {
 326                         reportError(bindings,
 327                                 Messages.format(Messages.NO_XPATH_EVAL_TO_NON_ELEMENT, nodeXPath));
 328                         return; // abort
 329                     }
 330 
 331                     if (!forest.logic.checkIfValidTargetNode(forest, bindings, (Element) rnode)) {
 332                         reportError(bindings,
 333                                 Messages.format(Messages.XPATH_EVAL_TO_NON_SCHEMA_ELEMENT,
 334                                 nodeXPath, rnode.getNodeName()));
 335                         return; // abort
 336                     }
 337                 }
 338             }
 339         }
 340 
 341         // look for @scd
 342         if( bindings.getAttributeNode("scd")!=null ) {
 343             String scdPath = bindings.getAttribute("scd");
 344             if(!enableSCD) {
 345                 // SCD selector was found, but it's not activated. report an error
 346                 // but recover by handling it anyway. this also avoids repeated error messages.
 347                 reportError(bindings,
 348                     Messages.format(Messages.SCD_NOT_ENABLED));
 349                 enableSCD = true;
 350             }
 351 
 352             try {
 353                 inheritedSCD = scdResult.createNewTarget( inheritedSCD, bindings,
 354                         SCD.create(scdPath, new NamespaceContextImpl(bindings)) );
 355             } catch (ParseException e) {
 356                 reportError( bindings, Messages.format(Messages.ERR_SCD_EVAL,e.getMessage()),e );
 357                 return; // abort processing this bindings
 358             }
 359         }
 360 
 361         // update the result map
 362         if (inheritedSCD != null) {
 363             inheritedSCD.addBinidng(bindings);
 364         } else if (!multiple || targetMultiple == null) {
 365             if (result.get(bindings) == null)
 366                 result.put(bindings, new ArrayList<Node>());
 367             result.get(bindings).add(target);
 368         } else {
 369             for (Node rnode : targetMultiple) {
 370                 if (result.get(bindings) == null)
 371                     result.put(bindings, new ArrayList<Node>());
 372 
 373                 result.get(bindings).add(rnode);
 374             }
 375 
 376         }
 377 
 378 
 379         // look for child <jaxb:bindings> and process them recursively
 380         Element[] children = DOMUtils.getChildElements( bindings, Const.JAXB_NSURI, "bindings" );
 381         for (Element value : children)
 382             if(!multiple || targetMultiple == null)
 383                 buildTargetNodeMap(value, target, inheritedSCD, result, scdResult);
 384             else {
 385                 for(Node rnode : targetMultiple) {
 386                     buildTargetNodeMap(value, rnode, inheritedSCD, result, scdResult);
 387                 }
 388             }
 389     }
 390 
 391     /**
 392      * Moves JAXB customizations under their respective target nodes.
 393      */
 394     private void move(Element bindings, Map<Element, List<Node>> targetNodes) {
 395         List<Node> nodelist = targetNodes.get(bindings);
 396 
 397         if(nodelist == null) {
 398                 return; // abort
 399         }
 400 
 401         for (Node target : nodelist) {
 402             if (target == null) // this must be the result of an error on the external binding.
 403             // recover from the error by ignoring this node
 404             {
 405                 return;
 406             }
 407 
 408             for (Element item : DOMUtils.getChildElements(bindings)) {
 409                 String localName = item.getLocalName();
 410 
 411                 if ("bindings".equals(localName)) {
 412                     // process child <jaxb:bindings> recursively
 413                     move(item, targetNodes);
 414                 } else if ("globalBindings".equals(localName)) {
 415                         // <jaxb:globalBindings> always go to the root of document.
 416                     Element root = forest.getOneDocument().getDocumentElement();
 417                     if (root.getNamespaceURI().equals(WSDL_NS)) {
 418                         NodeList elements = root.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema");
 419                         if ((elements == null) || (elements.getLength() < 1)) {
 420                             reportError(item, Messages.format(Messages.ORPHANED_CUSTOMIZATION, item.getNodeName()));
 421                             return;
 422                         } else {
 423                             moveUnder(item, (Element)elements.item(0));
 424                         }
 425                     } else {
 426                         moveUnder(item, root);
 427                     }
 428                 } else {
 429                     if (!(target instanceof Element)) {
 430                         reportError(item,
 431                                 Messages.format(Messages.CONTEXT_NODE_IS_NOT_ELEMENT));
 432                         return; // abort
 433                     }
 434 
 435                     if (!forest.logic.checkIfValidTargetNode(forest, item, (Element) target)) {
 436                         reportError(item,
 437                                 Messages.format(Messages.ORPHANED_CUSTOMIZATION, item.getNodeName()));
 438                         return; // abort
 439                     }
 440 
 441                     // move this node under the target
 442                     moveUnder(item, (Element) target);
 443                 }
 444             }
 445         }
 446     }
 447 
 448     /**
 449      * Moves the "decl" node under the "target" node.
 450      *
 451      * @param decl
 452      *      A JAXB customization element (e.g., &lt;jaxb:class>)
 453      *
 454      * @param target
 455      *      XML Schema element under which the declaration should move.
 456      *      For example, &lt;xs:element>
 457      */
 458     private void moveUnder( Element decl, Element target ) {
 459         Element realTarget = forest.logic.refineTarget(target);
 460 
 461         declExtensionNamespace( decl, target );
 462 
 463         // copy in-scope namespace declarations of the decl node
 464         // to the decl node itself so that this move won't change
 465         // the in-scope namespace bindings.
 466         Element p = decl;
 467         Set<String> inscopes = new HashSet<String>();
 468         while(true) {
 469             NamedNodeMap atts = p.getAttributes();
 470             for( int i=0; i<atts.getLength(); i++ ) {
 471                 Attr a = (Attr)atts.item(i);
 472                 if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
 473                     String prefix;
 474                     if( a.getName().indexOf(':')==-1 )  prefix = "";
 475                     else                                prefix = a.getLocalName();
 476 
 477                     if( inscopes.add(prefix) && p!=decl ) {
 478                         // if this is the first time we see this namespace bindings,
 479                         // copy the declaration.
 480                         // if p==decl, there's no need to. Note that
 481                         // we want to add prefix to inscopes even if p==Decl
 482 
 483                         decl.setAttributeNodeNS( (Attr)a.cloneNode(true) );
 484                     }
 485                 }
 486             }
 487 
 488             if( p.getParentNode() instanceof Document )
 489                 break;
 490 
 491             p = (Element)p.getParentNode();
 492         }
 493 
 494         if( !inscopes.contains("") ) {
 495             // if the default namespace was undeclared in the context of decl,
 496             // it must be explicitly set to "" since the new environment might
 497             // have a different default namespace URI.
 498             decl.setAttributeNS(Const.XMLNS_URI,"xmlns","");
 499         }
 500 
 501 
 502         // finally move the declaration to the target node.
 503         if( realTarget.getOwnerDocument()!=decl.getOwnerDocument() ) {
 504             // if they belong to different DOM documents, we need to clone them
 505             Element original = decl;
 506             decl = (Element)realTarget.getOwnerDocument().importNode(decl,true);
 507 
 508             // this effectively clones a ndoe,, so we need to copy locators.
 509             copyLocators( original, decl );
 510         }
 511 
 512         realTarget.appendChild( decl );
 513     }
 514 
 515     /**
 516      * Recursively visits sub-elements and declare all used namespaces.
 517      * TODO: the fact that we recognize all namespaces in the extension
 518      * is a bad design.
 519      */
 520     private void declExtensionNamespace(Element decl, Element target) {
 521         // if this comes from external namespaces, add the namespace to
 522         // @extensionBindingPrefixes.
 523         if( !Const.JAXB_NSURI.equals(decl.getNamespaceURI()) )
 524             declareExtensionNamespace( target, decl.getNamespaceURI() );
 525 
 526         NodeList lst = decl.getChildNodes();
 527         for( int i=0; i<lst.getLength(); i++ ) {
 528             Node n = lst.item(i);
 529             if( n instanceof Element )
 530                 declExtensionNamespace( (Element)n, target );
 531         }
 532     }
 533 
 534 
 535     /** Attribute name. */
 536     private static final String EXTENSION_PREFIXES = "extensionBindingPrefixes";
 537 
 538     /**
 539      * Adds the specified namespace URI to the jaxb:extensionBindingPrefixes
 540      * attribute of the target document.
 541      */
 542     private void declareExtensionNamespace( Element target, String nsUri ) {
 543         // look for the attribute
 544         Element root = target.getOwnerDocument().getDocumentElement();
 545         Attr att = root.getAttributeNodeNS(Const.JAXB_NSURI,EXTENSION_PREFIXES);
 546         if( att==null ) {
 547             String jaxbPrefix = allocatePrefix(root,Const.JAXB_NSURI);
 548             // no such attribute. Create one.
 549             att = target.getOwnerDocument().createAttributeNS(
 550                 Const.JAXB_NSURI,jaxbPrefix+':'+EXTENSION_PREFIXES);
 551             root.setAttributeNodeNS(att);
 552         }
 553 
 554         String prefix = allocatePrefix(root,nsUri);
 555         if( att.getValue().indexOf(prefix)==-1 )
 556             // avoid redeclaring the same namespace twice.
 557             att.setValue( att.getValue()+' '+prefix);
 558     }
 559 
 560     /**
 561      * Declares a new prefix on the given element and associates it
 562      * with the specified namespace URI.
 563      * <p>
 564      * Note that this method doesn't use the default namespace
 565      * even if it can.
 566      */
 567     private String allocatePrefix( Element e, String nsUri ) {
 568         // look for existing namespaces.
 569         NamedNodeMap atts = e.getAttributes();
 570         for( int i=0; i<atts.getLength(); i++ ) {
 571             Attr a = (Attr)atts.item(i);
 572             if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
 573                 if( a.getName().indexOf(':')==-1 )  continue;
 574 
 575                 if( a.getValue().equals(nsUri) )
 576                     return a.getLocalName();    // found one
 577             }
 578         }
 579 
 580         // none found. allocate new.
 581         while(true) {
 582             String prefix = "p"+(int)(Math.random()*1000000)+'_';
 583             if(e.getAttributeNodeNS(Const.XMLNS_URI,prefix)!=null)
 584                 continue;   // this prefix is already allocated.
 585 
 586             e.setAttributeNS(Const.XMLNS_URI,"xmlns:"+prefix,nsUri);
 587             return prefix;
 588         }
 589     }
 590 
 591 
 592     /**
 593      * Copies location information attached to the "src" node to the "dst" node.
 594      */
 595     private void copyLocators( Element src, Element dst ) {
 596         forest.locatorTable.storeStartLocation(
 597             dst, forest.locatorTable.getStartLocation(src) );
 598         forest.locatorTable.storeEndLocation(
 599             dst, forest.locatorTable.getEndLocation(src) );
 600 
 601         // recursively process child elements
 602         Element[] srcChilds = DOMUtils.getChildElements(src);
 603         Element[] dstChilds = DOMUtils.getChildElements(dst);
 604 
 605         for( int i=0; i<srcChilds.length; i++ )
 606             copyLocators( srcChilds[i], dstChilds[i] );
 607     }
 608 
 609 
 610     private void reportError( Element errorSource, String formattedMsg ) {
 611         reportError( errorSource, formattedMsg, null );
 612     }
 613 
 614     private void reportError( Element errorSource,
 615         String formattedMsg, Exception nestedException ) {
 616 
 617         SAXParseException e = new SAXParseException2( formattedMsg,
 618             forest.locatorTable.getStartLocation(errorSource),
 619             nestedException );
 620         errorHandler.error(e);
 621     }
 622 }