001 /** 002 * 003 */ 004 package org.wdssii.core; 005 006 import java.io.File; 007 import java.io.FileInputStream; 008 import java.io.InputStream; 009 import java.util.Set; 010 import java.util.zip.GZIPInputStream; 011 012 import javax.xml.parsers.SAXParser; 013 import javax.xml.parsers.SAXParserFactory; 014 015 import org.apache.commons.logging.Log; 016 import org.apache.commons.logging.LogFactory; 017 018 /** 019 * @author jianting.zhang 020 * @author lakshman 021 * 022 */ 023 public class XMLIndex extends Index { 024 private static Log log = LogFactory.getLog(XMLIndex.class); 025 026 private boolean initComplete = false; 027 028 public Index newInstance(String path, Set<IndexRecordListener> listeners) 029 throws DataUnavailableException { 030 return new XMLIndex(new File(path), listeners); 031 } 032 033 /** meant for prototype factory use only. */ 034 public XMLIndex() { 035 super(null, null); 036 } 037 038 public void update() { 039 if (!initComplete) { 040 return; 041 } 042 throw new UnsupportedOperationException("Not a real-time index"); 043 } 044 045 public XMLIndex(File path, Set<IndexRecordListener> listeners) 046 throws DataUnavailableException { 047 super(path.getParent(), listeners); 048 try { 049 if (log.isInfoEnabled()) { 050 log.info("Reading records from " + path.getAbsolutePath() 051 + " indexLocation=" + getIndexLocation()); 052 } 053 // open file 054 InputStream is = new FileInputStream(path); 055 if (path.getAbsolutePath().endsWith(".gz")) { 056 is = new GZIPInputStream(is); 057 } 058 // parse it 059 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); 060 saxParser.parse(is, new SAXIndexHandler(this)); 061 } catch (Exception e) { 062 log.error("Failed to load index from " + path, e); 063 throw new DataUnavailableException(e); 064 } 065 // avoid update() being called before we are complete 066 initComplete = true; 067 } 068 }