SchemaTypeLoader.getSourceAsStream(source); runs into java.lang.NullPointerException

Murali Pottlapelli <[email protected]>
Newsgroups gmane.text.xml.xmlbeans.user
Message-ID <[email protected]>
Hi,
I am running into java.lang.NullPointerException in the following 
snippet at getSourceAsStream(source);

        //get SchemaType for element 
        SchemaGlobalElement element = 
typeLoader.findElement(QName.valueOf("{http://xml.netbeans.org/schema/Synchronous}typeA"));
        //confirm element
        System.out.println( 
"{http://xml.netbeans.org/schema/Synchronous}typeA is:" + 
element.getName() );
        System.out.println("\t \t" + "  -- details  ns: " + 
element.getName().getNamespaceURI()
                + "  prefix: " + element.getName().getPrefix() + "  
local name: " + element.getName().getLocalPart());
        SchemaType st = element.getType();

        // get the name of the schema source file
        // Not sure, is it working right?
        //schema source doc queried: 
URI_SHA_1_E8BB73591F25D088F33651C66E86AE1809406E4B/Synchronous.xsd
        //vs original: 
file:/C:/tmp/mvnprojects/schemaParser/target/classes/resources/Synchronous.xsd
        String source = st.getSourceName();
        System.out.println("schema source doc queried: " + source + " vs 
original: " + url);

        SchemaTypeLoader stl = st.getTypeSystem();
        java.io.InputStream is =  stl.getSourceAsStream(source);

I am attaching the Java program and maven project to reproduce the 
issue. Is this an user error? or an issue?

To build and run the test case

   1. unzip attached zip file
   2. move to the root folder
   3. mvn clean assembly:assembly
   4. java -cp
      target\schemaParser-1.0-SNAPSHOT-jar-with-dependencies.jar myTest.App

Regards
Murali


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
schemaParser.zip (application/x-zip-compressed, 6 KB) - not displayed
App.java (text/plain, 3.2 KB)
package myTest;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.xmlbeans.SchemaGlobalElement;
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.SchemaTypeLoader;
import org.apache.xmlbeans.XmlBeans;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument.Schema;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws XmlException, IOException, URISyntaxException
    {
        System.out.println( "At the top" );
        new App().testAccessToSchemaFromSchemaType();
        System.out.println( "At the end" );
    }
    
    public void testAccessToSchemaFromSchemaType() throws XmlException, IOException, URISyntaxException{
        String schemaFile = "Synchronous.xsd";
        URL url = getClass().getClassLoader().getResource(schemaFile);
        SchemaDocument doc = SchemaDocument.Factory.parse(url);
        
        List<SchemaDocument> xmlObjList = new ArrayList<SchemaDocument>();
        xmlObjList.add(doc);
        
        //It is possible to query elementFormDefault 
        //for a given schema if there is access to SchemaDocument
        Schema schema = doc.getSchema();
        System.out.println( "elementFormDefault: " + schema.getElementFormDefault() );

        XmlOptions options = new XmlOptions();
        List<XmlError> events = new ArrayList<XmlError>(); 
        options.setErrorListener(events);
        
        SchemaTypeLoader typeLoader = XmlBeans.loadXsd(xmlObjList.toArray(new XmlObject[0]), options);

        //get SchemaType for element  
        SchemaGlobalElement element = typeLoader.findElement(QName.valueOf("{http://xml.netbeans.org/schema/Synchronous}typeA"));
        //confirm element
        System.out.println( "{http://xml.netbeans.org/schema/Synchronous}typeA is:" + element.getName() );
		System.out.println("\t \t" + "  -- details  ns: " + element.getName().getNamespaceURI() 
				+ "  prefix: " + element.getName().getPrefix() + "  local name: " + element.getName().getLocalPart());
        SchemaType st = element.getType();

        // get the name of the schema source file 
        // Not sure, is it working right? 
        //schema source doc queried: URI_SHA_1_E8BB73591F25D088F33651C66E86AE1809406E4B/Synchronous.xsd 
        //vs original: file:/C:/tmp/mvnprojects/schemaParser/target/classes/resources/Synchronous.xsd
        String source = st.getSourceName(); 
        System.out.println("schema source doc queried: " + source + " vs original: " + url);

        SchemaTypeLoader stl = st.getTypeSystem();
        java.io.InputStream is =  stl.getSourceAsStream(source); 
        System.out.println("InputStream for source: " + is);
        
        //Goal is to get to Schema.getElementFormDefault()
        SchemaDocument schemadoc = SchemaDocument.Factory.parse(is); 
        System.out.println( "elementFormDefault: " + schemadoc.getSchema().getElementFormDefault() );
    }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.