< prev index next >

src/jdk.xml.bind/share/classes/com/sun/tools/internal/xjc/reader/internalizer/Internalizer.java

Print this page


   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


  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 


 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


 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 


   1 /*
   2  * Copyright (c) 1997, 2015, 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


  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 {@code <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 


 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      * Determines the target node of the "bindings" element
 146      * by using the inherited target node, then put
 147      * the result into the "result" map and the "scd" map.
 148      *
 149      * @param inheritedTarget
 150      *      The current target node. This always exists, even if
 151      *      the user starts specifying targets via SCD (in that case
 152      *      this inherited target is just not going to be used.)
 153      * @param inheritedSCD
 154      *      If the ancestor {@code <bindings>} node specifies @scd to
 155      *      specify the target via SCD, then this parameter represents that context.
 156      */
 157     private void buildTargetNodeMap( Element bindings, @NotNull Node inheritedTarget,
 158                                      @Nullable SCDBasedBindingSet.Target inheritedSCD,
 159                                      Map<Element,List<Node>> result, SCDBasedBindingSet scdResult ) {
 160         // start by the inherited target
 161         Node target = inheritedTarget;
 162         ArrayList<Node> targetMultiple = null;
 163 
 164         // validate this node ?
 165         // validate(bindings);
 166 
 167         boolean required = true;
 168         boolean multiple = false;
 169 
 170         if(bindings.getAttribute("required") != null) {
 171             String requiredAttr = bindings.getAttribute("required");
 172 
 173             if(requiredAttr.equals("no") || requiredAttr.equals("false") || requiredAttr.equals("0"))
 174                 required = false;
 175         }
 176 
 177         if(bindings.getAttribute("multiple") != null) {
 178             String requiredAttr = bindings.getAttribute("multiple");
 179 
 180             if(requiredAttr.equals("yes") || requiredAttr.equals("true") || requiredAttr.equals("1"))
 181                 multiple = true;
 182         }
 183 
 184 
 185         // look for @schemaLocation


 399                         return; // abort
 400                     }
 401 
 402                     if (!forest.logic.checkIfValidTargetNode(forest, item, (Element) target)) {
 403                         reportError(item,
 404                                 Messages.format(Messages.ORPHANED_CUSTOMIZATION, item.getNodeName()));
 405                         return; // abort
 406                     }
 407 
 408                     // move this node under the target
 409                     moveUnder(item, (Element) target);
 410                 }
 411             }
 412         }
 413     }
 414 
 415     /**
 416      * Moves the "decl" node under the "target" node.
 417      *
 418      * @param decl
 419      *      A JAXB customization element (e.g., {@code <jaxb:class>})
 420      *
 421      * @param target
 422      *      XML Schema element under which the declaration should move.
 423      *      For example, {@code <xs:element>}
 424      */
 425     private void moveUnder( Element decl, Element target ) {
 426         Element realTarget = forest.logic.refineTarget(target);
 427 
 428         declExtensionNamespace( decl, target );
 429 
 430         // copy in-scope namespace declarations of the decl node
 431         // to the decl node itself so that this move won't change
 432         // the in-scope namespace bindings.
 433         Element p = decl;
 434         Set<String> inscopes = new HashSet<String>();
 435         while(true) {
 436             NamedNodeMap atts = p.getAttributes();
 437             for( int i=0; i<atts.getLength(); i++ ) {
 438                 Attr a = (Attr)atts.item(i);
 439                 if( Const.XMLNS_URI.equals(a.getNamespaceURI()) ) {
 440                     String prefix;
 441                     if( a.getName().indexOf(':')==-1 )  prefix = "";
 442                     else                                prefix = a.getLocalName();
 443 


< prev index next >