Example usage for javax.xml.bind Unmarshaller unmarshal

List of usage examples for javax.xml.bind Unmarshaller unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller unmarshal.

Prototype

public Object unmarshal(javax.xml.stream.XMLEventReader reader) throws JAXBException;

Source Link

Document

Unmarshal XML data from the specified pull parser and return the resulting content tree.

Usage

From source file:com.googlecode.refit.runner.TreeRunnerTest.java

public void checkXmlReport(File xmlReport) throws JAXBException {
    assertTrue(xmlReport.exists());//from w w w . j a  va 2s. co  m

    JAXBContext ctx = JAXBContext.newInstance(ReportIO.CONTEXT_PATH);
    Unmarshaller unmarshaller = ctx.createUnmarshaller();

    @SuppressWarnings("unchecked")
    JAXBElement<Summary> root = (JAXBElement<Summary>) unmarshaller.unmarshal(xmlReport);

    Summary summary = root.getValue();
    assertTrue(summary.getInputDir().endsWith("src/test/fit"));
    assertTrue(summary.getOutputDir().endsWith("target/fit"));
    assertEquals(1, summary.getExceptions());
    assertEquals(0, summary.getIgnored());
    assertEquals(10, summary.getWrong());
    assertEquals(339, summary.getRight());
    assertEquals(4, summary.getNumTests());

    List<TestResult> results = summary.getTest();
    checkResult(results.get(0), 0, 0, 0, 95, true, "MusicExample.html");
    checkResult(results.get(1), 1, 0, 0, 95, false, "MusicExampleWithErrors.html");
    checkResult(results.get(2), 0, 0, 10, 54, false, "MusicExampleWithFailures.html");
    checkResult(results.get(3), 0, 0, 0, 95, true, "subdir/MusicExample.html");
}

From source file:org.owasp.webgoat.plugin.XXE.java

private SearchForm parseXml(String xml) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(SearchForm.class);

    XMLInputFactory xif = XMLInputFactory.newFactory();
    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, true);
    xif.setProperty(XMLInputFactory.SUPPORT_DTD, true);
    XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    return (SearchForm) unmarshaller.unmarshal(xsr);
}

From source file:org.apereo.services.persondir.support.xml.CachingJaxbLoaderImpl.java

/**
 * @param xmlInputStream InputStream to read the XML from
 * @param unmarshaller Unmarshaller to generate the object from the XML with
 * @return An unmarshalled object model of the XML
 */// www  . ja  v  a2  s  .  c  o m
@SuppressWarnings("unchecked")
protected T unmarshal(final InputStream xmlInputStream, final Unmarshaller unmarshaller) {
    try {
        return (T) unmarshaller.unmarshal(xmlInputStream);
    } catch (final JAXBException e) {
        throw new RuntimeException("Unexpected JAXB error while unmarshalling  " + this.mappedXmlResource, e);
    }
}

From source file:it.cnr.icar.eric.common.cms.AbstractService.java

protected Object getBindingObjectFromNode(Node node) throws Exception {
    Object obj = null;/*from   ww w .j  ava 2  s  .  com*/

    Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
    obj = unmarshaller.unmarshal(node);

    return obj;
}

From source file:com.netscape.cmstools.system.KRAConnectorAddCLI.java

