1 37 package org.webharvest.definition; 38 39 import org.webharvest.utils.Catalog; 40 41 import java.io.*; 42 import java.net.URL ; 43 import java.util.ArrayList ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 51 public class ScraperConfiguration { 52 53 public static final String DEFAULT_CHARSET = "UTF-8"; 54 55 private Map functionDefs = new Catalog(); 57 58 private List operations = new ArrayList (); 60 61 private String charset = DEFAULT_CHARSET; 62 63 private File sourceFile; 64 private String url; 65 66 71 public ScraperConfiguration(InputStream in) { 72 XmlNode node = XmlNode.getInstance( new BufferedInputStream(in) ); 74 75 String charsetString = node.getString("charset"); 76 this.charset = charsetString != null ? charsetString : DEFAULT_CHARSET; 77 78 79 List elementList = node.getElementList(); 80 Iterator it = elementList.iterator(); 81 while (it.hasNext()) { 82 Object 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 98 public ScraperConfiguration(File sourceFile) throws FileNotFoundException { 99 this( new FileInputStream(sourceFile) ); 100 this.sourceFile = sourceFile; 101 } 102 103 108 public ScraperConfiguration(String sourceFilePath) throws FileNotFoundException { 109 this( new File(sourceFilePath) ); 110 } 111 112 118 public ScraperConfiguration(URL sourceUrl) throws IOException { 119 this( sourceUrl.openStream() ); 120 this.url = sourceUrl.toString(); 121 } 122 123 public List getOperations() { 124 return operations; 125 } 126 127 public String getCharset() { 128 return charset; 129 } 130 131 public FunctionDef getFunctionDef(String 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 getUrl() { 144 return this.url; 145 } 146 147 } | Popular Tags |