1 | package org.biohackathon.SPARQLBuilder.endpointMetadata;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Set;
|
---|
6 |
|
---|
7 | public class MetadataManager {
|
---|
8 |
|
---|
9 | HashMap<String,MetadataFile> metadataTable = null;
|
---|
10 |
|
---|
11 |
|
---|
12 | public static void main(String[] args) throws Exception{
|
---|
13 | String dirStr = "c:\\temp\\crawl";
|
---|
14 | MetadataManager manager = new MetadataManager(dirStr);
|
---|
15 | String[] uris = manager.getURIList();
|
---|
16 | for(String uri: uris){
|
---|
17 | System.out.println(uri);
|
---|
18 | MetadataFile mFile = manager.getMetadataFile(uri);
|
---|
19 | System.out.println(mFile.getStartDateTime().getTime().toString());
|
---|
20 | System.out.println(mFile.getEndDateTime().getTime().toString());
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | public MetadataFile[] getMetadataFiles(){
|
---|
25 | return metadataTable.values().toArray(new MetadataFile[0]);
|
---|
26 | }
|
---|
27 | public MetadataFile getMetadataFile(String uri){
|
---|
28 | return metadataTable.get(uri);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public String[] getURIList(){
|
---|
32 | Set<String> keySet = metadataTable.keySet();
|
---|
33 | return keySet.toArray(new String[0]);
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | public MetadataManager(String metadataDirStr) throws Exception {
|
---|
38 | init(metadataDirStr);
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 | public void init(String metadataDirStr) throws Exception{
|
---|
43 | metadataTable = new HashMap<String,MetadataFile>();
|
---|
44 | File metadataDir = new File(metadataDirStr);
|
---|
45 | File[] files = null;
|
---|
46 | if( metadataDir.exists() ){
|
---|
47 | if( metadataDir.isFile()){
|
---|
48 | files = new File[]{metadataDir};
|
---|
49 | }else{
|
---|
50 | if( metadataDir.isDirectory()){
|
---|
51 | files = metadataDir.listFiles();
|
---|
52 | }else{
|
---|
53 | throw new Exception("Inernal File/Directory error: " + metadataDirStr);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }else{
|
---|
57 | throw new Exception("File/Directory not found: " + metadataDirStr);
|
---|
58 | }
|
---|
59 | for(File file: files){
|
---|
60 | MetadataFile metadataFile = null;
|
---|
61 | try{
|
---|
62 | metadataFile = new MetadataFile(file);
|
---|
63 | String uri = metadataFile.getEndpointURI();
|
---|
64 | if( uri != null ){
|
---|
65 | if( metadataTable.containsKey(uri)){
|
---|
66 | MetadataFile tempMF = metadataTable.get(uri);
|
---|
67 | if(tempMF.getEndDateTime().before(metadataFile.getEndDateTime()) ){
|
---|
68 | metadataTable.remove(uri);
|
---|
69 | metadataTable.put(uri, metadataFile);
|
---|
70 | }
|
---|
71 | }else{
|
---|
72 | metadataTable.put(uri, metadataFile);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }catch(Exception ex){
|
---|
76 | // WARNING
|
---|
77 | System.out.println("Invalid metadata file: " + file.getAbsolutePath());
|
---|
78 | ex.printStackTrace();
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|