001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Java Object Management and Configuration
005     *   Copyright (C) Christian Schulte, 2005-206
006     *   All rights reserved.
007     *
008     *   Redistribution and use in source and binary forms, with or without
009     *   modification, are permitted provided that the following conditions
010     *   are met:
011     *
012     *     o Redistributions of source code must retain the above copyright
013     *       notice, this list of conditions and the following disclaimer.
014     *
015     *     o Redistributions in binary form must reproduce the above copyright
016     *       notice, this list of conditions and the following disclaimer in
017     *       the documentation and/or other materials provided with the
018     *       distribution.
019     *
020     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
021     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
022     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
023     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
024     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
025     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
029     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030     *
031     *   $JOMC: OptionFactory.java 4463 2012-03-28 00:46:22Z schulte2005 $
032     *
033     */
034    // </editor-fold>
035    // SECTION-END
036    package org.jomc.cli.model;
037    
038    import java.io.File;
039    import org.apache.commons.cli.Option;
040    
041    // SECTION-START[Documentation]
042    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
043    /**
044     * Creates {@code Option} objects describing a single command-line option by maintaining information regarding the short-name, the long-name, a flag indicating if an argument is required, and a self-documenting description.
045     *
046     * <dl>
047     *   <dt><b>Identifier:</b></dt><dd>JOMC CLI Option Factory</dd>
048     *   <dt><b>Name:</b></dt><dd>JOMC CLI</dd>
049     *   <dt><b>Abstract:</b></dt><dd>Yes</dd>
050     *   <dt><b>Final:</b></dt><dd>No</dd>
051     *   <dt><b>Stateless:</b></dt><dd>No</dd>
052     * </dl>
053     *
054     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
055     * @version 1.2.5
056     */
057    // </editor-fold>
058    // SECTION-END
059    // SECTION-START[Annotations]
060    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
061    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
062    // </editor-fold>
063    // SECTION-END
064    public final class OptionFactory
065    {
066        // SECTION-START[OptionFactory]
067    
068        public Option getObject()
069        {
070            final char valueSeparator = this.getValueSeparator() == ':' ? File.pathSeparatorChar : this.getValueSeparator();
071    
072            final Option option =
073                new Option( this.getOpt(), this.getDescription( this.getLocale(), Character.toString( valueSeparator ) ) );
074    
075            option.setArgs( this.getNumberOfArgs() );
076            option.setLongOpt( this.getLongOpt() );
077            option.setOptionalArg( this.isOptionalArg() );
078            option.setRequired( this.isRequired() );
079    
080            if ( ( option.getArgs() > 0 || option.getArgs() == Option.UNLIMITED_VALUES )
081                 && this.getArgumentDescription( this.getLocale() ).trim().length() > 0 )
082            {
083                option.setArgName( this.getArgumentDescription( this.getLocale() ) );
084            }
085    
086            if ( option.getArgs() > 1 || option.getArgs() == Option.UNLIMITED_VALUES )
087            {
088                option.setValueSeparator( valueSeparator );
089            }
090    
091            return option;
092        }
093    
094        // SECTION-END
095        // SECTION-START[Constructors]
096        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
097        /** Creates a new {@code OptionFactory} instance. */
098        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
099        public OptionFactory()
100        {
101            // SECTION-START[Default Constructor]
102            super();
103            // SECTION-END
104        }
105        // </editor-fold>
106        // SECTION-END
107        // SECTION-START[Dependencies]
108        // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
109        /**
110         * Gets the {@code <Locale>} dependency.
111         * <p>
112         *   This method returns the {@code <default>} object of the {@code <java.util.Locale>} specification at specification level 1.1.
113         *   That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.
114         * </p>
115         * <dl>
116         *   <dt><b>Final:</b></dt><dd>No</dd>
117         * </dl>
118         * @return The {@code <Locale>} dependency.
119         * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
120         */
121        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
122        private java.util.Locale getLocale()
123        {
124            final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
125            assert _d != null : "'Locale' dependency not found.";
126            return _d;
127        }
128        // </editor-fold>
129        // SECTION-END
130        // SECTION-START[Properties]
131        // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
132        /**
133         * Gets the value of the {@code <longOpt>} property.
134         * <p><dl>
135         *   <dt><b>Final:</b></dt><dd>No</dd>
136         * </dl></p>
137         * @return Long name of the option.
138         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
139         */
140        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
141        private java.lang.String getLongOpt()
142        {
143            final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "longOpt" );
144            assert _p != null : "'longOpt' property not found.";
145            return _p;
146        }
147        /**
148         * Gets the value of the {@code <numberOfArgs>} property.
149         * <p><dl>
150         *   <dt><b>Final:</b></dt><dd>No</dd>
151         * </dl></p>
152         * @return Number of argument values the option can take.
153         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
154         */
155        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
156        private int getNumberOfArgs()
157        {
158            final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "numberOfArgs" );
159            assert _p != null : "'numberOfArgs' property not found.";
160            return _p.intValue();
161        }
162        /**
163         * Gets the value of the {@code <opt>} property.
164         * <p><dl>
165         *   <dt><b>Final:</b></dt><dd>No</dd>
166         * </dl></p>
167         * @return Name of the option.
168         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
169         */
170        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
171        private java.lang.String getOpt()
172        {
173            final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "opt" );
174            assert _p != null : "'opt' property not found.";
175            return _p;
176        }
177        /**
178         * Gets the value of the {@code <optionalArg>} property.
179         * <p><dl>
180         *   <dt><b>Final:</b></dt><dd>No</dd>
181         * </dl></p>
182         * @return Query to see if the option can have an optional argument.
183         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
184         */
185        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
186        private boolean isOptionalArg()
187        {
188            final java.lang.Boolean _p = (java.lang.Boolean) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "optionalArg" );
189            assert _p != null : "'optionalArg' property not found.";
190            return _p.booleanValue();
191        }
192        /**
193         * Gets the value of the {@code <required>} property.
194         * <p><dl>
195         *   <dt><b>Final:</b></dt><dd>No</dd>
196         * </dl></p>
197         * @return Query to see if the option is required.
198         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
199         */
200        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
201        private boolean isRequired()
202        {
203            final java.lang.Boolean _p = (java.lang.Boolean) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "required" );
204            assert _p != null : "'required' property not found.";
205            return _p.booleanValue();
206        }
207        /**
208         * Gets the value of the {@code <valueSeparator>} property.
209         * <p><dl>
210         *   <dt><b>Final:</b></dt><dd>No</dd>
211         * </dl></p>
212         * @return Value separator of the option.
213         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
214         */
215        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
216        private char getValueSeparator()
217        {
218            final java.lang.Character _p = (java.lang.Character) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "valueSeparator" );
219            assert _p != null : "'valueSeparator' property not found.";
220            return _p.charValue();
221        }
222        // </editor-fold>
223        // SECTION-END
224        // SECTION-START[Messages]
225        // <editor-fold defaultstate="collapsed" desc=" Generated Messages ">
226        /**
227         * Gets the text of the {@code <argumentDescription>} message.
228         * <p><dl>
229         *   <dt><b>Languages:</b></dt>
230         *     <dd>English (default)</dd>
231         *   <dt><b>Final:</b></dt><dd>No</dd>
232         * </dl></p>
233         * @param locale The locale of the message to return.
234         * @return Display name for the argument value of the option.
235         * @throws org.jomc.ObjectManagementException if getting the message instance fails.
236         */
237        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
238        private String getArgumentDescription( final java.util.Locale locale )
239        {
240            final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "argumentDescription", locale );
241            assert _m != null : "'argumentDescription' message not found.";
242            return _m;
243        }
244        /**
245         * Gets the text of the {@code <description>} message.
246         * <p><dl>
247         *   <dt><b>Languages:</b></dt>
248         *     <dd>English (default)</dd>
249         *   <dt><b>Final:</b></dt><dd>No</dd>
250         * </dl></p>
251         * @param locale The locale of the message to return.
252         * @param valueSeparator Format argument.
253         * @return Display description of the option.
254         * @throws org.jomc.ObjectManagementException if getting the message instance fails.
255         */
256        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.2.2", comments = "See http://jomc.sourceforge.net/jomc/1.2/jomc-tools-1.2.2" )
257        private String getDescription( final java.util.Locale locale, final java.lang.String valueSeparator )
258        {
259            final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "description", locale, valueSeparator );
260            assert _m != null : "'description' message not found.";
261            return _m;
262        }
263        // </editor-fold>
264        // SECTION-END
265    }