-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support CSV external data #397
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
resources/org/javarosa/xform/parse/external-select-csv.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0"?> | ||
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:odk="http://www.opendatakit.org/xforms" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> | ||
<h:head> | ||
<h:title>external select 10</h:title> | ||
<model> | ||
<itext> | ||
<translation default="true()" lang="default"> | ||
<text id="static_instance-first-0"> | ||
<value>a</value> | ||
</text> | ||
<text id="static_instance-first-1"> | ||
<value>b</value> | ||
</text> | ||
<text id="static_instance-second-0"> | ||
<value>aa</value> | ||
</text> | ||
<text id="static_instance-second-1"> | ||
<value>ab</value> | ||
</text> | ||
<text id="static_instance-second-3"> | ||
<value>ba</value> | ||
</text> | ||
<text id="static_instance-second-4"> | ||
<value>bb</value> | ||
</text> | ||
</translation> | ||
</itext> | ||
<instance> | ||
<external_select_10 id="external_select_10"> | ||
<first/> | ||
<second/> | ||
<meta> | ||
<instanceID/> | ||
</meta> | ||
</external_select_10> | ||
</instance> | ||
<instance id="first"> | ||
<root> | ||
<item> | ||
<itextId>static_instance-first-0</itextId> | ||
<name>a</name> | ||
</item> | ||
<item> | ||
<itextId>static_instance-first-1</itextId> | ||
<name>b</name> | ||
</item> | ||
</root> | ||
</instance> | ||
<instance id="second" src="jr://file-csv/external-select.csv"> | ||
</instance> | ||
<bind nodeset="/external_select_10/first" type="select1"/> | ||
<bind nodeset="/external_select_10/second" type="select1"/> | ||
<bind calculate="concat('uuid:', uuid())" nodeset="/external_select_10/meta/instanceID" readonly="true()" type="string"/> | ||
</model> | ||
</h:head> | ||
<h:body> | ||
<select1 ref="/external_select_10/first"> | ||
<label>First</label> | ||
<item> | ||
<label>a</label> | ||
<value>a</value> | ||
</item> | ||
<item> | ||
<label>b</label> | ||
<value>b</value> | ||
</item> | ||
</select1> | ||
<select1 ref="/external_select_10/second"> | ||
<label>Second</label> | ||
<itemset nodeset="instance('second')/root/item[first= /external_select_10/first ]"> | ||
<value ref="name"/> | ||
<label ref="jr:itext(itextId)"/> | ||
</itemset> | ||
</select1> | ||
</h:body> | ||
</h:html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
itextId,name,first | ||
static_instance-second-0,aa,a | ||
static_instance-second-0,ab,a | ||
static_instance-second-0,ba,b | ||
static_instance-second-0,bb,b |
32 changes: 32 additions & 0 deletions
32
src/org/javarosa/core/model/instance/CsvExternalInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.javarosa.core.model.instance; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import org.javarosa.core.model.data.UncastData; | ||
|
||
public class CsvExternalInstance { | ||
public static TreeElement parse(String instanceId, String path) throws IOException { | ||
TreeElement root = new TreeElement("root", 0); | ||
root.setInstanceName(instanceId); | ||
BufferedReader br = new BufferedReader(new FileReader(path)); | ||
String csvLine = br.readLine(); | ||
|
||
if (csvLine != null) { | ||
String[] fieldNames = csvLine.split(","); | ||
|
||
while ((csvLine = br.readLine()) != null) { | ||
TreeElement item = new TreeElement("item", 0); | ||
String[] data = csvLine.split(","); | ||
for (int i = 0; i < fieldNames.length; ++i) { | ||
TreeElement field = new TreeElement(fieldNames[i], 0); | ||
field.setValue(new UncastData(data[i])); | ||
item.addChild(field); | ||
} | ||
|
||
root.addChild(item); | ||
} | ||
} | ||
return root; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/org/javarosa/core/model/instance/XmlExternalInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.javarosa.core.model.instance; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import org.javarosa.xml.ElementParser; | ||
import org.javarosa.xml.TreeElementParser; | ||
import org.javarosa.xml.util.InvalidStructureException; | ||
import org.javarosa.xml.util.UnfullfilledRequirementsException; | ||
import org.kxml2.io.KXmlParser; | ||
import org.xmlpull.v1.XmlPullParserException; | ||
|
||
public class XmlExternalInstance { | ||
public static TreeElement parse(String instanceId, String path) throws IOException, InvalidStructureException, XmlPullParserException, UnfullfilledRequirementsException { | ||
InputStream inputStream = new FileInputStream(path); | ||
KXmlParser xmlParser = ElementParser.instantiateParser(inputStream); | ||
TreeElementParser treeElementParser = new TreeElementParser(xmlParser, 0, instanceId); | ||
return treeElementParser.parse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
test/org/javarosa/core/reference/ReferenceManagerTestUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.javarosa.core.reference; | ||
|
||
import java.nio.file.Path; | ||
|
||
public class ReferenceManagerTestUtils { | ||
/** | ||
* Returns a new ReferenceFactory that will derive (resolve) the given scheme to the given | ||
* path. | ||
*/ | ||
public static PrefixedRootFactory buildReferenceFactory(String scheme, final String path) { | ||
return new PrefixedRootFactory(new String[]{scheme + "/"}) { | ||
@Override | ||
protected Reference factory(String terminal, String URI) { | ||
return new ResourceReference(path + "/" + terminal); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Sets up the ReferenceManager singleton to derive (resolve) the given schema to the | ||
* given path. | ||
* <p> | ||
* Please, be aware that this method resets the singleton ReferenceManager, which could | ||
* have unintended consequences for other classes using it during the same JVM session. | ||
* <p> | ||
* Use of this method is intended when only one scheme is to be derived. If your test | ||
* form uses more than one scheme, you will have to follow a more conventional setup of | ||
* having a reference factory for jr://file to a base path and some session translators | ||
* that derive any other scheme (e.g. jr://audio) to a jr://file path. | ||
*/ | ||
public static ReferenceManager setUpSimpleReferenceManager(String scheme, Path path) { | ||
ReferenceManager refManager = ReferenceManager.instance(); | ||
refManager.reset(); | ||
refManager.addReferenceFactory(buildReferenceFactory( | ||
scheme, | ||
path.toAbsolutePath().toString() | ||
)); | ||
return refManager; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dcbriccetti @ggalmazor This particular form doesn't actually use the instance loaded by
file-csv
because it relies on the Collect-only database-backed implementation of thesearch()
appearance/function: https://github.com/opendatakit/collect/blob/d52ce0bfc63a11fb7f3fc62e0c0fd7c58684527a/collect_app/src/main/java/org/odk/collect/android/external/handler/ExternalDataHandlerSearch.java. You'll see the form doesn't actually use the instance. It's not a big deal since this test only verifies that the form can be parsed but it would probably be worth changing to avoid confusion.I would also recommend avoiding the "preload' word unless referring to https://opendatakit.github.io/xforms-spec/#preload-attributes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should file an issue for this change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose removing that test in #452