Search in sources :

Example 1 with XMLContext

use of org.exolab.castor.xml.XMLContext in project hale by halestudio.

the class AlignmentBean method load.

/**
 * Load an AlignmentBean from an input stream. The stream is closed at the
 * end.
 *
 * @param in the input stream
 * @param reporter the I/O reporter to report any errors to, may be
 *            <code>null</code>
 * @return the AlignmentBean
 *
 * @throws MappingException if the mapping could not be loaded
 * @throws MarshalException if the alignment could not be read
 * @throws ValidationException if the input stream did not provide valid XML
 */
public static AlignmentBean load(InputStream in, IOReporter reporter) throws MappingException, MarshalException, ValidationException {
    Mapping mapping = new Mapping(AlignmentBean.class.getClassLoader());
    mapping.loadMapping(new InputSource(AlignmentBean.class.getResourceAsStream("AlignmentBean.xml")));
    XMLContext context = new XMLContext();
    context.addMapping(mapping);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    try {
        return (AlignmentBean) unmarshaller.unmarshal(new InputSource(in));
    } finally {
        try {
            in.close();
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) XMLContext(org.exolab.castor.xml.XMLContext) Mapping(org.exolab.castor.mapping.Mapping) IOException(java.io.IOException) Unmarshaller(org.exolab.castor.xml.Unmarshaller)

Example 2 with XMLContext

use of org.exolab.castor.xml.XMLContext in project OpenClinica by OpenClinica.

the class OpenRosaServices method getFormList.

/**
 * @api {get} /rest2/openrosa/:studyOID/formList Get Form List
 * @apiName getFormList
 * @apiPermission admin
 * @apiVersion 3.8.0
 * @apiParam {String} studyOID Study Oid.
 * @apiGroup Form
 * @apiDescription Retrieves a listing of the available OpenClinica forms.
 * @apiParamExample {json} Request-Example:
 *                  {
 *                  "studyOid": "S_SAMPLTE",
 *                  }
 * @apiSuccessExample {xml} Success-Response:
 *                    HTTP/1.1 200 OK
 *                    {
 *                    <xforms xmlns="http://openrosa.org/xforms/xformsList">
 *                    <xform>
 *                    <formID>F_FIRSTFORM_1</formID>
 *                    <name>First Form</name>
 *                    <majorMinorVersion>1</majorMinorVersion>
 *                    <version>1</version>
 *                    <hash>8678370cd92814d4e3216d58d821403f</hash>
 *                    <downloadUrl>http://oc1.openclinica.com/OpenClinica-web/rest2/openrosa/S_SAMPLTE/formXml?
 *                    formId=F_FIRSTFORM_1</downloadUrl>
 *                    </xform>
 *                    <xform>
 *                    <formID>F_SECONDFORM_1</formID>
 *                    <name>Second Form</name>
 *                    <majorMinorVersion>1</majorMinorVersion>
 *                    <version>1</version>
 *                    <hash>7ee60d1c6516b730bbe9bdbd7cad942f</hash>
 *                    <downloadUrl>http://oc1.openclinica.com/OpenClinica-web/rest2/openrosa/S_SAMPLTE/formXml?
 *                    formId=F_SECONDFORM_1</downloadUrl>
 *                    </xform>
 *                    </xforms>
 */
@GET
@Path("/{studyOID}/formList")
@Produces(MediaType.TEXT_XML)
public String getFormList(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("studyOID") String studyOID, @QueryParam("formID") String crfOID, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
    if (!mayProceedPreview(studyOID))
        return null;
    StudyDAO sdao = new StudyDAO(getDataSource());
    StudyBean study = sdao.findByOid(studyOID);
    CRFDAO cdao = new CRFDAO(getDataSource());
    Collection<CRFBean> crfs = cdao.findAll();
    CRFVersionDAO cVersionDao = new CRFVersionDAO(getDataSource());
    Collection<CRFVersionBean> crfVersions = cVersionDao.findAll();
    CrfVersionMediaDao mediaDao = (CrfVersionMediaDao) SpringServletAccess.getApplicationContext(context).getBean("crfVersionMediaDao");
    try {
        XFormList formList = new XFormList();
        for (CRFBean crf : crfs) {
            for (CRFVersionBean version : crfVersions) {
                if (version.getCrfId() == crf.getId()) {
                    XForm form = new XForm(crf, version);
                    // TODO: them.
                    if (version.getXformName() != null) {
                        form.setHash(DigestUtils.md5Hex(version.getXform()));
                    } else {
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(new Date());
                        form.setHash(DigestUtils.md5Hex(String.valueOf(cal.getTimeInMillis())));
                    }
                    String urlBase = getCoreResources().getDataInfo().getProperty("sysURL").split("/MainMenu")[0];
                    form.setDownloadURL(urlBase + "/rest2/openrosa/" + studyOID + "/formXml?formId=" + version.getOid());
                    List<CrfVersionMedia> mediaList = mediaDao.findByCrfVersionId(version.getId());
                    if (mediaList != null && mediaList.size() > 0) {
                        form.setManifestURL(urlBase + "/rest2/openrosa/" + studyOID + "/manifest?formId=" + version.getOid());
                    }
                    formList.add(form);
                }
            }
        }
        // Create the XML formList using a Castor mapping file.
        XMLContext xmlContext = new XMLContext();
        Mapping mapping = xmlContext.createMapping();
        mapping.loadMapping(getCoreResources().getURL("openRosaFormListMapping.xml"));
        xmlContext.addMapping(mapping);
        Marshaller marshaller = xmlContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.setWriter(writer);
        marshaller.marshal(formList);
        // Set response headers
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        Date currentDate = new Date();
        cal.setTime(currentDate);
        SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss zz");
        format.setCalendar(cal);
        response.setHeader("Content-Type", "text/xml; charset=UTF-8");
        response.setHeader("Date", format.format(currentDate));
        response.setHeader("X-OpenRosa-Version", "1.0");
        return writer.toString();
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        LOGGER.error(ExceptionUtils.getStackTrace(e));
        return "<Error>" + e.getMessage() + "</Error>";
    }
}
Also used : CRFDAO(org.akaza.openclinica.dao.admin.CRFDAO) XFormList(org.akaza.openclinica.web.pform.formlist.XFormList) Marshaller(org.exolab.castor.xml.Marshaller) CRFVersionDAO(org.akaza.openclinica.dao.submit.CRFVersionDAO) XForm(org.akaza.openclinica.web.pform.formlist.XForm) XMLContext(org.exolab.castor.xml.XMLContext) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) Calendar(java.util.Calendar) Mapping(org.exolab.castor.mapping.Mapping) CrfVersionMediaDao(org.akaza.openclinica.dao.hibernate.CrfVersionMediaDao) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) CRFBean(org.akaza.openclinica.bean.admin.CRFBean) CrfVersionMedia(org.akaza.openclinica.domain.datamap.CrfVersionMedia) StringWriter(java.io.StringWriter) CRFVersionBean(org.akaza.openclinica.bean.submit.CRFVersionBean) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) XPath(javax.xml.xpath.XPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with XMLContext

use of org.exolab.castor.xml.XMLContext in project OpenClinica by OpenClinica.

the class ImportRuleServlet method handleLoadCastor.

private RulesPostImportContainer handleLoadCastor(File xmlFile) {
    RulesPostImportContainer ruleImport = null;
    try {
        // create an XMLContext instance
        XMLContext xmlContext = new XMLContext();
        // create and set a Mapping instance
        Mapping mapping = xmlContext.createMapping();
        // mapping.loadMapping(SpringServletAccess.getPropertiesDir(context) + "mapping.xml");
        mapping.loadMapping(getCoreResources().getURL("mapping.xml"));
        xmlContext.addMapping(mapping);
        // create a new Unmarshaller
        Unmarshaller unmarshaller = xmlContext.createUnmarshaller();
        unmarshaller.setWhitespacePreserve(false);
        unmarshaller.setClass(RulesPostImportContainer.class);
        // Create a Reader to the file to unmarshal from
        FileReader reader = new FileReader(xmlFile);
        ruleImport = (RulesPostImportContainer) unmarshaller.unmarshal(reader);
        ruleImport.initializeRuleDef();
        logRuleImport(ruleImport);
        return ruleImport;
    } catch (FileNotFoundException ex) {
        throw new OpenClinicaSystemException(ex.getMessage(), ex.getCause());
    } catch (IOException ex) {
        throw new OpenClinicaSystemException(ex.getMessage(), ex.getCause());
    } catch (MarshalException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    } catch (ValidationException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    } catch (MappingException e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    }
}
Also used : RulesPostImportContainer(org.akaza.openclinica.domain.rule.RulesPostImportContainer) MarshalException(org.exolab.castor.xml.MarshalException) ValidationException(org.exolab.castor.xml.ValidationException) XMLContext(org.exolab.castor.xml.XMLContext) FileNotFoundException(java.io.FileNotFoundException) Mapping(org.exolab.castor.mapping.Mapping) FileReader(java.io.FileReader) IOException(java.io.IOException) Unmarshaller(org.exolab.castor.xml.Unmarshaller) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) MappingException(org.exolab.castor.mapping.MappingException)

