Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.drools.builder.help;
 
 import java.util.Map;
 
 
 
 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.

This api is experimental and thus the classes and the interfaces returned are subject to change.

 
 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" );
 

Parameters:
resource The resource to the XSD model
kbuilder the KnowledgeBuilder where the generated .class files will be placed
xjcOpts XJC Options
systemId XJC systemId
Returns:
Returns an array of class names that where generated by the XSD.
Throws:
java.io.IOException
 
     public static String[] addXsdModel(Resource resource,
                                        KnowledgeBuilder kbuilder,
                                        Options xjcOpts,
                                        String systemIdthrows IOException {
         return getDroolsJaxbHelperProvider().addXsdModelresource,
                                                           kbuilder,
                                                           xjcOpts,
                                                           systemId );
     }

    
Creates a new JAXBContext, from which the Marshaller and Unmarshaller can be created, which are used by the Transformer pipeline stage.

Parameters:
classNames An array of class names that can be resolved by this JAXBContext
properties JAXB properties
kbase The KnowledgeBase
Returns:
The JAXB Context
Throws:
javax.xml.bind.JAXBException
 
     public static JAXBContext newJAXBContext(String[] classNames,
                                              KnowledgeBase kbasethrows JAXBException {
         return newJAXBContextclassNames,
                                Collections.<StringObjectemptyMap(),
                               kbase );
    }
    public static JAXBContext newJAXBContext(String[] classNames,
                                             Map<String, ? > properties,
                                             KnowledgeBase kbasethrows JAXBException {
        return getDroolsJaxbHelperProvider().newJAXBContextclassNames,
                                                             properties,
                                                             kbase );
    }
    public static synchronized DroolsJaxbHelperProvider getDroolsJaxbHelperProvider() {
        if (  == null ) {
            loadProvider();
        }
        return ;
    }
    private static synchronized void setDroolsJaxbHelperProvider(DroolsJaxbHelperProvider provider) {
        . = provider;
    }
    private static void loadProvider() {
        try {
            Class<DroolsJaxbHelperProvidercls = (Class<DroolsJaxbHelperProvider>) Class.forName"org.drools.core.runtime.pipeline.impl.DroolsJaxbHelperProviderImpl" );
            setDroolsJaxbHelperProvidercls.newInstance() );
        } catch ( Exception e2 ) {
            throw new RuntimeException"Provider org.drools.core.runtime.pipeline.impl.DroolsJaxbHelperProviderImpl could not be set.",
                                                       e2 );
        }
    }
New to GrepCode? Check out our FAQ X