Skip to content

Commit

Permalink
Added functionality for conversion of XML to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
prabs3257 committed Jul 23, 2023
1 parent aff74fc commit e2cda8f
Show file tree
Hide file tree
Showing 23 changed files with 3,659 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@
import org.odk.collect.android.utilities.FormsRepositoryProvider;
import org.odk.collect.android.utilities.InstancesRepositoryProvider;
import org.odk.collect.android.utilities.PlayServicesChecker;
import org.odk.collect.android.utilities.ReadFileUtil;
import org.odk.collect.android.utilities.ScreenContext;
import org.odk.collect.android.utilities.SnackbarUtils;
import org.odk.collect.android.utilities.SoftKeyboardController;
import org.odk.collect.android.utilities.XmlToJsonConvertUtil;
import org.odk.collect.android.widgets.DateTimeWidget;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.range.RangePickerDecimalWidget;
Expand Down Expand Up @@ -201,7 +203,6 @@
import java.util.Optional;

import javax.inject.Inject;

import timber.log.Timber;

/**
Expand Down Expand Up @@ -1828,6 +1829,12 @@ private void handleSaveResult(FormSaveViewModel.SaveResult result) {

Instance instance = new InstancesRepositoryProvider(this).get().getOneByPath(getAbsoluteInstancePath());
if (instance != null) {

File instanceFile = new File(instance.getInstanceFilePath());
String XMLString = ReadFileUtil.FileRead(instanceFile);

String convertedJson = XmlToJsonConvertUtil.convertToJson(XMLString,instanceFile);
FormEventBus.INSTANCE.formSubmitted(instance.getFormId(), convertedJson);
FormEventBus.INSTANCE.formSaved(instance.getFormId(), instance.getInstanceFilePath());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ object FormEventBus: ODKEventBus<FormStateEvent>() {
state.onNext(FormStateEvent.OnFormSaved(formId, instancePath))
}

fun formSubmitted(formId: String, jsonData: String, ) {
state.onNext(FormStateEvent.OnFormSubmitted(formId, jsonData))
}

fun formSaveError(formId: String, errorMessage: String) {
state.onNext(FormStateEvent.OnFormSaveError(formId, errorMessage))
}
Expand Down Expand Up @@ -50,6 +54,9 @@ sealed class FormStateEvent {
/** Called when a form is saved. */
data class OnFormSaved(val formId: String, val instancePath: String): FormStateEvent()

/** Called when a form is submitted. */
data class OnFormSubmitted(val formId: String, val jsonData: String): FormStateEvent()

/** Called when a form save process errors out. */
data class OnFormSaveError(val formId: String, val errorMessage: String): FormStateEvent()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.odk.collect.android.helpers.xmltojsonlib

apply plugin: 'com.android.library'

android {
compileSdkVersion 33

defaultConfig {
minSdkVersion 14
targetSdkVersion 33
versionCode 25
versionName "1.5.3"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.annotation:annotation:1.5.0'

// androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation "androidx.test:runner:1.5.2"
androidTestImplementation "androidx.test:rules:1.5.0"
androidTestImplementation 'androidx.test:core:1.5.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/aguyon/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest package="fr.arnaudguyon.xmltojsonlib" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="gb2312"?>
<biblio>
<auteur id="4">
<nom>Herge</nom>
<ouvrages quantite="1">
<livre id="0">
<type>BD</type>
<titre>Le secret de la licorne</titre>
</livre>
</ouvrages>
</auteur>
<auteur id="5">
<nom>Franquin</nom>
<ouvrages quantite="3">
<livre id="0">
<type>BD</type>
<titre>Z comme Zorglub</titre>
</livre>
<livre id="1">
<type>BD</type>
<titre>Spirou et les hommes bulle</titre>
</livre>
<livre id="2">
<type>BD</type>
<titre>Le nid du marsupilami</titre>
</livre>
</ouvrages>
</auteur>
<result>1</result>
<result_info>succes</result_info>
</biblio>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<library>
<owner>John Doe</owner>
<book id="007">James Bond</book>
<book id="000">Book for the dummies</book>
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<main>
<test>aa="\11"</test>
<test2>aa="42"</test2>
<description><![CDATA[<img src="http://cdn.link.net/154335.jpg" align="left" hspace="7" width="120" height="90"/>any desc here.]]>
</description>
</main>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<map>
<a>42</a>
<b>1498094219318</b>
<c>0.100</c>
<d>1099511627776.5</d>
<e>true</e>
<f>false</f>
<g></g>
<h></h>
<i id="43"/>
<j>44</j>
</map>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<list>
<item id="item A 1"/>
<item id="item A 2">
<list>
<item id="item B 1"/>
</list>
</item>
<notitem id="notitem Z" />
</list>
<tlist>
<item id="item C 1"/>
</tlist>
</catalog>
Loading

0 comments on commit e2cda8f

Please sign in to comment.