Example usage for org.apache.commons.cli Option setArgs

List of usage examples for org.apache.commons.cli Option setArgs

Introduction

In this page you can find the example usage for org.apache.commons.cli Option setArgs.

Prototype

public void setArgs(int num) 

Source Link

Document

Sets the number of argument values this Option can take.

Usage

From source file:org.deegree.tools.rendering.manager.DataManager.java

/**
 * @param options// w w w .  j  a  va 2 s  .c o  m
 */
private static void addTypeParameters(Options options) {
    Option option = new Option(TYPE.substring(0, 1), TYPE, true,
            "defines the type of data the manager expects");
    option.setArgs(1);
    StringBuilder argNames = new StringBuilder();
    Type[] allTypes = Type.values();
    for (int i = 0; i < allTypes.length; ++i) {
        Type a = allTypes[i];
        argNames.append(a);
        if ((i + 1) < allTypes.length) {
            argNames.append("|");
        }
    }
    option.setArgName(argNames.toString());
    option.setRequired(true);
    options.addOption(option);
}

From source file:org.deegree.tools.rendering.manager.DataManager.java

/**
 * Database parameters/*  w  w w.  j a v a 2s  .com*/
 * 
 * @param options
 *            to add the database option to.
 */
private static void addDatabaseParameters(Options options) {

    Option option = new Option("host", DB_HOST, true, "url to the database, with or without port");
    option.setArgs(1);
    option.setArgName("for example jdbc:postgresql://dbhost:5432/db_name");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("fbd", OPT_FILE_BACKEND_DIR, true, "directory to be used for the file databackend");
    option.setArgs(1);
    option.setArgName("for example /home/file_backend");
    options.addOption(option);

    option = new Option(OPT_DB_USER.substring(0, 1), OPT_DB_USER, true,
            "username of the database, default will be ${user.name}");
    option.setArgs(1);
    option.setArgName("for example postgres");
    options.addOption(option);

    option = new Option(OPT_DB_PASS.substring(0, 1), OPT_DB_PASS, true,
            "password of the database, default will be empty");
    option.setArgs(1);
    option.setArgName("for example my_secret_password");
    options.addOption(option);

}

From source file:org.deegree.tools.rendering.manager.DataManager.java

/**
 * VRML file reading parameters/*  ww w. j  a va 2s  .c  om*/
 * 
 * @param options
 *            to add the vrml options to.
 */
private static void addVRMLParameters(Options options) {

    Option option = new Option("tx", OPT_VRML_TRANSLATION_X, true,
            "Translation of the x values of the vrml file.");
    option.setArgs(1);
    option.setArgName("Easting translation.");
    options.addOption(option);

    option = new Option("ty", OPT_VRML_TRANSLATION_Y, true, "Translation of the y values of the vrml file.");
    option.setArgs(1);
    option.setArgName("Northing translation.");
    options.addOption(option);

    option = new Option("tz", OPT_VRML_TRANSLATION_Z, true, "Translation of the z values of the vrml file.");
    option.setArgs(1);
    option.setArgName("Up translation.");
    options.addOption(option);

    option = new Option("ra", OPT_VRML_ROTATION_AXIS, true,
            "Rotation axis, comma separated values: x,y,z,angle.");
    option.setArgs(1);
    option.setArgName("Rotation axis.");
    options.addOption(option);

    option = new Option("yz", OPT_VRML_FLIP_Y_Z, false, "Flip the y and z coordinates of the vrml file.");
    options.addOption(option);

    option = new Option("mtd", OPT_VRML_MAX_TEX_DIM, true,
            "The maximum dimension of the textures of the given vrml file, will be 'upped' to the power of two.");
    option.setArgs(1);
    option.setArgName("The maximum dimension of a texture.");
    options.addOption(option);

}

From source file:org.deegree.tools.rendering.manager.DataManager.java

/**
 * CityGML file reading parameters/*from  ww  w .ja  va2 s  .co m*/
 * 
 * @param options
 *            to add the vrml options to.
 */
private static void addCityGMLParameters(Options options) {

    Option option = new Option("cgsl", OPT_CITY_GML_SCHEMA, true,
            "Local location of the citygml schema files.");
    option.setArgs(1);
    option.setArgName("File location.");
    options.addOption(option);

    option = new Option("bc", OPT_CITY_GML_COLOR, true,
            "Default color of the citygml buildings in #FFUUFF format.");
    option.setArgs(1);
    option.setArgName("Default color.");
    options.addOption(option);

    option = new Option("ogcns", OPT_USE_OPENGIS, false,
            "Optional flag for using the opengis namespace for citygml buildings.");
    options.addOption(option);

}

