Search in sources :

Example 6 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 7 with XMLContext

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

the class DownloadRuleSetXmlServlet method handleLoadCastor.

private FileWriter handleLoadCastor(FileWriter writer, RulesPostImportContainer rpic) {
    try {
        // Create Mapping
        Mapping mapping = new Mapping();
        mapping.loadMapping(getCoreResources().getURL("mappingMarshaller.xml"));
        // Create XMLContext
        XMLContext xmlContext = new XMLContext();
        xmlContext.addMapping(mapping);
        Marshaller marshaller = xmlContext.createMarshaller();
        marshaller.setWriter(writer);
        marshaller.marshal(rpic);
        return writer;
    } 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());
    } catch (Exception e) {
        throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
    }
}
Also used : Marshaller(org.exolab.castor.xml.Marshaller) 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) IOException(java.io.IOException) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) MappingException(org.exolab.castor.mapping.MappingException) InsufficientPermissionException(org.akaza.openclinica.web.InsufficientPermissionException) ValidationException(org.exolab.castor.xml.ValidationException) MarshalException(org.exolab.castor.xml.MarshalException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) MappingException(org.exolab.castor.mapping.MappingException)

Example 8 with XMLContext

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

the class OpenRosaServices method getManifest.

/**
     * @api {get} /rest2/openrosa/:studyOID/manifest Get Form Manifest
     * @apiName getManifest
     * @apiPermission admin
     * @apiVersion 3.8.0
     * @apiParam {String} studyOID Study Oid.
     * @apiGroup Form
     * @apiDescription Gets additional information on a particular Form, including links to associated media.
     */
@GET
@Path("/{studyOID}/manifest")
@Produces(MediaType.TEXT_XML)
public String getManifest(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("studyOID") String studyOID, @QueryParam("formId") String uniqueId, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
    if (!mayProceedPreview(studyOID))
        return null;
    String formLayoutOid = getFormLayoutOid(uniqueId);
    FormLayout formLayout = formLayoutDao.findByOcOID(formLayoutOid);
    Manifest manifest = new Manifest();
    List<FormLayoutMedia> mediaList = formLayoutMediaDao.findByFormLayoutIdForNoteTypeMedia(formLayout.getFormLayoutId());
    String urlBase = getCoreResources().getDataInfo().getProperty("sysURL").split("/MainMenu")[0];
    if (mediaList != null && mediaList.size() > 0) {
        for (FormLayoutMedia media : mediaList) {
            MediaFile mediaFile = new MediaFile();
            mediaFile.setFilename(media.getName());
            File image = new File(Utils.getCrfMediaSysPath() + media.getPath() + media.getName());
            mediaFile.setHash(DigestUtils.md5Hex(media.getName()) + Double.toString(image.length()));
            mediaFile.setDownloadUrl(urlBase + "/rest2/openrosa/" + studyOID + "/downloadMedia?formLayoutMediaId=" + media.getFormLayoutMediaId());
            manifest.add(mediaFile);
        }
    }
    // Add user list
    MediaFile userList = new MediaFile();
    LinkedHashMap<String, Object> subjectContextCache = (LinkedHashMap<String, Object>) context.getAttribute("subjectContextCache");
    if (subjectContextCache != null) {
        String userXml = getUserXml(context);
        userList.setHash((DigestUtils.md5Hex(userXml)));
    }
    userList.setFilename("users.xml");
    userList.setDownloadUrl(urlBase + "/rest2/openrosa/" + studyOID + "/downloadUsers");
    manifest.add(userList);
    try {
        // Create the XML manifest using a Castor mapping file.
        XMLContext xmlContext = new XMLContext();
        Mapping mapping = xmlContext.createMapping();
        mapping.loadMapping(getCoreResources().getURL("openRosaManifestMapping.xml"));
        xmlContext.addMapping(mapping);
        Marshaller marshaller = xmlContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.setWriter(writer);
        marshaller.marshal(manifest);
        // 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 : FormLayout(org.akaza.openclinica.domain.datamap.FormLayout) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) Marshaller(org.exolab.castor.xml.Marshaller) XMLContext(org.exolab.castor.xml.XMLContext) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) Calendar(java.util.Calendar) Mapping(org.exolab.castor.mapping.Mapping) Manifest(org.akaza.openclinica.web.pform.manifest.Manifest) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) StringWriter(java.io.StringWriter) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 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)9 XMLContext (org.exolab.castor.xml.XMLContext)9 IOException (java.io.IOException)5 Marshaller (org.exolab.castor.xml.Marshaller)5 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 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 FormLayout (org.akaza.openclinica.domain.datamap.FormLayout)2 FormLayoutMedia (org.akaza.openclinica.domain.datamap.FormLayoutMedia)2 Unmarshaller (org.exolab.castor.xml.Unmarshaller)2