View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // DescriptionXMLReader.java
20  // Since: Oct 10, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.format.legacy;
26  
27  import java.io.BufferedReader;
28  import java.io.IOException;
29  import java.io.InputStreamReader;
30  import java.io.Reader;
31  import java.net.URL;
32  import java.util.List;
33  
34  import org.utgenome.gwt.utgb.client.track.bean.LegacyTrackInformation;
35  import org.xerial.core.XerialException;
36  import org.xerial.db.DBException;
37  import org.xerial.db.sql.sqlite.SQLiteAccess;
38  import org.xerial.lens.XMLLens;
39  
40  public class OldDescriptionXMLReader {
41  	public static void main(String[] args) {
42  		try {
43  			SQLiteAccess sqlite = new SQLiteAccess("resource/legacy-track.db");
44  
45  			List<LegacyTrackInformation> trackInfoList = sqlite.query("select * from tracks", LegacyTrackInformation.class);
46  			for (LegacyTrackInformation lt : trackInfoList) {
47  				boolean isValid = validate(lt);
48  				if (!isValid) {
49  					System.out.println(lt.getName() + ": " + lt.getDescriptionXML() + " is not valid");
50  				}
51  				else {
52  					System.out.println(lt.getName() + ": " + lt.getDescriptionXML() + " is valid");
53  
54  				}
55  			}
56  		}
57  		catch (DBException e) {
58  			// TODO Auto-generated catch block
59  			e.printStackTrace();
60  		}
61  
62  	}
63  
64  	public static boolean speciesIsValid(String species, LegacyTrackInformation lt) {
65  		if (species.equals("any"))
66  			return true;
67  		else
68  			return species.equals(lt.getSpecies());
69  	}
70  
71  	public static boolean revisioIsValid(String revision, LegacyTrackInformation lt) {
72  		if (revision.equals("any"))
73  			return true;
74  		else
75  			return revision.equals(lt.getRevision());
76  	}
77  
78  	public static boolean validate(LegacyTrackInformation lt) {
79  
80  		try {
81  			//System.out.println(lt.getName() + ": " + lt.getDescriptionXML());
82  
83  			URL descriptionXMLURL = new URL(lt.getDescriptionXML().trim());
84  			Reader xmlReader = new BufferedReader(new InputStreamReader(descriptionXMLURL.openStream()));
85  			OldDescriptionXML descriptionXMLData = XMLLens.createXMLBean(OldDescriptionXML.class, xmlReader);
86  
87  			for (SpeciesEntry se : descriptionXMLData.getAccept_species().getEntry()) {
88  				if (speciesIsValid(se.getSpecies(), lt) && revisioIsValid(se.getRevision(), lt))
89  					return true;
90  			}
91  		}
92  		catch (XerialException e) {
93  			// TODO Auto-generated catch block
94  			e.printStackTrace();
95  		}
96  		catch (IOException e) {
97  			// TODO Auto-generated catch block
98  			e.printStackTrace();
99  		}
100 		return false;
101 	}
102 }