org.springframework.oxm.support.SaxResourceUtils Java Examples

The following examples show how to use org.springframework.oxm.support.SaxResourceUtils. 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: Jaxb2Marshaller.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")  // on JDK 9
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
	if (logger.isDebugEnabled()) {
		logger.debug("Setting validation schema to " +
				StringUtils.arrayToCommaDelimitedString(this.schemaResources));
	}
	Assert.notEmpty(resources, "No resources given");
	Assert.hasLength(schemaLanguage, "No schema language provided");
	Source[] schemaSources = new Source[resources.length];
	XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
	xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
	for (int i = 0; i < resources.length; i++) {
		Resource resource = resources[i];
		Assert.isTrue(resource != null && resource.exists(), () -> "Resource does not exist: " + resource);
		InputSource inputSource = SaxResourceUtils.createInputSource(resource);
		schemaSources[i] = new SAXSource(xmlReader, inputSource);
	}
	SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
	if (this.schemaResourceResolver != null) {
		schemaFactory.setResourceResolver(this.schemaResourceResolver);
	}
	return schemaFactory.newSchema(schemaSources);
}
 
Example #2
Source File: Jaxb2Marshaller.java    From java-technology-stack with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")  // on JDK 9
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
	if (logger.isDebugEnabled()) {
		logger.debug("Setting validation schema to " +
				StringUtils.arrayToCommaDelimitedString(this.schemaResources));
	}
	Assert.notEmpty(resources, "No resources given");
	Assert.hasLength(schemaLanguage, "No schema language provided");
	Source[] schemaSources = new Source[resources.length];
	XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
	xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
	for (int i = 0; i < resources.length; i++) {
		Resource resource = resources[i];
		Assert.isTrue(resource != null && resource.exists(), () -> "Resource does not exist: " + resource);
		InputSource inputSource = SaxResourceUtils.createInputSource(resource);
		schemaSources[i] = new SAXSource(xmlReader, inputSource);
	}
	SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
	if (this.schemaResourceResolver != null) {
		schemaFactory.setResourceResolver(this.schemaResourceResolver);
	}
	return schemaFactory.newSchema(schemaSources);
}
 
Example #3
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 #4
Source File: Jaxb2Marshaller.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
	if (logger.isDebugEnabled()) {
		logger.debug("Setting validation schema to " +
				StringUtils.arrayToCommaDelimitedString(this.schemaResources));
	}
	Assert.notEmpty(resources, "No resources given");
	Assert.hasLength(schemaLanguage, "No schema language provided");
	Source[] schemaSources = new Source[resources.length];
	XMLReader xmlReader = XMLReaderFactory.createXMLReader();
	xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
	for (int i = 0; i < resources.length; i++) {
		Assert.notNull(resources[i], "Resource is null");
		Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
		InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
		schemaSources[i] = new SAXSource(xmlReader, inputSource);
	}
	SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
	if (this.schemaResourceResolver != null) {
		schemaFactory.setResourceResolver(this.schemaResourceResolver);
	}
	return schemaFactory.newSchema(schemaSources);
}
 
Example #5
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;
}