From source file:org.deegree.tools.rendering.manager.ModelGeneralizor.java

private static Options initOptions() {
    Options options = new Options();

    Option option = new Option("type", TYPE, true, "the type of object to assign a prototype to (building)");
    option.setArgs(1);
    option.setArgName("building");
    option.setRequired(true);/*from  ww  w  . j av a  2s .  c  o m*/
    options.addOption(option);

    option = new Option("ql", TARGET_QL, true,
            "defines the quality level of the data the generalistation should be assigned to");
    option.setArgs(1);
    option.setArgName("[0,1]");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("source", SOURCE_QL, true,
            "defines the quality level of the data from which the generalistation should be constructed from");
    option.setArgs(1);
    option.setArgName("[2,5]");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("tt", WPVS_TRANSLATION_TO, true,
            "A comma seperated translation vector to the nullpoint of the WPVS (see the WPVS-Config for more information).");
    option.setArgs(1);
    option.setRequired(true);
    option.setArgName("e.g. \"-2568000,-5606000\"  .");
    options.addOption(option);

    option = new Option("sw", SQL_WHERE, true, "SQL where statement which defines a where cause on the db.");
    option.setArgs(1);
    option.setArgName("\"WHERE id='building_id'\"");
    options.addOption(option);

    addDatabaseParameters(options);

    CommandUtils.addDefaultOptions(options);
    return options;

}

From source file:org.deegree.tools.rendering.manager.ModelGeneralizor.java

/**
 * Database parameters/*  w  w  w.j a  va 2s. c o  m*/
 * 
 * @param options
 *            to add the database option to.
 */
private static void addDatabaseParameters(Options options) {

    Option option = new Option("host", DB_HOST, true, "url to the database, with or without port");
    option.setArgs(1);
    option.setArgName("for example jdbc:postgresql://dbhost:5432/db_name");
    option.setRequired(true);
    options.addOption(option);

    option = new Option(OPT_DB_USER.substring(0, 1), OPT_DB_USER, true,
            "username of the database, default will be ${user.name}");
    option.setArgs(1);
    option.setArgName("for example postgres");
    options.addOption(option);

    option = new Option(OPT_DB_PASS.substring(0, 1), OPT_DB_PASS, true,
            "password of the database, default will be empty");
    option.setArgs(1);
    option.setArgName("for example my_secret_password");
    options.addOption(option);

}

From source file:org.deegree.tools.rendering.manager.PrototypeAssigner.java

private static Options initOptions() {
    Options options = new Options();

    Option option = new Option("type", TYPE, true, "the type of object to assign a prototype to (building)");
    option.setArgs(1);
    option.setArgName("building");
    option.setRequired(true);//from w  w  w. j  a v  a2  s .  c o m
    options.addOption(option);

    option = new Option("ql", QL, true,
            "defines the quality level of the data the prototype should be assigned to");
    option.setArgs(1);
    option.setArgName("[0-5]");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("id", BUILDING_ID, true, "UUID of the building to assign the prototype to.");
    option.setArgs(1);
    option.setArgName("The uuid of the building.");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("pid", PROTOTYPE_ID, true, "UUID of the prototype to assign to the building.");
    option.setArgs(1);
    option.setArgName("The uuid of the prototype.");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("tt", WPVS_TRANSLATION_TO, true,
            "A comma seperated translation vector to the nullpoint of the WPVS (see the WPVS-Config for more information).");
    option.setArgs(1);
    option.setRequired(true);
    option.setArgName("e.g. \"-2568000,-5606000\"  .");
    options.addOption(option);

    option = new Option("t", TRANSLATION, true,
            "A comma seperated 3D translation vector to the middle point of the bbox of this building.");
    option.setArgs(1);
    option.setArgName("Building origin e.g. \"2568000,5606000,18\"  .");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("r", ROTATION, true, "Z-Axis rotation in degrees.");
    option.setArgs(1);
    option.setArgName("A rotation in degrees.");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("w", WIDTH, true, "Width of the prototype (x-axis).");
    option.setArgs(1);
    option.setArgName("The width of the building.");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("h", HEIGHT, true, "Height of the prototype (z-axis).");
    option.setArgs(1);
    option.setArgName("The height of the building.");
    option.setRequired(true);
    options.addOption(option);

    option = new Option("d", DEPTH, true, "Depth of the prototype (y-axis).");
    option.setArgs(1);
    option.setArgName("The depth of the building.");
    option.setRequired(true);
    options.addOption(option);

    addDatabaseParameters(options);

    CommandUtils.addDefaultOptions(options);

    return options;

}

