private static void inlineSvgNodes ( final Document doc , final String basePath ) { try { // handle null inputs if ( doc == null ) return ; final String fixedBasePath = basePath == null ? "" : basePath ; final List < Node > nodes = XMLUtilities . getChildNodes ( doc . getDocumentElement ( ) , "object" ) ; for ( final Node node : nodes ) { final NamedNodeMap attributes = node . getAttributes ( ) ; final Node typeAttribute = attributes . getNamedItem ( "type" ) ; final Node dataAttribute = attributes . getNamedItem ( "data" ) ; if ( typeAttribute != null && typeAttribute . getTextContent ( ) . equals ( "image/svg+xml" ) && dataAttribute != null ) { final String data = dataAttribute . getTextContent ( ) ; final File dataFile = new File ( fixedBasePath + "/" + data ) ; if ( dataFile . exists ( ) ) { final String fileString = FileUtilities . readFileContents ( dataFile ) ; final Document svgDoc = XMLUtilities . convertStringToDocument ( fileString ) ; if ( svgDoc != null ) { final Element svgDocElement = svgDoc . getDocumentElement ( ) ; if ( svgDocElement != null && svgDocElement . getNodeName ( ) . equals ( "svg" ) ) { final Node parent = node . getParentNode ( ) ; if ( parent != null ) { final Node importedNode = doc . importNode ( svgDocElement , true ) ; parent . replaceChild ( importedNode , node ) ; } } } } } } } catch ( final Exception ex ) { LOG . error ( "Unable to convert external SVG image to DOM Document" , ex ) ; } }
Finds any reference to an external SVG image and replaces the node with the SVG inline data .