/*
* Copyright 2004-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.webflow.engine.model.builder.xml;
import java.io.IOException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* EntityResolver implementation for the Spring Web Flow XML Schema. This will load the XSD from the classpath.
* <p>
* The xmlns of the XSD expected to be resolved:
*
* <pre>
* <?xml version="1.0" encoding="UTF-8"?>
* <flow xmlns="http://www.springframework.org/schema/webflow"
* xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
* xsi:schemaLocation="http://www.springframework.org/schema/webflow
* http://www.springframework.org/schema/webflow/spring-webflow.xsd">
* </pre>
*
* @author Erwin Vervaet
* @author Ben Hale
*/
class WebFlowEntityResolver implements EntityResolver {
private static final String[] WEBFLOW_VERSIONS = new String[] { "spring-webflow-2.4", "spring-webflow-2.0" };
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (systemId != null && systemId.indexOf("spring-webflow.xsd") > -1) {
return createInputSource(publicId, systemId, WEBFLOW_VERSIONS[0] + ".xsd");
}
for (String element : WEBFLOW_VERSIONS) {
if (systemId != null && systemId.indexOf(element) > systemId.lastIndexOf("/")) {
String fileName = systemId.substring(systemId.indexOf(element));
return createInputSource(publicId, systemId, fileName);
}
}
// let the parser handle it
return null;
}
private InputSource createInputSource(String publicId, String systemId, String fileName) {
try {
Resource resource = new ClassPathResource(fileName, getClass());
InputSource source = new InputSource(resource.getInputStream());
source.setPublicId(publicId);
source.setSystemId(systemId);
return source;
} catch (IOException ex) {
// fall through below
}
return null;
}
}