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
}
}
}
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>";
}
}
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());
}
}
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;
}
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;
}
Aggregations