From source file:org.dspace.app.mediafilter.MediaFilterCLITool.java

public static void main(String[] argv) throws Exception {
    // set headless for non-gui workstations
    System.setProperty("java.awt.headless", "true");

    // create an options object and populate it
    CommandLineParser parser = new PosixParser();

    int status = 0;

    Options options = new Options();

    options.addOption("v", "verbose", false, "print all extracted text and other details to STDOUT");
    options.addOption("q", "quiet", false, "do not print anything except in the event of errors.");
    options.addOption("f", "force", false, "force all bitstreams to be processed");
    options.addOption("i", "identifier", true, "ONLY process bitstreams belonging to identifier");
    options.addOption("m", "maximum", true, "process no more than maximum items");
    options.addOption("h", "help", false, "help");

    //create a "plugin" option (to specify specific MediaFilter plugins to run)
    OptionBuilder.withLongOpt("plugins");
    OptionBuilder.withValueSeparator(',');
    OptionBuilder.withDescription("ONLY run the specified Media Filter plugin(s)\n" + "listed from '"
            + MEDIA_FILTER_PLUGINS_KEY + "' in dspace.cfg.\n" + "Separate multiple with a comma (,)\n"
            + "(e.g. MediaFilterManager -p \n\"Word Text Extractor\",\"PDF Text Extractor\")");
    Option pluginOption = OptionBuilder.create('p');
    pluginOption.setArgs(Option.UNLIMITED_VALUES); //unlimited number of args
    options.addOption(pluginOption);/*  w ww.  ja va 2 s .  co m*/

    //create a "skip" option (to specify communities/collections/items to skip)
    OptionBuilder.withLongOpt("skip");
    OptionBuilder.withValueSeparator(',');
    OptionBuilder.withDescription(
            "SKIP the bitstreams belonging to identifier\n" + "Separate multiple identifiers with a comma (,)\n"
                    + "(e.g. MediaFilterManager -s \n 123456789/34,123456789/323)");
    Option skipOption = OptionBuilder.create('s');
    skipOption.setArgs(Option.UNLIMITED_VALUES); //unlimited number of args
    options.addOption(skipOption);

    boolean isVerbose = false;
    boolean isQuiet = false;
    boolean isForce = false; // default to not forced
    String identifier = null; // object scope limiter
    int max2Process = Integer.MAX_VALUE;
    Map<String, List<String>> filterFormats = new HashMap<>();

    CommandLine line = null;
    try {
        line = parser.parse(options, argv);
    } catch (MissingArgumentException e) {
        System.out.println("ERROR: " + e.getMessage());
        HelpFormatter myhelp = new HelpFormatter();
        myhelp.printHelp("MediaFilterManager\n", options);
        System.exit(1);
    }

    if (line.hasOption('h')) {
        HelpFormatter myhelp = new HelpFormatter();
        myhelp.printHelp("MediaFilterManager\n", options);

        System.exit(0);
    }

    if (line.hasOption('v')) {
        isVerbose = true;
    }

    isQuiet = line.hasOption('q');

    if (line.hasOption('f')) {
        isForce = true;
    }

    if (line.hasOption('i')) {
        identifier = line.getOptionValue('i');
    }

    if (line.hasOption('m')) {
        max2Process = Integer.parseInt(line.getOptionValue('m'));
        if (max2Process <= 1) {
            System.out.println("Invalid maximum value '" + line.getOptionValue('m') + "' - ignoring");
            max2Process = Integer.MAX_VALUE;
        }
    }

    String filterNames[] = null;
    if (line.hasOption('p')) {
        //specified which media filter plugins we are using
        filterNames = line.getOptionValues('p');

        if (filterNames == null || filterNames.length == 0) { //display error, since no plugins specified
            System.err.println("\nERROR: -p (-plugin) option requires at least one plugin to be specified.\n"
                    + "(e.g. MediaFilterManager -p \"Word Text Extractor\",\"PDF Text Extractor\")\n");
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("MediaFilterManager\n", options);
            System.exit(1);
        }
    } else {
        //retrieve list of all enabled media filter plugins!
        filterNames = DSpaceServicesFactory.getInstance().getConfigurationService()
                .getArrayProperty(MEDIA_FILTER_PLUGINS_KEY);
    }

    MediaFilterService mediaFilterService = MediaFilterServiceFactory.getInstance().getMediaFilterService();
    mediaFilterService.setForce(isForce);
    mediaFilterService.setQuiet(isQuiet);
    mediaFilterService.setVerbose(isVerbose);
    mediaFilterService.setMax2Process(max2Process);

    //initialize an array of our enabled filters
    List<FormatFilter> filterList = new ArrayList<FormatFilter>();

    //set up each filter
    for (int i = 0; i < filterNames.length; i++) {
        //get filter of this name & add to list of filters
        FormatFilter filter = (FormatFilter) CoreServiceFactory.getInstance().getPluginService()
                .getNamedPlugin(FormatFilter.class, filterNames[i]);
        if (filter == null) {
            System.err.println(
                    "\nERROR: Unknown MediaFilter specified (either from command-line or in dspace.cfg): '"
                            + filterNames[i] + "'");
            System.exit(1);
        } else {
            filterList.add(filter);

            String filterClassName = filter.getClass().getName();

            String pluginName = null;

            //If this filter is a SelfNamedPlugin,
            //then the input formats it accepts may differ for
            //each "named" plugin that it defines.
            //So, we have to look for every key that fits the
            //following format: filter.<class-name>.<plugin-name>.inputFormats
            if (SelfNamedPlugin.class.isAssignableFrom(filter.getClass())) {
                //Get the plugin instance name for this class
                pluginName = ((SelfNamedPlugin) filter).getPluginInstanceName();
            }

            //Retrieve our list of supported formats from dspace.cfg
            //For SelfNamedPlugins, format of key is:
            //  filter.<class-name>.<plugin-name>.inputFormats
            //For other MediaFilters, format of key is:
            //  filter.<class-name>.inputFormats
            String[] formats = DSpaceServicesFactory.getInstance().getConfigurationService()
                    .getArrayProperty(FILTER_PREFIX + "." + filterClassName
                            + (pluginName != null ? "." + pluginName : "") + "." + INPUT_FORMATS_SUFFIX);

            //add to internal map of filters to supported formats
            if (ArrayUtils.isNotEmpty(formats)) {
                //For SelfNamedPlugins, map key is:
                //  <class-name><separator><plugin-name>
                //For other MediaFilters, map key is just:
                //  <class-name>
                filterFormats.put(filterClassName
                        + (pluginName != null ? MediaFilterService.FILTER_PLUGIN_SEPARATOR + pluginName : ""),
                        Arrays.asList(formats));
            }
        } //end if filter!=null
    } //end for

    //If verbose, print out loaded mediafilter info
    if (isVerbose) {
        System.out.println("The following MediaFilters are enabled: ");
        Iterator<String> i = filterFormats.keySet().iterator();
        while (i.hasNext()) {
            String filterName = i.next();
            System.out.println("Full Filter Name: " + filterName);
            String pluginName = null;
            if (filterName.contains(MediaFilterService.FILTER_PLUGIN_SEPARATOR)) {
                String[] fields = filterName.split(MediaFilterService.FILTER_PLUGIN_SEPARATOR);
                filterName = fields[0];
                pluginName = fields[1];
            }

            System.out.println(filterName + (pluginName != null ? " (Plugin: " + pluginName + ")" : ""));
        }
    }

    mediaFilterService.setFilterFormats(filterFormats);
    //store our filter list into an internal array
    mediaFilterService.setFilterClasses(filterList);

    //Retrieve list of identifiers to skip (if any)
    String skipIds[] = null;
    if (line.hasOption('s')) {
        //specified which identifiers to skip when processing
        skipIds = line.getOptionValues('s');

        if (skipIds == null || skipIds.length == 0) { //display error, since no identifiers specified to skip
            System.err.println("\nERROR: -s (-skip) option requires at least one identifier to SKIP.\n"
                    + "Make sure to separate multiple identifiers with a comma!\n"
                    + "(e.g. MediaFilterManager -s 123456789/34,123456789/323)\n");
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("MediaFilterManager\n", options);
            System.exit(0);
        }

        //save to a global skip list
        mediaFilterService.setSkipList(Arrays.asList(skipIds));
    }

    Context c = null;

    try {
        c = new Context();

        // have to be super-user to do the filtering
        c.turnOffAuthorisationSystem();

        // now apply the filters
        if (identifier == null) {
            mediaFilterService.applyFiltersAllItems(c);
        } else // restrict application scope to identifier
        {
            DSpaceObject dso = HandleServiceFactory.getInstance().getHandleService().resolveToObject(c,
                    identifier);
            if (dso == null) {
                throw new IllegalArgumentException("Cannot resolve " + identifier + " to a DSpace object");
            }

            switch (dso.getType()) {
            case Constants.COMMUNITY:
                mediaFilterService.applyFiltersCommunity(c, (Community) dso);
                break;
            case Constants.COLLECTION:
                mediaFilterService.applyFiltersCollection(c, (Collection) dso);
                break;
            case Constants.ITEM:
                mediaFilterService.applyFiltersItem(c, (Item) dso);
                break;
            }
        }

        c.complete();
        c = null;
    } catch (Exception e) {
        status = 1;
    } finally {
        if (c != null) {
            c.abort();
        }
    }
    System.exit(status);
}

