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