use of org.kxml2.kdom.Element in project collect by opendatakit.
the class FileUtils method parseXML.
public static HashMap<String, String> parseXML(File xmlFile) {
final HashMap<String, String> fields = new HashMap<String, String>();
final InputStream is;
try {
is = new FileInputStream(xmlFile);
} catch (FileNotFoundException e1) {
Timber.d(e1);
throw new IllegalStateException(e1);
}
InputStreamReader isr;
try {
isr = new InputStreamReader(is, "UTF-8");
} catch (UnsupportedEncodingException uee) {
Timber.w(uee, "Trying default encoding as UTF 8 encoding unavailable");
isr = new InputStreamReader(is);
}
final Document doc;
try {
doc = XFormParser.getXMLDocument(isr);
} catch (IOException e) {
Timber.e(e, "Unable to parse XML document %s", xmlFile.getAbsolutePath());
throw new IllegalStateException("Unable to parse XML document", e);
} finally {
try {
isr.close();
} catch (IOException e) {
Timber.w("%s error closing from reader", xmlFile.getAbsolutePath());
}
}
final String xforms = "http://www.w3.org/2002/xforms";
final String html = doc.getRootElement().getNamespace();
final Element head = doc.getRootElement().getElement(html, "head");
final Element title = head.getElement(html, "title");
if (title != null) {
fields.put(TITLE, XFormParser.getXMLText(title, true));
}
final Element model = getChildElement(head, "model");
Element cur = getChildElement(model, "instance");
final int idx = cur.getChildCount();
int i;
for (i = 0; i < idx; ++i) {
if (cur.isText(i)) {
continue;
}
if (cur.getType(i) == Node.ELEMENT) {
break;
}
}
if (i < idx) {
// this is the first data element
cur = cur.getElement(i);
final String id = cur.getAttributeValue(null, "id");
final String version = cur.getAttributeValue(null, "version");
final String uiVersion = cur.getAttributeValue(null, "uiVersion");
if (uiVersion != null) {
// pre-OpenRosa 1.0 variant of spec
Timber.e("Obsolete use of uiVersion -- IGNORED -- only using version: %s", version);
}
fields.put(FORMID, (id == null) ? cur.getNamespace() : id);
fields.put(VERSION, (version == null) ? null : version);
} else {
throw new IllegalStateException(xmlFile.getAbsolutePath() + " could not be parsed");
}
try {
final Element submission = model.getElement(xforms, "submission");
final String base64RsaPublicKey = submission.getAttributeValue(null, "base64RsaPublicKey");
final String autoDelete = submission.getAttributeValue(null, "auto-delete");
final String autoSend = submission.getAttributeValue(null, "auto-send");
fields.put(SUBMISSIONURI, submission.getAttributeValue(null, "action"));
fields.put(BASE64_RSA_PUBLIC_KEY, (base64RsaPublicKey == null || base64RsaPublicKey.trim().length() == 0) ? null : base64RsaPublicKey.trim());
fields.put(AUTO_DELETE, autoDelete);
fields.put(AUTO_SEND, autoSend);
} catch (Exception e) {
Timber.i("XML file %s does not have a submission element", xmlFile.getAbsolutePath());
// and that's totally fine.
}
return fields;
}
use of org.kxml2.kdom.Element in project collect by opendatakit.
the class DownloadFormListTask method downloadMediaFileList.
private List<MediaFile> downloadMediaFileList(String manifestUrl) {
if (manifestUrl == null) {
return null;
}
// get shared HttpContext so that authentication and cookies are retained.
HttpContext localContext = Collect.getInstance().getHttpContext();
HttpClient httpclient = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT);
DocumentFetchResult result = WebUtils.getXmlDocument(manifestUrl, localContext, httpclient);
if (result.errorMessage != null) {
return null;
}
String errMessage = Collect.getInstance().getString(R.string.access_error, manifestUrl);
if (!result.isOpenRosaResponse) {
errMessage += Collect.getInstance().getString(R.string.manifest_server_error);
Timber.e(errMessage);
return null;
}
// Attempt OpenRosa 1.0 parsing
Element manifestElement = result.doc.getRootElement();
if (!manifestElement.getName().equals("manifest")) {
errMessage += Collect.getInstance().getString(R.string.root_element_error, manifestElement.getName());
Timber.e(errMessage);
return null;
}
String namespace = manifestElement.getNamespace();
if (!DownloadFormsTask.isXformsManifestNamespacedElement(manifestElement)) {
errMessage += Collect.getInstance().getString(R.string.root_namespace_error, namespace);
Timber.e(errMessage);
return null;
}
int elements = manifestElement.getChildCount();
List<MediaFile> files = new ArrayList<>();
for (int i = 0; i < elements; ++i) {
if (manifestElement.getType(i) != Element.ELEMENT) {
// e.g., whitespace (text)
continue;
}
Element mediaFileElement = manifestElement.getElement(i);
if (!DownloadFormsTask.isXformsManifestNamespacedElement(mediaFileElement)) {
// someone else's extension?
continue;
}
String name = mediaFileElement.getName();
if (name.equalsIgnoreCase("mediaFile")) {
String filename = null;
String hash = null;
String downloadUrl = null;
// don't process descriptionUrl
int childCount = mediaFileElement.getChildCount();
for (int j = 0; j < childCount; ++j) {
if (mediaFileElement.getType(j) != Element.ELEMENT) {
// e.g., whitespace (text)
continue;
}
Element child = mediaFileElement.getElement(j);
if (!DownloadFormsTask.isXformsManifestNamespacedElement(child)) {
// someone else's extension?
continue;
}
String tag = child.getName();
switch(tag) {
case "filename":
filename = XFormParser.getXMLText(child, true);
if (filename != null && filename.length() == 0) {
filename = null;
}
break;
case "hash":
hash = XFormParser.getXMLText(child, true);
if (hash != null && hash.length() == 0) {
hash = null;
}
break;
case "downloadUrl":
downloadUrl = XFormParser.getXMLText(child, true);
if (downloadUrl != null && downloadUrl.length() == 0) {
downloadUrl = null;
}
break;
}
}
if (filename == null || downloadUrl == null || hash == null) {
errMessage += Collect.getInstance().getString(R.string.manifest_tag_error, Integer.toString(i));
Timber.e(errMessage);
return null;
}
files.add(new MediaFile(filename, hash, downloadUrl));
}
}
return files;
}
use of org.kxml2.kdom.Element in project collect by opendatakit.
the class DownloadFormsTask method downloadManifestAndMediaFiles.
private String downloadManifestAndMediaFiles(String tempMediaPath, String finalMediaPath, FormDetails fd, int count, int total) throws Exception {
if (fd.getManifestUrl() == null) {
return null;
}
publishProgress(Collect.getInstance().getString(R.string.fetching_manifest, fd.getFormName()), String.valueOf(count), String.valueOf(total));
List<MediaFile> files = new ArrayList<MediaFile>();
// get shared HttpContext so that authentication and cookies are retained.
HttpContext localContext = Collect.getInstance().getHttpContext();
HttpClient httpclient = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT);
DocumentFetchResult result = WebUtils.getXmlDocument(fd.getManifestUrl(), localContext, httpclient);
if (result.errorMessage != null) {
return result.errorMessage;
}
String errMessage = Collect.getInstance().getString(R.string.access_error, fd.getManifestUrl());
if (!result.isOpenRosaResponse) {
errMessage += Collect.getInstance().getString(R.string.manifest_server_error);
Timber.e(errMessage);
return errMessage;
}
// Attempt OpenRosa 1.0 parsing
Element manifestElement = result.doc.getRootElement();
if (!manifestElement.getName().equals("manifest")) {
errMessage += Collect.getInstance().getString(R.string.root_element_error, manifestElement.getName());
Timber.e(errMessage);
return errMessage;
}
String namespace = manifestElement.getNamespace();
if (!isXformsManifestNamespacedElement(manifestElement)) {
errMessage += Collect.getInstance().getString(R.string.root_namespace_error, namespace);
Timber.e(errMessage);
return errMessage;
}
int elements = manifestElement.getChildCount();
for (int i = 0; i < elements; ++i) {
if (manifestElement.getType(i) != Element.ELEMENT) {
// e.g., whitespace (text)
continue;
}
Element mediaFileElement = manifestElement.getElement(i);
if (!isXformsManifestNamespacedElement(mediaFileElement)) {
// someone else's extension?
continue;
}
String name = mediaFileElement.getName();
if (name.equalsIgnoreCase("mediaFile")) {
String filename = null;
String hash = null;
String downloadUrl = null;
// don't process descriptionUrl
int childCount = mediaFileElement.getChildCount();
for (int j = 0; j < childCount; ++j) {
if (mediaFileElement.getType(j) != Element.ELEMENT) {
// e.g., whitespace (text)
continue;
}
Element child = mediaFileElement.getElement(j);
if (!isXformsManifestNamespacedElement(child)) {
// someone else's extension?
continue;
}
String tag = child.getName();
switch(tag) {
case "filename":
filename = XFormParser.getXMLText(child, true);
if (filename != null && filename.length() == 0) {
filename = null;
}
break;
case "hash":
hash = XFormParser.getXMLText(child, true);
if (hash != null && hash.length() == 0) {
hash = null;
}
break;
case "downloadUrl":
downloadUrl = XFormParser.getXMLText(child, true);
if (downloadUrl != null && downloadUrl.length() == 0) {
downloadUrl = null;
}
break;
}
}
if (filename == null || downloadUrl == null || hash == null) {
errMessage += Collect.getInstance().getString(R.string.manifest_tag_error, Integer.toString(i));
Timber.e(errMessage);
return errMessage;
}
files.add(new MediaFile(filename, hash, downloadUrl));
}
}
// OK we now have the full set of files to download...
Timber.i("Downloading %d media files.", files.size());
int mediaCount = 0;
if (files.size() > 0) {
File tempMediaDir = new File(tempMediaPath);
File finalMediaDir = new File(finalMediaPath);
FileUtils.checkMediaPath(tempMediaDir);
FileUtils.checkMediaPath(finalMediaDir);
for (MediaFile toDownload : files) {
++mediaCount;
publishProgress(Collect.getInstance().getString(R.string.form_download_progress, fd.getFormName(), String.valueOf(mediaCount), String.valueOf(files.size())), String.valueOf(count), String.valueOf(total));
// try {
File finalMediaFile = new File(finalMediaDir, toDownload.getFilename());
File tempMediaFile = new File(tempMediaDir, toDownload.getFilename());
if (!finalMediaFile.exists()) {
downloadFile(tempMediaFile, toDownload.getDownloadUrl());
} else {
String currentFileHash = FileUtils.getMd5Hash(finalMediaFile);
String downloadFileHash = getMd5Hash(toDownload.getHash());
if (currentFileHash != null && downloadFileHash != null && !currentFileHash.contentEquals(downloadFileHash)) {
// if the hashes match, it's the same file
// otherwise delete our current one and replace it with the new one
FileUtils.deleteAndReport(finalMediaFile);
downloadFile(tempMediaFile, toDownload.getDownloadUrl());
} else {
// exists, and the hash is the same
// no need to download it again
Timber.i("Skipping media file fetch -- file hashes identical: %s", finalMediaFile.getAbsolutePath());
}
}
// } catch (Exception e) {
// return e.getLocalizedMessage();
// }
}
}
return null;
}
use of org.kxml2.kdom.Element in project javarosa by opendatakit.
the class ChildProcessingTest method recognizesThatOneChildIsATemplate.
@Test
public void recognizesThatOneChildIsATemplate() {
Element el = new Element();
Element child1 = el.createElement(null, "Child Name");
child1.setAttribute(NAMESPACE_JAVAROSA, "template", "");
el.addChild(ELEMENT, child1);
el.addChild(ELEMENT, el.createElement(null, "Child Name"));
assertFalse(XFormParser.childOptimizationsOk(el));
}
use of org.kxml2.kdom.Element in project javarosa by opendatakit.
the class ChildProcessingTest method worksWithTwoMatchingChildren.
@Test
public void worksWithTwoMatchingChildren() {
Element el = new Element();
el.addChild(ELEMENT, el.createElement(null, "Child Name"));
el.addChild(ELEMENT, el.createElement(null, "Child Name"));
assertTrue(XFormParser.childOptimizationsOk(el));
}
Aggregations