/*
* Copyright 2006 Hippo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http: *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.hippo.webdav.utils;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
/**
* <p>
* Example of how to use the Hippo WebDAV search utility class for querying the Hippo repository (or any other WebDAV compliant server)
* </p><p>
* Implements a commandline frontend for use as a standalone application, it:
* <ol>
* <li>Reads a configuration file and a DASL query file</li>
* <li>Creates and initializes a Search Object</li>
* <li>Executes the DASL query and sends the results (an XML String) to stdout</li>
* </ol>
* </p><p>
* These are the supported commandline options (all optional):
* <pre>
* -c,--config Configuration file (defaults to 'etc/search.properties')
* -d,--dasl Dasl query file (defaults to 'etc/dasl.xml')
* -h,--help Print usage information
* </pre>
* </p>
*
* @author wgrevink
*/
public class SearchMain {
private static final String DEFAULT_CONFIG = "etc/search.properties";
private static final String DEFAULT_DASL = "etc/dasl.xml";
static String configFile;
static String daslFile;
/**
* Main entry point: Parses the commandline, creates and initializes a Search object and executes it.
* The result of the query is sent to standard out.
*
* @param args Commandline arguments.
*/
public static void main(String[] args) {
parseCommandLine(args);
Properties config = loadConfigFile();
String dasl = loadDaslfile();
try {
Search search = new Search();
search.setConfig(config);
search.setDasl(dasl);
String result = search.execute();
System.out.println(result);
}
catch (RuntimeException e) {
System.err.println(e.getMessage());
}
}
/**
* Parse the commandline arguments to locate the
* configuration propertiesfile and dasl query file.
*/
private static void parseCommandLine(String[] args) {
try {
Options options = new Options();
options.addOption("c", "config", true, "Configuration file (default '" +DEFAULT_CONFIG+ "')");
options.addOption("d", "dasl", true, "Dasl query file (default '" +DEFAULT_DASL+ "')");
options.addOption("h", "help", false, "Print usage information");
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption("h")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("SearchMain", options);
System.exit(0);
}
configFile = cmd.getOptionValue("c", DEFAULT_CONFIG);
daslFile = cmd.getOptionValue("d", DEFAULT_DASL);
}
catch (Exception e) {
System.err.println("Error while parsing commandline: " + e.getMessage());
System.exit(-1);
}
}
/**
* Load the configuration file
*/
private static Properties loadConfigFile() {
Properties properties = new Properties();
try {
properties.load(new BufferedInputStream(new FileInputStream(configFile)));
}
catch (Exception e) {
System.err.println("Error while loading configuration file: " + e.getMessage());
System.exit(-1);
}
return properties;
}
/**
* Load a DASL query file
*/
private static String loadDaslfile() {
final String linesep = System.getProperty("line.separator");
StringBuffer dasl = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(daslFile));
String line;
while ((line = br.readLine()) != null) {
dasl.append(line).append(linesep);
}
}
catch (Exception e) {
System.err.println("Error while loading DASL file '" + daslFile + "': " + e.getMessage());
System.exit(-1);
}
return dasl.toString();
}
}