Skip to content

Commit

Permalink
Merge pull request #9 from equella/issue/8
Browse files Browse the repository at this point in the history
#8 ExportItems now supports xpaths returning multiple nodes.
  • Loading branch information
cbeach47 authored Aug 22, 2018
2 parents 1b3c908 + ed475d3 commit 6239ab9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
import org.apache.logging.log4j.Logger;
import org.apereo.openequella.tools.toolbox.api.OpenEquellaRestUtils;
import org.apereo.openequella.tools.toolbox.utils.EquellaItem;
import org.apereo.openequella.tools.toolbox.utils.MigrationUtils;
import org.apereo.openequella.tools.toolbox.utils.sorts.SortOpenEquellaItemByName;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ExportItemsDriver {
Expand Down Expand Up @@ -168,7 +168,7 @@ public void execute(Config config) {

// While MigrationUtils.findFirstOccurrenceInXml() is similar, this combines the 'reserved
// keywords' with a single invocation of parsing the XML.
private List<String> buildRecord(List<String> headers, EquellaItem ei, Config config) throws Exception {
public List<String> buildRecord(List<String> headers, EquellaItem ei, Config config) throws Exception {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(new StringReader(ei.getMetadata())));
Expand All @@ -192,8 +192,14 @@ private List<String> buildRecord(List<String> headers, EquellaItem ei, Config co
} else {
//Assume it's a metadata path
try {
XPathExpression expr = xpath.compile("/xml/" + header + "/text()");
record.add((String) expr.evaluate(doc, XPathConstants.STRING));
XPathExpression expr = xpath.compile("/xml/" + header);
List<String> results = new ArrayList<>();
Object xmlResults = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodeResults = (NodeList) xmlResults;
for(int i = 0; i < nodeResults.getLength(); i++) {
results.add(nodeResults.item(i).getTextContent());
}
record.add(String.join(",", results));
} catch (Exception e) {
e.printStackTrace();
throw new Exception("Unable to parse column format xpath "+header);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.apereo.openequella.tools.toolbox;

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

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

import org.apereo.openequella.tools.toolbox.utils.EquellaItem;
Expand Down Expand Up @@ -84,4 +87,42 @@ public void testFindFirstKalturaIdInAttachments() {
assertEquals("0_12345", eid.findFirstKalturaIdInAttachments(ei));

}

@Test
public void testBuildRecordMultipleValuesTogether() {
EquellaItem ei = new EquellaItem();
ei.setMetadata("<xml><metadata><general>asdf</general><keywords><keyword>k1</keyword><keyword>k2</keyword></keywords></metadata></xml>");

List<String> headers = new ArrayList<>();
headers.add("metadata/keywords/keyword");

ExportItemsDriver eid = new ExportItemsDriver();
try {
List<String> result = eid.buildRecord(headers, ei, null);
assertEquals(1, result.size());
assertEquals("k1,k2", result.get(0));
} catch (Exception e) {
fail("buildRecord failed with: " + e.getMessage());
}

}

@Test
public void testBuildRecordMultipleValuesApart() {
EquellaItem ei = new EquellaItem();
ei.setMetadata("<xml><metadata><keywords><keyword>k1</keyword></keywords><general>asdf</general><keywords><keyword>k2</keyword><keyword>k3</keyword></keywords></metadata></xml>");

List<String> headers = new ArrayList<>();
headers.add("metadata/keywords/keyword");

ExportItemsDriver eid = new ExportItemsDriver();
try {
List<String> result = eid.buildRecord(headers, ei, null);
assertEquals(1, result.size());
assertEquals("k1,k2,k3", result.get(0));
} catch (Exception e) {
fail("buildRecord failed with: " + e.getMessage());
}

}
}

0 comments on commit 6239ab9

Please sign in to comment.