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.addon.episode;
038:
039: import java.io.File;
040: import java.io.FileOutputStream;
041: import java.io.IOException;
042: import java.io.OutputStream;
043: import java.util.ArrayList;
044: import java.util.HashMap;
045: import java.util.List;
046: import java.util.Map;
047:
048: import com.sun.tools.xjc.BadCommandLineException;
049: import com.sun.tools.xjc.Options;
050: import com.sun.tools.xjc.Plugin;
051: import com.sun.tools.xjc.outline.ClassOutline;
052: import com.sun.tools.xjc.outline.Outline;
053: import com.sun.tools.xjc.reader.Const;
054: import com.sun.xml.txw2.TXW;
055: import com.sun.xml.txw2.output.StreamSerializer;
056: import com.sun.xml.xsom.XSAnnotation;
057: import com.sun.xml.xsom.XSAttGroupDecl;
058: import com.sun.xml.xsom.XSAttributeDecl;
059: import com.sun.xml.xsom.XSAttributeUse;
060: import com.sun.xml.xsom.XSComplexType;
061: import com.sun.xml.xsom.XSComponent;
062: import com.sun.xml.xsom.XSContentType;
063: import com.sun.xml.xsom.XSDeclaration;
064: import com.sun.xml.xsom.XSElementDecl;
065: import com.sun.xml.xsom.XSFacet;
066: import com.sun.xml.xsom.XSIdentityConstraint;
067: import com.sun.xml.xsom.XSModelGroup;
068: import com.sun.xml.xsom.XSModelGroupDecl;
069: import com.sun.xml.xsom.XSNotation;
070: import com.sun.xml.xsom.XSParticle;
071: import com.sun.xml.xsom.XSSchema;
072: import com.sun.xml.xsom.XSSimpleType;
073: import com.sun.xml.xsom.XSWildcard;
074: import com.sun.xml.xsom.XSXPath;
075: import com.sun.xml.xsom.visitor.XSFunction;
076: import com.sun.xml.bind.v2.schemagen.episode.Bindings;
077:
078: import org.xml.sax.ErrorHandler;
079: import org.xml.sax.SAXException;
080: import org.xml.sax.SAXParseException;
081:
082: /**
083: * Creates the episode file,
084: *
085: * @author Kohsuke Kawaguchi
086: */
087: public class PluginImpl extends Plugin {
088:
089: private File episodeFile;
090:
091: public String getOptionName() {
092: return "episode";
093: }
094:
095: public String getUsage() {
096: return " -episode <FILE> : generate the episode file for separate compilation";
097: }
098:
099: public int parseArgument(Options opt, String[] args, int i)
100: throws BadCommandLineException, IOException {
101: if (args[i].equals("-episode")) {
102: episodeFile = new File(opt.requireArgument("-episode",
103: args, ++i));
104: return 2;
105: }
106: return 0;
107: }
108:
109: /**
110: * Capture all the generated classes from global schema components
111: * and generate them in an episode file.
112: */
113: public boolean run(Outline model, Options opt,
114: ErrorHandler errorHandler) throws SAXException {
115: try {
116: // reorganize qualifying components by their namespaces to
117: // generate the list nicely
118: Map<XSSchema, List<ClassOutline>> perSchema = new HashMap<XSSchema, List<ClassOutline>>();
119: boolean hasComponentInNoNamespace = false;
120:
121: for (ClassOutline co : model.getClasses()) {
122: XSComponent sc = co.target.getSchemaComponent();
123: if (sc == null)
124: continue;
125: if (!(sc instanceof XSDeclaration))
126: continue;
127: XSDeclaration decl = (XSDeclaration) sc;
128: if (decl.isLocal())
129: continue; // local components cannot be referenced from outside, so no need to list.
130:
131: List<ClassOutline> list = perSchema.get(decl
132: .getOwnerSchema());
133: if (list == null) {
134: list = new ArrayList<ClassOutline>();
135: perSchema.put(decl.getOwnerSchema(), list);
136: }
137:
138: list.add(co);
139:
140: if (decl.getTargetNamespace().equals(""))
141: hasComponentInNoNamespace = true;
142: }
143:
144: OutputStream os = new FileOutputStream(episodeFile);
145: Bindings bindings = TXW.create(Bindings.class,
146: new StreamSerializer(os, "UTF-8"));
147: if (hasComponentInNoNamespace) // otherwise jaxb binding NS should be the default namespace
148: bindings._namespace(Const.JAXB_NSURI, "jaxb");
149: else
150: bindings._namespace(Const.JAXB_NSURI, "");
151: bindings.version("2.1");
152: bindings._comment("\n\n" + opt.getPrologComment() + "\n ");
153:
154: // generate listing per schema
155: for (Map.Entry<XSSchema, List<ClassOutline>> e : perSchema
156: .entrySet()) {
157: Bindings group = bindings.bindings();
158: String tns = e.getKey().getTargetNamespace();
159: if (!tns.equals(""))
160: group._namespace(tns, "tns");
161:
162: group.scd("x-schema::" + (tns.equals("") ? "" : "tns"));
163: group.schemaBindings().map(false);
164:
165: for (ClassOutline co : e.getValue()) {
166: Bindings child = group.bindings();
167: child
168: .scd(co.target.getSchemaComponent().apply(
169: SCD));
170: child.klass().ref(co.implClass.fullName());
171: }
172: group.commit(true);
173: }
174:
175: bindings.commit();
176:
177: return true;
178: } catch (IOException e) {
179: errorHandler.error(new SAXParseException(
180: "Failed to write to " + episodeFile, null, e));
181: return false;
182: }
183: }
184:
185: /**
186: * Computes SCD.
187: * This is fairly limited as JAXB can only map a certain kind of components to classes.
188: */
189: private static final XSFunction<String> SCD = new XSFunction<String>() {
190: private String name(XSDeclaration decl) {
191: if (decl.getTargetNamespace().equals(""))
192: return decl.getName();
193: else
194: return "tns:" + decl.getName();
195: }
196:
197: public String complexType(XSComplexType type) {
198: return "~" + name(type);
199: }
200:
201: public String simpleType(XSSimpleType simpleType) {
202: return "~" + name(simpleType);
203: }
204:
205: public String elementDecl(XSElementDecl decl) {
206: return name(decl);
207: }
208:
209: // the rest is doing nothing
210: public String annotation(XSAnnotation ann) {
211: throw new UnsupportedOperationException();
212: }
213:
214: public String attGroupDecl(XSAttGroupDecl decl) {
215: throw new UnsupportedOperationException();
216: }
217:
218: public String attributeDecl(XSAttributeDecl decl) {
219: throw new UnsupportedOperationException();
220: }
221:
222: public String attributeUse(XSAttributeUse use) {
223: throw new UnsupportedOperationException();
224: }
225:
226: public String schema(XSSchema schema) {
227: throw new UnsupportedOperationException();
228: }
229:
230: public String facet(XSFacet facet) {
231: throw new UnsupportedOperationException();
232: }
233:
234: public String notation(XSNotation notation) {
235: throw new UnsupportedOperationException();
236: }
237:
238: public String identityConstraint(XSIdentityConstraint decl) {
239: throw new UnsupportedOperationException();
240: }
241:
242: public String xpath(XSXPath xpath) {
243: throw new UnsupportedOperationException();
244: }
245:
246: public String particle(XSParticle particle) {
247: throw new UnsupportedOperationException();
248: }
249:
250: public String empty(XSContentType empty) {
251: throw new UnsupportedOperationException();
252: }
253:
254: public String wildcard(XSWildcard wc) {
255: throw new UnsupportedOperationException();
256: }
257:
258: public String modelGroupDecl(XSModelGroupDecl decl) {
259: throw new UnsupportedOperationException();
260: }
261:
262: public String modelGroup(XSModelGroup group) {
263: throw new UnsupportedOperationException();
264: }
265: };
266: }
|