Changeset 509 for trunk

Show
Ignore:
Timestamp:
03/22/11 12:23:07 (14 months ago)
Author:
martijn
Message:

Start defining aliases, using a new xml configuration format

Location:
trunk
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/org.bridgedb.bio/src/org/bridgedb/bio/BioDataSource.java

    r471 r509  
    2525import java.util.regex.Pattern; 
    2626 
     27import javax.xml.parsers.ParserConfigurationException; 
     28 
    2729import org.bridgedb.DataSource; 
    2830import org.bridgedb.DataSourcePatterns; 
     31import org.bridgedb.impl.InternalUtils; 
     32import org.xml.sax.InputSource; 
     33import org.xml.sax.SAXException; 
    2934 
    3035/** 
     
    532537                                if (fields.length > 8) builder.urnBase(fields[8]); 
    533538                        } 
     539                         
     540                        InternalUtils.readXmlConfig( 
     541                                        new InputSource( 
     542                                                        BioDataSource.class.getClassLoader().getResourceAsStream( 
     543                                                                        "org/bridgedb/bio/datasources.xml"))); 
     544                         
    534545                } 
    535546                catch (IOException ex) 
     
    537548                        throw new Error(ex); 
    538549                } 
     550                catch (ParserConfigurationException e) 
     551                { 
     552                        throw new Error(e); 
     553                } 
     554                catch (SAXException e) 
     555                { 
     556                        throw new Error(e); 
     557                } 
     558                 
    539559        } 
    540          
    541560} 
  • trunk/org.bridgedb.bio/test/org/bridgedb/bio/Test.java

    r434 r509  
    124124        } 
    125125         
     126        @org.junit.Test 
     127        public void testAlias() 
     128        { 
     129                DataSource ds = DataSource.getByAlias("ensembl_gene_id"); 
     130                assertSame(ds, BioDataSource.ENSEMBL); 
     131        } 
     132         
    126133} 
  • trunk/org.bridgedb/src/org/bridgedb/DataSource.java

    r426 r509  
    5959        private static Map<String, DataSource> byFullName = new HashMap<String, DataSource>(); 
    6060        private static Set<DataSource> registry = new HashSet<DataSource>(); 
     61        private static Map<String, DataSource> byAlias = new HashMap<String, DataSource>(); 
    6162         
    6263        private String sysCode = null; 
     
    321322        } 
    322323         
     324        public void registerAlias(String alias) 
     325        { 
     326                byAlias.put (alias, this); 
     327        } 
     328         
    323329        /** 
    324330         * Helper method to determine if a String is allowed as key for bySysCode and byFullname hashes. 
     
    363369        } 
    364370         
     371        public static DataSource getByAlias(String alias) 
     372        { 
     373                return byAlias.get(alias); 
     374        } 
     375 
    365376        /** 
    366377                get all registered datasoures as a set. 
  • trunk/org.bridgedb/src/org/bridgedb/impl/InternalUtils.java

    r409 r509  
    2929import java.util.Set; 
    3030 
     31import javax.xml.parsers.ParserConfigurationException; 
     32import javax.xml.parsers.SAXParser; 
     33import javax.xml.parsers.SAXParserFactory; 
     34 
    3135import org.bridgedb.DataSource; 
    3236import org.bridgedb.IDMapper; 
    3337import org.bridgedb.IDMapperException; 
    3438import org.bridgedb.Xref; 
     39import org.xml.sax.Attributes; 
     40import org.xml.sax.InputSource; 
     41import org.xml.sax.SAXException; 
     42import org.xml.sax.XMLReader; 
     43import org.xml.sax.helpers.DefaultHandler; 
    3544 
    3645/** 
     
    261270        } 
    262271 
     272        /** read a configuration file in the bridgedb xml format */ 
     273        public static void readXmlConfig(InputSource is) throws ParserConfigurationException, SAXException, IOException 
     274        {        
     275                SAXParserFactory spf = SAXParserFactory.newInstance(); 
     276        spf.setNamespaceAware(true); 
     277        SAXParser saxParser = spf.newSAXParser(); 
     278 
     279        XMLReader xmlReader = saxParser.getXMLReader(); 
     280        xmlReader.setContentHandler(new ConfigXmlHandler()); 
     281        xmlReader.parse(is); 
     282        } 
     283         
     284        private static class ConfigXmlHandler extends DefaultHandler 
     285        { 
     286                DataSource current = null; 
     287                 
     288                @Override 
     289                public void startElement(String namespaceURI, String localName, 
     290                                String qName, Attributes atts) 
     291                throws SAXException 
     292                { 
     293                        if ("datasource".equals (localName)) 
     294                        { 
     295                                String fullname = atts.getValue("fullname"); 
     296                                if (fullname == null) throw new SAXException ("missing attribute fullname"); 
     297                                current = DataSource.getByFullName(fullname); 
     298                        } 
     299                         
     300                        if ("alias".equals (localName)) 
     301                        { 
     302                                String alias = atts.getValue ("name"); 
     303                                if (alias != null && current != null) 
     304                                        current.registerAlias(alias); 
     305                        } 
     306                } 
     307                 
     308                @Override 
     309                public void endElement(String namespaceURI, String localName, String qName) throws SAXException 
     310                { 
     311                        if ("datasource".equals (localName)) 
     312                        { 
     313                                current = null; 
     314                        } 
     315                } 
     316 
     317        } 
     318 
    263319}