Example 4 with XMLContext

use of org.exolab.castor.xml.XMLContext in project spring-framework by spring-projects.

the class CastorMarshaller method createXMLContext.

/**
	 * Create the Castor {@code XMLContext}. Subclasses can override this to create a custom context.
	 * <p>The default implementation loads mapping files if defined, or the target class or packages if defined.
	 * @return the created resolver
	 * @throws MappingException when the mapping file cannot be loaded
	 * @throws IOException in case of I/O errors
	 * @see XMLContext#addMapping(org.exolab.castor.mapping.Mapping)
	 * @see XMLContext#addClass(Class)
	 */
protected XMLContext createXMLContext(Resource[] mappingLocations, Class<?>[] targetClasses, String[] targetPackages) throws MappingException, ResolverException, IOException {
    XMLContext context = new XMLContext();
    if (!ObjectUtils.isEmpty(mappingLocations)) {
        Mapping mapping = new Mapping();
        for (Resource mappingLocation : mappingLocations) {
            mapping.loadMapping(SaxResourceUtils.createInputSource(mappingLocation));
        }
        context.addMapping(mapping);
    }
    if (!ObjectUtils.isEmpty(targetClasses)) {
        context.addClasses(targetClasses);
    }
    if (!ObjectUtils.isEmpty(targetPackages)) {
        context.addPackages(targetPackages);
    }
    if (this.castorProperties != null) {
        for (Map.Entry<String, String> property : this.castorProperties.entrySet()) {
            context.setProperty(property.getKey(), property.getValue());
        }
    }
    return context;
}
Also used : XMLContext(org.exolab.castor.xml.XMLContext) Resource(org.springframework.core.io.Resource) Mapping(org.exolab.castor.mapping.Mapping) Map(java.util.Map)

