diff --git a/api/src/main/java/edu/cornell/mannlib/semservices/service/impl/LCSHService.java b/api/src/main/java/edu/cornell/mannlib/semservices/service/impl/LCSHService.java index 4e5146ed..a8cec31f 100644 --- a/api/src/main/java/edu/cornell/mannlib/semservices/service/impl/LCSHService.java +++ b/api/src/main/java/edu/cornell/mannlib/semservices/service/impl/LCSHService.java @@ -49,7 +49,7 @@ public class LCSHService implements ExternalConceptService { String results = null; String dataUrl = baseUri + "?q=" + URLEncoder.encode(term, "UTF-8") + "&q=cs%3Ahttp%3A%2F%2Fid.loc.gov%2Fauthorities%2Fsubjects" - + "&format=XML"; + + "&format=atom"; log.debug("dataURL " + dataUrl); try { @@ -81,7 +81,7 @@ public class LCSHService implements ExternalConceptService { return conceptList; } - // Results are in json format (atom) - atom entries need to be extracted + // Results are in XML format (atom) - atom entries need to be extracted // retrieve the URIs and get the SKOS version of the entry, getting broader // and narrower terms as applicable as well as any description (skos:note) // that might exist @@ -99,7 +99,7 @@ public class LCSHService implements ExternalConceptService { //This is the URL for retrieving the concept - the pattern is http://id.loc.gov/authorities/subjects/sh85014203.skos.rdf //This is not the URI itself which would be http://id.loc.gov/authorities/subjects/sh85014203 String conceptURLString = getSKOSURL(uri); - String baseConceptURI = getConceptURI(uri); + String baseConceptURI = uri; URL conceptURL = null; try { conceptURL = new URL(conceptURLString); @@ -154,23 +154,12 @@ public class LCSHService implements ExternalConceptService { private String getSKOSURL(String uri) { - // Strip .xml at the end and replace with .skos.rdf - String skosURI = uri; - if (uri.endsWith(".xml")) { - skosURI = uri.substring(0, uri.length() - 4); - skosURI += skosSuffix; - } - return hostUri + skosURI; + String skosURI = uri + skosSuffix; + + return skosURI; } - //Given the URI from the xml, get just the base URI - private String getConceptURI(String uri) { - String skosURI = uri; - if (uri.endsWith(".xml")) { - skosURI = uri.substring(0, uri.length() - 4); - } - return hostUri + skosURI; - } + public List getConceptURISFromJSON(String results) { List uris = new ArrayList(); @@ -192,14 +181,25 @@ public class LCSHService implements ExternalConceptService { String conceptUri = new String(); try { Document doc = XMLUtils.parse(rdf); - NodeList nodes = doc.getElementsByTagName("search:result"); + NodeList nodes = doc.getElementsByTagName("entry"); int len = nodes.getLength(); - int i; + int i, j; for (i = 0; i < len; i++) { Node node = nodes.item(i); - NamedNodeMap attrs = node.getAttributes(); - Attr idAttr = (Attr) attrs.getNamedItem("uri"); - conceptUri = idAttr.getTextContent(); + NodeList childNodes = node.getChildNodes(); + for(j = 0; j < childNodes.getLength(); j++) { + Node childNode = childNodes.item(j); + if(childNode.getNodeName().equals("link")) { + NamedNodeMap attrs = childNode.getAttributes(); + Attr hrefAttr = (Attr) attrs.getNamedItem("href"); + //if type doesn't exist, this is the direct URL without extension + if((hrefAttr != null) && ((Attr)attrs.getNamedItem("type") == null)) { + conceptUri = hrefAttr.getTextContent(); + } + + } + } + log.debug("concept uri is " + conceptUri); uris.add(conceptUri); } @@ -249,11 +249,5 @@ public class LCSHService implements ExternalConceptService { return null; } - - - - - - }