Android Open Source - openhds-tablet Form Xml Reader






From Project

Back to project page openhds-tablet.

License

The source code is released under:

OPENHDS PLATFORM OPENSOURCE LICENSE AGREEMENT Copyright (c) 2013 University of Southern Maine. All rights reserved. Redistribution and use in source and binary forms, with or without mo...

If you think the Android project openhds-tablet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.openhds.mobile.model;
//w w  w  .j a  v  a  2  s.co  m
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class FormXmlReader {

    private XPath xpath = XPathFactory.newInstance().newXPath();
    private DocumentBuilder builder;

    public Location readLocation(InputStream is, String jrFormId) {
        try {
            Location location = new Location();
            
            Document doc = buildDocument(is);
            if(xpath.evaluate("/"+jrFormId+"/locationId/text()", doc).length()==0) {
              jrFormId ="data";
            }
            location.setName(xpath.evaluate("/"+jrFormId+"/locationName/text()", doc));
            location.setExtId(xpath.evaluate("/"+jrFormId+"/locationId/text()", doc));
            location.setHierarchy(xpath.evaluate("/"+jrFormId+"/hierarchyId/text()", doc));

            return location;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }

        return null;
    }

    private Document buildDocument(InputStream is) throws ParserConfigurationException, SAXException, IOException {
        if (builder == null) {
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        }
        
        Document doc = builder.parse(is);
        return doc;
    }

    public Visit readVisit(InputStream input, String jrFormId) {
        try {
            Visit visit = new Visit();
            Document doc = buildDocument(input);
            if(xpath.evaluate("/"+jrFormId+"/visitId/text()", doc).length()==0) {
              jrFormId ="data";
            }
            visit.setDate(xpath.evaluate("/"+jrFormId+"/visitDate/text()", doc));
            visit.setExtId(xpath.evaluate("/"+jrFormId+"/visitId/text()", doc));
            visit.setLocation(xpath.evaluate("/"+jrFormId+"/locationId/text()", doc));
            visit.setRound(xpath.evaluate("/"+jrFormId+"/roundNumber/text()", doc));
            visit.setIntervieweeId(xpath.evaluate("/"+jrFormId+"/intervieweeId/text()", doc));
            return visit;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }
        return null;
    }

    
    public Membership readMembership(InputStream input, String jrFormId) {
        try {
            Membership membership = new Membership();
            Document doc = buildDocument(input);
            if(xpath.evaluate("/"+jrFormId+"/individualId/text()", doc).length()==0) {
              jrFormId ="data";
            }
            membership.setIndExtId(xpath.evaluate("/"+jrFormId+"/individualId/text()", doc));
            membership.setGroupextId(xpath.evaluate("/"+jrFormId+"/householdId/text()", doc));
           
            return membership;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }
        return null;
    }
    
    
    public SocialGroup readSocialGroup(InputStream input, String jrFormId) {
        try {
            Document doc = buildDocument(input);
            if(xpath.evaluate("/"+jrFormId+"/individualId/text()", doc).length()==0) {
              jrFormId ="data";
            }
            SocialGroup sg = new SocialGroup();
            sg.setExtId(xpath.evaluate("/"+jrFormId+"/householdId/text()", doc));
            sg.setGroupHead(xpath.evaluate("/"+jrFormId+"/individualId/text()", doc));
            sg.setGroupName(xpath.evaluate("/"+jrFormId+"/householdName/text()", doc));
            
            return sg;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }
        return null;
    }

    public Individual readInMigration(FileInputStream fileInputStream, String jrFormId)  {
        try {
            Document doc = buildDocument(fileInputStream);
            if(xpath.evaluate("/"+jrFormId+"/visitId/text()", doc).length()==0) {
              jrFormId ="data";
            }
            Individual individual = new Individual();
            individual.setCurrentResidence(xpath.evaluate("/"+jrFormId+"/locationId/text()", doc));
            if(xpath.evaluate("/"+jrFormId+"/individualInfo/dateOfBirth/text()", doc).length()>0) {
              individual.setDobIn(xpath.evaluate("/"+jrFormId+"/individualInfo/dateOfBirth/text()", doc));
            }
            individual.setExtId(xpath.evaluate("/"+jrFormId+"/individualInfo/individualId/text()", doc));
            individual.setFather(xpath.evaluate("/"+jrFormId+"/individualInfo/fatherId/text()", doc));
            individual.setFirstName(xpath.evaluate("/"+jrFormId+"/individualInfo/firstName/text()", doc));
            individual.setMiddleName(xpath.evaluate("/"+jrFormId+"/individualInfo/middleName/text()", doc));
            individual.setGender(xpath.evaluate("/"+jrFormId+"/individualInfo/gender/text()", doc));
            individual.setLastName(xpath.evaluate("/"+jrFormId+"/individualInfo/lastName/text()", doc));
            individual.setMother(xpath.evaluate("/"+jrFormId+"/individualInfo/motherId/text()", doc));
            
            return individual;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }
        return null;
    }

    public PregnancyOutcome readPregnancyOutcome(FileInputStream fileInputStream, String jrFormId)  {
        try {
            Document doc = buildDocument(fileInputStream);
            
            PregnancyOutcome pregOutcome = new PregnancyOutcome();
            if(xpath.evaluate("/"+jrFormId+"/motherId/text()", doc).length()==0) {
              jrFormId ="data";
            }
            Individual mother = new Individual();
            mother.setExtId(xpath.evaluate("/"+jrFormId+"/motherId/text()", doc));
            pregOutcome.setMother(mother);
            
            Individual father = new Individual();
            father.setExtId(xpath.evaluate("/"+jrFormId+"/fatherId/text()", doc));
            pregOutcome.setFather(father);
            
            pregOutcome.setRecordedDate(xpath.evaluate("/"+jrFormId+"/recordedDate/text()", doc));
            
            // read the children that were born
            NodeList nodeList = (NodeList) xpath.evaluate("//outcomes", doc, XPathConstants.NODESET);
            for(int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                String outcomeType = xpath.evaluate("./outcomeType/text()", node);
                if (!"LBR".equalsIgnoreCase(outcomeType)) {
                    continue;
                } 
                
                Individual individual = new Individual();
                individual.setDobIn(pregOutcome.getRecordedDate());
                individual.setExtId(xpath.evaluate("./childId/text()", node));
                individual.setFather(father.getExtId());
                individual.setMother(mother.getExtId());
                individual.setFirstName(xpath.evaluate("./firstName/text()", node));
                individual.setGender(xpath.evaluate("./gender/text()", node));
                individual.setLastName(xpath.evaluate(".//lastName/text()", node));
                SocialGroup group = new SocialGroup();
                group.setExtId(xpath.evaluate("./socialGroupId/text()", node));
                individual.setSocialGroups(Arrays.asList(group));
                
                pregOutcome.addChild(individual);
            }
            
            return pregOutcome;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }
        return null;
    }

    public Relationship readRelationship(FileInputStream fileInputStream, String jrFormId)  {
        try {
            Document doc = buildDocument(fileInputStream);
            if(xpath.evaluate("/"+jrFormId+"/individualA/text()", doc).length()==0) {
              jrFormId ="data";
            }
            Relationship relationship = new Relationship();
            relationship.setIndividualA(xpath.evaluate("/"+jrFormId+"/individualA/text()", doc));
            relationship.setIndividualB(xpath.evaluate("/"+jrFormId+"/individualB/text()", doc));
            relationship.setStartDate(xpath.evaluate("/"+jrFormId+"/startDate/text()", doc));
            
            return relationship;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }
        return null;
    }
    
    public Individual readOutMigration(FileInputStream fileInputStream, String jrFormId)  {
        try {
            Document doc = buildDocument(fileInputStream);
            Individual individual = new Individual();
            if(xpath.evaluate("/"+jrFormId+"/individualId/text()", doc).length()==0) {
              jrFormId ="data";
            }
            individual.setExtId(xpath.evaluate("/"+jrFormId+"/individualId/text()", doc));
            return individual;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }
        return null;
    }

    public Individual readDeath(FileInputStream fileInputStream, String jrFormId) {
        try {
            Document doc = buildDocument(fileInputStream);
            Individual individual = new Individual();
            if(xpath.evaluate("/"+jrFormId+"/individualId/text()", doc).length()==0) {
              jrFormId ="data";
            }
            individual.setExtId(xpath.evaluate("/"+jrFormId+"/individualId/text()", doc));
            return individual;
        } catch (ParserConfigurationException e) {
        } catch (SAXException e) {
        } catch (IOException e) {
        } catch (XPathExpressionException e) {
        }
        return null;
    }

}