From source file:org.dspace.app.mediafilter.MediaFilterManager.java

public static void main(String[] argv) throws Exception {
    // set headless for non-gui workstations
    System.setProperty("java.awt.headless", "true");

    // create an options object and populate it
    CommandLineParser parser = new PosixParser();

    int status = 0;

    Options options = new Options();

    options.addOption("v", "verbose", false, "print all extracted text and other details to STDOUT");
    options.addOption("q", "quiet", false, "do not print anything except in the event of errors.");
    options.addOption("f", "force", false, "force all bitstreams to be processed");
    options.addOption("n", "noindex", false, "do NOT update the search index after filtering bitstreams");
    options.addOption("i", "identifier", true, "ONLY process bitstreams belonging to identifier");
    options.addOption("m", "maximum", true, "process no more than maximum items");
    options.addOption("h", "help", false, "help");

    //create a "plugin" option (to specify specific MediaFilter plugins to run)
    OptionBuilder.withLongOpt("plugins");
    OptionBuilder.withValueSeparator(',');
    OptionBuilder.withDescription("ONLY run the specified Media Filter plugin(s)\n" + "listed from '"
            + MEDIA_FILTER_PLUGINS_KEY + "' in dspace.cfg.\n" + "Separate multiple with a comma (,)\n"
            + "(e.g. MediaFilterManager -p \n\"Word Text Extractor\",\"PDF Text Extractor\")");
    Option pluginOption = OptionBuilder.create('p');
    pluginOption.setArgs(Option.UNLIMITED_VALUES); //unlimited number of args
    options.addOption(pluginOption);//from  w  w  w .ja v  a  2 s . c o m

    //create a "skip" option (to specify communities/collections/items to skip)
    OptionBuilder.withLongOpt("skip");
    OptionBuilder.withValueSeparator(',');
    OptionBuilder.withDescription(
            "SKIP the bitstreams belonging to identifier\n" + "Separate multiple identifiers with a comma (,)\n"
                    + "(e.g. MediaFilterManager -s \n 123456789/34,123456789/323)");
    Option skipOption = OptionBuilder.create('s');
    skipOption.setArgs(Option.UNLIMITED_VALUES); //unlimited number of args
    options.addOption(skipOption);

    CommandLine line = null;
    try {
        line = parser.parse(options, argv);
    } catch (MissingArgumentException e) {
        System.out.println("ERROR: " + e.getMessage());
        HelpFormatter myhelp = new HelpFormatter();
        myhelp.printHelp("MediaFilterManager\n", options);
        System.exit(1);
    }

    if (line.hasOption('h')) {
        HelpFormatter myhelp = new HelpFormatter();
        myhelp.printHelp("MediaFilterManager\n", options);

        System.exit(0);
    }

    if (line.hasOption('v')) {
        isVerbose = true;
    }

    isQuiet = line.hasOption('q');

    if (line.hasOption('n')) {
        updateIndex = false;
    }

    if (line.hasOption('f')) {
        isForce = true;
    }

    if (line.hasOption('i')) {
        identifier = line.getOptionValue('i');
    }

    if (line.hasOption('m')) {
        max2Process = Integer.parseInt(line.getOptionValue('m'));
        if (max2Process <= 1) {
            System.out.println("Invalid maximum value '" + line.getOptionValue('m') + "' - ignoring");
            max2Process = Integer.MAX_VALUE;
        }
    }

    String filterNames[] = null;
    if (line.hasOption('p')) {
        //specified which media filter plugins we are using
        filterNames = line.getOptionValues('p');

        if (filterNames == null || filterNames.length == 0) { //display error, since no plugins specified
            System.err.println("\nERROR: -p (-plugin) option requires at least one plugin to be specified.\n"
                    + "(e.g. MediaFilterManager -p \"Word Text Extractor\",\"PDF Text Extractor\")\n");
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("MediaFilterManager\n", options);
            System.exit(1);
        }
    } else {
        //retrieve list of all enabled media filter plugins!
        String enabledPlugins = ConfigurationManager.getProperty(MEDIA_FILTER_PLUGINS_KEY);
        filterNames = enabledPlugins.split(",\\s*");
    }

    //initialize an array of our enabled filters
    List<FormatFilter> filterList = new ArrayList<FormatFilter>();

    //set up each filter
    for (int i = 0; i < filterNames.length; i++) {
        //get filter of this name & add to list of filters
        FormatFilter filter = (FormatFilter) PluginManager.getNamedPlugin(FormatFilter.class, filterNames[i]);
        if (filter == null) {
            System.err.println(
                    "\nERROR: Unknown MediaFilter specified (either from command-line or in dspace.cfg): '"
                            + filterNames[i] + "'");
            System.exit(1);
        } else {
            filterList.add(filter);

            String filterClassName = filter.getClass().getName();

            String pluginName = null;

            //If this filter is a SelfNamedPlugin,
            //then the input formats it accepts may differ for
            //each "named" plugin that it defines.
            //So, we have to look for every key that fits the
            //following format: filter.<class-name>.<plugin-name>.inputFormats
            if (SelfNamedPlugin.class.isAssignableFrom(filter.getClass())) {
                //Get the plugin instance name for this class
                pluginName = ((SelfNamedPlugin) filter).getPluginInstanceName();
            }

            //Retrieve our list of supported formats from dspace.cfg
            //For SelfNamedPlugins, format of key is:  
            //  filter.<class-name>.<plugin-name>.inputFormats
            //For other MediaFilters, format of key is: 
            //  filter.<class-name>.inputFormats
            String formats = ConfigurationManager.getProperty(FILTER_PREFIX + "." + filterClassName
                    + (pluginName != null ? "." + pluginName : "") + "." + INPUT_FORMATS_SUFFIX);

            //add to internal map of filters to supported formats   
            if (formats != null) {
                //For SelfNamedPlugins, map key is:  
                //  <class-name><separator><plugin-name>
                //For other MediaFilters, map key is just:
                //  <class-name>
                filterFormats.put(
                        filterClassName + (pluginName != null ? FILTER_PLUGIN_SEPARATOR + pluginName : ""),
                        Arrays.asList(formats.split(",[\\s]*")));
            }
        } //end if filter!=null
    } //end for

    //If verbose, print out loaded mediafilter info
    if (isVerbose) {
        System.out.println("The following MediaFilters are enabled: ");
        Iterator<String> i = filterFormats.keySet().iterator();
        while (i.hasNext()) {
            String filterName = i.next();
            System.out.println("Full Filter Name: " + filterName);
            String pluginName = null;
            if (filterName.contains(FILTER_PLUGIN_SEPARATOR)) {
                String[] fields = filterName.split(FILTER_PLUGIN_SEPARATOR);
                filterName = fields[0];
                pluginName = fields[1];
            }

            System.out.println(filterName + (pluginName != null ? " (Plugin: " + pluginName + ")" : ""));
        }
    }

    //store our filter list into an internal array
    filterClasses = (FormatFilter[]) filterList.toArray(new FormatFilter[filterList.size()]);

    //Retrieve list of identifiers to skip (if any)
    String skipIds[] = null;
    if (line.hasOption('s')) {
        //specified which identifiers to skip when processing
        skipIds = line.getOptionValues('s');

        if (skipIds == null || skipIds.length == 0) { //display error, since no identifiers specified to skip
            System.err.println("\nERROR: -s (-skip) option requires at least one identifier to SKIP.\n"
                    + "Make sure to separate multiple identifiers with a comma!\n"
                    + "(e.g. MediaFilterManager -s 123456789/34,123456789/323)\n");
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("MediaFilterManager\n", options);
            System.exit(0);
        }

        //save to a global skip list
        skipList = Arrays.asList(skipIds);
    }

    Context c = null;

    try {
        c = new Context();

        // have to be super-user to do the filtering
        c.turnOffAuthorisationSystem();

        // now apply the filters
        if (identifier == null) {
            applyFiltersAllItems(c);
        } else // restrict application scope to identifier
        {
            DSpaceObject dso = HandleManager.resolveToObject(c, identifier);
            if (dso == null) {
                throw new IllegalArgumentException("Cannot resolve " + identifier + " to a DSpace object");
            }

            switch (dso.getType()) {
            case Constants.COMMUNITY:
                applyFiltersCommunity(c, (Community) dso);
                break;
            case Constants.COLLECTION:
                applyFiltersCollection(c, (Collection) dso);
                break;
            case Constants.ITEM:
                applyFiltersItem(c, (Item) dso);
                break;
            }
        }

        // update search index?
        if (updateIndex) {
            if (!isQuiet) {
                System.out.println("Updating search index:");
            }
            DSIndexer.setBatchProcessingMode(true);
            try {
                DSIndexer.updateIndex(c);
            } finally {
                DSIndexer.setBatchProcessingMode(false);
            }
        }

        c.complete();
        c = null;
    } catch (Exception e) {
        status = 1;
    } finally {
        if (c != null) {
            c.abort();
        }
    }
    System.exit(status);
}

