This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve all jr:// URIs to media directory
Needed to build a FormDef for files with external secondary instances.
- Loading branch information
1 parent
b55a171
commit c5c11dd
Showing
11 changed files
with
217 additions
and
23 deletions.
There are no files selected for viewing
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
77 changes: 77 additions & 0 deletions
77
src/org/opendatakit/aggregate/parser/MediaFileReferenceFactory.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,77 @@ | ||
package org.opendatakit.aggregate.parser; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import org.javarosa.core.reference.InvalidReferenceException; | ||
import org.javarosa.core.reference.Reference; | ||
import org.javarosa.core.reference.ReferenceFactory; | ||
|
||
/** | ||
* ReferenceFactory implementation that resolves any jr:// URI to the specified media folder. Most methods are | ||
* unimplemented and throw UnsupportedOperationException if used. | ||
*/ | ||
@SuppressWarnings("checkstyle:ParameterName") | ||
public class MediaFileReferenceFactory implements ReferenceFactory { | ||
private final File mediaDirectory; | ||
|
||
public MediaFileReferenceFactory(File mediaDirectory) { | ||
this.mediaDirectory = mediaDirectory; | ||
} | ||
|
||
@Override | ||
public boolean derives(String URI) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Reference derive(String URI) throws InvalidReferenceException { | ||
return new Reference() { | ||
@Override | ||
public String getLocalURI() { | ||
return mediaDirectory.getAbsolutePath() + URI.substring(URI.lastIndexOf('/')); | ||
} | ||
|
||
@Override | ||
public boolean doesBinaryExist() throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public InputStream getStream() throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public String getURI() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean isReadOnly() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public OutputStream getOutputStream() throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void remove() throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Reference[] probeAlternativeReferences() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Reference derive(String URI, String context) throws InvalidReferenceException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
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
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
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
55 changes: 55 additions & 0 deletions
55
test/java/org/opendatakit/briefcase/model/BriefcaseFormDefinitionWithExternalDataTest.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,55 @@ | ||
package org.opendatakit.briefcase.model; | ||
|
||
import static java.nio.file.Files.delete; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.opendatakit.briefcase.util.BadFormDefinition; | ||
|
||
public class BriefcaseFormDefinitionWithExternalDataTest { | ||
private Path formDir; | ||
private Path formFile; | ||
private Path mediaDir; | ||
private Path mediaFile; | ||
|
||
@Before | ||
public void setUp() { | ||
try { | ||
formDir = Files.createTempDirectory("briefcase_test"); | ||
formFile = formDir.resolve("form.xml"); | ||
Path sourceFile = Paths.get(BriefcaseFormDefinitionWithExternalDataTest.class.getResource("form-with-external-secondary-instance.xml").toURI()); | ||
Files.copy(sourceFile, formFile); | ||
|
||
mediaDir = formDir.resolve(formDir.getFileName().toString() + "-media"); | ||
Files.createDirectories(mediaDir); | ||
mediaFile = mediaDir.resolve("external-xml.xml"); | ||
Path sourceMedia = Paths.get(BriefcaseFormDefinitionWithExternalDataTest.class.getResource("external-xml.xml").toURI()); | ||
Files.copy(sourceMedia, mediaFile); | ||
} catch (Throwable t) { | ||
throw new RuntimeException(t); | ||
} | ||
} | ||
|
||
@Test | ||
public void buildsFormDef_whenDefinitionReferencesExternalSecondaryInstance() throws BadFormDefinition { | ||
BriefcaseFormDefinition briefcaseFormDefinition = new BriefcaseFormDefinition(formDir.toFile(), | ||
formFile.toFile()); | ||
|
||
assertThat(briefcaseFormDefinition.getFormName(), is("Form with external secondary instance")); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
delete(formFile); | ||
delete(mediaFile); | ||
delete(mediaDir); | ||
delete(formDir); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
test/resources/org/opendatakit/briefcase/model/external-xml.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,14 @@ | ||
<root> | ||
<item> | ||
<label>A</label> | ||
<name>a</name> | ||
</item> | ||
<item> | ||
<label>B</label> | ||
<name>b</name> | ||
</item> | ||
<item> | ||
<label>C</label> | ||
<name>c</name> | ||
</item> | ||
</root> |
22 changes: 22 additions & 0 deletions
22
test/resources/org/opendatakit/briefcase/model/form-with-external-secondary-instance.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,22 @@ | ||
<h:html xmlns="http://www.w3.org/2002/xforms" | ||
xmlns:h="http://www.w3.org/1999/xhtml"> | ||
|
||
<h:head> | ||
<h:title>Form with external secondary instance</h:title> | ||
<model> | ||
<instance> | ||
<data id="external-instance"> | ||
<first_item_label/> | ||
</data> | ||
</instance> | ||
|
||
<instance id="external-xml" src="jr://file/external-xml.xml"/> | ||
|
||
<bind nodeset="/data/first_item_label" calculate="instance('external-xml')/item[name = 'a']/label"/> | ||
</model> | ||
</h:head> | ||
|
||
<h:body> | ||
</h:body> | ||
|
||
</h:html> |