Source code: com/telefonicasoluciones/search/server/HLConfig.java
1 package com.telefonicasoluciones.search.server;
2
3 /**
4 * Recoje la configuración.
5 * Creation date: (15/11/2001 9:43:08)
6 *
7 * @author: Ricardo Lorenzo
8 */
9 import java.io.*;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import org.apache.xerces.parsers.DOMParser;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17
18 public class HLConfig {
19 private HashMap configurations;
20 private ArrayList jobs;
21 /**
22 * Crea un objeto HLConfig, debe especificarse
23 * la ruta del fichero de configración.
24 */
25 public HLConfig(String path) throws HLConfigException {
26 boolean loadConfig = false;
27 try {
28 File configFile = new File(path);
29 if(configFile.exists()) {
30 if(configFile.isDirectory()) {
31 if(path.lastIndexOf(File.separator)==path.length())
32 configFile = new File(path+"headlight.xml");
33 else
34 configFile = new File(path+File.separator+"headlight.xml");
35 if(configFile.exists()) loadConfig=true;
36 } else loadConfig=true;
37 }
38 if(loadConfig) {
39 parse(configFile);
40 } else throw new HLConfigException("Can't find config file");
41 } catch (HLConfigException e) {
42 throw e;
43 }
44 }
45 /**
46 * Insert the method's description here.
47 * Creation date: (10/01/2002 9:11:58)
48 * @return java.util.HashMap
49 * @param user java.lang.String
50 */
51 public HashMap getConfiguration(String user) {
52 return (HashMap) configurations.get(user);
53 }
54 /**
55 * Insert the method's description here.
56 * Creation date: (10/01/2002 9:11:58)
57 * @return java.util.ArrayList
58 * @param user java.lang.String
59 */
60 public ArrayList getJobs() {
61 return jobs;
62 }
63 /**
64 * Insert the method's description here.
65 * Creation date: (15/11/2001 9:29:37)
66 * @param configFile java.lang.String
67 */
68 private void parse(File configFile) throws HLConfigException {
69 try {
70 DOMParser parser = new DOMParser();
71 parser.parse(new org.xml.sax.InputSource(new FileInputStream(configFile)));
72 Document documento = parser.getDocument();
73 configurations = new HashMap();
74 jobs = new ArrayList();
75 NodeList parentList = documento.getElementsByTagName("headlight");
76 Element parentElement = (Element) parentList.item(0);
77 NodeList configurationList = parentElement.getElementsByTagName("user");
78 Element elemento;
79 for(int i=0; i<configurationList.getLength(); i++) {
80 Element configuration = (Element) configurationList.item(i);
81 HashMap configItems = new HashMap();
82 String item;
83 NodeList tempNode = configuration.getElementsByTagName("indexdir");
84 item = tempNode.item(0).getFirstChild().getNodeValue();
85 if(item.lastIndexOf("/")<item.length())
86 item+="/";
87 configItems.put("indexdir",item);
88 tempNode = configuration.getElementsByTagName("tempdir");
89 item = tempNode.item(0).getFirstChild().getNodeValue();
90 if(item.lastIndexOf("/")<item.length())
91 item+="/";
92 configItems.put("tempdir",item);
93 tempNode = configuration.getElementsByTagName("logdir");
94 item = tempNode.item(0).getFirstChild().getNodeValue();
95 if(item.lastIndexOf("/")<item.length())
96 item+="/";
97 configItems.put("logdir",item);
98 tempNode = configuration.getElementsByTagName("file-type");
99 HashMap fileTypes = new HashMap();
100 for(int j=0; j<tempNode.getLength(); j++) {
101 Element fileType = (Element) tempNode.item(j);
102 fileTypes.put(fileType.getAttribute("id"),fileType.getFirstChild().getNodeValue());
103 }
104 configItems.put("file-type",fileTypes);
105 configurations.put(configuration.getAttribute("id"),configItems);
106 tempNode = configuration.getElementsByTagName("job");
107 for(int j=0; j<tempNode.getLength(); j++) {
108 Element fileType = (Element) tempNode.item(j);
109 if(fileType.getAttribute("type").toLowerCase().trim().equals("weekly")) {
110 ArrayList a = new ArrayList();
111 a.add(0,"weekly");
112 a.add(1,fileType.getFirstChild().getNodeValue());
113 jobs.add(a);
114 } else if(fileType.getAttribute("type").toLowerCase().trim().equals("monthly")) {
115 ArrayList a = new ArrayList();
116 a.add(0,"monthly");
117 a.add(1,fileType.getFirstChild().getNodeValue());
118 jobs.add(a);
119 } else if(fileType.getAttribute("type").toLowerCase().trim().equals("daily")) {
120 ArrayList a = new ArrayList();
121 a.add(0,"daily");
122 a.add(1,fileType.getFirstChild().getNodeValue());
123 jobs.add(a);
124 }
125 }
126 }
127 } catch (NullPointerException npe) {
128 throw new HLConfigException("Null pointer");
129 } catch (Exception e) {
130 throw new HLConfigException(e.getMessage());
131 }
132 }
133 }