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

org.fiirs.drools.knowledge.packager.KnowledgeEntries Maven / Gradle / Ivy

Go to download

This is a Maven plugin that will JBoss Drools 5.2 knowledge files and compile them to a Drools PKG file

There is a newer version: 0.0.6
Show newest version
package org.fiirs.drools.knowledge.packager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KnowledgeEntries {
	private Map> entries;
	private List errors;

	public KnowledgeEntries(KnowledgeEntryBuilder builder) throws IOException {
		entries = new HashMap>();
		errors = new ArrayList();
		buildEntries(builder);
	}

	public Map> getEntries() {
		return entries;
	}

	public List getErrors() {
		return errors;
	}

	private void buildEntries(KnowledgeEntryBuilder builder) throws IOException {
		for (String s : builder.getJarFiles()) {
			JarFile jarFile = null;
			try {
				jarFile = new JarFile(s);
				scanJarFile(builder, jarFile);
			} finally {
				if (jarFile != null) {
					jarFile.close();
				}
			}
		}
	}

	private void scanJarFile(KnowledgeEntryBuilder builder, JarFile jarFile) {
		Enumeration jarFileEntries = jarFile.entries();
		while (jarFileEntries.hasMoreElements()) {
			JarEntry jarEntry = jarFileEntries.nextElement();
			for (String patternString : builder.getPatternStrings()) {
				if (Pattern.matches(convertToRegEx(patternString),
						jarEntry.getName())) {
					addEntry(jarFile.getName(), jarEntry.getName());
				}
			}

		}
	}

	private void addEntry(String jarName, String entryName) {
		List entryList = entries.get(jarName);
		if (null == entryList) {
			entryList = new ArrayList();
			entries.put(jarName, entryList);
		}
		entryList.add(entryName);
	}

	private String convertToRegEx(String s) {
		Pattern p = Pattern.compile(KnowledgeEntryBuilder.WILD_CARD_PATTERN);
		Matcher m = p.matcher(s);

		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			if ("*".equals(m.group())) {
				m.appendReplacement(sb, "[^/]*");
			}
			if ("**".equals(m.group())) {
				m.appendReplacement(sb, ".*");
			}
		}
		m.appendTail(sb);
		return sb.toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy