KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > definition > ScraperConfiguration


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 package org.webharvest.definition;
38
39 import org.webharvest.utils.Catalog;
40
41 import java.io.*;
42 import java.net.URL JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * Basic configuration.
50  */

51 public class ScraperConfiguration {
52     
53     public static final String JavaDoc DEFAULT_CHARSET = "UTF-8";
54
55     // map of function definitions
56
private Map JavaDoc functionDefs = new Catalog();
57
58     // sequence of operationDefs
59
private List JavaDoc operations = new ArrayList JavaDoc();
60     
61     private String JavaDoc charset = DEFAULT_CHARSET;
62
63     private File sourceFile;
64     private String JavaDoc url;
65
66     /**
67      * Creates configuration instance loaded from the specified input stream.
68      *
69      * @param in
70      */

71     public ScraperConfiguration(InputStream in) {
72         // loads configuration from input stream to the internal structure
73
XmlNode node = XmlNode.getInstance( new BufferedInputStream(in) );
74
75         String JavaDoc charsetString = node.getString("charset");
76         this.charset = charsetString != null ? charsetString : DEFAULT_CHARSET;
77
78
79         List JavaDoc elementList = node.getElementList();
80         Iterator JavaDoc it = elementList.iterator();
81         while (it.hasNext()) {
82             Object JavaDoc element = it.next();
83             if (element instanceof XmlNode) {
84                 XmlNode currElementNode = (XmlNode) element;
85                 operations.add( DefinitionResolver.createElementDefinition(currElementNode) );
86             } else {
87                 operations.add( new ConstantDef(element.toString()) );
88             }
89         }
90     }
91
92     /**
93      * Creates configuration instance loaded from the specified File.
94      *
95      * @param sourceFile
96      * @throws FileNotFoundException
97      */

98     public ScraperConfiguration(File sourceFile) throws FileNotFoundException {
99         this( new FileInputStream(sourceFile) );
100         this.sourceFile = sourceFile;
101     }
102
103     /**
104      * Creates configuration instance loaded from the file specified by filename.
105      *
106      * @param sourceFilePath
107      */

108     public ScraperConfiguration(String JavaDoc sourceFilePath) throws FileNotFoundException {
109         this( new File(sourceFilePath) );
110     }
111
112     /**
113      * Creates configuration instance loaded from specified URL.
114      *
115      * @param sourceUrl
116      * @throws IOException
117      */

118     public ScraperConfiguration(URL JavaDoc sourceUrl) throws IOException {
119         this( sourceUrl.openStream() );
120         this.url = sourceUrl.toString();
121     }
122
123     public List JavaDoc getOperations() {
124         return operations;
125     }
126
127     public String JavaDoc getCharset() {
128         return charset;
129     }
130
131     public FunctionDef getFunctionDef(String JavaDoc name) {
132         return (FunctionDef) functionDefs.get(name);
133     }
134
135     public void addFunctionDef(FunctionDef funcDef) {
136         functionDefs.put(funcDef.getName(), funcDef);
137     }
138
139     public File getSourceFile() {
140         return this.sourceFile;
141     }
142
143     public String JavaDoc getUrl() {
144         return this.url;
145     }
146
147 }
Popular Tags