0

I have a java application that contains of 2 parts: Core project(module) and App project(module). Core module is dependent by several apps. I configure the work of my App module in config.xml file. I want to put xml file with common settings to Core module's resources to let all apps use xinclude to include this part to their configs. I use SAXParser to parse config.xml. This is what my config.xml looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<myapp>
    <xi:include href="common.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
    <app-specific-data>
    ...
    </app-specific-data>
</myapp>

File common.xml is places in CoreModule/src/resources. How can I access this file from config.xml that is places in working directory on AppModule? Thank you.

Vasilii Ruzov
  • 554
  • 1
  • 10
  • 27

1 Answers1

0

When you do

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setXIncludeAware(true);
factory.setNamespaceAware( true );
SAXParser parser = factory.newSAXParser();
parser.parse(input, new MyHandler());

in your MyHandler class override resolveEntity(publicId, systemId) method. In config change "common.xml" to "cp://common.xml". It will let you get "cp://common.xml" as systemId parameter in resolveEntity() method.

<xi:include href="cp://common.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>

In resolveEntity() method do next:

if( systemId.startsWith( "cp://" )  ){
    InputStream is = this.getClass().getClassLoader().getResourceAsStream( systemId.substring( 5 ) );
    InputSource isource = new InputSource( is );
    isource.setPublicId( publicId );
    isource.setSystemId( systemId );
    return isource;
} else {
    return super.resolveEntity( publicId, systemId );
}

If you omit

isource.setPublicId( publicId );
isource.setSystemId( systemId );

you'll get NPE in searchforrecursiveincludes() in XIncludeHandler.

Vasilii Ruzov
  • 554
  • 1
  • 10
  • 27