All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.petals.se.xslt.utils.ServiceUnitURIResolver Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/**
 * Copyright (c) 2008-2012 EBM WebSourcing, 2012-2016 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.se.xslt.utils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

/**
 * @author Roland Naudin - EBM WebSourcing
 * @author Vincent Zurczak - EBM WebSourcing
 */
public class ServiceUnitURIResolver implements URIResolver {

	/**
	 * The SU's install root.
	 */
	private final String installRoot;

	/**
	 * The component's logger.
	 */
	private final Logger logger;

	/**
	 * The service-unit name (for the log).
	 */
	private final String suName;


	/**
	 * Constructor.
	 * @param installRoot the SU's install root
	 * @param logger the component's logger
	 * @param suName the service-unit name
	 */
	public ServiceUnitURIResolver( final String installRoot, final Logger logger, final String suName ) {
		this.installRoot = installRoot;
		this.logger = logger;
		this.suName = suName;
	}


	/*
	 * (non-Javadoc)
	 * @see javax.xml.transform.URIResolver
	 * #resolve(java.lang.String, java.lang.String)
	 */
	public final Source resolve(final String href, final String base) throws TransformerException {

		final Source ret;

		final URI uriToResolve = URI.create(href);
		final String scheme = uriToResolve.getScheme();
		if (scheme != null && scheme.length() > 0) {
			// URI with scheme part
			try {
				ret = new StreamSource(uriToResolve.toURL().openStream());
			} catch (final MalformedURLException e) {
				throw new TransformerException(e);
			} catch (final IOException e) {
				throw new TransformerException(e);
			}
		} else {
			// URI with not scheme part. We try to load from the filesystem
			final File fileToLoad;
			if (href.charAt(0) == '/') {
				// Absolute URI
				fileToLoad = new File(href);
			} else {
				// Relative URI. We try to load it from the SU installation directory
				fileToLoad = new File(this.installRoot, href);
			}

			if (fileToLoad.exists()) {
				ret = new StreamSource(fileToLoad);
			} else if( this.logger.isLoggable( Level.FINE )){
				this.logger.fine(this.suName + ": a file reference could not be resolved (" + href + ").");
				ret = null;
			} else {
				ret = null;
			}
		}

		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy