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/SCDBasedBindingSet.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,246 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, 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.util.ArrayList; 1.32 +import java.util.Collection; 1.33 +import java.util.Iterator; 1.34 +import java.util.List; 1.35 + 1.36 +import javax.xml.bind.JAXBException; 1.37 +import javax.xml.bind.Unmarshaller; 1.38 +import javax.xml.bind.UnmarshallerHandler; 1.39 +import javax.xml.validation.ValidatorHandler; 1.40 + 1.41 +import com.sun.istack.internal.NotNull; 1.42 +import com.sun.istack.internal.SAXParseException2; 1.43 +import com.sun.tools.internal.xjc.ErrorReceiver; 1.44 +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIDeclaration; 1.45 +import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BindInfo; 1.46 +import com.sun.tools.internal.xjc.util.ForkContentHandler; 1.47 +import com.sun.tools.internal.xjc.util.DOMUtils; 1.48 +import com.sun.xml.internal.xsom.SCD; 1.49 +import com.sun.xml.internal.xsom.XSAnnotation; 1.50 +import com.sun.xml.internal.xsom.XSComponent; 1.51 +import com.sun.xml.internal.xsom.XSSchemaSet; 1.52 + 1.53 +import org.w3c.dom.Element; 1.54 +import org.xml.sax.SAXException; 1.55 +import org.xml.sax.SAXParseException; 1.56 + 1.57 +/** 1.58 + * Set of binding nodes that have target nodes specified via SCD. 1.59 + * 1.60 + * This is parsed during {@link Internalizer} works on the tree, 1.61 + * but applying this has to wait for {@link XSSchemaSet} to be parsed. 1.62 + * 1.63 + * @author Kohsuke Kawaguchi 1.64 + * @see SCD 1.65 + */ 1.66 +public final class SCDBasedBindingSet { 1.67 + 1.68 + /** 1.69 + * Represents the target schema component of the 1.70 + * customization identified by SCD. 1.71 + * 1.72 + * @author Kohsuke Kawaguchi 1.73 + */ 1.74 + final class Target { 1.75 + /** 1.76 + * SCDs can be specified via multiple steps, like: 1.77 + * 1.78 + * <xmp> 1.79 + * <bindings scd="foo/bar"> 1.80 + * <bindings scd="zot/xyz"> 1.81 + * </xmp> 1.82 + * 1.83 + * This field and {@link #nextSibling} form a single-linked list that 1.84 + * represent the children that shall be evaluated within this target. 1.85 + * Think of it as {@code List<Target>}. 1.86 + */ 1.87 + private Target firstChild; 1.88 + private final Target nextSibling; 1.89 + 1.90 + /** 1.91 + * Compiled SCD. 1.92 + */ 1.93 + private final @NotNull SCD scd; 1.94 + 1.95 + /** 1.96 + * The element on which SCD was found. 1.97 + */ 1.98 + private final @NotNull Element src; 1.99 + 1.100 + /** 1.101 + * Bindings that apply to this SCD. 1.102 + */ 1.103 + private final List<Element> bindings = new ArrayList<Element>(); 1.104 + 1.105 + private Target(Target parent, Element src, SCD scd) { 1.106 + if(parent==null) { 1.107 + this.nextSibling = topLevel; 1.108 + topLevel = this; 1.109 + } else { 1.110 + this.nextSibling = parent.firstChild; 1.111 + parent.firstChild = this; 1.112 + } 1.113 + this.src = src; 1.114 + this.scd = scd; 1.115 + } 1.116 + 1.117 + /** 1.118 + * Adds a new binding declaration to be associated to the schema component 1.119 + * identified by {@link #scd}. 1.120 + */ 1.121 + void addBinidng(Element binding) { 1.122 + bindings.add(binding); 1.123 + } 1.124 + 1.125 + /** 1.126 + * Applies bindings to the schema component for this and its siblings. 1.127 + */ 1.128 + private void applyAll(Collection<? extends XSComponent> contextNode) { 1.129 + for( Target self=this; self!=null; self=self.nextSibling ) 1.130 + self.apply(contextNode); 1.131 + } 1.132 + 1.133 + /** 1.134 + * Applies bindings to the schema component for just this node. 1.135 + */ 1.136 + private void apply(Collection<? extends XSComponent> contextNode) { 1.137 + // apply the SCD... 1.138 + Collection<XSComponent> childNodes = scd.select(contextNode); 1.139 + if(childNodes.isEmpty()) { 1.140 + // no node matched 1.141 + if(src.getAttributeNode("if-exists")!=null) { 1.142 + // if this attribute exists, it's not an error if SCD didn't match. 1.143 + return; 1.144 + } 1.145 + 1.146 + reportError( src, Messages.format(Messages.ERR_SCD_EVALUATED_EMPTY,scd) ); 1.147 + return; 1.148 + } 1.149 + 1.150 + if(firstChild!=null) 1.151 + firstChild.applyAll(childNodes); 1.152 + 1.153 + if(!bindings.isEmpty()) { 1.154 + // error to match more than one components 1.155 + Iterator<XSComponent> itr = childNodes.iterator(); 1.156 + XSComponent target = itr.next(); 1.157 + if(itr.hasNext()) { 1.158 + reportError( src, Messages.format(Messages.ERR_SCD_MATCHED_MULTIPLE_NODES,scd,childNodes.size()) ); 1.159 + errorReceiver.error( target.getLocator(), Messages.format(Messages.ERR_SCD_MATCHED_MULTIPLE_NODES_FIRST) ); 1.160 + errorReceiver.error( itr.next().getLocator(), Messages.format(Messages.ERR_SCD_MATCHED_MULTIPLE_NODES_SECOND) ); 1.161 + } 1.162 + 1.163 + // apply bindings to the target 1.164 + for (Element binding : bindings) { 1.165 + for (Element item : DOMUtils.getChildElements(binding)) { 1.166 + String localName = item.getLocalName(); 1.167 + 1.168 + if ("bindings".equals(localName)) 1.169 + continue; // this should be already in Target.bindings of some SpecVersion. 1.170 + 1.171 + try { 1.172 + new DOMForestScanner(forest).scan(item,loader); 1.173 + BIDeclaration decl = (BIDeclaration)unmarshaller.getResult(); 1.174 + 1.175 + // add this binding to the target 1.176 + XSAnnotation ann = target.getAnnotation(true); 1.177 + BindInfo bi = (BindInfo)ann.getAnnotation(); 1.178 + if(bi==null) { 1.179 + bi = new BindInfo(); 1.180 + ann.setAnnotation(bi); 1.181 + } 1.182 + bi.addDecl(decl); 1.183 + } catch (SAXException e) { 1.184 + // the error should have already been reported. 1.185 + } catch (JAXBException e) { 1.186 + // if validation didn't fail, then unmarshalling can't go wrong 1.187 + throw new AssertionError(e); 1.188 + } 1.189 + } 1.190 + } 1.191 + } 1.192 + } 1.193 + } 1.194 + 1.195 + private Target topLevel; 1.196 + 1.197 + /** 1.198 + * The forest where binding elements came from. Needed to report line numbers for errors. 1.199 + */ 1.200 + private final DOMForest forest; 1.201 + 1.202 + 1.203 + // variables used only during the apply method 1.204 + // 1.205 + private ErrorReceiver errorReceiver; 1.206 + private UnmarshallerHandler unmarshaller; 1.207 + private ForkContentHandler loader; // unmarshaller+validator 1.208 + 1.209 + SCDBasedBindingSet(DOMForest forest) { 1.210 + this.forest = forest; 1.211 + } 1.212 + 1.213 + Target createNewTarget(Target parent, Element src, SCD scd) { 1.214 + return new Target(parent,src,scd); 1.215 + } 1.216 + 1.217 + /** 1.218 + * Applies the additional binding customizations. 1.219 + */ 1.220 + public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) { 1.221 + if(topLevel!=null) { 1.222 + this.errorReceiver = errorReceiver; 1.223 + Unmarshaller u = BindInfo.getCustomizationUnmarshaller(); 1.224 + this.unmarshaller = u.getUnmarshallerHandler(); 1.225 + ValidatorHandler v = BindInfo.bindingFileSchema.newValidator(); 1.226 + v.setErrorHandler(errorReceiver); 1.227 + loader = new ForkContentHandler(v,unmarshaller); 1.228 + 1.229 + topLevel.applyAll(schema.getSchemas()); 1.230 + 1.231 + this.loader = null; 1.232 + this.unmarshaller = null; 1.233 + this.errorReceiver = null; 1.234 + } 1.235 + } 1.236 + 1.237 + private void reportError( Element errorSource, String formattedMsg ) { 1.238 + reportError( errorSource, formattedMsg, null ); 1.239 + } 1.240 + 1.241 + private void reportError( Element errorSource, 1.242 + String formattedMsg, Exception nestedException ) { 1.243 + 1.244 + SAXParseException e = new SAXParseException2( formattedMsg, 1.245 + forest.locatorTable.getStartLocation(errorSource), 1.246 + nestedException ); 1.247 + errorReceiver.error(e); 1.248 + } 1.249 +}