@@ -315,9 +315,9 @@ class GenerateDataFromManPages extends DefaultTask {
315315
316316 String filename = file. getName()
317317
318- def builder = dbf . newDocumentBuilder( )
318+ Document document = buildDocumentProcessingIncludes(file )
319319
320- def records = builder . parse(file) . documentElement
320+ def records = document . documentElement
321321
322322 /*
323323 We technically should be looking for variablelist element with class 'unit-directives' however some sections in
@@ -434,8 +434,18 @@ class GenerateDataFromManPages extends DefaultTask {
434434 * @return
435435 */
436436 private generateDocumentationHtmlFromManPages (String fileType , File sourceFile ) {
437- DocumentBuilder builder = dbf. newDocumentBuilder()
438437
438+
439+ Document document = buildDocumentProcessingIncludes(sourceFile)
440+ Transformer transformer = getXsltTransformer()
441+
442+ String xsltOutput = transformDocument(document, transformer)
443+
444+ segmentParametersIntoFiles(fileType, sourceFile. getName(), xsltOutput)
445+ }
446+
447+ private Document buildDocumentProcessingIncludes (File sourceFile ) {
448+ DocumentBuilder builder = dbf. newDocumentBuilder()
439449 String xmlContent = sourceFile. text
440450
441451
@@ -455,11 +465,7 @@ class GenerateDataFromManPages extends DefaultTask {
455465 outputFile. text = xmlContent // Save to file
456466
457467 Document document = builder. parse(outputFile)
458- Transformer transformer = getXsltTransformer()
459-
460- String xsltOutput = transformDocument(document, transformer)
461-
462- segmentParametersIntoFiles(fileType, sourceFile. getName(), xsltOutput)
468+ document
463469 }
464470
465471 private String processXIncludesWithRegex (String xmlContent , File baseDir ) {
@@ -484,55 +490,72 @@ class GenerateDataFromManPages extends DefaultTask {
484490 }
485491 }
486492
487- private static final Map<String , String > cache = new ConcurrentHashMap<> ()
493+ // 🔥 Static cache for storing extracted XML elements
494+ private static final Map<String , Map<String , String > > fileCache = new ConcurrentHashMap<> ()
488495
489- private static String extractElementById(File xmlFile, String elementId) {
490- String cacheKey = xmlFile. getAbsolutePath() + " ::" + elementId
496+ static String extractElementById(File xmlFile, String elementId) {
497+ // ✅ Check if the entire file has already been cached
498+ String filePath = xmlFile. getAbsolutePath()
499+ if (! fileCache. containsKey(filePath)) {
500+ // 🚀 Populate the cache for this file
501+ cacheAllElements(xmlFile)
502+ }
491503
492- // ✅ Check cache before processing
493- if (cache. containsKey(cacheKey)) {
494- return cache. get(cacheKey)
504+ // ✅ Retrieve element from cache
505+ Map<String , String > cachedElements = fileCache. get(filePath)
506+
507+ if (elementId == null ) {
508+ elementId = DOCUMENT_CACHE_KEY
495509 }
496510
497- try {
498- println (" Processing $cacheKey " )
511+ if (cachedElements. containsKey(elementId)) {
512+ return cachedElements. get(elementId)
513+ } else {
514+ println " ⚠️ WARNING: Element with id='$elementId ' not found in ${ xmlFile.name} "
515+ return null
516+ }
517+ }
518+
519+ private static final String DOCUMENT_CACHE_KEY = " [[[ROOT_ELEMENT]]]"
499520
521+ private static void cacheAllElements(File xmlFile) {
522+ try {
500523 DocumentBuilderFactory factory = DocumentBuilderFactory . newInstance()
501524 factory. setNamespaceAware(true ) // Needed for XML ID lookup
502525 DocumentBuilder builder = factory. newDocumentBuilder()
503526
504527 Document document = builder. parse(new InputSource (xmlFile. newReader()))
505528 document. getDocumentElement(). normalize()
506529
507- // Find element with matching id
508- Node targetNode = findElementById(document, elementId)
509- if (targetNode == null ) {
510- println " ⚠️ WARNING: Element with id='$elementId ' not found in ${ xmlFile.name} "
511- return null
530+ // ✅ Create a new cache for this file
531+ Map<String , String > elementCache = new ConcurrentHashMap<> ()
532+
533+ String rootDoc = nodeToString(document. documentElement)
534+ elementCache. put(DOCUMENT_CACHE_KEY , rootDoc)
535+ // 🚀 Find all elements with an `id` attribute and store them in cache
536+ NodeList elements = document. getElementsByTagName(" *" )
537+ for (int i = 0 ; i < elements. length; i++ ) {
538+ Element element = elements. item(i)
539+ if (element. hasAttribute(" id" )) {
540+ String elementId = element. getAttribute(" id" )
541+ String extractedXml = nodeToString(element)
542+
543+ String wrappedXml = " <!--xi:include='${ xmlFile.name} ' xpointer='${ elementId} '-->" +
544+ extractedXml +
545+ " <!-- /xi:include='${ xmlFile.name} ' xpointer='${ elementId} ' -->"
546+
547+ elementCache. put(elementId, wrappedXml)
548+ }
512549 }
513550
514- // ✅ Convert the found node back to an XML string
515- String extractedXml = nodeToString(targetNode)
516- String value = " <!--xi:include='$xmlFile . name ' xpointer='$elementId '-->" + extractedXml + " <!-- /xi:include='$xmlFile . name ' xpointer='$elementId ' -->"
517- cache. put(cacheKey, value)
551+ // ✅ Store parsed elements in the file cache
552+ fileCache. put(xmlFile. getAbsolutePath(), elementCache)
518553
519- return value
554+ println " ✅ Cached ${ elementCache.size() } elements from ${ xmlFile.name } "
520555
521556 } catch (Exception e) {
522- println " ❌ ERROR: Failed to extract element by id='$elementId ' from ${ xmlFile.name} : ${ e.message} "
523- cache. put(cacheKey, null )
524- }
525- }
526-
527- private static Node findElementById(Document document, String elementId) {
528- def elements = document. getElementsByTagName(" *" ) // Get all elements
529- for (int i = 0 ; i < elements. length; i++ ) {
530- Element element = elements. item(i)
531- if (element. hasAttribute(" id" ) && element. getAttribute(" id" ) == elementId) {
532- return element
533- }
557+ println " ❌ ERROR: Failed to parse ${ xmlFile.name} : ${ e.message} "
534558 }
535- return null
536559 }
537560
538561 private static String nodeToString(Node node) {
@@ -548,6 +571,7 @@ class GenerateDataFromManPages extends DefaultTask {
548571 return " "
549572 }
550573 }
574+
551575 /**
552576 * Transforms the supplied document with the supplied transformer
553577 * @param document - XML Document to transform
0 commit comments