Example 5 with XMLContext

use of org.exolab.castor.xml.XMLContext in project camel by apache.

the class AbstractCastorDataFormat method createXMLContext.

protected XMLContext createXMLContext(ClassResolver resolver, ClassLoader contextClassLoader) throws Exception {
    XMLContext xmlContext = new XMLContext();
    if (ObjectHelper.isNotEmpty(getMappingFile())) {
        Mapping xmlMap;
        if (contextClassLoader != null) {
            xmlMap = new Mapping(contextClassLoader);
        } else {
            xmlMap = new Mapping();
        }
        xmlMap.loadMapping(resolver.loadResourceAsURL(getMappingFile()));
        xmlContext.addMapping(xmlMap);
    }
    if (getPackages() != null) {
        xmlContext.addPackages(getPackages());
    }
    if (getClassNames() != null) {
        for (String name : getClassNames()) {
            Class<?> clazz = resolver.resolveClass(name);
            xmlContext.addClass(clazz);
        }
    }
    return xmlContext;
}
Also used : XMLContext(org.exolab.castor.xml.XMLContext) Mapping(org.exolab.castor.mapping.Mapping)

Aggregations

Mapping (org.exolab.castor.mapping.Mapping)11 XMLContext (org.exolab.castor.xml.XMLContext)11 IOException (java.io.IOException)7 Marshaller (org.exolab.castor.xml.Marshaller)6 StringWriter (java.io.StringWriter)4 FileNotFoundException (java.io.FileNotFoundException)3 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 MappingException (org.exolab.castor.mapping.MappingException)3 MarshalException (org.exolab.castor.xml.MarshalException)3 Unmarshaller (org.exolab.castor.xml.Unmarshaller)3 ValidationException (org.exolab.castor.xml.ValidationException)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 XPath (javax.xml.xpath.XPath)2 CRFVersionBean (org.akaza.openclinica.bean.submit.CRFVersionBean)2