public void execute(String[] args) throws Exception {
    // Always check for "--help" prior to parsing
    if (Arrays.asList(args).contains("--help")) {
        printHelp();// w  w w . j  a  va 2 s.  c  o m
        return;
    }

    CommandLine cmd = parser.parse(options, args);

    String[] cmdArgs = cmd.getArgs();

    if (cmdArgs.length != 0) {
        throw new Exception("Too many arguments specified.");
    }

    String kraHost = cmd.getOptionValue("host");
    String kraPort = cmd.getOptionValue("port");
    String inputFile = cmd.getOptionValue("input-file");

    KRAConnectorClient kraConnectorClient = kraConnectorCLI.getKRAConnectorClient();

    //check if connector exists
    boolean connectorExists = true;
    try {
        @SuppressWarnings("unused")
        KRAConnectorInfo info = kraConnectorClient.getConnectorInfo();
    } catch (ConnectorNotFoundException e) {
        connectorExists = false;
    }

    if (inputFile != null) {
        if (connectorExists) {
            throw new Exception(
                    "Cannot add new connector from file.  " + "Delete the existing connector first");
        }
        FileInputStream fis = new FileInputStream(inputFile);
        JAXBContext context = JAXBContext.newInstance(KRAConnectorInfo.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        KRAConnectorInfo info = (KRAConnectorInfo) unmarshaller.unmarshal(fis);

        kraConnectorClient.addConnector(info);
        MainCLI.printMessage("Added KRA connector");

    } else {
        if (!connectorExists) {
            throw new Exception(
                    "Cannot add new host to existing connector.  " + "No connector currently exists");
        }
        kraConnectorClient.addHost(kraHost, kraPort);
        MainCLI.printMessage("Added KRA host \"" + kraHost + ":" + kraPort + "\"");
    }
}

From source file:gov.nih.nci.cabig.caaers.web.admin.InvestigatorImporter.java

public void processEntities(File xmlFile, ImportCommand command) {
    boolean valid = validateAgainstSchema(xmlFile, command, getXSDLocation(INVESTIGATOR_IMPORT));
    if (!valid) {
        return;/*from  w  w w.j  a v  a 2 s .  c  o  m*/
    }
    try {
        //DefaultInvestigatorMigratorService svc = (DefaultInvestigatorMigratorService) getApplicationContext().getBean("investigatorMigratorService");
        JAXBContext jaxbContext = JAXBContext
                .newInstance("gov.nih.nci.cabig.caaers.integration.schema.investigator");
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        Object importObject = unmarshaller.unmarshal(xmlFile);

        if (!validRootElement(importObject, INVESTIGATOR_IMPORT, command))
            return;

        gov.nih.nci.cabig.caaers.integration.schema.investigator.Staff staff = (gov.nih.nci.cabig.caaers.integration.schema.investigator.Staff) importObject;
        for (InvestigatorType xmlInvestigator : staff.getInvestigator()) {
            DomainObjectImportOutcome<Investigator> investigatorOutcome = investigatorMigratorService
                    .processInvestigator(xmlInvestigator);
            List<String> errors = domainObjectValidator.validate(investigatorOutcome.getImportedDomainObject());
            if (investigatorOutcome.isSavable() && errors.size() == 0) {
                command.addImportableInvestigator(investigatorOutcome);
            } else {
                for (String errMsg : errors) {
                    investigatorOutcome.addErrorMessage(errMsg, Severity.ERROR);
                }
                command.addNonImportableInvestigator(investigatorOutcome);
            }
        }
        //Remove Duplicate Investigators from the ImportableInvestigators List.
        List<DomainObjectImportOutcome<Investigator>> dupList = new ArrayList<DomainObjectImportOutcome<Investigator>>();
        for (int k = 0; k < command.getImportableInvestigators().size() - 1; k++) {
            Investigator inv1 = command.getImportableInvestigators().get(k).getImportedDomainObject();
            for (int l = k + 1; l < command.getImportableInvestigators().size(); l++) {
                Investigator inv2 = command.getImportableInvestigators().get(l).getImportedDomainObject();
                if (inv1.equals(inv2)) {
                    command.getImportableInvestigators().get(l).addErrorMessage("Duplicate Investigator",
                            Severity.ERROR);
                    command.addNonImportableInvestigator(command.getImportableInvestigators().get(l));
                    dupList.add(command.getImportableInvestigators().get(l));
                    logger.debug("Duplicate Investigator :: " + inv2.getFullName());
                    break;
                }
            }
        }
        for (DomainObjectImportOutcome<Investigator> obj : dupList) {
            command.getImportableInvestigators().remove(obj);
        }
    } catch (JAXBException e) {
        throw new RuntimeException("JAXB Exception", e);
    }
}

From source file:gov.nih.nci.cabig.caaers.web.admin.ResearchStaffImporter.java

public void processEntities(File xmlFile, ImportCommand command) {
    boolean valid = validateAgainstSchema(xmlFile, command, getXSDLocation(RESEARCH_STAFF_IMPORT));
    if (!valid) {
        return;//from w  ww.  j av  a  2  s.c om
    }
    try {
        //DefaultResearchStaffMigratorService svc = (DefaultResearchStaffMigratorService) getApplicationContext().getBean("researchStaffMigratorService");
        JAXBContext jaxbContext = JAXBContext
                .newInstance("gov.nih.nci.cabig.caaers.integration.schema.researchstaff");
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        Object importObject = unmarshaller.unmarshal(xmlFile);
        if (!validRootElement(importObject, RESEARCH_STAFF_IMPORT, command))
            return;

        gov.nih.nci.cabig.caaers.integration.schema.researchstaff.Staff staff = (gov.nih.nci.cabig.caaers.integration.schema.researchstaff.Staff) importObject;
        for (ResearchStaffType researchStaff : staff.getResearchStaff()) {
            DomainObjectImportOutcome<ResearchStaff> researchStaffOutcome = researchStaffMigratorService
                    .processResearchStaff(researchStaff);
            List<String> errors = domainObjectValidator
                    .validate(researchStaffOutcome.getImportedDomainObject());
            if (researchStaffOutcome.isSavable() && errors.size() == 0) {
                command.addImportableResearchStaff(researchStaffOutcome);
            } else {
                for (String errMsg : errors) {
                    researchStaffOutcome.addErrorMessage(errMsg, Severity.ERROR);
                }
                command.addNonImportableResearchStaff(researchStaffOutcome);
            }
        }
        //Remove Duplicate Investigators from the ImportableInvestigators List.
        List<DomainObjectImportOutcome<ResearchStaff>> dupList = new ArrayList<DomainObjectImportOutcome<ResearchStaff>>();
        for (int k = 0; k < command.getImportableResearchStaff().size() - 1; k++) {
            ResearchStaff rStaff1 = command.getImportableResearchStaff().get(k).getImportedDomainObject();
            for (int l = k + 1; l < command.getImportableResearchStaff().size(); l++) {
                ResearchStaff rStaff2 = command.getImportableResearchStaff().get(l).getImportedDomainObject();
                if (rStaff1.equals(rStaff2)) {
                    command.getImportableResearchStaff().get(l).addErrorMessage("Duplicate ResearchStaff",
                            Severity.ERROR);
                    command.addNonImportableResearchStaff(command.getImportableResearchStaff().get(l));
                    dupList.add(command.getImportableResearchStaff().get(l));
                    logger.debug("Duplicate Investigator :: " + rStaff2.getFullName());
                    break;
                }
            }
        }
        for (DomainObjectImportOutcome<ResearchStaff> obj : dupList) {
            command.getImportableResearchStaff().remove(obj);
        }
    } catch (JAXBException e) {
        throw new RuntimeException("JAXB Exception", e);
    }
}

From source file:gov.nih.nci.cabig.caaers.web.admin.StudyImporter.java

public void processEntities(File xmlFile, ImportCommand command) {
    boolean valid = validateAgainstSchema(xmlFile, command, getXSDLocation(STUDY_IMPORT));
    if (!valid) {
        return;/*from w ww .j a  v  a2s  .  c o  m*/
    }

    Studies studies;

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance("gov.nih.nci.cabig.caaers.integration.schema.study");
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        Object importObject = unmarshaller.unmarshal(xmlFile);
        if (!validRootElement(importObject, STUDY_IMPORT, command))
            return;

        studies = (Studies) importObject;
        if (studies != null) {
            for (gov.nih.nci.cabig.caaers.integration.schema.study.Study studyDto : studies.getStudy()) {
                DomainObjectImportOutcome<Study> studyImportOutcome = studyProcessorImpl.importStudy(studyDto);
                List<String> errors = domainObjectValidator
                        .validate(studyImportOutcome.getImportedDomainObject());
                if (studyImportOutcome.isSavable() && errors.size() == 0) {
                    command.addImportableStudy(studyImportOutcome);
                } else {
                    for (String errMsg : errors) {
                        studyImportOutcome.addErrorMessage(errMsg, Severity.ERROR);
                    }
                    command.addNonImportableStudy(studyImportOutcome);
                }
            }

            //Remove Duplicate Studies in the List.
            List<DomainObjectImportOutcome<Study>> dupList = new ArrayList<DomainObjectImportOutcome<Study>>();
            for (int i = 0; i < command.getImportableStudies().size() - 1; i++) {
                Study study1 = command.getImportableStudies().get(i).getImportedDomainObject();
                for (int j = i + 1; j < command.getImportableStudies().size(); j++) {
                    Study study2 = command.getImportableStudies().get(j).getImportedDomainObject();
                    if (study1.equals(study2)) {
                        command.getImportableStudies().get(j).addErrorMessage(
                                "Study Identifier already used in a different Study", Severity.ERROR);
                        command.addNonImportableStudy(command.getImportableStudies().get(j));
                        dupList.add(command.getImportableStudies().get(j));
                        logger.debug("Duplicate Study :: " + study2.getShortTitle());
                        break;
                    }
                }
            }
            for (DomainObjectImportOutcome<Study> obj : dupList) {
                command.getImportableStudies().remove(obj);
            }
        }
    } catch (JAXBException e) {
        throw new CaaersSystemException(
                "There was an error converting study data transfer object to study domain object", e);
    }
}

