Skip to content

Commit

Permalink
Merge pull request #30 from mmalmeida/fix26
Browse files Browse the repository at this point in the history
Fix26
  • Loading branch information
jmelo-lyncode committed Aug 1, 2014
2 parents 201e6da + c7f118d commit a92602e
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@

package com.lyncode.xoai.serviceprovider.handler;

import static com.lyncode.xml.matchers.QNameMatchers.localPart;
import static com.lyncode.xml.matchers.XmlEventMatchers.aStartElement;
import static com.lyncode.xml.matchers.XmlEventMatchers.anEndElement;
import static com.lyncode.xml.matchers.XmlEventMatchers.elementName;
import static com.lyncode.xml.matchers.XmlEventMatchers.text;
import static com.lyncode.xoai.model.oaipmh.Verb.Type.ListSets;
import static com.lyncode.xoai.serviceprovider.parameters.Parameters.parameters;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.AllOf.allOf;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.events.XMLEvent;

import org.hamcrest.Matcher;

import com.lyncode.xml.XmlReader;
import com.lyncode.xml.exceptions.XmlReaderException;
import com.lyncode.xoai.model.oaipmh.Set;
Expand All @@ -25,19 +43,6 @@
import com.lyncode.xoai.serviceprovider.lazy.Source;
import com.lyncode.xoai.serviceprovider.model.Context;
import com.lyncode.xoai.serviceprovider.parsers.ListSetsParser;
import org.hamcrest.Matcher;

import javax.xml.stream.events.XMLEvent;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static com.lyncode.xml.matchers.QNameMatchers.localPart;
import static com.lyncode.xml.matchers.XmlEventMatchers.*;
import static com.lyncode.xoai.model.oaipmh.Verb.Type.ListSets;
import static com.lyncode.xoai.serviceprovider.parameters.Parameters.parameters;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.AllOf.allOf;

