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.binding.generator;
030:
031: import java.io.FileInputStream;
032: import java.io.IOException;
033: import java.util.List;
034: import java.util.Map;
035:
036: import org.jibx.binding.model.IClassLocator;
037: import org.jibx.runtime.BindingDirectory;
038: import org.jibx.runtime.IBindingFactory;
039: import org.jibx.runtime.IUnmarshallable;
040: import org.jibx.runtime.IUnmarshallingContext;
041: import org.jibx.runtime.JiBXException;
042:
043: /**
044: * Command line processing specifically for the {@link BindingGenerator} class.
045: */
046: public class BindingGeneratorCommandLine extends
047: CustomizationCommandLineBase {
048: /** Ordered array of extra usage lines. */
049: protected static final String[] EXTRA_USAGE_LINES = new String[] {
050: " -a force abstract mappings for specified classes",
051: " -b generated root binding name (default is 'binding.xml')",
052: " -c force concrete mappings for specified classes" };
053:
054: /** <code>TRUE</code> if abstract mappings forced, <code>FALSE</code> if
055: concrete mappings forced, <code>null</code> if left to class settings. */
056: private Boolean m_abstract;
057:
058: /** Customizations model root. */
059: private GlobalCustom m_global;
060:
061: /** Name used for root binding. */
062: private String m_bindingName = "binding.xml";
063:
064: /**
065: * Get force abstract mapping setting.
066: *
067: * @return <code>TRUE</code> if abstract mappings forced, <code>FALSE</code>
068: * if concrete mappings forced, <code>null</code> if left to class settings
069: */
070: public Boolean getAbstract() {
071: return m_abstract;
072: }
073:
074: /**
075: * Get customizations model root.
076: *
077: * @return customizations
078: */
079: public GlobalCustom getGlobal() {
080: return m_global;
081: }
082:
083: /**
084: * Get binding name.
085: *
086: * @return name
087: */
088: public String getBindingName() {
089: return m_bindingName;
090: }
091:
092: /* (non-Javadoc)
093: * @see org.jibx.binding.generator.CustomizationCommandLineBase#checkParameter(org.jibx.binding.generator.CustomizationCommandLineBase.ArgList)
094: */
095: protected boolean checkParameter(ArgList alist) {
096: boolean match = true;
097: String arg = alist.current();
098: if ("-a".equalsIgnoreCase(arg)) {
099: m_abstract = Boolean.TRUE;
100: } else if ("-b".equalsIgnoreCase(arg)) {
101: m_bindingName = alist.next();
102: } else if ("-c".equalsIgnoreCase(arg)) {
103: m_abstract = Boolean.FALSE;
104: } else {
105: match = false;
106: }
107: return match;
108: }
109:
110: /* (non-Javadoc)
111: * @see org.jibx.binding.generator.CustomizationCommandLineBase#verboseDetails
112: */
113: protected void verboseDetails() {
114: System.out.println("Starting from classes:");
115: List types = getExtraArgs();
116: for (int i = 0; i < types.size(); i++) {
117: System.out.println(" " + types.get(i));
118: }
119: }
120:
121: /* (non-Javadoc)
122: * @see org.jibx.binding.generator.CustomizationCommandLineBase#loadCustomizations(String,IClassLocator)
123: */
124: protected void loadCustomizations(String path, IClassLocator loc)
125: throws JiBXException, IOException {
126:
127: // load or create customization information
128: m_global = new GlobalCustom(loc);
129: if (path != null) {
130: IBindingFactory fact = BindingDirectory
131: .getFactory(GlobalCustom.class);
132: IUnmarshallingContext ictx = fact
133: .createUnmarshallingContext();
134: FileInputStream is = new FileInputStream(path);
135: ictx.setDocument(is, null);
136: ((IUnmarshallable) m_global).unmarshal(ictx);
137: }
138: }
139:
140: /* (non-Javadoc)
141: * @see org.jibx.binding.generator.CustomizationCommandLineBase#applyOverrides(Map)
142: */
143: protected Map applyOverrides(Map overmap) {
144: Map unknowns = applyKeyValueMap(overmap, m_global);
145: m_global.fillClasses();
146: return unknowns;
147: }
148:
149: /* (non-Javadoc)
150: * @see org.jibx.binding.generator.CustomizationCommandLineBase#printUsage()
151: */
152: public void printUsage() {
153: System.out
154: .println("\nUsage: java org.jibx.binding.generator.BindingGenerator "
155: + "[options] class1 class2 ...\nwhere options are:");
156: String[] usages = mergeUsageLines(BASE_USAGE_LINES,
157: EXTRA_USAGE_LINES);
158: for (int i = 0; i < usages.length; i++) {
159: System.out.println(usages[i]);
160: }
161: System.out
162: .println("The class# files are different classes to be included in "
163: + "the binding (references from\nthese classes will also be "
164: + "included).\n");
165: }
166: }
|