From source file:gov.nih.nci.cabig.caaers.web.admin.SubjectImporter.java

public void processEntities(File xmlFile, ImportCommand command) {
    boolean valid = validateAgainstSchema(xmlFile, command, getXSDLocation(SUBJECT_IMPORT));
    if (!valid) {
        return;//from   w w  w.  ja v  a 2  s.c o m
    }
    Participants participants;
    try {
        JAXBContext jaxbContext = JAXBContext
                .newInstance("gov.nih.nci.cabig.caaers.integration.schema.participant");
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        Object importObject = unmarshaller.unmarshal(xmlFile);
        if (!validRootElement(importObject, SUBJECT_IMPORT, command))
            return;

        participants = (Participants) importObject;
        if (participants != null) {
            for (ParticipantType participantDto : participants.getParticipant()) {
                DomainObjectImportOutcome<Participant> participantImportOutcome = participantServiceImpl
                        .processParticipant(participantDto);
                List<String> errors = domainObjectValidator
                        .validate(participantImportOutcome.getImportedDomainObject());
                if (participantImportOutcome.isSavable() && errors.size() == 0) {
                    command.addImportableParticipant(participantImportOutcome);
                } else {
                    for (String errMsg : errors) {
                        participantImportOutcome.addErrorMessage(errMsg, Severity.ERROR);
                    }
                    command.addNonImportableParticipant(participantImportOutcome);
                }
            }

            //Remove Duplicate Participants from the ImportableParticipants  List.
            List<DomainObjectImportOutcome<Participant>> dupList = new ArrayList<DomainObjectImportOutcome<Participant>>();
            for (int k = 0; k < command.getImportableParticipants().size() - 1; k++) {
                Participant par1 = command.getImportableParticipants().get(k).getImportedDomainObject();
                for (int l = k + 1; l < command.getImportableParticipants().size(); l++) {
                    Participant par2 = command.getImportableParticipants().get(l).getImportedDomainObject();
                    if (par1.equals(par2)) {
                        command.getImportableParticipants().get(l)
                                .addErrorMessage("Participant Identifier already used", Severity.ERROR);
                        command.addNonImportableParticipant(command.getImportableParticipants().get(l));
                        dupList.add(command.getImportableParticipants().get(l));
                        logger.debug("Duplicate Participant :: " + par2.getFullName());
                        break;
                    }
                }
            }
            for (DomainObjectImportOutcome<Participant> obj : dupList) {
                command.getImportableParticipants().remove(obj);
            }
        }
    } catch (JAXBException e) {
        throw new CaaersSystemException(
                "There was an error converting participant data transfer object to participant domain object",
                e);
    }
}

From source file:net.urlgrey.mythpodcaster.dao.TranscodingProfilesDAOImpl.java

private TranscodingProfileGroup loadTranscodingProfilesDocument() {
    final File encodingProfilesFile = new File(transcodingProfilesFilePath);
    TranscodingProfileGroup encodingProfiles = null;
    if (encodingProfilesFile.exists()) {
        try {/*  w  ww .  j a v a  2 s.co  m*/
            final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            encodingProfiles = (TranscodingProfileGroup) unmarshaller.unmarshal(encodingProfilesFile);
        } catch (JAXBException e) {
            LOGGER.error("Unable to unmarshal transcoding profiles document from XML", e);
        }

        if (encodingProfiles == null) {
            encodingProfiles = new TranscodingProfileGroup();
        }
    } else {
        encodingProfiles = new TranscodingProfileGroup();
    }
    return encodingProfiles;
}