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

org.sourceprojects.xmlparser.internal.resolver.OsgiBasicNamespaceResolver Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
/*
 * 
 *  This file is part of XmlParser.
 *
 *  Foobar is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see .
 *
 */
package org.sourceprojects.xmlparser.internal.resolver;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.osgi.framework.Bundle;
import org.sourceprojects.xmlparser.NamespaceResolver;
import org.sourceprojects.xmlparser.internal.dom.LSInputImpl;
import org.sourceprojects.xmlparser.internal.resolver.BasicNamespaceResolver.NamespaceResolverHandlerContainer;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;

/**
 * 
 * @author noctarius
 * @since 0.0.5
 */
public class OsgiBasicNamespaceResolver implements NamespaceResolver {
	private final NamespaceResolverHandlerContainer[] containers;
	private final Bundle bundle;

	public OsgiBasicNamespaceResolver(
			NamespaceResolverHandlerContainer[] containers, Bundle bundle) {
		this.containers = containers;
		this.bundle = bundle;
	}

	@Override
	public LSInput getInput(String type, String namespaceURI, String publicId,
			String systemId, String baseURI) {

		for (NamespaceResolverHandlerContainer container : containers) {
			if (container.getNamespaceURI().equals(namespaceURI)
					|| container.getNamespaceURI().equals(systemId)) {
				InputStream is = getByteStream(container.getRedirectedURI());

				LSInput input = new LSInputImpl();
				input.setBaseURI(baseURI);
				input.setPublicId(publicId);
				input.setSystemId(systemId);
				input.setByteStream(is);

				return input;
			}
		}

		try {
			DOMImplementationRegistry registry = DOMImplementationRegistry
					.newInstance();
			DOMImplementationLS impl = (DOMImplementationLS) registry
					.getDOMImplementation("XML").getFeature("LS", "");

			return impl.createLSInput();
		} catch (ClassCastException e) {
			return new LSInputImpl();
		} catch (ClassNotFoundException e) {
			return new LSInputImpl();
		} catch (InstantiationException e) {
			return new LSInputImpl();
		} catch (IllegalAccessException e) {
			return new LSInputImpl();
		}
	}

	@Override
	public String getNamespaceURI() {
		return null;
	}

	private InputStream getByteStream(String uri) {
		URL url = bundle.getResource(uri);
		try {
			return url.openStream();
		} catch (IOException e) {
			return null;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy