Skip to content

Commit

Permalink
Add optional endpoint description resource institution field
Browse files Browse the repository at this point in the history
  • Loading branch information
Querela committed Jan 18, 2024
1 parent 72d1d9e commit 67436dd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
45 changes: 42 additions & 3 deletions src/main/java/eu/clarin/sru/server/fcs/ResourceInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ResourceInfo {
private final String pid;
private final Map<String, String> title;
private final Map<String, String> description;
private final Map<String, String> institution;
private final String landingPageURI;
private final List<String> languages;
private final List<DataView> availableDataViews;
Expand All @@ -50,6 +51,10 @@ public class ResourceInfo {
* the description of the resource represented as a map with
* pairs of language code and description or <code>null</code> if
* not applicable
* @param institution
* the institution of the resource represented as a map with
* pairs of language code and institution names or <code>null</code>
* if not applicable
* @param landingPageURI
* a URI to the landing page of the resource or <code>null</code>
* if not applicable
Expand All @@ -66,9 +71,9 @@ public class ResourceInfo {
* <code>null</code> if not applicable
*/
public ResourceInfo(String pid, Map<String, String> title,
Map<String, String> description, String landingPageURI,
List<String> languages, List<DataView> availableDataViews,
List<Layer> availableLayers,
Map<String, String> description, Map<String, String> institution,
String landingPageURI, List<String> languages,
List<DataView> availableDataViews, List<Layer> availableLayers,
List<ResourceInfo> subResources) {
if (pid == null) {
throw new NullPointerException("pid == null");
Expand All @@ -87,6 +92,11 @@ public ResourceInfo(String pid, Map<String, String> title,
} else {
this.description = null;
}
if ((institution != null) && !institution.isEmpty()) {
this.institution = Collections.unmodifiableMap(institution);
} else {
this.institution = null;
}

this.landingPageURI = landingPageURI;
if (languages == null) {
Expand Down Expand Up @@ -185,6 +195,35 @@ public String getDescription(String language) {
}


/**
* Get the institution of this resource.
*
* This is an optional attribute for endpoints that host resources from
* different institution but still want to bundle them in one endpoint.
* If not specified then it is the default institution that hosts the
* FCS endpoint.
*
* @return the institution of this resource or <code>null</code> if not
* applicable or specified
*/
public Map<String, String> getInstitution() {
return institution;
}


/**
* Get the institution of this resource for a specific language code.
*
* @param language
* the language code
* @return the institution of this resource or <code>null</code> if not
* applicable or specified
*/
public String getInstitution(String language) {
return (institution != null) ? institution.get(language) : null;
}


/**
* Get the landing page of this resource.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,18 @@ private void writeResourceInfos(XMLStreamWriter writer,
}
}

// institution
final Map<String, String> institution = resource.getInstitution();
if (institution != null) {
for (Map.Entry<String, String> i : institution.entrySet()) {
writer.writeStartElement(ED_NS, "Institution");
writer.writeAttribute(XMLConstants.XML_NS_URI, "lang",
i.getKey());
writer.writeCharacters(i.getValue());
writer.writeEndElement(); // "Institution" element
}
}

// landing page
final String landingPageURI = resource.getLandingPageURI();
if (landingPageURI != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ private static List<ResourceInfo> parseResources(XPath xpath,
String pid = null;
Map<String, String> titles = null;
Map<String, String> descrs = null;
Map<String, String> insts = null;
String link = null;
List<String> langs = null;
List<DataView> availableDataViews = null;
Expand Down Expand Up @@ -558,6 +559,38 @@ private static List<ResourceInfo> parseResources(XPath xpath,
}
}

exp = xpath.compile("ed:Institution");
list = (NodeList) exp.evaluate(node, XPathConstants.NODESET);
if ((list != null) && (list.getLength() > 0)) {
for (int i = 0; i < list.getLength(); i++) {
Element n = (Element) list.item(i);

String lang = getLangAttribute(n);
if (lang == null) {
throw new SRUConfigException("Element <Institution> " +
"must have a proper 'xml:lang' attribute");

}
String inst = cleanString(n.getTextContent());

if (insts == null) {
insts = new HashMap<>();
}

if (insts.containsKey(lang)) {
logger.warn("institution with language '{}' " +
"already exists", lang);
} else {
logger.debug("institution: '{}' '{}'", lang, inst);
insts.put(lang, inst);
}
}
if ((insts != null) && !insts.containsKey(LANG_EN)) {
throw new SRUConfigException(
"A <Institution> with language 'en' is mandatory");
}
}

exp = xpath.compile("ed:LandingPageURI");
list = (NodeList) exp.evaluate(node, XPathConstants.NODESET);
if ((list != null) && (list.getLength() > 0)) {
Expand Down Expand Up @@ -698,7 +731,7 @@ private static List<ResourceInfo> parseResources(XPath xpath,
"includes information about <AvailableLayers> for " +
"resource with pid '{}'", pid);
}
ris.add(new ResourceInfo(pid, titles, descrs, link, langs,
ris.add(new ResourceInfo(pid, titles, descrs, insts, link, langs,
availableDataViews, availableLayers, sub));
}
return ris;
Expand Down

0 comments on commit 67436dd

Please sign in to comment.