package com.netapp.myasup.reports.urp.util.validator; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.log4j.Logger; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.ParserAdapter; /** * XMLParser is SAX parser class which is a generic parser * to parse data from url based on the filtered list *

* * @author Shashi * @version %I%, %G% * @since 1.0 */ public class XMLParser extends DefaultHandler { private static Logger logger = Logger.getLogger(XMLParser.class); //Currently parsed element private String currentElementData; //tag_name,tag_content_data pair private Map parsedDataMap; //list of tag that need to be parsed private List filterList; //append the data from multiple alike tags or not private boolean append = false; //Parser Adapter private ParserAdapter pa; private boolean contentParseEnded=false; private boolean isSysVersion=false; public XMLParser() { logger.info("Initializing the SAX parser for Custom parsing module"); try{ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); logger.debug("CLASS :"+sp.getClass()); logger.debug(System.getProperty("javax.xml.parsers.SAXParserFactory")); pa = new ParserAdapter(sp.getParser()); parsedDataMap = new HashMap(); }catch(Exception e){ logger.error("Parser initialization failed",e); } } //Method called when parser encounters open tag public synchronized void startElement(String namespace, String localName, String qName, Attributes atts) throws SAXException { logger.debug("StartElement Name:"+localName); this.currentElementData=""; this.contentParseEnded=false; } //Method called when parser encounters close tag public synchronized void endElement(String uri, String localName, String qName) throws SAXException { logger.debug("EndElement Name:"+localName); this.contentParseEnded=true; for (String key : filterList) { //parsed data map should collect only data which is in the filter list if (localName.equalsIgnoreCase(key)) { if(localName.equalsIgnoreCase("sys_version")){ if(isSysVersion){ this.isSysVersion=false; break; }else{ this.isSysVersion=true; } } if (parsedDataMap.get(key) != null && append == true) { //append if the data is already exists and append is true logger.debug("Appending data for the tag: "+key); parsedDataMap.put(key, parsedDataMap.get(key) + ParserConstants.SAX_DELIM + currentElementData); } else { //just add the data if append is false, so it will contain the contents of the last tag logger.debug("Adding data for the tag: "+key); parsedDataMap.put(key, currentElementData); } } } } //Method called when parser encounters data between open-close tag public synchronized void characters(char[] incbuf, int start, int len) throws SAXException { /*char[] cbuf = new char[len]; System.arraycopy(incbuf, 0, cbuf, 0, len);*/ if(!this.contentParseEnded){ if(this.currentElementData.equals("")){ this.currentElementData=new String(incbuf,start,len); }else{ this.currentElementData=this.currentElementData+new String(incbuf,start,len); } logger.debug("Character parsed: "+this.currentElementData); } } public synchronized void processingInstruction(String target, String data) throws SAXException{ logger.debug("ProcessingInstruction:"+target+" "+data); } public synchronized Map parse(InputStream is, List filterList, boolean append) throws SAXException, IOException{ //Initialize the variables this.filterList = filterList; this.append = append; //Initalize new parser content handler pa.setContentHandler(this); pa.parse(new InputSource(is)); return this.parsedDataMap; } //Unit testing module /*public static void main(String[] args) { String sectionURL = "http://rtpwil-rest-qa01.rtp.netapp.com:8013/asup-rest-interface/ASUP_DATA/client_id/nishantp/biz_key/N%7C56C55F79-0517-9798-A84D-579FACDA3235%7C8640734950%7C3077666/section_view/PRIORITY-SHOW"; ArrayList dataToBeFound = new ArrayList(); dataToBeFound.add("data"); Utilities util = new Utilities(); Map parsedSectionData = util.parseData(sectionURL, dataToBeFound, true); Setkeys = parsedSectionData.keySet(); System.out.println("--------------- Main --------------------"); for (String string : keys) { System.out.println(string+":"+parsedSectionData.get(string)); } }*/ }