Team:Freiburg software/Code/BioBrick DAS Feature Handler.java

From 2009.igem.org

Revision as of 00:08, 22 October 2009 by JonasOnlyJonas (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

/* Copyright: synbiowave License: GPL Authors: Jörg Wassolesk Version: 0.1 DESCRIPTION: This class provides the methods for the handling of DAS feature requests. */ package org.synbiowave.biobrick; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; import java.util.ArrayList ; import java.util.HashMap ; import java.util.List; import java.util.Map; // TODO: Auto-generated Javadoc /** * a class to parse the response of a DAS - Feature request. * * @author Andreas Prlic */ public class BioBrick_DAS_Feature_Handler extends DefaultHandler{ /** The features. */ List<Map<String,String>> features ; /** The first_flag. */ boolean first_flag ; /** The feature. */ Map<String,String> feature ; /** The featurefield. */ String featurefield ; /** The characterdata. */ StringBuffer characterdata ; /** The das command. */ String dasCommand ; /** The come back later. */ int comeBackLater ; /** The max features. */ int maxFeatures ; /** The segment id. */ String segmentId ; /** The version. */ String version; /** The type_id. */ String type_id; /** The type_category. */ String type_category; /** * Instantiates a new bio brick_ da s_ feature_ handler. */ public BioBrick_DAS_Feature_Handler() { super(); features= new ArrayList<Map<String, String>>() ; first_flag = true ; featurefield = "" ; characterdata = new StringBuffer(); dasCommand = "" ; comeBackLater = -1; maxFeatures = -1; segmentId = ""; version = ""; type_id = ""; type_category=""; } /** * get the id information specified int the SEGMENT field of the DAS response. * * @return the segmentId or an emtpy string if not available */ public String getSegmentId() { return segmentId; } /** * get the version informationspecified in the SEGMENT field of the DAS response. * * @return the version information of an empty string if not available */ public String getVersion() { return version; } /** * Checks if is m d5 checksum. * * @return true, if is m d5 checksum */ public boolean isMD5Checksum(){ if ((version != null) && (version.length() == 32)) return true; return false; } /** * specifies a maximum number of features to be downloaded. if a * server returns more, they will be ignored. default is to load * all features * * @param max the maximium number of features to be downloaded */ public void setMaxFeatures(int max) { maxFeatures = max; } /** * Gets the max features. * * @return the max features */ public int getMaxFeatures() { return maxFeatures; } /** * Sets the dAS command. * * @param cmd the new dAS command */ public void setDASCommand(String cmd) { dasCommand = cmd ;} /** * Gets the dAS command. * * @return the dAS command */ public String getDASCommand() { return dasCommand; } /** * Gets the _features. * * @return the _features */ public List<Map<String,String>> get_features() { return features ; } /** * Gets the com back later. * * @return the com back later */ public int getComBackLater(){ return comeBackLater; } /** * Start_feature. * * @param uri the uri * @param name the name * @param qName the q name * @param atts the atts */ void start_feature(String uri, String name, String qName, Attributes atts) { if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) { characterdata = new StringBuffer(); return; } feature = new HashMap<String,String>() ; String id = atts.getValue("id"); feature.put("id",id); feature.put("dassource",dasCommand); characterdata = new StringBuffer(); } /** * Add_featuredata. * * @param uri the uri * @param name the name * @param qName the q name */ void add_featuredata(String uri, String name, String qName) { //System.out.println("featurefield "+featurefield+ " data "+characterdata); // NOTE can have multiple lines .. if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) { return; } String data = (String)feature.get(featurefield); String featureText = characterdata.toString(); if (data != null){ featureText = data + " " + featureText; } if ( qName.equals("TYPE")){ if ( featureText.length() < 1) featureText = type_id; feature.put("TYPE_ID",type_id); feature.put("TYPE_CATEGORY", type_category); type_id = ""; type_category=""; } feature.put(featurefield,featureText); featurefield = ""; characterdata = new StringBuffer(); } /** * Adds the link. * * @param uri the uri * @param name the name * @param qName the q name * @param atts the atts */ private void addLink(String uri, String name, String qName, Attributes atts) { String href = atts.getValue("href"); feature.put("LINK",href); characterdata= new StringBuffer(); featurefield = "LINK-TEXT"; } /** * Adds the group. * * @param uri the uri * @param name the name * @param qName the q name * @param atts the atts */ private void addGroup(String uri, String name, String qName, Attributes atts) { String id = atts.getValue("id"); feature.put("GROUP",id); characterdata= new StringBuffer(); featurefield = "GROUP"; } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) */ public void startElement (String uri, String name, String qName, Attributes atts){ //System.out.println("new element "+qName); if (qName.equals("FEATURE")) start_feature(uri, name, qName, atts); else if ( qName.equals("LINK")) addLink(uri,name,qName, atts); else if ( qName.equals("GROUP")) addGroup(uri,name,qName, atts); else if ( qName.equals("METHOD") || qName.equals("TYPE") || qName.equals("START") || qName.equals("END") || qName.equals("NOTE") || qName.equals("SCORE") || qName.equals("ORIENTATION") ){ characterdata = new StringBuffer(); featurefield = qName ; } else if (qName.equals("SEGMENT")){ String id = atts.getValue("id"); if (id != null) segmentId = id; String v = atts.getValue("version"); if ( v != null) version = v; } if ( qName.equals("TYPE")){ type_id = atts.getValue("id"); type_category= atts.getValue("category"); } } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String) */ public void endElement(String uri, String name, String qName) { if ( qName.equals("METHOD") || qName.equals("TYPE") || qName.equals("START") || qName.equals("END") || qName.equals("NOTE") || qName.equals("LINK") || qName.equals("SCORE") || qName.equals("ORIENTATION") || qName.equals("GROUP") ) { add_featuredata(uri,name,qName); } else if ( qName.equals("FEATURE")) { if ( maxFeatures > 0 ) { if ( features.size() < maxFeatures ) { features.add(feature); } } else { // no restriction features.add(feature); } } } /* (non-Javadoc) * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int) */ public void characters (char ch[], int start, int length){ if ( maxFeatures > 0) if ( features.size() > maxFeatures ) return; for (int i = start; i < start + length; i++) { characterdata.append(ch[i]); } } }