package net.aeten.core.spi;

import java.io.InputStreamReader;
import net.aeten.core.Format;
import net.aeten.core.Predicate;
import net.aeten.core.parsing.Document;
import net.aeten.core.parsing.MarkupNode;
import net.aeten.core.parsing.Parser;
import net.aeten.core.parsing.ParsingException;

/**
 *
 * @author Thomas Pérennou
 */
public class SpiConfiguration {
	public final Document.Element root;

	public SpiConfiguration(Document.Element configuration) {
		this.root = configuration;
	}

	
	@SuppressWarnings("unchecked")
	public SpiConfiguration(String packageName, String resourceName, String parserIdentifier, Class<?> instanceClass) {
		String resource = packageName.replace('.', '/') + "/" + resourceName;
		Parser<MarkupNode> parser;
		if (parserIdentifier == null || parserIdentifier.isEmpty()) {
			int extensionIndex = resourceName.lastIndexOf('.');
			if ((extensionIndex == -1) || (extensionIndex == (resourceName.length() + 1))) {
				throw new IllegalArgumentException("Unable to find parser without extension in resource file " + resourceName);
			}
			final String extension = resourceName.substring(extensionIndex+1);
			@SuppressWarnings("rawtypes")
			Predicate<Parser> parserPredicate = new Predicate<Parser>() {
				@Override
				public boolean evaluate(Parser element) {
					Format format = element.getClass().getAnnotation(Format.class);
					return (format != null) && format.value().equals(extension);
				}
			};
			parser = (Parser<MarkupNode>) Service.getProvider(Parser.class, parserPredicate);
		} else {
			parser = (Parser<MarkupNode>) Service.getProvider(Parser.class, parserIdentifier);
		}
		try {
			this.root = Document.loadElements(new InputStreamReader(instanceClass.getClassLoader().getResourceAsStream(resource)), parser).root;
		} catch (ParsingException ex) {
			throw new IllegalArgumentException(ex);
		}
	}

}

@@ -52,24 +52,25 @@
 reamReader;%0A
+%0A
 import net.a
@@ -489,16 +489,214 @@
 on;%0A%09%7D%0A%0A
+%09%0A%09public SpiConfiguration(Package package_, String resourceName, String parserIdentifier, Class%3C?%3E instanceClass) %7B%0A%09%09this (package_.getName (), resourceName, parserIdentifier, instanceClass); %0A%09%7D%0A
 %09%0A%09@Supp

Add constructor with Package