/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 org.apache.oodt.opendapps;

//JDK imports
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

//OODT imports
import org.apache.commons.io.FileUtils;
import org.apache.oodt.opendapps.util.ProfileChecker;
import org.apache.oodt.opendapps.util.ProfileSerializer;
import org.apache.oodt.profile.Profile;
import org.apache.oodt.profile.handlers.ProfileHandler;
import org.apache.oodt.xmlquery.XMLQuery;
import org.xml.sax.SAXException;

/**
 * Command line class to drive the creation of OODT profiles from THREDDS
 * catalogs with OpenDAP endpoints.
 * <p/>
 * This class reads the list of THREDDS catalog URLs from the given opendapps
 * configuration file, parses the catalogs, and it writes the OODT profiles (one
 * for each THREDDS dataset) in the file "profiles.xml" in the specified
 * directory, or in the local execution directory if none is specified.
 * <p/>
 * Usage: java -classpath [path to opendapps-version-jar-with-dependencies.jar]
 * org.apache.oodt.opendapps.Profiler [config_file_location]
 * [optional_output_dir]
 * <p/>
 * Usage example: java -classpath
 * ./target/opendapps-0.4-SNAPSHOT-jar-with-dependencies.jar
 * org.apache.oodt.opendapps.Profiler /home/users/testuser/opendap.config.xml
 * /tmp
 * 
 * @author Luca Cinquini
 * 
 */
public class Profiler {

  private static Logger LOG = Logger.getLogger(Profiler.class.getName());

  /**
   * Command line invocation method.
   * 
   * @param args
   */
  public static void main(String[] args) throws Exception {

    // parse command line input
    if (args.length != 1 && args.length != 2) {
      usage();
    }
    File configFile = new File(args[0]);
    File outputDir = null;
    if (args.length == 2)
      outputDir = new File(args[1]);

    // parse THREDDS catalogs, create OODT profiles
    ProfileHandler profileHandler = new OpendapProfileHandler();
    XMLQuery xmlQuery = Profiler.buildXMLQuery(configFile);
    @SuppressWarnings(value = "unchecked")
    final List<Profile> profiles = profileHandler.findProfiles(xmlQuery);
    
    // check profiles
    for (final Profile profile : profiles) {
    	final StringBuilder sb = new StringBuilder();
    	boolean ok = ProfileChecker.check(profile, sb);
    	// print out the profile summary for quick review by the publisher
    	System.out.println(sb.toString());
    	if (!ok) {
    		LOG.log(Level.SEVERE, "ERROR: invalid profile:"+profile.getResourceAttributes().getIdentifier());
    	} 	
    }

    // serialize profiles to XML
    String xml = ProfileSerializer.toXML(profiles);
    LOG.log(Level.FINE, xml);

    // write XML to disk
    if (outputDir != null) {
      final File file = new File(outputDir, "profiles.xml");
      FileUtils.writeStringToFile(file, xml);
    }

  }

  private static XMLQuery buildXMLQuery(final File file) throws SAXException {

    final String query = "<query><queryKWQString>"
        + "PFunction=findall?ConfigUrl=" + file.getAbsolutePath()
        + "</queryKWQString></query>";
    final XMLQuery xmlQuery = new XMLQuery(query);
    return xmlQuery;

  }

  private final static void usage() {
    System.out
        .println("Usage: java -classpath [path to opendapps-version-jar-with-dependencies.jar] org.apache.oodt.opendapps.Profiler <config file location> [<output_dir>]");
    System.out
        .println("Example: java -classpath ./target/opendapps-0.4-SNAPSHOT-jar-with-dependencies.jar org.apache.oodt.opendapps.Profiler /home/users/testuser/opendap.config.xml /tmp");
    System.exit(-1);
  }

}

@@ -2218,16 +2218,111 @@
 Name());
+%0A  %0A  /**%0A   * Optional directory to serialize the profiles to.%0A   */%0A  private File outputDir;
 %0A%0A  /**%0A
@@ -2598,29 +2598,42 @@
     
-File outputDir = null
+Profiler profiler = new Profiler()
 ;%0A  
@@ -2659,24 +2659,38 @@
 = 2)
+ %7B
 %0A      
-o
+profiler.setO
 utputDir
  = n
@@ -2685,18 +2685,17 @@
 utputDir
- =
+(
  new Fil
@@ -2704,17 +2704,560 @@
 args%5B1%5D)
-;
+ );%0A    %7D%0A    %0A    // run profiler%0A    profiler.makeProfiles(configFile);%0A    %0A  %7D%0A  %0A  /**%0A   * No argument constructor.%0A   */%0A  public Profiler() %7B%7D%0A  %0A  /**%0A   * Setter method for output directory.%0A   * @param outputDir%0A   */%0A  public void setOutputDir(File outputDir) %7B%0A%09%09this.outputDir = outputDir;%0A%09%7D%0A%0A%09/**%0A   * Method to generate OODT profiles according to the specifications contained in a configuration file.%0A   * %0A   * @param configFile %0A   * @return%0A   */%0A  public List%3CProfile%3E makeProfiles(final File configFile) throws Exception %7B
 %0A%0A    //
@@ -4252,16 +4252,42 @@
 %0A    %7D%0A%0A
+    return profiles;%0A    %0A
   %7D%0A%0A  p

Move business logic into public method, out of main, so it can be invoked by clients.