From source file:org.dspace.papaya.mediafilter.PapayaMediaFilterManager.java

public static void main(String[] argv) throws Exception {
    // set headless for non-gui workstations
    System.setProperty("java.awt.headless", "true");

    // create an options object and populate it
    CommandLineParser parser = new PosixParser();

    int status = 0;

    Options options = new Options();

    options.addOption("v", "verbose", false, "print all extracted text and other details to STDOUT");
    options.addOption("q", "quiet", false, "do not print anything except in the event of errors.");
    options.addOption("f", "force", false, "force all bitstreams to be processed");
    options.addOption("i", "identifier", true, "ONLY process bitstreams belonging to identifier");
    options.addOption("m", "maximum", true, "process no more than maximum items");
    options.addOption("h", "help", false, "help");

    //create a "plugin" option (to specify specific MediaFilter plugins to run)
    OptionBuilder.withLongOpt("plugins");
    OptionBuilder.withValueSeparator(',');
    OptionBuilder.withDescription("ONLY run the specified Media Filter plugin(s)\n" + "listed from '"
            + MEDIA_FILTER_PLUGINS_KEY + "' in dspace.cfg.\n" + "Separate multiple with a comma (,)\n"
            + "(e.g. MediaFilterManager -p \n\"Word Text Extractor\",\"PDF Text Extractor\")");
    Option pluginOption = OptionBuilder.create('p');
    pluginOption.setArgs(Option.UNLIMITED_VALUES); //unlimited number of args
    options.addOption(pluginOption);/*from   ww w  . j  a  v a2  s .  c o  m*/

    //create a "skip" option (to specify communities/collections/items to skip)
    OptionBuilder.withLongOpt("skip");
    OptionBuilder.withValueSeparator(',');
    OptionBuilder.withDescription(
            "SKIP the bitstreams belonging to identifier\n" + "Separate multiple identifiers with a comma (,)\n"
                    + "(e.g. MediaFilterManager -s \n 123456789/34,123456789/323)");
    Option skipOption = OptionBuilder.create('s');
    skipOption.setArgs(Option.UNLIMITED_VALUES); //unlimited number of args
    options.addOption(skipOption);

    CommandLine line = null;
    try {
        line = parser.parse(options, argv);
    } catch (MissingArgumentException e) {
        System.out.println("ERROR: " + e.getMessage());
        HelpFormatter myhelp = new HelpFormatter();
        myhelp.printHelp("MediaFilterManager\n", options);
        System.exit(1);
    }

    if (line.hasOption('h')) {
        HelpFormatter myhelp = new HelpFormatter();
        myhelp.printHelp("MediaFilterManager\n", options);

        System.exit(0);
    }

    if (line.hasOption('v')) {
        isVerbose = true;
    }

    isQuiet = line.hasOption('q');

    if (line.hasOption('f')) {
        isForce = true;
    }

    if (line.hasOption('i')) {
        identifier = line.getOptionValue('i');
    }

    if (line.hasOption('m')) {
        max2Process = Integer.parseInt(line.getOptionValue('m'));
        if (max2Process <= 1) {
            System.out.println("Invalid maximum value '" + line.getOptionValue('m') + "' - ignoring");
            max2Process = Integer.MAX_VALUE;
        }
    }

    String filterNames[] = null;
    if (line.hasOption('p')) {
        //specified which media filter plugins we are using
        filterNames = line.getOptionValues('p');

        if (filterNames == null || filterNames.length == 0) { //display error, since no plugins specified
            System.err.println("\nERROR: -p (-plugin) option requires at least one plugin to be specified.\n"
                    + "(e.g. MediaFilterManager -p \"Word Text Extractor\",\"PDF Text Extractor\")\n");
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("MediaFilterManager\n", options);
            System.exit(1);
        }
    } else {
        //retrieve list of all enabled media filter plugins!
        String enabledPlugins = ConfigurationManager.getProperty(MEDIA_FILTER_PLUGINS_KEY);
        filterNames = enabledPlugins.split(",\\s*");
    }

    //initialize an array of our enabled filters
    List<FormatFilter> filterList = new ArrayList<FormatFilter>();

    //set up each filter
    for (int i = 0; i < filterNames.length; i++) {
        //get filter of this name & add to list of filters
        FormatFilter filter = (FormatFilter) PluginManager.getNamedPlugin(FormatFilter.class, filterNames[i]);
        if (filter == null) {
            System.err.println(
                    "\nERROR: Unknown MediaFilter specified (either from command-line or in dspace.cfg): '"
                            + filterNames[i] + "'");
            System.exit(1);
        } else {
            filterList.add(filter);

            String filterClassName = filter.getClass().getName();

            String pluginName = null;
            //If this filter is a SelfNamedPlugin,
            //then the input formats it accepts may differ for
            //each "named" plugin that it defines.
            //So, we have to look for every key that fits the
            //following format: filter.<class-name>.<plugin-name>.inputFormats
            if (SelfNamedPlugin.class.isAssignableFrom(filter.getClass())) {
                //Get the plugin instance name for this class
                pluginName = ((SelfNamedPlugin) filter).getPluginInstanceName();
            }

            //Retrieve our list of supported formats from dspace.cfg
            //For SelfNamedPlugins, format of key is:  
            //  filter.<class-name>.<plugin-name>.inputFormats
            //For other MediaFilters, format of key is: 
            //  filter.<class-name>.inputFormats
            String formats = ConfigurationManager.getProperty(FILTER_PREFIX + "." + filterClassName
                    + (pluginName != null ? "." + pluginName : "") + "." + INPUT_FORMATS_SUFFIX);

            //add to internal map of filters to supported formats   
            if (formats != null) {
                //For SelfNamedPlugins, map key is:  
                //  <class-name><separator><plugin-name>
                //For other MediaFilters, map key is just:
                //  <class-name>
                filterFormats.put(
                        filterClassName + (pluginName != null ? FILTER_PLUGIN_SEPARATOR + pluginName : ""),
                        Arrays.asList(formats.split(",[\\s]*")));
            }
        } //end if filter!=null
    } //end for

    //If verbose, print out loaded mediafilter info
    if (isVerbose) {
        System.out.println("The following MediaFilters are enabled: ");
        Iterator<String> i = filterFormats.keySet().iterator();
        while (i.hasNext()) {
            String filterName = i.next();
            System.out.println("Full Filter Name: " + filterName);
            String pluginName = null;
            if (filterName.contains(FILTER_PLUGIN_SEPARATOR)) {
                String[] fields = filterName.split(FILTER_PLUGIN_SEPARATOR);
                filterName = fields[0];
                pluginName = fields[1];
            }

            System.out.println(filterName + (pluginName != null ? " (Plugin: " + pluginName + ")" : ""));
        }
    }

    //store our filter list into an internal array
    filterClasses = (FormatFilter[]) filterList.toArray(new FormatFilter[filterList.size()]);

    //Retrieve list of identifiers to skip (if any)
    String skipIds[] = null;
    if (line.hasOption('s')) {
        //specified which identifiers to skip when processing
        skipIds = line.getOptionValues('s');

        if (skipIds == null || skipIds.length == 0) { //display error, since no identifiers specified to skip
            System.err.println("\nERROR: -s (-skip) option requires at least one identifier to SKIP.\n"
                    + "Make sure to separate multiple identifiers with a comma!\n"
                    + "(e.g. MediaFilterManager -s 123456789/34,123456789/323)\n");
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("MediaFilterManager\n", options);
            System.exit(0);
        }

        //save to a global skip list
        skipList = Arrays.asList(skipIds);
    }

    Context c = null;

    try {
        c = new Context();
        // have to be super-user to do the filtering
        c.turnOffAuthorisationSystem();

        // now apply the filters
        if (identifier == null) {
            applyFiltersAllItems(c);
        } else // restrict application scope to identifier
        {
            DSpaceObject dso = HandleManager.resolveToObject(c, identifier);
            if (dso == null) {
                throw new IllegalArgumentException("Cannot resolve " + identifier + " to a DSpace object");
            }

            switch (dso.getType()) {
            case Constants.COMMUNITY:
                applyFiltersCommunity(c, (Community) dso);
                break;
            case Constants.COLLECTION:
                applyFiltersCollection(c, (Collection) dso);
                break;
            case Constants.ITEM:
                applyFiltersItem(c, (Item) dso);
                break;
            }
        }

        c.complete();
        c = null;
    } catch (Exception e) {
        status = 1;
    } finally {
        if (c != null) {
            c.abort();
        }
    }
    System.exit(status);
}