Java Source Code List

org.openhds.mobile.BadXmlException.java
org.openhds.mobile.Converter.java
org.openhds.mobile.FieldWorkerProvider.java
org.openhds.mobile.FormsProviderAPI.java
org.openhds.mobile.InstanceProviderAPI.java
org.openhds.mobile.OpenHDS.java
org.openhds.mobile.Queries.java
org.openhds.mobile.activity.AbstractActivity.java
org.openhds.mobile.activity.FieldWorkerLoginActivity.java
org.openhds.mobile.activity.FilterActivity.java
org.openhds.mobile.activity.FilterFormActivity.java
org.openhds.mobile.activity.FilterLocationActivity.java
org.openhds.mobile.activity.FilterVisitActivity.java
org.openhds.mobile.activity.FormListActivity.java
org.openhds.mobile.activity.FormViewActivity.java
org.openhds.mobile.activity.OpeningActivity.java
org.openhds.mobile.activity.ServerPreferencesActivity.java
org.openhds.mobile.activity.ShowMapActivity.java
org.openhds.mobile.activity.SupervisorLoginActivity.java
org.openhds.mobile.activity.SupervisorMainActivity.java
org.openhds.mobile.activity.SyncDatabaseActivity.java
org.openhds.mobile.activity.UpdateActivity.java
org.openhds.mobile.adapter.AdapterContent.java
org.openhds.mobile.adapter.MapAdapter.java
org.openhds.mobile.database.DatabaseAdapter.java
org.openhds.mobile.database.DeathUpdate.java
org.openhds.mobile.database.ExternalInMigrationUpdate.java
org.openhds.mobile.database.HouseholdUpdate.java
org.openhds.mobile.database.InternalInMigrationUpdate.java
org.openhds.mobile.database.LocationUpdate.java
org.openhds.mobile.database.MembershipUpdate.java
org.openhds.mobile.database.OutMigrationUpdate.java
org.openhds.mobile.database.PregnancyOutcomeUpdate.java
org.openhds.mobile.database.RelationshipUpdate.java
org.openhds.mobile.database.Updatable.java
org.openhds.mobile.database.VisitUpdate.java
org.openhds.mobile.fragment.EventFragment.java
org.openhds.mobile.fragment.ProgressFragment.java
org.openhds.mobile.fragment.SelectionFilterFragment.java
org.openhds.mobile.fragment.SelectionFilterLocFragment.java
org.openhds.mobile.fragment.SelectionFormFragment.java
org.openhds.mobile.fragment.SelectionFragment.java
org.openhds.mobile.fragment.ValueFormFragment.java
org.openhds.mobile.fragment.ValueFragment.java
org.openhds.mobile.fragment.ValueLocFragment.java
org.openhds.mobile.listener.CollectEntitiesListener.java
org.openhds.mobile.listener.OdkFormLoadListener.java
org.openhds.mobile.listener.RetrieveFieldWorkersListener.java
org.openhds.mobile.listener.TaskCompleteListener.java
org.openhds.mobile.listener.ValueSelectedListener.java
org.openhds.mobile.model.Child.java
org.openhds.mobile.model.FieldWorker.java
org.openhds.mobile.model.FilledForm.java
org.openhds.mobile.model.FilledParams.java
org.openhds.mobile.model.FormFiller.java
org.openhds.mobile.model.FormSubmissionRecord.java
org.openhds.mobile.model.FormXmlReader.java
org.openhds.mobile.model.Form.java
org.openhds.mobile.model.HierarchySelection.java
org.openhds.mobile.model.Individual.java
org.openhds.mobile.model.LocationHierarchy.java
org.openhds.mobile.model.LocationVisit.java
org.openhds.mobile.model.Location.java
org.openhds.mobile.model.Membership.java
org.openhds.mobile.model.PregnancyObservationUpdate.java
org.openhds.mobile.model.PregnancyOutcome.java
org.openhds.mobile.model.Relationship.java
org.openhds.mobile.model.Result.java
org.openhds.mobile.model.Round.java
org.openhds.mobile.model.SocialGroup.java
org.openhds.mobile.model.StateMachine.java
org.openhds.mobile.model.Supervisor.java
org.openhds.mobile.model.UpdateEvent.java
org.openhds.mobile.model.UpdateParams.java
org.openhds.mobile.model.UpdateStatus.java
org.openhds.mobile.model.Visit.java
org.openhds.mobile.provider.OpenHDSProvider.java
org.openhds.mobile.task.AbstractHttpTask.java
org.openhds.mobile.task.AuthenticateTask.java
org.openhds.mobile.task.DownloadFormsTask.java
org.openhds.mobile.task.FieldWorkerLoginTask.java
org.openhds.mobile.task.OdkFormLoadTask.java
org.openhds.mobile.task.OdkGeneratedFormLoadTask.java
org.openhds.mobile.task.SupervisorLoginTask.java
org.openhds.mobile.task.SyncEntitiesTask.java
org.openhds.mobile.task.SyncFormsTask.java
org.openhds.mobile.utilities.L.java
org.openhds.mobile.utilities.Logg.java
org.openhds.mobile.utilities.UrlUtils.java