1

I managed to create the classes from an xsd file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="author" type="author"/>

  <xs:element name="book" type="book"/>

  <xs:complexType name="author">
    <xs:sequence>
      <xs:element name="firstName" type="xs:string" minOccurs="0"/>
      <xs:element name="lastName" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="book">
    <xs:sequence>
      <xs:element ref="author" minOccurs="0"/>
      <xs:element name="pages" type="xs:int"/>
      <xs:element name="publicationDate" type="xs:dateTime" minOccurs="0"/>
      <xs:element name="title" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

but I can't generate the same classes from a String representing the same xsd.

I pasted my code below:

  • the main_working generates the classes from a file and works fine
  • while the main_not_working throws an exception.

This is the code:

import java.io.File;
import java.io.StringReader;

import org.xml.sax.InputSource;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;

public class XsdMain {

    private static File out = new File("out");
    private static String xsd = 
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
            "<xs:schema version=\"1.0\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">" +
                "<xs:element name=\"author\" type=\"author\"/>" +
                "<xs:element name=\"book\" type=\"book\"/>" +
                "<xs:complexType name=\"author\">" +
                    "<xs:sequence>" +
                        "<xs:element name=\"firstName\" type=\"xs:string\" minOccurs=\"0\"/><xs:element name=\"lastName\" type=\"xs:string\" minOccurs=\"0\"/>" +
                    "</xs:sequence>" +
                "</xs:complexType>" +
                "<xs:complexType name=\"book\">" +
                    "<xs:sequence>" +
                        "<xs:element ref=\"author\" minOccurs=\"0\"/>" +
                        "<xs:element name=\"pages\" type=\"xs:int\"/>" +
                        "<xs:element name=\"publicationDate\" type=\"xs:dateTime\" minOccurs=\"0\"/>" +
                        "<xs:element name=\"title\" type=\"xs:string\" minOccurs=\"0\"/>" +
                    "</xs:sequence>" +
                "</xs:complexType>" +
            "</xs:schema>";

    public static void main(String[] args) throws Exception {
        main_working();       // this works
        main_not_working();   // this doesn't work
    }

    public static void main_working() throws Exception {
        // Setup schema compiler
        SchemaCompiler sc = XJC.createSchemaCompiler();
        sc.forcePackageName("com.xyz.schema");

        // Setup SAX InputSource
        File schemaFile = new File("in/test.xsd");
        InputSource is = new InputSource(schemaFile.toURI().toString());

        // Parse & build
        sc.parseSchema(is);
        S2JJAXBModel model = sc.bind();
        JCodeModel jCodeModel = model.generateCode(null, null);
        jCodeModel.build(out);

    }

    public static void main_not_working() throws Exception {
        // Setup schema compiler
        SchemaCompiler sc = XJC.createSchemaCompiler();
        sc.forcePackageName("com.xyz.schema");

        // Setup SAX InputSource
        InputSource is = new InputSource( new StringReader( xsd ) );

        // Parse & build
        sc.parseSchema(is);
        S2JJAXBModel model = sc.bind();
        JCodeModel jCodeModel = model.generateCode(null, null);
        jCodeModel.build(out);

    }
}

And the exeption thrown is this one:

Exception in thread "main" java.lang.NullPointerException
    at java.net.URI$Parser.parse(URI.java:3035)
    at java.net.URI.<init>(URI.java:607)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.checkAbsoluteness(SchemaCompilerImpl.java:163)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.parseSchema(SchemaCompilerImpl.java:130)
    at com.madx.XsdMain.main_not_working(XsdMain.java:71)
    at com.madx.XsdMain.main(XsdMain.java:42)

1 Answer 1

2

See http://docs.oracle.com/javase/7/docs/api/org/xml/sax/InputSource.html#setCharacterStream(java.io.Reader)

Application writers should use setSystemId() to provide a base for resolving relative URIs, and may use setPublicId to include a public identifier.

You're probably getting a nullpointer because SystemId isn't set.

Sign up to request clarification or add additional context in comments.

1 Comment

I added a line is.setSystemId("foo"); immediately after the InputSource initialization inside the main_not_workingand it worked perfectly, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.