X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Findexdata%2Fmkjsf%2Fpazpar2%2Fdata%2FResponseParser.java;h=8036a2992e845de1189d1b5ce9bb8cccb51ad2f9;hb=1ab622be3bcde018e6b7816d4af8b79f80159630;hp=9466473d1be86da9e66f21d5b68b8fa7a2db694c;hpb=8adf7eecfbcb98af079f4f2d129a1f76b146aefc;p=mkjsf-moved-to-github.git diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java index 9466473..8036a29 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/ResponseParser.java @@ -18,19 +18,17 @@ import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; -import com.indexdata.mkjsf.pazpar2.data.ByTarget; -import com.indexdata.mkjsf.pazpar2.data.Hit; -import com.indexdata.mkjsf.pazpar2.data.Location; -import com.indexdata.mkjsf.pazpar2.data.ResponseDataObject; -import com.indexdata.mkjsf.pazpar2.data.RecordResponse; -import com.indexdata.mkjsf.pazpar2.data.ShowResponse; -import com.indexdata.mkjsf.pazpar2.data.StatResponse; -import com.indexdata.mkjsf.pazpar2.data.Target; -import com.indexdata.mkjsf.pazpar2.data.TermListResponse; -import com.indexdata.mkjsf.pazpar2.data.TermListsResponse; -import com.indexdata.mkjsf.pazpar2.data.TermResponse; -import com.indexdata.mkjsf.pazpar2.data.TermXTargetResponse; +import com.indexdata.mkjsf.pazpar2.ClientCommandResponse; +import com.indexdata.mkjsf.pazpar2.data.sp.AuthResponse; +import com.indexdata.mkjsf.pazpar2.data.sp.CategoriesResponse; +import com.indexdata.mkjsf.pazpar2.data.sp.TargetCategory; +/** + * Parses the XML stored in ClientCommandResponses and builds ResponseDataObjects from it. + * + * @author Niels Erik + * + */ public class ResponseParser extends DefaultHandler { private XMLReader xmlReader = null; @@ -40,10 +38,10 @@ public class ResponseParser extends DefaultHandler { private String xml = null; private static Logger logger = Logger.getLogger(ResponseParser.class); - public static final List docTypes = - Arrays.asList("bytarget","termlist","show","stat","record","search"); + public static List docTypes = Arrays.asList( "bytarget","termlist","show","stat","record","search","init","info", + /* SP extras */ "auth", "categories" ); - public ResponseParser() { + public ResponseParser() { try { initSax(); } catch (ParserConfigurationException e) { @@ -74,18 +72,15 @@ public class ResponseParser extends DefaultHandler { * @param response XML response string from Pazpar2 * @return Response data object */ - public ResponseDataObject getDataObject (String response) { - this.xml = response; + public ResponseDataObject getDataObject (ClientCommandResponse response) { + this.xml = response.getResponseString(); try { - xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getBytes("UTF-8")))); + xmlReader.parse(new InputSource(new ByteArrayInputStream(response.getResponseToParse()))); } catch (UnsupportedEncodingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + e.printStackTrace(); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { - // TODO Auto-generated catch block e.printStackTrace(); } return result; @@ -97,14 +92,20 @@ public class ResponseParser extends DefaultHandler { */ @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { - if (localName.equals("show")) { + if (localName.equals("init")) { + currentElement = new InitResponse(); + } else if (localName.equals("search")) { + currentElement = new SearchResponse(); + } else if (localName.equals("show")) { currentElement = new ShowResponse(); } else if (localName.equals("hit")) { currentElement = new Hit(); } else if (localName.equals("location")) { currentElement = new Location(); + } else if (localName.equals("record")) { + currentElement = new RecordResponse(); } else if (localName.equals("bytarget")) { - currentElement = new ByTarget(); + currentElement = new ByTargetResponse(); } else if (localName.equals("target")) { currentElement = new Target(); } else if (localName.equals("stat")) { @@ -122,14 +123,22 @@ public class ResponseParser extends DefaultHandler { currentElement = new TermResponse(); } ((TermListResponse)dataElements.peek()).addTerm((TermResponse)currentElement); - } else if (localName.equals("record")) { - currentElement = new RecordResponse(); - } else if (localName.equals("search")) { - currentElement = new SearchResponse(); + } else if (localName.equals("info")) { + currentElement = new InfoResponse(); + } else if (localName.equals("version") && dataElements.peek().getType().equals("info")) { + currentElement = new Pazpar2VersionResponse(); } else if (localName.equals("applicationerror")) { currentElement = new CommandError(); } else if (localName.equals("error") && dataElements.peek().getType().equals("applicationerror")) { - currentElement = new Pazpar2Error(); + currentElement = new ServiceError(); + // Service Proxy extras + } else if (localName.equals("auth")) { + currentElement = new AuthResponse(); + } else if (localName.equals("categories")) { + currentElement = new CategoriesResponse(); + } else if (localName.equals("category") && dataElements.peek().getType().equals("categories")) { + currentElement = new TargetCategory(); + // Catch all } else { currentElement = new ResponseDataObject(); }