source: SRUAggregator/trunk/src/main/java/eu/clarin/sru/fcs/aggregator/data/CenterRegistry.java @ 2527

Last change on this file since 2527 was 2527, checked in by yana, 11 years ago

Logging organized, progress info about waiting for search results added, SRUThreadedClient is one per application now.

File size: 5.1 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package eu.clarin.sru.fcs.aggregator.data;
7
8import eu.clarin.sru.fcs.aggregator.sparam.CorpusTreeNode;
9import java.io.IOException;
10import java.io.InputStream;
11import java.net.URL;
12import java.net.URLConnection;
13import java.util.ArrayList;
14import java.util.List;
15import java.util.logging.Level;
16import java.util.logging.Logger;
17import javax.xml.parsers.DocumentBuilder;
18import javax.xml.parsers.DocumentBuilderFactory;
19import javax.xml.xpath.XPath;
20import javax.xml.xpath.XPathConstants;
21import javax.xml.xpath.XPathExpressionException;
22import javax.xml.xpath.XPathFactory;
23import org.w3c.dom.NodeList;
24
25/**
26 * Center registry node. Its children are centers (institutions).
27 *
28 * @author Yana Panchenko
29 */
30public class CenterRegistry implements CorpusTreeNode {
31   
32    private static final Logger logger = Logger.getLogger(CenterRegistry.class.getName());
33
34    private static final String crStartpoint = "http://130.183.206.32/restxml/";
35
36    //https://centerregistry-clarin.esc.rzg.mpg.de/restxml/
37   
38    private boolean hasChildrenLoaded = false;
39    private List<Institution> centers = new ArrayList<Institution>(); 
40
41    @Override
42    public boolean hasChildrenLoaded() {
43        return hasChildrenLoaded;
44    }
45
46    @Override
47    public void loadChildren() {
48        //TODO change to use Alex binding for that...
49        if (hasChildrenLoaded) {
50            return;
51        }
52        hasChildrenLoaded = true;
53        InputStream is = null;
54        URL u;
55        NodeList instituteNames;
56        NodeList institutionsUrls;
57        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
58        DocumentBuilder builder;
59        URLConnection urlConn;
60        try {
61            u = new URL(crStartpoint);
62            urlConn = u.openConnection();
63
64            //HttpsURLConnection urlConn = (HttpsURLConnection) u.openConnection();
65           
66            urlConn.setConnectTimeout(5000);                                   
67            urlConn.setReadTimeout(15000);
68            urlConn.setAllowUserInteraction(false);
69                                   
70            is = urlConn.getInputStream();
71
72
73            //InputStream is = u.openStream();
74
75            builder = factory.newDocumentBuilder();
76            org.w3c.dom.Document document = builder.parse(is);
77
78           
79            instituteNames = evaluateXPath("//Centername", document);
80            institutionsUrls = evaluateXPath("//Center_id_link", document);
81
82            for (int i = 0; i < institutionsUrls.getLength(); i++) {
83                String institutionUrl = institutionsUrls.item(i).getTextContent();
84                String institutionName = instituteNames.item(i).getTextContent();
85                Institution institution = new Institution(institutionName, institutionUrl);
86                // display in the tree only those institutions that have CQL endpoints:
87                if (!institution.getChildren().isEmpty()) {
88                    centers.add(institution);
89                }
90            }
91
92        } catch (Exception ex) {
93            logger.log(Level.SEVERE, "Error accessing central registry information {0} {1}", new String[]{ex.getClass().getName(), ex.getMessage()});
94        } finally {
95            if (is != null) {
96                try {
97                    is.close();
98                } catch (IOException ex) {
99                    logger.log(Level.SEVERE, ex.getMessage());
100                }
101            }
102        }
103        logger.log(Level.FINE, "Number of Centers: {0}", centers.size());
104
105    }
106
107    @Override
108    public List<? extends CorpusTreeNode> getChildren() {
109        loadChildren();
110        return centers;
111    }
112
113    @Override
114    public CorpusTreeNode getChild(int index) {
115        loadChildren();
116        if (index >= centers.size()) {
117            return null;
118        }
119        return centers.get(index);
120    }
121   
122   
123    public void addForeignPoint(String endpointUrl, String institutionLink) {
124       
125        //TODO: ask what functionality is required here??
126       
127//        boolean added = false;
128//        for (Institution center : this.centers) {
129//            if (center.getLink().equals(institutionLink)) {
130//                EndpointY ep = new EndpointY(endpointUrl, center);
131//                center.loadChildren();
132//                //center.loadChildren();
133//                //ep.loadChildren();
134//            }
135//        }
136//        if (!added) {
137//            Institution institution = new Institution("unknown", institutionLink);
138//            this.centers.add(institution);
139//            //EndpointY ep = new EndpointY(endpointUrl, institution);
140//            //this.loadChildren();
141//            //institution.loadChildren();
142//        }
143    }
144   
145   
146    public static NodeList evaluateXPath(String statement, org.w3c.dom.Document domtree) {
147        NodeList result = null;
148
149        XPath xpath = XPathFactory.newInstance().newXPath();
150        try {
151            result = (NodeList) xpath.evaluate(statement, domtree, XPathConstants.NODESET);
152        } catch (XPathExpressionException ex) {
153            System.out.println(ex.getMessage());
154        }
155        return result;
156    }
157
158}
Note: See TracBrowser for help on using the repository browser.