001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.reader.internalizer;
038:
039: import java.util.ArrayList;
040: import java.util.Collection;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import javax.xml.bind.JAXBException;
045: import javax.xml.bind.UnmarshallerHandler;
046: import javax.xml.validation.ValidatorHandler;
047:
048: import com.sun.istack.NotNull;
049: import com.sun.istack.SAXParseException2;
050: import com.sun.tools.xjc.ErrorReceiver;
051: import com.sun.tools.xjc.reader.xmlschema.bindinfo.BIDeclaration;
052: import com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo;
053: import com.sun.tools.xjc.util.ForkContentHandler;
054: import com.sun.tools.xjc.util.DOMUtils;
055: import com.sun.xml.xsom.SCD;
056: import com.sun.xml.xsom.XSAnnotation;
057: import com.sun.xml.xsom.XSComponent;
058: import com.sun.xml.xsom.XSSchemaSet;
059: import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl;
060:
061: import org.w3c.dom.Element;
062: import org.xml.sax.SAXException;
063: import org.xml.sax.SAXParseException;
064:
065: /**
066: * Set of binding nodes that have target nodes specified via SCD.
067: *
068: * This is parsed during {@link Internalizer} works on the tree,
069: * but applying this has to wait for {@link XSSchemaSet} to be parsed.
070: *
071: * @author Kohsuke Kawaguchi
072: * @see SCD
073: */
074: public final class SCDBasedBindingSet {
075:
076: /**
077: * Represents the target schema component of the
078: * customization identified by SCD.
079: *
080: * @author Kohsuke Kawaguchi
081: */
082: final class Target {
083: /**
084: * SCDs can be specified via multiple steps, like:
085: *
086: * <xmp>
087: * <bindings scd="foo/bar">
088: * <bindings scd="zot/xyz">
089: * </xmp>
090: *
091: * This field and {@link #nextSibling} form a single-linked list that
092: * represent the children that shall be evaluated within this target.
093: * Think of it as {@code List<Target>}.
094: */
095: private Target firstChild;
096: private final Target nextSibling;
097:
098: /**
099: * Compiled SCD.
100: */
101: private final @NotNull
102: SCD scd;
103:
104: /**
105: * The element on which SCD was found.
106: */
107: private final @NotNull
108: Element src;
109:
110: /**
111: * Bindings that apply to this SCD.
112: */
113: private final List<Element> bindings = new ArrayList<Element>();
114:
115: private Target(Target parent, Element src, SCD scd) {
116: if (parent == null) {
117: this .nextSibling = topLevel;
118: topLevel = this ;
119: } else {
120: this .nextSibling = parent.firstChild;
121: parent.firstChild = this ;
122: }
123: this .src = src;
124: this .scd = scd;
125: }
126:
127: /**
128: * Adds a new binding declaration to be associated to the schema component
129: * identified by {@link #scd}.
130: */
131: void addBinidng(Element binding) {
132: bindings.add(binding);
133: }
134:
135: /**
136: * Applies bindings to the schema component for this and its siblings.
137: */
138: private void applyAll(
139: Collection<? extends XSComponent> contextNode) {
140: for (Target self = this ; self != null; self = self.nextSibling)
141: self.apply(contextNode);
142: }
143:
144: /**
145: * Applies bindings to the schema component for just this node.
146: */
147: private void apply(Collection<? extends XSComponent> contextNode) {
148: // apply the SCD...
149: Collection<XSComponent> childNodes = scd
150: .select(contextNode);
151: if (childNodes.isEmpty()) {
152: // no node matched
153: if (src.getAttributeNode("if-exists") != null) {
154: // if this attribute exists, it's not an error if SCD didn't match.
155: return;
156: }
157:
158: reportError(src, Messages.format(
159: Messages.ERR_SCD_EVALUATED_EMPTY, scd));
160: return;
161: }
162:
163: if (firstChild != null)
164: firstChild.applyAll(childNodes);
165:
166: if (!bindings.isEmpty()) {
167: // error to match more than one components
168: Iterator<XSComponent> itr = childNodes.iterator();
169: XSComponent target = itr.next();
170: if (itr.hasNext()) {
171: reportError(src, Messages.format(
172: Messages.ERR_SCD_MATCHED_MULTIPLE_NODES,
173: scd, childNodes.size()));
174: errorReceiver
175: .error(
176: target.getLocator(),
177: Messages
178: .format(Messages.ERR_SCD_MATCHED_MULTIPLE_NODES_FIRST));
179: errorReceiver
180: .error(
181: itr.next().getLocator(),
182: Messages
183: .format(Messages.ERR_SCD_MATCHED_MULTIPLE_NODES_SECOND));
184: }
185:
186: // apply bindings to the target
187: for (Element binding : bindings) {
188: for (Element item : DOMUtils
189: .getChildElements(binding)) {
190: String localName = item.getLocalName();
191:
192: if ("bindings".equals(localName))
193: continue; // this should be already in Target.bindings of some SpecVersion.
194:
195: try {
196: new DOMForestScanner(forest).scan(item,
197: loader);
198: BIDeclaration decl = (BIDeclaration) unmarshaller
199: .getResult();
200:
201: // add this binding to the target
202: XSAnnotation ann = target
203: .getAnnotation(true);
204: BindInfo bi = (BindInfo) ann
205: .getAnnotation();
206: if (bi == null) {
207: bi = new BindInfo();
208: ann.setAnnotation(bi);
209: }
210: bi.addDecl(decl);
211: } catch (SAXException e) {
212: // the error should have already been reported.
213: } catch (JAXBException e) {
214: // if validation didn't fail, then unmarshalling can't go wrong
215: throw new AssertionError(e);
216: }
217: }
218: }
219: }
220: }
221: }
222:
223: private Target topLevel;
224:
225: /**
226: * The forest where binding elements came from. Needed to report line numbers for errors.
227: */
228: private final DOMForest forest;
229:
230: // variables used only during the apply method
231: //
232: private ErrorReceiver errorReceiver;
233: private UnmarshallerHandler unmarshaller;
234: private ForkContentHandler loader; // unmarshaller+validator
235:
236: SCDBasedBindingSet(DOMForest forest) {
237: this .forest = forest;
238: }
239:
240: Target createNewTarget(Target parent, Element src, SCD scd) {
241: return new Target(parent, src, scd);
242: }
243:
244: /**
245: * Applies the additional binding customizations.
246: */
247: public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
248: if (topLevel != null) {
249: this .errorReceiver = errorReceiver;
250: UnmarshallerImpl u = BindInfo.getJAXBContext()
251: .createUnmarshaller();
252: this .unmarshaller = u.getUnmarshallerHandler();
253: ValidatorHandler v = BindInfo.bindingFileSchema
254: .newValidator();
255: v.setErrorHandler(errorReceiver);
256: loader = new ForkContentHandler(v, unmarshaller);
257:
258: topLevel.applyAll(schema.getSchemas());
259:
260: this .loader = null;
261: this .unmarshaller = null;
262: this .errorReceiver = null;
263: }
264: }
265:
266: private void reportError(Element errorSource, String formattedMsg) {
267: reportError(errorSource, formattedMsg, null);
268: }
269:
270: private void reportError(Element errorSource, String formattedMsg,
271: Exception nestedException) {
272:
273: SAXParseException e = new SAXParseException2(formattedMsg,
274: forest.locatorTable.getStartLocation(errorSource),
275: nestedException);
276: errorReceiver.error(e);
277: }
278: }
|