[jboss-svn-commits] JBL Code SVN: r24491 - in labs/jbossrules/trunk/drools-pipeline: drools-transformer-jaxb and 21 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Dec 23 14:34:03 EST 2008


Author: mark.proctor at jboss.com
Date: 2008-12-23 14:34:03 -0500 (Tue, 23 Dec 2008)
New Revision: 24491

Added:
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/.classpath
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/.project
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/pom.xml
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/dataloaders/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/DroolsJaxbHelperProviderImpl.java
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/JaxbTransformer.java
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/resources/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/dataloaders/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/runtime/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/runtime/pipeline/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/runtime/pipeline/impl/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/runtime/pipeline/impl/DroolsJaxbTest.java
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/dataloaders/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/data.xml
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/order.xml
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/order.xsd
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/test.xsd
   labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/test_Jaxb.drl
Modified:
   labs/jbossrules/trunk/drools-pipeline/pom.xml
Log:
JBRULES-1903 Pipeline support for DataLoaders and Feeders\n-renamed folders

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/.classpath
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/.classpath	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/.classpath	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,37 @@
+<classpath>
+  <classpathentry kind="src" path="src/main/java"/>
+  <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
+  <classpathentry kind="src" path="src/test/java" output="target/test-classes"/>
+  <classpathentry kind="src" path="src/test/resources" output="target/test-classes" including="**" excluding="**/*.java"/>
+  <classpathentry kind="output" path="target/classes"/>
+  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+  <classpathentry kind="var" path="M2_REPO/javax/activation/activation/1.1/activation-1.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/antlr/antlr-runtime/3.1.1/antlr-runtime-3.1.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.3/commons-lang-2.3.jar"/>
+  <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1/commons-logging-1.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/eclipse/jdt/core/3.4.2.v_883_R34x/core-3.4.2.v_883_R34x.jar"/>
+  <classpathentry kind="src" path="/drools-api"/>
+  <classpathentry kind="src" path="/drools-compiler"/>
+  <classpathentry kind="src" path="/drools-core"/>
+  <classpathentry kind="var" path="M2_REPO/org/freemarker/freemarker/2.3.9/freemarker-2.3.9.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-library/1.1/hamcrest-library-1.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/janino/janino/2.5.15/janino-2.5.15.jar"/>
+  <classpathentry kind="var" path="M2_REPO/javax/xml/bind/jaxb-api/2.0/jaxb-api-2.0.jar"/>
+  <classpathentry kind="var" path="M2_REPO/com/sun/xml/bind/jaxb-impl/2.0.3/jaxb-impl-2.0.3.jar"/>
+  <classpathentry kind="var" path="M2_REPO/com/sun/xml/bind/jaxb-xjc/2.0.3/jaxb-xjc-2.0.3.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/jmock/jmock/2.5.0.1/jmock-2.5.0.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/joda-time/joda-time/1.5.2/joda-time-1.5.2.jar"/>
+  <classpathentry kind="var" path="M2_REPO/javax/xml/bind/jsr173_api/1.0/jsr173_api-1.0.jar"/>
+  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.13/log4j-1.2.13.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/milyn/milyn-commons/1.1/milyn-commons-1.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/milyn/milyn-smooks-core/1.1/milyn-smooks-core-1.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/milyn/milyn-smooks-javabean/1.1/milyn-smooks-javabean-1.1.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/mvel/mvel/1.3.12-java1.5/mvel-1.3.12-java1.5.jar"/>
+  <classpathentry kind="var" path="M2_REPO/org/mvel/mvel2/2.0.4-SNAPSHOT/mvel2-2.0.4-SNAPSHOT.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.8.0/xercesImpl-2.8.0.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xml-apis/xml-apis/2.0.2/xml-apis-2.0.2.jar"/>
+  <classpathentry kind="var" path="M2_REPO/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar"/>
+  <classpathentry kind="var" path="M2_REPO/com/thoughtworks/xstream/xstream/1.3/xstream-1.3.jar"/>
+</classpath>
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/.project
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/.project	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/.project	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,17 @@
+<projectDescription>
+  <name>drools-transformer-jaxb</name>
+  <comment>A rule production system</comment>
+  <projects>
+    <project>drools-api</project>
+    <project>drools-compiler</project>
+    <project>drools-core</project>
+  </projects>
+  <buildSpec>
+    <buildCommand>
+      <name>org.eclipse.jdt.core.javabuilder</name>
+    </buildCommand>
+  </buildSpec>
+  <natures>
+    <nature>org.eclipse.jdt.core.javanature</nature>
+  </natures>
+</projectDescription>
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/pom.xml
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/pom.xml	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/pom.xml	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <artifactId>drools</artifactId>
+    <groupId>org.drools</groupId>
+    <version>5.0.0.SNAPSHOT</version>
+  </parent>
+
+  <artifactId>drools-transformer-jaxb</artifactId>
+  <packaging>jar</packaging>
+  <name>Drools :: Transformer :: JAXB</name>
+  
+      <dependencies>
+         <dependency>
+            <groupId>org.drools</groupId>
+            <artifactId>drools-core</artifactId>
+            <version>${project.version}</version>  
+         </dependency>                     
+
+         <dependency>
+            <groupId>org.drools</groupId>
+            <artifactId>drools-compiler</artifactId>
+            <version>${project.version}</version>  
+         </dependency>   
+
+        <dependency>
+          <groupId>com.sun.xml.bind</groupId>
+          <artifactId>jaxb-impl</artifactId>
+          <version>2.0.3</version>
+        </dependency>  
+        
+        <dependency>
+          <groupId>com.sun.xml.bind</groupId>
+          <artifactId>jaxb-xjc</artifactId>
+          <version>2.0.3</version>
+        </dependency>                
+      </dependencies>     
+
+    <repositories>
+        <repository>
+            <id>dist.codehaus.org</id>
+            <url>http://dist.codehaus.org</url>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+            <releases>
+               <enabled>true</enabled>
+            </releases>
+        </repository>
+    </repositories>    
+</project>

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/DroolsJaxbHelperProviderImpl.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/DroolsJaxbHelperProviderImpl.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/DroolsJaxbHelperProviderImpl.java	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,304 @@
+package org.drools.runtime.pipeline.impl;
+
+/*
+ * Copyright 2005 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.
+ */
+
+import java.io.ByteArrayOutputStream;
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+import org.drools.KnowledgeBase;
+import org.drools.RuleBase;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.help.DroolsJaxbHelperProvider;
+import org.drools.builder.impl.KnowledgeBuilderImpl;
+import org.drools.common.InternalRuleBase;
+import org.drools.compiler.PackageBuilder;
+import org.drools.compiler.PackageRegistry;
+import org.drools.impl.KnowledgeBaseImpl;
+import org.drools.io.Resource;
+import org.drools.lang.descr.PackageDescr;
+import org.drools.rule.builder.dialect.java.JavaDialect;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXParseException;
+
+import com.sun.codemodel.CodeWriter;
+import com.sun.codemodel.JCodeModel;
+import com.sun.codemodel.JPackage;
+import com.sun.tools.xjc.BadCommandLineException;
+import com.sun.tools.xjc.ErrorReceiver;
+import com.sun.tools.xjc.ModelLoader;
+import com.sun.tools.xjc.Options;
+import com.sun.tools.xjc.model.Model;
+import com.sun.tools.xjc.outline.Outline;
+
+public class DroolsJaxbHelperProviderImpl
+    implements
+    DroolsJaxbHelperProvider {
+    public String[] addXsdModel(Resource resource,
+                             KnowledgeBuilder kbuilder,
+                             Options xjcOpts,
+                             String systemId) throws IOException {
+        PackageBuilder pkgBuilder = ((KnowledgeBuilderImpl) kbuilder).pkgBuilder;
+
+        InputSource source = new InputSource( new CachingRewindableReader( resource.getReader() ) );
+        source.setSystemId( systemId.trim().startsWith( "." ) ? systemId : "." + systemId );
+
+        xjcOpts.addGrammar( source );
+
+        try {
+            xjcOpts.parseArguments( new String[]{"-npa"} );
+        } catch ( BadCommandLineException e ) {
+            throw new IllegalArgumentException( "Unable to parse arguments",
+                                                e );
+        }
+
+        ErrorReceiver errorReceiver = new JaxbErrorReceiver4Drools();
+
+        Model model = ModelLoader.load( xjcOpts,
+                                        new JCodeModel(),
+                                        errorReceiver );
+
+        final Outline outline = model.generateCode( xjcOpts,
+                                                    errorReceiver );
+
+        MapVfsCodeWriter codeWriter = new MapVfsCodeWriter();
+        model.codeModel.build( xjcOpts.createCodeWriter( codeWriter ) );
+
+        Set<JavaDialect> dialects = new HashSet<JavaDialect>();
+        List<String> classNames = new ArrayList<String>();
+        for ( Entry<String, byte[]> entry : codeWriter.getMap().entrySet() ) {
+            String name = entry.getKey();
+
+            String pkgName = null;
+            int dotPos = name.lastIndexOf( '.' );
+            pkgName = name.substring( 0,
+                                      dotPos );
+
+            if ( !name.endsWith( "package-info.java" ) ) {
+                classNames.add( pkgName );
+            }
+
+            dotPos = pkgName.lastIndexOf( '.' );
+            if ( dotPos != -1 ) {
+                pkgName = pkgName.substring( 0,
+                                             dotPos );
+            }
+
+            PackageRegistry pkgReg = pkgBuilder.getPackageRegistry( pkgName );
+            if ( pkgReg == null ) {
+                pkgBuilder.addPackage( new PackageDescr( pkgName ) );
+                pkgReg = pkgBuilder.getPackageRegistry( pkgName );
+            }
+
+            JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect( "java" );
+            dialects.add( dialect );
+            dialect.addSrc( convertToResource( entry.getKey() ),
+                            entry.getValue() );
+        }
+
+        pkgBuilder.compileAll();
+        pkgBuilder.updateResults();
+
+        return (String[]) classNames.toArray( new String[classNames.size()] );
+    }
+
+    public JAXBContext newJAXBContext(String[] classNames,
+                                          KnowledgeBase kbase) throws JAXBException {
+        return newJAXBContext( classNames,
+                            Collections.<String, Object> emptyMap(),
+                            kbase );
+    }
+
+    public JAXBContext newJAXBContext(String[] classNames,
+                                      Map<String, ? > properties,
+                                      KnowledgeBase kbase) throws JAXBException {
+        ClassLoader classLoader = ((InternalRuleBase) ((KnowledgeBaseImpl) kbase).getRuleBase()).getRootClassLoader();
+
+        Class[] classes = new Class[classNames.length];
+        int i = 0;
+        try {
+            for ( i = 0; i < classNames.length; i++ ) {
+                classes[i] = classLoader.loadClass( classNames[i] );
+            }
+        } catch ( ClassNotFoundException e ) {
+            throw new JAXBException( "Unable to resolve class '" + classNames[i] + "'",
+                                     e );
+        }
+
+        return JAXBContext.newInstance( classes,
+                                        properties );
+    }
+
+    private static String convertToResource(String string) {
+        int lastDot = string.lastIndexOf( '.' );
+        return string.substring( 0,
+                                 lastDot ).replace( '.',
+                                                    '/' ) + string.substring( lastDot,
+                                                                              string.length() );
+    }
+
+    public static class MapVfsCodeWriter extends CodeWriter {
+
+        private final Map<String, byte[]> map;
+
+        private ByteArrayOutputStream     currentBaos;
+        private String                    currentPath;
+
+        public MapVfsCodeWriter() {
+            this.map = new LinkedHashMap<String, byte[]>();
+        }
+
+        public OutputStream openBinary(JPackage pkg,
+                                       String fileName) throws IOException {
+            String pkgName = pkg.name();
+
+            if ( pkgName.length() != 0 ) {
+                pkgName += '.';
+            }
+
+            if ( this.currentBaos != null ) {
+                this.currentBaos.close();
+                this.map.put( this.currentPath,
+                              this.currentBaos.toByteArray() );
+            }
+
+            this.currentPath = pkgName + fileName;
+            this.currentBaos = new ByteArrayOutputStream();
+
+            return new FilterOutputStream( this.currentBaos ) {
+                public void close() {
+                    // don't let this stream close
+                }
+            };
+        }
+
+        public void close() throws IOException {
+            if ( this.currentBaos != null ) {
+                this.currentBaos.close();
+                this.map.put( this.currentPath,
+                              this.currentBaos.toByteArray() );
+            }
+        }
+
+        public Map<String, byte[]> getMap() {
+            return this.map;
+        }
+
+    }
+
+    public static class JaxbErrorReceiver4Drools extends ErrorReceiver {
+
+        public String stage = "processing";
+
+        public void warning(SAXParseException e) {
+            e.printStackTrace();
+        }
+
+        public void error(SAXParseException e) {
+            e.printStackTrace();
+        }
+
+        public void fatalError(SAXParseException e) {
+            e.printStackTrace();
+        }
+
+        public void info(SAXParseException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static class CachingRewindableReader extends Reader {
+        private Reader                 source;
+        private boolean                sourceClosed;
+        private RewindableStringReader cache;
+        private StringBuilder          strBuilder;
+
+        public CachingRewindableReader(Reader source) {
+            this.source = source;
+            this.strBuilder = new StringBuilder();
+        }
+
+        public int read(char[] cbuf,
+                        int off,
+                        int len) throws IOException {
+            int value = 0;
+            if ( this.cache == null ) {
+                value = this.source.read( cbuf,
+                                          off,
+                                          len );
+                if ( value != -1 ) {
+                    // keep appening to the stringBuilder until we are at the end
+                    this.strBuilder.append( cbuf,
+                                            off,
+                                            value );
+                } else {
+                    // we are at the end, so switch to cache
+                    this.cache = new RewindableStringReader( strBuilder.toString() );
+                }
+            } else {
+                value = this.cache.read( cbuf,
+                                         off,
+                                         len );
+            }
+            return value;
+        }
+
+        public void close() throws IOException {
+            if ( !sourceClosed ) {
+                // close the source, we only do this once.
+                this.source.close();
+                this.sourceClosed = true;
+            }
+
+            if ( cache == null ) {
+                // switch to cache if we haven't already
+                this.cache = new RewindableStringReader( strBuilder.toString() );
+            } else {
+                // reset the cache, so it can be read again.
+                this.cache.reset();
+            }
+        }
+    }
+
+    public static class RewindableStringReader extends StringReader {
+        public RewindableStringReader(String s) {
+            super( s );
+        }
+
+        public void close() {
+            try {
+                reset();
+            } catch ( IOException e ) {
+                e.printStackTrace();
+            }
+        }
+    }
+}

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/JaxbTransformer.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/JaxbTransformer.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/main/java/org/drools/runtime/pipeline/impl/JaxbTransformer.java	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,61 @@
+package org.drools.runtime.pipeline.impl;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.Reader;
+
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.Source;
+
+import org.drools.definition.pipeline.JaxbPipelineProvider;
+import org.drools.definition.pipeline.Transformer;
+import org.drools.runtime.pipeline.PipelineContext;
+import org.drools.runtime.pipeline.impl.BaseEmitter;
+import org.drools.runtime.pipeline.impl.BaseStage;
+import org.xml.sax.InputSource;
+
+public class JaxbTransformer extends BaseEmitter
+    implements
+    Transformer {
+    private Unmarshaller            unmarshaller;
+
+    public JaxbTransformer(Unmarshaller unmarshaller) {
+        this.unmarshaller = unmarshaller;
+    }
+
+    public void signal(Object object,
+                       PipelineContext context) {
+        Object result = null;
+        try {
+            if ( object instanceof File ) {
+                result = this.unmarshaller.unmarshal( (File) object );
+            } else if ( object instanceof InputStream ) {
+                result = this.unmarshaller.unmarshal( (InputStream) object );
+            } else if ( object instanceof Reader ) {
+                result = this.unmarshaller.unmarshal( (Reader) object );
+            } else if ( object instanceof Source ) {
+                result = this.unmarshaller.unmarshal( (Source) object );
+            } else if ( object instanceof InputSource ) {
+                result = this.unmarshaller.unmarshal( (InputSource) object );
+            }
+        } catch ( Exception e ) {
+            handleException( this,
+                             object,
+                             e );
+            if ( result instanceof JAXBElement ) {
+                result = ((JAXBElement) object).getValue().getClass().getName();
+            }
+        }
+        emit( result,
+              context );
+
+    }
+
+    public static class JaxbPipelineProviderImpl implements JaxbPipelineProvider {
+        public Transformer newJaxbTransformer(Unmarshaller unmarshaller) {
+            return new JaxbTransformer( unmarshaller );
+        }
+    }
+
+}

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/runtime/pipeline/impl/DroolsJaxbTest.java
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/runtime/pipeline/impl/DroolsJaxbTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/java/org/drools/runtime/pipeline/impl/DroolsJaxbTest.java	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,180 @@
+package org.drools.runtime.pipeline.impl;
+
+/*
+ * Copyright 2005 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.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.stream.StreamSource;
+
+import junit.framework.TestCase;
+
+import org.drools.KnowledgeBase;
+import org.drools.KnowledgeBaseFactory;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderFactory;
+import org.drools.builder.ResourceType;
+import org.drools.builder.help.KnowledgeBuilderHelper;
+import org.drools.definition.pipeline.Expression;
+import org.drools.definition.pipeline.PipelineFactory;
+import org.drools.definition.pipeline.Splitter;
+import org.drools.definition.pipeline.Transformer;
+import org.drools.io.ResourceFactory;
+import org.drools.runtime.StatefulKnowledgeSession;
+import org.drools.runtime.dataloader.StatefulKnowledgeSessionDataLoader;
+import org.drools.runtime.dataloader.impl.StatefulKnowledgeSessionDataLoaderImpl;
+import org.drools.runtime.rule.FactHandle;
+
+import com.sun.tools.xjc.Language;
+import com.sun.tools.xjc.Options;
+
+public class DroolsJaxbTest extends TestCase {
+
+    //    public void testModelLoad() throws Exception {
+    //        Options xjcOpts = new Options();
+    //        xjcOpts.setSchemaLanguage( Language.XMLSCHEMA );
+    //        PackageBuilder pkgBuilder = new PackageBuilder();
+    //
+    //        InputStream stream = getClass().getResourceAsStream( "test.xsd" );
+    //        String[] classNames = DroolsJaxbHelper.addModel( new InputStreamReader( stream ),
+    //                                                         pkgBuilder,
+    //                                                         xjcOpts,
+    //                                                         "xsd" );
+    //
+    //        assertFalse( pkgBuilder.hasErrors() );
+    //
+    //        RuleBase rb = RuleBaseFactory.newRuleBase();
+    //        rb.addPackage( pkgBuilder.getPackage() );
+    //
+    //        JAXBContext jaxbCtx = DroolsJaxbHelper.newInstance( classNames,
+    //                                                            rb );
+    //        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
+    //        JAXBElement elm = ( JAXBElement ) unmarshaller.unmarshal( getClass().getResourceAsStream( "data.xml" ) );
+    //       
+    //        assertEquals( "com.oracle.sample3.USAddress",
+    //                      elm.getValue().getClass().getName() );        
+    //    }
+
+    public void testDirectRoot() throws Exception {
+        Options xjcOpts = new Options();
+        xjcOpts.setSchemaLanguage( Language.XMLSCHEMA );
+        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+        String[] classNames = KnowledgeBuilderHelper.addXsdModel( ResourceFactory.newClassPathResource( "order.xsd",
+                                                                                                        getClass() ),
+                                                                  kbuilder,
+                                                                  xjcOpts,
+                                                                  "xsd" );
+
+        assertFalse( kbuilder.hasErrors() );
+
+        kbuilder.add( ResourceFactory.newClassPathResource( "test_Jaxb.drl",
+                                                            getClass() ),
+                      ResourceType.DRL );
+
+        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+        kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
+
+        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
+        List list1 = new ArrayList();
+        ksession.setGlobal( "list1",
+                            list1 );
+
+        JAXBContext jaxbCtx = KnowledgeBuilderHelper.newJAXBContext( classNames,
+                                                                     kbase );
+        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
+        Transformer transformer = PipelineFactory.newJaxbTransformer( unmarshaller );
+        transformer.addReceiver( PipelineFactory.newStatefulKnowledgeSessionReceiverAdapter() );
+
+        StatefulKnowledgeSessionDataLoader dataLoader = new StatefulKnowledgeSessionDataLoaderImpl( ksession,
+                                                                                                    transformer );
+        Map<FactHandle, Object> handles = dataLoader.insert( new StreamSource( getClass().getResourceAsStream( "order.xml" ) ) );
+
+        ksession.fireAllRules();
+
+        assertEquals( 1,
+                      handles.size() );
+        assertEquals( 1,
+                      list1.size() );
+
+        assertEquals( "org.drools.model.order.Order",
+                      list1.get( 0 ).getClass().getName() );
+    }
+
+    public void testNestedIterable() throws Exception {
+        Options xjcOpts = new Options();
+        xjcOpts.setSchemaLanguage( Language.XMLSCHEMA );
+        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+
+        String[] classNames = KnowledgeBuilderHelper.addXsdModel( ResourceFactory.newClassPathResource( "order.xsd",
+                                                                                                        getClass() ),
+                                                                  kbuilder,
+                                                                  xjcOpts,
+                                                                  "xsd" );
+
+        assertFalse( kbuilder.hasErrors() );
+
+        kbuilder.add( ResourceFactory.newClassPathResource( "test_Jaxb.drl",
+                                                            getClass() ),
+                      ResourceType.DRL );
+
+        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+        kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
+
+        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
+        List list1 = new ArrayList();
+        List list2 = new ArrayList();
+        ksession.setGlobal( "list1",
+                            list1 );
+        ksession.setGlobal( "list2",
+                            list2 );
+
+        JAXBContext jaxbCtx = KnowledgeBuilderHelper.newJAXBContext( classNames,
+                                                                     kbase );
+        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
+        Transformer transformer = PipelineFactory.newJaxbTransformer( unmarshaller );
+        Expression expression = PipelineFactory.newMvelExpression( "this.orderItem" );
+        transformer.addReceiver( expression );
+        Splitter splitter = PipelineFactory.newIterateSplitter();
+        expression.addReceiver( splitter );
+        splitter.addReceiver( PipelineFactory.newStatefulKnowledgeSessionReceiverAdapter() );
+        StatefulKnowledgeSessionDataLoader dataLoader = new StatefulKnowledgeSessionDataLoaderImpl( ksession,
+                                                                                                    transformer );
+        Map<FactHandle, Object> handles = dataLoader.insert( new StreamSource( getClass().getResourceAsStream( "order.xml" ) ) );
+
+        ksession.fireAllRules();
+
+        assertEquals( 2,
+                      handles.size() );
+        assertEquals( 1,
+                      list1.size() );
+        assertEquals( 1,
+                      list2.size() );
+
+        assertEquals( "org.drools.model.order.Order$OrderItem",
+                      list1.get( 0 ).getClass().getName() );
+
+        assertEquals( "org.drools.model.order.Order$OrderItem",
+                      list2.get( 0 ).getClass().getName() );
+
+        assertNotSame( list1.get( 0 ),
+                       list2.get( 0 ) );
+    }
+}

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/data.xml
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/data.xml	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/data.xml	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<myAddress xmlns="http://www.oracle.com/sample3/" 
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	     xsi:schemaLocation="http://www.oracle.com/sample3 sample3.xsd">
+	<name>James Bond</name>
+	<doorNumber>420</doorNumber>
+	<street>Oracle parkway</street>
+	<city>Redwood shores</city>
+	<state>CA</state>
+	<zip>94065</zip>
+	<country>United States</country>
+</myAddress>
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/order.xml
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/order.xml	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/order.xml	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<order xmlns="http://drools.org/model/order" 
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	   xsi:schemaLocation="http://drools.org/model/order order.xsd">
+    <order-item>
+        <product-id>111</product-id>
+        <quantity>2</quantity>
+        <price>8.90</price>
+    </order-item>
+    <order-item>
+        <product-id>222</product-id>
+        <quantity>7</quantity>
+        <price>5.20</price>
+    </order-item>
+</order>
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/order.xsd
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/order.xsd	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/order.xsd	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,43 @@
+<xsd:schema xmlns:order="http://drools.org/model/order" 
+            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+	        targetNamespace="http://drools.org/model/order"
+	        elementFormDefault="qualified" >
+	        
+        
+	<xsd:element name="order">
+		<xsd:complexType>
+			<xsd:sequence>
+				<xsd:element name="order-item" minOccurs="0" maxOccurs="unbounded">
+					<xsd:complexType>
+						<xsd:sequence>
+							<xsd:element name="product-id" type="xsd:long" />
+							<xsd:element name="quantity" type="xsd:integer" />
+							<xsd:element name="price" type="xsd:decimal" />
+						</xsd:sequence>	
+					</xsd:complexType>
+				</xsd:element>
+			</xsd:sequence>
+		</xsd:complexType>		
+	</xsd:element>
+
+<!--
+	
+	<xsd:element name="order" type="order:OrderType" />
+	
+	<xsd:complexType name="OrderType">
+		<xsd:sequence>
+			<xsd:element name="orderItem" type="order:orderItemType" minOccurs="0" maxOccurs="unbounded" />
+		</xsd:sequence>
+	</xsd:complexType>	
+	
+	        
+	<xsd:complexType name="orderItem">	
+		<xsd:sequence>
+			<xsd:element name="product-id" type="xsd:long" />
+			<xsd:element name="quantity" type="xsd:integer" />
+			<xsd:element name="price" type="xsd:decimal" />
+		</xsd:sequence>
+	</xsd:complexType>	
+-->
+
+</xsd:schema>
\ No newline at end of file

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/test.xsd
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/test.xsd	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/test.xsd	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,29 @@
+<schema xmlns = "http://www.w3.org/2001/XMLSchema"
+        xmlns:exp="http://www.oracle.com/sample3/"
+        targetNamespace="http://www.oracle.com/sample3/"
+        elementFormDefault="qualified">
+ 
+   <complexType name="Address">
+      <sequence>
+         <element name="name" type="string"/>
+         <element name="doorNumber" type="short"/>
+         <element name="street" type="string"/>
+         <element name="city" type="string"/>
+      </sequence>
+   </complexType>
+ 
+  <complexType name="USAddress">
+    <complexContent>
+     <extension base="exp:Address">
+       <sequence>
+          <element name="state" type="string"/>
+          <element name="zip" type="integer"/>
+          <element name="country" type="string"/>
+       </sequence>
+     </extension>
+    </complexContent>
+  </complexType>
+ 
+  <element name="myAddress" type="exp:USAddress"/>
+ 
+</schema>

Added: labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/test_Jaxb.drl
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/test_Jaxb.drl	                        (rev 0)
+++ labs/jbossrules/trunk/drools-pipeline/drools-transformer-jaxb/src/test/resources/org/drools/runtime/pipeline/impl/test_Jaxb.drl	2008-12-23 19:34:03 UTC (rev 24491)
@@ -0,0 +1,29 @@
+package org.drools.xxx
+
+import org.drools.model.order.Order
+import org.drools.model.order.Order.OrderItem
+
+global java.util.List list1;
+global java.util.List list2;
+
+
+rule orderRule
+when
+    $i : Order()
+then
+    list1.add( $i );
+end
+
+rule orderItemRule1
+when
+    $i : OrderItem( price > 6)
+then
+    list1.add( $i );
+end
+
+rule orderItemRule2
+when
+    $i : OrderItem( price < 6)
+then
+    list2.add( $i );
+end
\ No newline at end of file

Modified: labs/jbossrules/trunk/drools-pipeline/pom.xml
===================================================================
--- labs/jbossrules/trunk/drools-pipeline/pom.xml	2008-12-23 19:23:34 UTC (rev 24490)
+++ labs/jbossrules/trunk/drools-pipeline/pom.xml	2008-12-23 19:34:03 UTC (rev 24491)
@@ -8,9 +8,9 @@
     <version>5.0.0.SNAPSHOT</version>
   </parent>
 
-  <artifactId>drools-dataloaders</artifactId>
+  <artifactId>drools-transformer</artifactId>
   <packaging>pom</packaging>
-  <name>Drools :: DataLoaders</name>
+  <name>Drools :: Transformer</name>
   
   <modules>
       <module>drools-transformer-smooks</module>




More information about the jboss-svn-commits mailing list