final Path resource ) {
if ( packageHeaderInfo.getHeader() == null ) {
return xml;
}
final Imports imports = ImportsParser.parseImports( packageHeaderInfo.getHeader() );
if ( imports == null ) {
return xml;
}
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
Document doc = dombuilder.parse( new ByteArrayInputStream( xml.getBytes( Charsets.UTF_8 ) ) );
if ( doc.getElementsByTagName( "imports" ).getLength() != 0 ) {
return xml;
}
/* The imports should have following format (used by the workbench):
* <imports>
* <imports>
* <org.drools.workbench.models.datamodel.imports.Import>
* <type>java.lang.Number</type>
* </org.drools.workbench.models.datamodel.imports.Import>
* </imports>
* </imports>
*/
Element root = doc.getDocumentElement();
Element topImportsElement = doc.createElement( "imports" );
Element nestedImportsElement = doc.createElement( "imports" );
topImportsElement.appendChild(nestedImportsElement);
for ( final Import i : imports.getImports() ) {
Element importElement = doc.createElement( Import.class.getCanonicalName() );
Element typeElement = doc.createElement( "type" );
typeElement.appendChild( doc.createTextNode( i.getType() ) );
importElement.appendChild( typeElement );
nestedImportsElement.appendChild( importElement );