001 /** 002 * 003 */ 004 package org.wdssii.core; 005 006 import org.xml.sax.Attributes; 007 import org.xml.sax.SAXException; 008 import org.xml.sax.helpers.DefaultHandler; 009 010 /** 011 * @author lakshman 012 */ 013 public class SAXIndexHandler extends DefaultHandler { 014 015 static class ElementHandler { 016 private String elementName; 017 private StringBuilder text = new StringBuilder(); 018 ElementHandler(String elementName){ 019 this.elementName = elementName; 020 } 021 void handleCharacters(char[] str, int start, int len){ 022 text.append(str, start, len); 023 } 024 String getText(){ 025 return text.toString(); 026 } 027 } 028 029 private Index index; 030 private ElementHandler item; 031 private ElementHandler time; 032 private ElementHandler params; 033 private ElementHandler selections; 034 private ElementHandler[] all; 035 private ElementHandler current; 036 private String frac; 037 038 private void reinit(){ 039 item = new ElementHandler("item"); 040 time = new ElementHandler("time"); 041 params = new ElementHandler("params"); 042 selections = new ElementHandler("selections"); 043 all = new ElementHandler[]{item,time,params,selections}; 044 current = null; 045 frac = null; 046 } 047 048 public SAXIndexHandler(Index index) { 049 this.index = index; 050 reinit(); 051 } 052 053 @Override 054 public void characters(char[] buf, int start, int len) throws SAXException { 055 if (current != null){ 056 current.handleCharacters(buf,start,len); 057 } 058 } 059 060 @Override 061 public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException { 062 if (qualifiedName.equals("item")){ 063 addRecord(); 064 } 065 } 066 067 @Override 068 public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes atts) throws SAXException { 069 if (qualifiedName.equals("item")){ 070 reinit(); 071 } 072 for (ElementHandler e : all){ 073 if (e.elementName.equals(qualifiedName)){ 074 current = e; 075 } 076 } 077 if (current == time){ 078 frac = atts.getValue("fractional"); 079 } 080 } 081 082 private void addRecord(){ 083 String[] paramList = new String[] {params.getText()}; 084 String[] changes = new String[] {null}; 085 IndexRecord rec = IndexRecord.createIndexRecord(time.getText(), frac, paramList, changes, selections.getText(), index.getIndexLocation()); 086 index.addRecord(rec); 087 } 088 089 090 }