source: FCSSimpleEndpoint/tags/FCSSimpleEndpoint-1.3.0/src/main/java/eu/clarin/sru/server/fcs/utils/SimpleEndpointDescription.java @ 6957

Last change on this file since 6957 was 6957, checked in by Oliver Schonefeld, 8 years ago
  • tag version 1.3.0
  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1/**
2 * This software is copyright (c) 2013-2016 by
3 *  - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
4 * This is free software. You can redistribute it
5 * and/or modify it under the terms described in
6 * the GNU General Public License v3 of which you
7 * should have received a copy. Otherwise you can download
8 * it from
9 *
10 *   http://www.gnu.org/licenses/gpl-3.0.txt
11 *
12 * @copyright Institut fuer Deutsche Sprache (http://www.ids-mannheim.de)
13 *
14 * @license http://www.gnu.org/licenses/gpl-3.0.txt
15 *  GNU General Public License v3
16 */
17package eu.clarin.sru.server.fcs.utils;
18
19import java.net.URI;
20import java.util.Collections;
21import java.util.List;
22
23import eu.clarin.sru.server.SRUException;
24import eu.clarin.sru.server.fcs.DataView;
25import eu.clarin.sru.server.fcs.EndpointDescription;
26import eu.clarin.sru.server.fcs.Layer;
27import eu.clarin.sru.server.fcs.ResourceInfo;
28
29
30/**
31 * A very simple implementation of an endpoint description that is initialized
32 * from static information supplied at construction time. Mostly used together
33 * with {@link SimpleEndpointDescriptionParser}, but it is agnostic how the
34 * static list of resource info records is generated.
35 *
36 * @see EndpointDescription
37 * @see SimpleEndpointDescriptionParser
38 */
39public class SimpleEndpointDescription extends AbstractEndpointDescriptionBase {
40    private final boolean pidCaseSensitive;
41    private final List<ResourceInfo> entries;
42
43
44    /**
45     * Constructor.
46     *
47     * @param capabilities
48     *            a list of capabilities supported by this endpoint
49     * @param supportedDataViews
50     *            a list of data views that are supported by this endpoint
51     * @param supportedLayers
52     *            a list of layers supported for Advanced Search by this
53     *            endpoint or <code>null</code>
54     * @param resources
55     *            a static list of resource info records
56     * @param pidCaseSensitive
57     *            <code>true</code> if comparison of persistent identifiers
58     *            should be performed case-sensitive, <code>false</code>
59     *            otherwise
60     */
61    public SimpleEndpointDescription(List<URI> capabilities,
62            List<DataView> supportedDataViews,
63            List<Layer> supportedLayers,
64            List<ResourceInfo> resources,
65            boolean pidCaseSensitive) {
66        super(capabilities, supportedDataViews, supportedLayers);
67
68        if (resources == null) {
69            throw new NullPointerException("entries == null");
70        }
71        this.entries = Collections.unmodifiableList(resources);
72        this.pidCaseSensitive = pidCaseSensitive;
73    }
74
75
76    @Override
77    public void destroy() {
78    }
79
80
81    @Override
82    public List<ResourceInfo> getResourceList(String pid) throws SRUException {
83        if (pid == null) {
84            throw new NullPointerException("pid == null");
85        }
86        if (pid.isEmpty()) {
87            throw new IllegalArgumentException("pid is empty");
88        }
89        if (!pidCaseSensitive) {
90            pid = pid.toLowerCase();
91        }
92        if (pid.equals(PID_ROOT)) {
93            return entries;
94        } else {
95            ResourceInfo ri = findRecursive(entries, pid);
96            if (ri != null) {
97                return ri.getSubResources();
98            }
99        }
100        return null;
101    }
102
103
104    private ResourceInfo findRecursive(List<ResourceInfo> items, String pid) {
105        if ((items != null) && !items.isEmpty()) {
106            for (ResourceInfo item : items) {
107                if (pidCaseSensitive) {
108                    if (pid.equals(item.getPid())) {
109                        return item;
110                    }
111                } else {
112                    if (pid.equalsIgnoreCase(item.getPid())) {
113                        return item;
114                    }
115                }
116                if (item.hasSubResources()) {
117                    ResourceInfo ri =
118                            findRecursive(item.getSubResources(), pid);
119                    if (ri != null) {
120                        return ri;
121                    }
122                }
123            } // for
124        }
125        return null;
126    }
127
128} // class SimpleEndpointDescription
Note: See TracBrowser for help on using the repository browser.