public Configuration(String file, String dbHome) throws DatabaseConfigurationException { try { String pathSep = System.getProperty("file.separator", "/");  File f = new File(file); if((!f.isAbsolute()) && dbHome != null) { file = dbHome + pathSep + file; f = new File(file); } if (!f.canRead()) { System.err.println("unable to read configuration. Trying to guess location ...");   if (dbHome == null) {  dbHome = System.getProperty("exist.home");  if (dbHome == null) dbHome = System.getProperty("user.dir"); }  if (dbHome != null) file = dbHome + pathSep + file; f = new File(file); if(!f.canRead()) { LOG.warn("giving up"); throw new DatabaseConfigurationException("unable to read configuration file"); } } this.file = file;   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); builder.setErrorHandler(this); InputSource src = new InputSource(new FileReader(file)); Document doc = builder.parse(src); Element root = doc.getDocumentElement(); NodeList parser = doc.getElementsByTagName("indexer");  if (parser.getLength() > 0) { Element p = (Element) parser.item(0); String tmp = p.getAttribute("tmpDir"); String batchLoad = p.getAttribute("batchLoad"); String parseNum = p.getAttribute("parseNumbers"); String indexDepth = p.getAttribute("index-depth"); String stemming = p.getAttribute("stemming"); String ctlDir = p.getAttribute("controls"); String suppressWS = p.getAttribute("suppress-whitespace"); String caseSensitive = p.getAttribute("caseSensitive"); String tokenizer = p.getAttribute("tokenizer"); String validation = p.getAttribute("validation");  if (tmp != null) config.put("tmpDir", tmp);  if (batchLoad != null) config.put( "batchLoad", new Boolean(batchLoad.equals("true")));  if (parseNum != null) config.put( "indexer.indexNumbers", new Boolean(parseNum.equals("true")));  if (ctlDir != null) config.put("parser.ctlDir", ctlDir);  if (stemming != null) config.put("indexer.stem", new Boolean(stemming.equals("true")));  if (caseSensitive != null) config.put( "indexer.case-sensitive", new Boolean(caseSensitive.equals("true")));  if (suppressWS != null) config.put("indexer.suppress-whitespace", suppressWS);  if (validation != null) config.put("indexer.validation", validation); if (tokenizer != null) config.put("indexer.tokenizer", tokenizer);  if (indexDepth != null) try { int depth = Integer.parseInt(indexDepth); config.put("indexer.index-depth", new Integer(depth)); } catch (NumberFormatException e) { }  NodeList index = p.getElementsByTagName("index");  for (int i = 0; i < index.getLength(); i++) { Element idx = (Element) index.item(i); String doctype = idx.getAttribute("doctype"); String def = idx.getAttribute("default"); IndexPaths paths = new IndexPaths(def.equals("all")); String indexAttributes = idx.getAttribute("attributes");  if (indexAttributes != null) paths.setIncludeAttributes( indexAttributes.equals("true"));  String indexAlphaNum = idx.getAttribute("alphanum");  if (indexAlphaNum != null) paths.setIncludeAlphaNum(indexAlphaNum.equals("true"));  indexDepth = idx.getAttribute("index-depth"); if (indexDepth != null) try { int depth = Integer.parseInt(indexDepth); paths.setIndexDepth(depth); } catch (NumberFormatException e) { }  NodeList include = idx.getElementsByTagName("include"); String ps;  for (int j = 0; j < include.getLength(); j++) { ps = ((Element) include.item(j)).getAttribute("path"); paths.addInclude(ps); }  config.put("indexScheme." + doctype, paths);  NodeList exclude = idx.getElementsByTagName("exclude");  for (int j = 0; j < exclude.getLength(); j++) { ps = ((Element) exclude.item(j)).getAttribute("path"); paths.addExclude(ps); } }  NodeList stopwords = p.getElementsByTagName("stopwords");  if (stopwords.getLength() > 0) { String stopwordFile = ((Element) stopwords.item(0)).getAttribute("file"); File sf = new File(stopwordFile);  if (!sf.canRead()) { stopwordFile = dbHome + pathSep + stopwordFile; sf = new File(stopwordFile);  if (sf.canRead()) config.put("stopwords", stopwordFile); } }  CatalogResolver resolver = new CatalogResolver(); config.put("resolver", resolver);  NodeList entityResolver = p.getElementsByTagName("entity-resolver");  if (entityResolver.getLength() > 0) { Element r = (Element) entityResolver.item(0); NodeList catalogs = r.getElementsByTagName("catalog"); String catalog; File catalogFile;  for (int i = 0; i < catalogs.getLength(); i++) { catalog = ((Element) catalogs.item(i)).getAttribute("file");  if (pathSep.equals("\\")) catalog = catalog.replace('/', '\\');  if (dbHome == null) catalogFile = new File(catalog); else catalogFile = new File(dbHome + pathSep + catalog);  if (catalogFile.exists()) resolver.getCatalog().parseCatalog( catalogFile.getAbsolutePath()); } } }  NodeList dbcon = doc.getElementsByTagName("db-connection");  if (dbcon.getLength() > 0) { Element con = (Element) dbcon.item(0); String cacheSize = con.getAttribute("cacheSize"); String pageSize = con.getAttribute("pageSize"); String dataFiles = con.getAttribute("files"); String buffers = con.getAttribute("buffers"); String collBuffers = con.getAttribute("collection_buffers"); String wordBuffers = con.getAttribute("words_buffers"); String elementBuffers = con.getAttribute("elements_buffers"); String freeMem = con.getAttribute("free_mem_min"); String driver = con.getAttribute("driver"); String url = con.getAttribute("url"); String user = con.getAttribute("user"); String pass = con.getAttribute("password"); String compress = con.getAttribute("compress"); String mysql = con.getAttribute("database"); String service = con.getAttribute("serviceName"); String encoding = con.getAttribute("encoding");  if (compress != null) config.put("db-connection.compress", compress);  if (driver != null) config.put("driver", driver);  if (url != null) config.put("url", url);  if (user != null) config.put("user", user);  if (pass != null) config.put("password", pass);  if (mysql != null) config.put("database", mysql);  if (encoding != null) config.put("encoding", encoding);  if (service != null) config.put("db-connection.serviceName", service);   if (dataFiles != null) { File df = new File(dataFiles); if ((!df.isAbsolute()) && dbHome != null) { dataFiles = dbHome + pathSep + dataFiles; df = new File(dataFiles); } if (!df.canRead()) throw new DatabaseConfigurationException( "cannot read data directory: " + df.getAbsolutePath());  config.put("db-connection.data-dir", df.getAbsolutePath()); }  if (cacheSize != null) try { config.put( "db-connection.cache-size", new Integer(cacheSize)); } catch (NumberFormatException nfe) { }  if (buffers != null) try { config.put( "db-connection.buffers", new Integer(buffers)); } catch (NumberFormatException nfe) { }  if (pageSize != null) try { config.put( "db-connection.page-size", new Integer(pageSize)); } catch (NumberFormatException nfe) { }  if (collBuffers != null) try { config.put( "db-connection.collections.buffers", new Integer(collBuffers)); } catch (NumberFormatException nfe) { }  if (wordBuffers != null) try { config.put( "db-connection.words.buffers", new Integer(wordBuffers)); } catch (NumberFormatException nfe) { }  if (elementBuffers != null) try { config.put( "db-connection.elements.buffers", new Integer(elementBuffers)); } catch (NumberFormatException nfe) { }  if (freeMem != null) try { config.put( "db-connection.min_free_memory", new Integer(freeMem)); } catch (NumberFormatException nfe) { } NodeList poolConf = con.getElementsByTagName("pool"); if(poolConf.getLength() > 0) { Element pool = (Element)poolConf.item(0); String min = pool.getAttribute("min"); String max = pool.getAttribute("max"); String idle = pool.getAttribute("idle"); if(min != null) try { config.put("db-connection.pool.min", new Integer(min)); } catch(NumberFormatException e) { } if(max != null) try { config.put("db-connection.pool.max", new Integer(max)); } catch(NumberFormatException e) { } if(idle != null) try { config.put("db-connection.pool.idle", new Integer(idle)); } catch(NumberFormatException e) { } } }  NodeList serializers = doc.getElementsByTagName("serializer"); Element serializer; if(serializers.getLength() > 0) { serializer = (Element)serializers.item(0); String xinclude = serializer.getAttribute("enable-xinclude"); if(xinclude != null) config.put("serialization.enable-xinclude", xinclude); String xsl = serializer.getAttribute("enable-xsl"); if(xsl != null) config.put("serialization.enable-xsl", xsl); String indent = serializer.getAttribute("indent"); if(indent != null) config.put("serialization.indent", indent); String internalId = serializer.getAttribute("add-exist-id"); if(internalId != null) config.put("serialization.add-exist-id", internalId); String matchTagging = serializer.getAttribute("match-tagging"); if(matchTagging != null) config.put("serialization.match-tagging", matchTagging); }  NodeList log4j = doc.getElementsByTagName("log4j:configuration");  if (log4j.getLength() > 0) { Element logRoot = (Element) log4j.item(0);   if (dbHome != null) { NodeList params = logRoot.getElementsByTagName("param"); Element param; String name; String path;  if (pathSep.equals("\\")) dbHome = dbHome.replace('\\', '/');  for (int i = 0; i < params.getLength(); i++) { param = (Element) params.item(i); name = param.getAttribute("name");  if ((name != null) && name.equalsIgnoreCase("File")) { path = param.getAttribute("value");  if (path != null) {   f = new File(path);  if (!f.isAbsolute()) path = dbHome + '/' + path; }  param.setAttribute("value", path); } } }  DOMConfigurator.configure(logRoot); } } catch (SAXException e) { LOG.warn("error while reading config file: " + file, e); throw new DatabaseConfigurationException(e.getMessage()); } catch (ParserConfigurationException cfg) { LOG.warn("error while reading config file: " + file, cfg); throw new DatabaseConfigurationException(cfg.getMessage()); } catch (IOException io) { LOG.warn("error while reading config file: " + file, io); throw new DatabaseConfigurationException(io.getMessage()); }
public Configuration(String file) throws DatabaseConfigurationException { this(file, null);
	public Configuration(String file, String dbHome)		throws DatabaseConfigurationException {		try {			String pathSep = System.getProperty("file.separator", "/");				File f = new File(file);			if((!f.isAbsolute()) && dbHome != null) {				file = dbHome + pathSep + file;				f = new File(file);			}			if (!f.canRead()) {				System.err.println("unable to read configuration. Trying to guess location ...");								// fall back and try to read from home directory				if (dbHome == null) {					// try to determine exist home directory					dbHome = System.getProperty("exist.home");					if (dbHome == null)						dbHome = System.getProperty("user.dir");				}				if (dbHome != null)					file = dbHome + pathSep + file;				f = new File(file);				if(!f.canRead()) {					LOG.warn("giving up");					throw new DatabaseConfigurationException("unable to read configuration file");				}			}			this.file = file;							// initialize xml parser			DocumentBuilderFactory factory =				DocumentBuilderFactory.newInstance();			builder = factory.newDocumentBuilder();			builder.setErrorHandler(this);			InputSource src = new InputSource(new FileReader(file));			Document doc = builder.parse(src);			Element root = doc.getDocumentElement();			NodeList parser = doc.getElementsByTagName("indexer");			if (parser.getLength() > 0) {				Element p = (Element) parser.item(0);				String tmp = p.getAttribute("tmpDir");				String batchLoad = p.getAttribute("batchLoad");				String parseNum = p.getAttribute("parseNumbers");				String indexDepth = p.getAttribute("index-depth");				String stemming = p.getAttribute("stemming");				String ctlDir = p.getAttribute("controls");				String suppressWS = p.getAttribute("suppress-whitespace");				String caseSensitive = p.getAttribute("caseSensitive");				String tokenizer = p.getAttribute("tokenizer");				String validation = p.getAttribute("validation");				if (tmp != null)					config.put("tmpDir", tmp);				if (batchLoad != null)					config.put(						"batchLoad",						new Boolean(batchLoad.equals("true")));				if (parseNum != null)					config.put(						"indexer.indexNumbers",						new Boolean(parseNum.equals("true")));				if (ctlDir != null)					config.put("parser.ctlDir", ctlDir);				if (stemming != null)					config.put("indexer.stem", new Boolean(stemming.equals("true")));				if (caseSensitive != null)					config.put(						"indexer.case-sensitive",						new Boolean(caseSensitive.equals("true")));				if (suppressWS != null)					config.put("indexer.suppress-whitespace", suppressWS);				if (validation != null)					config.put("indexer.validation", validation);				if (tokenizer != null)					config.put("indexer.tokenizer", tokenizer);									if (indexDepth != null)					try {						int depth = Integer.parseInt(indexDepth);						config.put("indexer.index-depth", new Integer(depth));					} catch (NumberFormatException e) {					}				NodeList index = p.getElementsByTagName("index");				for (int i = 0; i < index.getLength(); i++) {					Element idx = (Element) index.item(i);					String doctype = idx.getAttribute("doctype");					String def = idx.getAttribute("default");					IndexPaths paths = new IndexPaths(def.equals("all"));					String indexAttributes = idx.getAttribute("attributes");					if (indexAttributes != null)						paths.setIncludeAttributes(							indexAttributes.equals("true"));					String indexAlphaNum = idx.getAttribute("alphanum");					if (indexAlphaNum != null)						paths.setIncludeAlphaNum(indexAlphaNum.equals("true"));					indexDepth = idx.getAttribute("index-depth");					if (indexDepth != null)						try {							int depth = Integer.parseInt(indexDepth);							paths.setIndexDepth(depth);						} catch (NumberFormatException e) {						}					NodeList include = idx.getElementsByTagName("include");					String ps;					for (int j = 0; j < include.getLength(); j++) {						ps = ((Element) include.item(j)).getAttribute("path");						paths.addInclude(ps);					}					config.put("indexScheme." + doctype, paths);					NodeList exclude = idx.getElementsByTagName("exclude");					for (int j = 0; j < exclude.getLength(); j++) {						ps = ((Element) exclude.item(j)).getAttribute("path");						paths.addExclude(ps);					}				}				NodeList stopwords = p.getElementsByTagName("stopwords");				if (stopwords.getLength() > 0) {					String stopwordFile =						((Element) stopwords.item(0)).getAttribute("file");					File sf = new File(stopwordFile);					if (!sf.canRead()) {						stopwordFile = dbHome + pathSep + stopwordFile;						sf = new File(stopwordFile);						if (sf.canRead())							config.put("stopwords", stopwordFile);					}				}				CatalogResolver resolver = new CatalogResolver();				config.put("resolver", resolver);				NodeList entityResolver =					p.getElementsByTagName("entity-resolver");				if (entityResolver.getLength() > 0) {					Element r = (Element) entityResolver.item(0);					NodeList catalogs = r.getElementsByTagName("catalog");					String catalog;					File catalogFile;					for (int i = 0; i < catalogs.getLength(); i++) {						catalog =							((Element) catalogs.item(i)).getAttribute("file");						if (pathSep.equals("\\"))							catalog = catalog.replace('/', '\\');						if (dbHome == null)							catalogFile = new File(catalog);						else							catalogFile = new File(dbHome + pathSep + catalog);						if (catalogFile.exists())							resolver.getCatalog().parseCatalog(								catalogFile.getAbsolutePath());					}				}			}			NodeList dbcon = doc.getElementsByTagName("db-connection");			if (dbcon.getLength() > 0) {				Element con = (Element) dbcon.item(0);				String cacheSize = con.getAttribute("cacheSize");				String pageSize = con.getAttribute("pageSize");				String dataFiles = con.getAttribute("files");				String buffers = con.getAttribute("buffers");				String collBuffers = con.getAttribute("collection_buffers");				String wordBuffers = con.getAttribute("words_buffers");				String elementBuffers = con.getAttribute("elements_buffers");				String freeMem = con.getAttribute("free_mem_min");				String driver = con.getAttribute("driver");				String url = con.getAttribute("url");				String user = con.getAttribute("user");				String pass = con.getAttribute("password");				String compress = con.getAttribute("compress");				String mysql = con.getAttribute("database");				String service = con.getAttribute("serviceName");				String encoding = con.getAttribute("encoding");				if (compress != null)					config.put("db-connection.compress", compress);				if (driver != null)					config.put("driver", driver);				if (url != null)					config.put("url", url);				if (user != null)					config.put("user", user);				if (pass != null)					config.put("password", pass);				if (mysql != null)					config.put("database", mysql);				if (encoding != null)					config.put("encoding", encoding);				if (service != null)					config.put("db-connection.serviceName", service);				// directory for database files				if (dataFiles != null) {					File df = new File(dataFiles);					if ((!df.isAbsolute()) && dbHome != null) {						dataFiles = dbHome + pathSep + dataFiles;						df = new File(dataFiles);					}					if (!df.canRead())						throw new DatabaseConfigurationException(							"cannot read data directory: "								+ df.getAbsolutePath());					config.put("db-connection.data-dir", df.getAbsolutePath());				}				if (cacheSize != null)					try {						config.put(							"db-connection.cache-size",							new Integer(cacheSize));					} catch (NumberFormatException nfe) {					}				if (buffers != null)					try {						config.put(							"db-connection.buffers",							new Integer(buffers));					} catch (NumberFormatException nfe) {					}				if (pageSize != null)					try {						config.put(							"db-connection.page-size",							new Integer(pageSize));					} catch (NumberFormatException nfe) {					}				if (collBuffers != null)					try {						config.put(							"db-connection.collections.buffers",							new Integer(collBuffers));					} catch (NumberFormatException nfe) {					}				if (wordBuffers != null)					try {						config.put(							"db-connection.words.buffers",							new Integer(wordBuffers));					} catch (NumberFormatException nfe) {					}				if (elementBuffers != null)					try {						config.put(							"db-connection.elements.buffers",							new Integer(elementBuffers));					} catch (NumberFormatException nfe) {					}				if (freeMem != null)					try {						config.put(							"db-connection.min_free_memory",							new Integer(freeMem));					} catch (NumberFormatException nfe) {					}				NodeList poolConf = con.getElementsByTagName("pool");				if(poolConf.getLength() > 0) {					Element pool = (Element)poolConf.item(0);					String min = pool.getAttribute("min");					String max = pool.getAttribute("max");					String idle = pool.getAttribute("idle");					if(min != null)						try {							config.put("db-connection.pool.min",								new Integer(min));						} catch(NumberFormatException e) {						}					if(max != null)						try {							config.put("db-connection.pool.max",								new Integer(max));						} catch(NumberFormatException e) {						}					if(idle != null)						try {							config.put("db-connection.pool.idle",								new Integer(idle));						} catch(NumberFormatException e) {						}				}			}			NodeList serializers = doc.getElementsByTagName("serializer");			Element serializer;			if(serializers.getLength() > 0) {				serializer = (Element)serializers.item(0);				String xinclude = serializer.getAttribute("enable-xinclude");				if(xinclude != null)					config.put("serialization.enable-xinclude", xinclude);				String xsl = serializer.getAttribute("enable-xsl");				if(xsl != null)					config.put("serialization.enable-xsl", xsl);				String indent = serializer.getAttribute("indent");				if(indent != null)					config.put("serialization.indent", indent);                String internalId = serializer.getAttribute("add-exist-id");                if(internalId != null)                    config.put("serialization.add-exist-id", internalId);                String matchTagging = serializer.getAttribute("match-tagging");                if(matchTagging != null)                	config.put("serialization.match-tagging", matchTagging);			}						NodeList log4j = doc.getElementsByTagName("log4j:configuration");			if (log4j.getLength() > 0) {				Element logRoot = (Element) log4j.item(0);				// make files relative if dbHome != null				if (dbHome != null) {					NodeList params = logRoot.getElementsByTagName("param");					Element param;					String name;					String path;					if (pathSep.equals("\\"))						dbHome = dbHome.replace('\\', '/');					for (int i = 0; i < params.getLength(); i++) {						param = (Element) params.item(i);						name = param.getAttribute("name");						if ((name != null) && name.equalsIgnoreCase("File")) {							path = param.getAttribute("value");							if (path != null) {								//if ( pathSep.equals( "\\" ) )								//    path = path.replace( '/', '\\' );								f = new File(path);								if (!f.isAbsolute())									path = dbHome + '/' + path;							}							param.setAttribute("value", path);						}					}				}				DOMConfigurator.configure(logRoot);			}		} catch (SAXException e) {			LOG.warn("error while reading config file: " + file, e);			throw new DatabaseConfigurationException(e.getMessage());		} catch (ParserConfigurationException cfg) {			LOG.warn("error while reading config file: " + file, cfg);			throw new DatabaseConfigurationException(cfg.getMessage());		} catch (IOException io) {			LOG.warn("error while reading config file: " + file, io);			throw new DatabaseConfigurationException(io.getMessage());		}	}