public class ListSetsHandler implements Source<Set> {
private Context context;
Expand Down Expand Up @@ -66,8 +71,7 @@ public List<Set> nextIteration() {

XmlReader reader = new XmlReader(stream);
ListSetsParser parser = new ListSetsParser(reader);
while (parser.hasNext())
sets.add(parser.next());
sets = parser.parse();

if (reader.current(resumptionToken())) {
if (reader.next(text(), anEndElement()).current(text())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.lyncode.xml.matchers.QNameMatchers.localPart;
import static com.lyncode.xml.matchers.XmlEventMatchers.aStartElement;
import static com.lyncode.xml.matchers.XmlEventMatchers.anEndElement;
import static com.lyncode.xml.matchers.XmlEventMatchers.elementName;
import static com.lyncode.xml.matchers.XmlEventMatchers.text;
import static com.lyncode.xml.matchers.XmlEventMatchers.theEndOfDocument;
Expand All @@ -26,6 +27,9 @@
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.equalTo;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.stream.events.XMLEvent;

Expand Down Expand Up @@ -83,11 +87,14 @@ private Set parseSet() throws XmlReaderException {
reader.next(aStartElement());
QName elementName = reader.getName();
reader.next(text());

String extractedText = reader.getText();
while(reader.next(anEndElement(),text()).current(text())){
extractedText += reader.getText();
}
if(elementName.getLocalPart().equals("setName")) {
setName = reader.getText();
setName = extractedText;
} else if(elementName.getLocalPart().equals("setSpec")) {
setSpec = reader.getText();
setSpec = extractedText;
}
}
set.withName(setName);
Expand All @@ -103,4 +110,19 @@ private Matcher<XMLEvent> errorElement() {
private Matcher<XMLEvent> setElement() {
return allOf(aStartElement(), elementName(localPart(equalTo("set"))));
}
private Matcher<XMLEvent> endSetElement() {
return allOf(anEndElement(), elementName(localPart(equalTo("set"))));
}

/**
* Parses its xml completely
* @return - All sets within the xml
* @throws XmlReaderException
*/
public List<Set> parse() throws XmlReaderException {
List<Set> sets = new ArrayList<Set>();
while (hasNext())
sets.add(next());
return sets;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.lyncode.xoai.serviceprovider.parsers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.InputStream;
import java.util.List;

import org.junit.Test;

import com.lyncode.xml.XmlReader;
import com.lyncode.xml.exceptions.XmlReaderException;
import com.lyncode.xoai.model.oaipmh.Set;

public class ListSetsParserTest {

private static final String OAI_DC_SETS_CDATA_XML = "test/oai_dc/listsets/oai_dc-sets-CDATA.xml";

@Test
public void normalParsing(){
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(
"test/oai_dc/listsets/oai_dc-sets.xml");

List<Set> sets = parseXML(inputStream);
assertEquals(2,sets.size());
assertEquals("setOne",sets.get(0).getSpec());
assertEquals("Set One",sets.get(0).getName());
assertEquals("setTwo",sets.get(1).getSpec());
assertEquals("Set Two",sets.get(1).getName());
}

@Test
public void cdataIsParsed(){
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(
OAI_DC_SETS_CDATA_XML);

List<Set> sets = parseXML(inputStream);
assertEquals("cdataSPEC",sets.get(0).getSpec());
assertEquals("Set with CDATA",sets.get(0).getName());

}

@Test
public void multipleCDATAParsed(){
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(
OAI_DC_SETS_CDATA_XML);

List<Set> sets = parseXML(inputStream);
assertEquals(2,sets.size());
assertEquals("First CDATA, Set 2 with CDATA",sets.get(1).getName());
}


private List<Set> parseXML(InputStream inputStream){
XmlReader reader;
try {
reader = new XmlReader(inputStream);
ListSetsParser parser = new ListSetsParser(reader );
List<Set> sets = parser.parse();
return sets;

} catch (XmlReaderException e) {
fail("unexpected exception reading the xml: "+ e.getCause().toString());
}
return null;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,17 @@ public void xmlLangIsParsed() throws Exception {
Record record = parser.parse(reader);
assertEquals(2,record.getMetadata().getValue().searcher().findAll("dc.rights:xml:lang").size());
}

@Test
public void cdataParsing() throws Exception {
input = getClass().getClassLoader().getResourceAsStream(
"test/oai_dc-CDATA.xml");

parser = new RecordParser(context, "oai_dc");
XmlReader reader = new XmlReader(input);
Record record = parser.parse(reader);
assertEquals(1,record.getMetadata().getValue().searcher().findAll("dc.title").size());
assertEquals("Article Title-additional CDATA",record.getMetadata().getValue().searcher().findOne("dc.title"));
}

}
29 changes: 29 additions & 0 deletions xoai-service-provider/src/test/resources/test/oai_dc-CDATA.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://spmfrjournal.org/lib/pkp/xml/oai2.xsl" ?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/
http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>2014-07-29T13:34:25Z</responseDate>
<request verb="GetRecord" metadataPrefix="oai_dc" identifier="oai:ojs.spmfrjournal.org:article/88">http://spmfrjournal.org/index.php/spmfr/oai</request>
<GetRecord>
<record>
<header>
<identifier>oai:ojs.spmfrjournal.org:article/88</identifier>
<datestamp>2013-10-14T06:01:49Z</datestamp>
<setSpec>spmfr:EDI</setSpec>
<setSpec>driver</setSpec>
</header>
<metadata>
<oai_dc:dc
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title><![CDATA[Article Title]]><![CDATA[-additional CDATA]]></dc:title>
</oai_dc:dc>
</metadata>
</record>
</GetRecord>
</OAI-PMH>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/
http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>2014-07-31T09:25:24Z</responseDate>
<request verb="ListSets">http://www.scielo.br/oai/scielo-oai.php</request>
<ListSets>

<set>
<setSpec><![CDATA[cdataSPEC]]></setSpec>
<setName><![CDATA[Set with CDATA]]></setName>
</set>
<set>
<setSpec>setTwo</setSpec>
<setName><![CDATA[First CDATA, ]]><![CDATA[Set 2 with CDATA]]></setName>
</set>
</ListSets>
</OAI-PMH>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/
http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>2014-07-31T09:25:24Z</responseDate>
<request verb="ListSets">http://www.scielo.br/oai/scielo-oai.php</request>
<ListSets>

<set>
<setSpec>setOne</setSpec>
<setName>Set One</setName>
</set>
<set>
<setSpec>setTwo</setSpec>
<setName>Set Two</setName>
</set>
</ListSets>
</OAI-PMH>

0 comments on commit a92602e

Please sign in to comment.