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

org.drools.builder.help.KnowledgeBuilderHelper Maven / Gradle / Ivy

There is a newer version: 5.1.1
Show newest version
package org.drools.builder.help;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import org.drools.KnowledgeBase;
import org.drools.ProviderInitializationException;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;

import com.sun.tools.xjc.Language;
import com.sun.tools.xjc.Options;

/**
 * Provides helper methods for working with JAXB and a Knowledgebase, it takes care of the Classpath issues when
 * mapping against internal type declarations.
 *
 */
public class KnowledgeBuilderHelper {

    private static DroolsJaxbHelperProvider provider;

    /**
     * Generates pojos for a given XSD using XJC and adds them to the specified KnowlegeBase.
     * 
     * Options xjcOpts = new Options();
     * xjcOpts.setSchemaLanguage( Language.XMLSCHEMA );
     * KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
     *
     * String[] classNames = KnowledgeBuilderHelper.addXsdModel( ResourceFactory.newClassPathResource( "order.xsd",
     *                                                                                                 getClass() ),
     *                                                           kbuilder,
     *                                                           xjcOpts,
     *                                                           "xsd" );
     * 
* * @param resource * The resource to the XSD model * @param kbuilder * the KnowledgeBuilder where the generated .class files will be placed * @param xjcOpts * XJC Options * @param systemId * XJC systemId * @return * Returns an array of class names that where generated by the XSD. * @throws IOException */ public static String[] addXsdModel(Resource resource, KnowledgeBuilder kbuilder, Options xjcOpts, String systemId) throws IOException { return getDroolsJaxbHelperProvider().addXsdModel( resource, kbuilder, xjcOpts, systemId ); } /** * Creates a new JAXBContext, from which the Marshaller and Unmarshaller can be created, which are used by the Transformer * pipeline stage. * * @param classNames * An array of class names that can be resolved by this JAXBContext * @param properties * JAXB properties * @param kbase * The KnowledgeBase * @return * The JAXB Context * @throws JAXBException */ public static JAXBContext newJAXBContext(String[] classNames, KnowledgeBase kbase) throws JAXBException { return newJAXBContext( classNames, Collections. emptyMap(), kbase ); } public static JAXBContext newJAXBContext(String[] classNames, Map properties, KnowledgeBase kbase) throws JAXBException { return getDroolsJaxbHelperProvider().newJAXBContext( classNames, properties, kbase ); } public static synchronized DroolsJaxbHelperProvider getDroolsJaxbHelperProvider() { if ( provider == null ) { loadProvider(); } return provider; } private static synchronized void setDroolsJaxbHelperProvider(DroolsJaxbHelperProvider provider) { KnowledgeBuilderHelper.provider = provider; } private static void loadProvider() { try { Class cls = (Class) Class.forName( "org.drools.runtime.pipeline.impl.DroolsJaxbHelperProviderImpl" ); setDroolsJaxbHelperProvider( cls.newInstance() ); } catch ( Exception e2 ) { throw new ProviderInitializationException( "Provider org.drools.runtime.pipeline.impl.DroolsJaxbHelperProviderImpl could not be set.", e2 ); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy