View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer;
22  
23  import org.apache.log4j.Level;
24  import org.apache.log4j.Logger;
25  import org.apache.log4j.PatternLayout;
26  import org.apache.log4j.WriterAppender;
27  import org.xmlhammer.cli.XMLHammer;
28  
29  public class Runner {
30      public static void main(String[] args) {
31          if (useCommandLineInterface(args)) {
32              if (args.length > 0) {
33                  String file = null;
34                  boolean defaultPrefs = false;
35                  boolean logSettings = false;
36                  
37                  for (String arg : args) {
38                      if ("-default".equals(arg)) {
39                          defaultPrefs = true;
40                      } else if ("-settings".equals(arg)) {
41                          logSettings = true;
42                      } else if ("-cli".equals(arg) || "-exe".equals(arg)) {
43                      } else if (file == null) {
44                          file = arg;
45                      }
46                  }
47      
48                  if (file != null && file.trim().length() > 0) {
49                      XMLHammer hammer = new XMLHammer(defaultPrefs);
50                      
51                      Logger logger = Logger.getLogger(XMLHammer.class.getName() + "@" + Integer.toHexString(XMLHammer.class.hashCode()));
52                      logger.setLevel(Level.INFO);
53                      logger.addAppender(new WriterAppender(new PatternLayout("%m%n"), System.out));
54          
55                      hammer.run(file, logger, logSettings);
56      
57                      return;
58                  }
59              } 
60      
61              if (usingExe(args)) {
62                  System.err.println("Usage: cli.exe [options] ${project-file}");
63                  System.err.println("Options:");
64              } else {
65                  System.err.println("Usage: java -jar xmlhammer-"+Identity.getInstance().getVersion()+".jar -cli [options] ${project-file}");
66                  System.err.println("Options:");
67                  System.err.println("  -cli          Run the command line interface (required)");
68              }
69  
70              System.err.println("  -settings     Show the project specific settings");
71              System.err.println("  -default      Use the default preferences for better interoperability");
72          } else {
73              org.xmlhammer.gui.XMLHammer.main(args);
74          }
75      }
76      
77      private static boolean useCommandLineInterface(String[] args) {
78          for (String arg : args) {
79              if (arg.equals("-cli")) {
80                  return true;
81              }
82          }
83          
84          return false;
85      }
86  
87      private static boolean usingExe(String[] args) {
88          for (String arg : args) {
89              if (arg.equals("-exe")) {
90                  return true;
91              }
92          }
93          
94          return false;
95      }
96  }