KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CommandLine


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

37 import java.util.Properties JavaDoc;
38 import java.io.FileNotFoundException JavaDoc;
39
40 import org.apache.log4j.PropertyConfigurator;
41 import org.webharvest.definition.ScraperConfiguration;
42 import org.webharvest.runtime.Scraper;
43
44 public class CommandLine {
45     
46     private static String JavaDoc getArgValue(String JavaDoc[] args, String JavaDoc name) {
47         for (int i = 0; i < args.length; i++) {
48             String JavaDoc curr = args[i];
49             int eqIndex = curr.indexOf('=');
50             if (eqIndex >= 0) {
51                 String JavaDoc argName = curr.substring(0, eqIndex).trim();
52                 String JavaDoc argValue = curr.substring(eqIndex+1).trim();
53                 
54                 if (argName.toLowerCase().startsWith(name.toLowerCase())) {
55                     return argValue;
56                 }
57             }
58         }
59         
60         return "";
61     }
62     
63     public static void main(String JavaDoc[] args) throws FileNotFoundException JavaDoc {
64         String JavaDoc configFilePath = getArgValue(args, "config");
65         if ("".equals(configFilePath)) {
66             System.err.println("You must specify configuration file path using config=<path> argument!");
67             System.exit(1);
68         }
69         
70         String JavaDoc workingDir = getArgValue(args, "workdir");
71         if ("".equals(workingDir)) {
72             System.err.println("You must specify working directory path using workdir=<path> argument!");
73             System.exit(1);
74         }
75         
76         String JavaDoc isDebug = getArgValue(args, "debug");
77         
78         Properties JavaDoc props = new Properties JavaDoc();
79         props.setProperty("log4j.rootLogger", "INFO, stdout");
80         props.setProperty("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
81         props.setProperty("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout");
82         props.setProperty("log4j.appender.stdout.layout.ConversionPattern", "%-5p (%20F:%-3L) - %m\n");
83
84         props.setProperty("log4j.appender.file", "org.apache.log4j.DailyRollingFileAppender");
85         props.setProperty("log4j.appender.file.File", workingDir + "/out.log");
86         props.setProperty("log4j.appender.file.DatePattern", "yyyy-MM-dd");
87         props.setProperty("log4j.appender.file.layout", "org.apache.log4j.PatternLayout");
88         props.setProperty("log4j.appender.file.layout.ConversionPattern", "%-5p (%20F:%-3L) - %m\n");
89       
90         PropertyConfigurator.configure(props);
91
92         ScraperConfiguration config = new ScraperConfiguration(configFilePath);
93         Scraper scraper = new Scraper(config, workingDir);
94         
95         if ("yes".equalsIgnoreCase(isDebug)) {
96             scraper.setDebug(true);
97         }
98         
99         String JavaDoc proxyHost = getArgValue(args, "proxyHost");
100         if (!"".equals(proxyHost)) {
101             String JavaDoc proxyPort = getArgValue(args, "proxyPort");
102             if (!"".equals(proxyPort)) {
103                 int port = Integer.parseInt(proxyPort);
104                 scraper.getHttpClientManager().setHttpProxy(proxyHost, port);
105             } else {
106                 scraper.getHttpClientManager().setHttpProxy(proxyHost);
107             }
108         }
109
110         String JavaDoc proxyUser = getArgValue(args, "proxyUser");
111         if (!"".equals(proxyUser)) {
112             String JavaDoc proxyPassword = getArgValue(args, "proxyPassword");
113             scraper.getHttpClientManager().setHttpProxyCredentials(proxyUser, proxyPassword);
114         }
115
116         scraper.execute();
117     }
118
119 }
Popular Tags