private static void inlineCssNodes ( final Document doc , final String basePath ) { if ( doc == null ) return ; final String fixedBasePath = basePath == null ? "" : basePath ; final List < Node > nodes = XMLUtilities . getChildNodes ( doc . getDocumentElement ( ) , "link" ) ; for ( final Node node : nodes ) { final NamedNodeMap attributes = node . getAttributes ( ) ; final Node relAttribute = attributes . getNamedItem ( "rel" ) ; final Node hrefAttribute = attributes . getNamedItem ( "href" ) ; final Node mediaAttribute = attributes . getNamedItem ( "media" ) ; if ( relAttribute != null && relAttribute . getTextContent ( ) . equals ( "stylesheet" ) && hrefAttribute != null ) { final String href = hrefAttribute . getTextContent ( ) ; final File hrefFile = new File ( fixedBasePath + "/" + href ) ; if ( hrefFile . exists ( ) ) { // find the base path for any import statements that // might be in the css file String cssBasePath = "" ; int end = href . lastIndexOf ( "/" ) ; if ( end == - 1 ) end = href . lastIndexOf ( "\\" ) ; if ( end != - 1 ) cssBasePath = href . substring ( 0 , end ) ; final String fileString = inlineCssImports ( FileUtilities . readFileContents ( hrefFile ) , basePath + "/" + cssBasePath ) ; final Node parent = node . getParentNode ( ) ; if ( parent != null ) { final Element newNode = doc . createElement ( "style" ) ; newNode . setAttribute ( "type" , "text/css" ) ; if ( mediaAttribute != null ) newNode . setAttribute ( "media" , mediaAttribute . getTextContent ( ) ) ; newNode . setTextContent ( fileString ) ; parent . replaceChild ( newNode , node ) ; } } } } }
Finds any reference to an external CSS scripts and replaces the node with inline CSS data .