org.exolab.castor.xml.XMLContext Java Examples

The following examples show how to use org.exolab.castor.xml.XMLContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CastorMarshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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(@Nullable Resource[] mappingLocations,
		@Nullable Class<?>[] targetClasses, @Nullable 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) {
		this.castorProperties.forEach(context::setProperty);
	}
	return context;
}
 
Example #2
Source File: CastorMarshaller.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: Castor.java    From marshalsec with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see marshalsec.MarshallerBase#marshal(java.lang.Object)
 */
@Override
public String marshal ( Object o ) throws Exception {
    XMLContext context = new XMLContext();
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    m.setWriter(sw);
    return sw.toString();
}
 
Example #4
Source File: Castor.java    From marshalsec with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see marshalsec.MarshallerBase#unmarshal(java.lang.Object)
 */
@Override
public Object unmarshal ( String data ) throws Exception {
    XMLContext context = new XMLContext();
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return unmarshaller.unmarshal(new StringReader(data));
}
 
Example #5
Source File: CastorUtil.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
private XMLContext getWriteXmlContext()
{
	String targetVersion = JRPropertiesUtil.getInstance(jasperReportsContext).getProperty(
			JRXmlBaseWriter.PROPERTY_REPORT_VERSION);
	if (log.isDebugEnabled())
	{
		log.debug("using write mappings for version " + targetVersion);
	}
	
	return getXmlContext(CASTOR_WRITE_XML_CONTEXT_KEY, targetVersion);
}
 
Example #6
Source File: CastorUtil.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected Map<Object, XMLContext> getXmlContextCache(String contextCacheKey)
{
	Map<Object, XMLContext> xmlContextCache = 
			(Map<Object, XMLContext>) jasperReportsContext.getOwnValue(contextCacheKey);
	if (xmlContextCache == null)
	{
		//TODO lucianc prevent double cache creation?
		xmlContextCache = Collections.synchronizedMap(
				new ReferenceMap<Object, XMLContext>(ReferenceMap.ReferenceStrength.WEAK, ReferenceMap.ReferenceStrength.SOFT));//using soft values is safer
		jasperReportsContext.setValue(contextCacheKey, xmlContextCache);
	}
	return xmlContextCache;
}
 
Example #7
Source File: CastorUtil.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
private XMLContext getReadXmlContext()
{
	return getXmlContext(CASTOR_READ_XML_CONTEXT_KEY, null);//always reading with the last version mappings
}