[rules-dev] XJC and JAXB DataLoader

Mark Proctor mproctor at codehaus.org
Wed Jul 9 12:37:25 EDT 2008


Sorry my mistake, I referenced a non included class. Here is the java 
code again, but with the static ErrorReceiver class.
    public void test1() throws Exception {     
        Options xjcOpts = new Options();       
        xjcOpts.setSchemaLanguage( Language.XMLSCHEMA );
        InputStream stream = getClass().getResourceAsStream( "test.xsd" 
);       
        assertNotNull( stream );
        InputSource source = new InputSource( new InputStreamReader( 
stream )  );
        source.setSystemId( ".xsd" );
        xjcOpts.addGrammar( source );
       
        ErrorReceiver errorReceiver = new JaxbErrorReceiver4Drools();
       
        Model model = ModelLoader.load( xjcOpts, new JCodeModel(),  
errorReceiver);
       
        final Outline outline = model.generateCode(xjcOpts,
                                                   errorReceiver);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
        model.codeModel.build( xjcOpts.createCodeWriter( new 
SingleStreamCodeWriter( baos ) ) );
       
        String str = new String( baos.toByteArray() );
        System.out.println( str );                       
    }
   
    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();
        }
    }   

Mark Proctor wrote:
> I got the Smooks Data Loader working last week, as mentioned in my 
> blog, and now trying to do something similar for JAXB. However for 
> this to work you need to generate the java source code at runtime and 
> add it to PackageBuilder for compilation. I'm told that XJC can indeed 
> generate source code, without needing to use the disk, but I'm having 
> problems getting it to work. Any one done this before, and have some 
> feedback on how to make this work?
>
> -----------The test.xsd file-----------
> 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>
>
> --------The Test-------------
>    public void test1() throws Exception {            Options xjcOpts = 
> new Options();              xjcOpts.setSchemaLanguage( 
> Language.XMLSCHEMA );
>        InputStream stream = getClass().getResourceAsStream( "test.xsd" 
> );              assertNotNull( stream );
>        InputSource source = new InputSource( new InputStreamReader( 
> stream )  );
>        source.setSystemId( ".xsd" );
>        xjcOpts.addGrammar( source );
>              ErrorReceiver errorReceiver = new 
> DroolsJaxb.JaxbErrorReceiver4Mvn();
>              Model model = ModelLoader.load( xjcOpts, new 
> JCodeModel(),  errorReceiver);
>              final Outline outline = model.generateCode(xjcOpts,
>                                                   errorReceiver);
>        ByteArrayOutputStream baos = new ByteArrayOutputStream();
>              model.codeModel.build( xjcOpts.createCodeWriter( new 
> SingleStreamCodeWriter( baos ) ) );
>              String str = new String( baos.toByteArray() );
>        System.out.println( str );                          }
>
> -------------The Error----------------
> java.io.IOException: Stream closed
>    at 
> com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.parseEntity(NGCCRuntimeEx.java:305) 
>
>    at 
> com.sun.xml.xsom.impl.parser.ParserContext.parse(ParserContext.java:88)
>    at com.sun.xml.xsom.parser.XSOMParser.parse(XSOMParser.java:147)
>    at 
> com.sun.tools.xjc.ModelLoader.createXSOMSpeculative(ModelLoader.java:496)
>    at com.sun.tools.xjc.ModelLoader.loadXMLSchema(ModelLoader.java:366)
>    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:167)
>    at com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:113)
>    at 
> org.drools.dataloaders.jaxb.DroolsJaxbTest.test1(DroolsJaxbTest.java:36)
>    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>    at java.lang.reflect.Method.invoke(Unknown Source)
>    at junit.framework.TestCase.runTest(TestCase.java:154)
>    at junit.framework.TestCase.runBare(TestCase.java:127)
>    at junit.framework.TestResult$1.protect(TestResult.java:106)
>    at junit.framework.TestResult.runProtected(TestResult.java:124)
>    at junit.framework.TestResult.run(TestResult.java:109)
>    at junit.framework.TestCase.run(TestCase.java:118)
>    at junit.framework.TestSuite.runTest(TestSuite.java:208)
>    at junit.framework.TestSuite.run(TestSuite.java:203)
>    at 
> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) 
>
>    at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) 
>
>
> _______________________________________________
> rules-dev mailing list
> rules-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-dev
>




More information about the rules-dev mailing list