001: /*
002: Copyright (c) 2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.ws.wsdl;
030:
031: import java.io.FileInputStream;
032: import java.io.IOException;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037:
038: import org.jibx.binding.generator.CustomizationCommandLineBase;
039: import org.jibx.binding.generator.GlobalCustom;
040: import org.jibx.binding.model.IClassLocator;
041: import org.jibx.runtime.BindingDirectory;
042: import org.jibx.runtime.IBindingFactory;
043: import org.jibx.runtime.IUnmarshallable;
044: import org.jibx.runtime.IUnmarshallingContext;
045: import org.jibx.runtime.JiBXException;
046:
047: /**
048: * Command line processing specifically for the {@link Jibx2Wsdl} class.
049: */
050: public class WsdlGeneratorCommandLine extends
051: CustomizationCommandLineBase {
052: /** Ordered array of extra usage lines. */
053: protected static final String[] EXTRA_USAGE_LINES = new String[] {
054: " -b generated root binding name (default is 'binding.xml')",
055: // " -d use pure doc/lit (not wrapped) style"
056: " -x class,... names of extra classes to be included in binding" };
057:
058: /** Class locator used to complete customizations. */
059: private IClassLocator m_locator;
060:
061: /** Global customizations model root. */
062: private GlobalCustom m_global;
063:
064: /** WSDL customizations model root. */
065: private WsdlCustom m_wsdlCustom;
066:
067: /** List of extra classes for binding. */
068: private List m_extraTypes = new ArrayList();
069:
070: /** Name used for root binding. */
071: private String m_bindingName = "binding.xml";
072:
073: /**
074: * Get class locator.
075: *
076: * @return locator
077: */
078: public IClassLocator getLocator() {
079: return m_locator;
080: }
081:
082: /**
083: * Get customizations model root.
084: *
085: * @return customizations
086: */
087: public GlobalCustom getGlobal() {
088: return m_global;
089: }
090:
091: /**
092: * Get WSDL customizations model root.
093: *
094: * @return WSDL customizations
095: */
096: public WsdlCustom getWsdlCustom() {
097: return m_wsdlCustom;
098: }
099:
100: /**
101: * Get binding name.
102: *
103: * @return name
104: */
105: public String getBindingName() {
106: return m_bindingName;
107: }
108:
109: /**
110: * Get extra classes to be included in binding.
111: *
112: * @return list
113: */
114: public List getExtraTypes() {
115: return m_extraTypes;
116: }
117:
118: /* (non-Javadoc)
119: * @see org.jibx.binding.generator.CustomizationCommandLineBase#checkParameter(org.jibx.binding.generator.CustomizationCommandLineBase.ArgList)
120: */
121: protected boolean checkParameter(ArgList alist) {
122: String arg = alist.current();
123: boolean match = true;
124: if ("-b".equalsIgnoreCase(arg)) {
125: m_bindingName = alist.next();
126: } else if ("-x".equalsIgnoreCase(arg)) {
127: String text = alist.next();
128: if (text != null) {
129: int split;
130: int base = 0;
131: while ((split = text.indexOf(',', base)) > 0) {
132: m_extraTypes.add(text.substring(base, split));
133: base = split + 1;
134: }
135: m_extraTypes.add(text.substring(base));
136: }
137: } else {
138: match = false;
139: }
140: return match;
141: }
142:
143: /* (non-Javadoc)
144: * @see org.jibx.binding.generator.CustomizationCommandLineBase#verboseDetails
145: */
146: protected void verboseDetails() {
147: System.out.println("Starting from service classes:");
148: List types = getExtraArgs();
149: for (int i = 0; i < types.size(); i++) {
150: System.out.println(" " + types.get(i));
151: }
152: }
153:
154: /* (non-Javadoc)
155: * @see org.jibx.binding.generator.CustomizationCommandLineBase#loadCustomizations(String,IClassLocator)
156: */
157: protected void loadCustomizations(String path, IClassLocator loc)
158: throws JiBXException, IOException {
159:
160: // load or create customization information
161: m_global = new GlobalCustom(loc);
162: if (path == null) {
163: m_global.setAddConstructors(true);
164: m_global.setForceClasses(true);
165: m_global.setMapAbstract(Boolean.TRUE);
166: } else {
167: IBindingFactory fact = BindingDirectory
168: .getFactory(WsdlCustom.class);
169: IUnmarshallingContext ictx = fact
170: .createUnmarshallingContext();
171: FileInputStream is = new FileInputStream(path);
172: ictx.setDocument(is, null);
173: ((IUnmarshallable) m_global).unmarshal(ictx);
174: }
175:
176: // find or build WSDL customization
177: WsdlCustom custom = null;
178: List extens = m_global.getExtensionChildren();
179: for (Iterator iter = extens.iterator(); iter.hasNext();) {
180: Object exten = (Object) iter.next();
181: if (exten instanceof WsdlCustom) {
182: custom = (WsdlCustom) exten;
183: break;
184: }
185: }
186: if (custom == null) {
187: custom = new WsdlCustom(m_global);
188: m_global.addExtensionChild(custom);
189: }
190: m_wsdlCustom = custom;
191: m_locator = loc;
192: }
193:
194: /* (non-Javadoc)
195: * @see org.jibx.binding.generator.CustomizationCommandLineBase#applyOverrides(Map)
196: */
197: protected Map applyOverrides(Map overmap) {
198: Map unknowns = applyKeyValueMap(overmap, m_global);
199: unknowns = applyKeyValueMap(unknowns, m_wsdlCustom);
200: m_global.fillClasses();
201: return unknowns;
202: }
203:
204: /* (non-Javadoc)
205: * @see org.jibx.binding.generator.CustomizationCommandLineBase#printUsage()
206: */
207: public void printUsage() {
208: System.out.println("\nUsage: java org.jibx.wsdl.Jibx2Wsdl "
209: + "[options] class1 class2 ...\nwhere options are:");
210: String[] usages = mergeUsageLines(BASE_USAGE_LINES,
211: EXTRA_USAGE_LINES);
212: for (int i = 0; i < usages.length; i++) {
213: System.out.println(usages[i]);
214: }
215: System.out
216: .println("The class# files are different classes to be exposed as "
217: + "services (references from\nthese classes will automatically be "
218: + "included in the generated bindings).\n");
219: }
220: }
|