Skip to content

Commit 1661419

Browse files
author
Steve Ramage
committed
Checkpoint
1 parent b90c909 commit 1661419

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

buildSrc/src/main/groovy/GenerateDataFromManPages.groovy

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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,57 +490,65 @@ 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+
if (cachedElements.containsKey(elementId)) {
507+
return cachedElements.get(elementId)
508+
} else {
509+
println "⚠️ WARNING: Element with id='$elementId' not found in ${xmlFile.name}"
510+
return null
495511
}
512+
}
496513

514+
private static void cacheAllElements(File xmlFile) {
497515
try {
498-
println("Processing $cacheKey")
499-
500516
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
501517
factory.setNamespaceAware(true) // Needed for XML ID lookup
502518
DocumentBuilder builder = factory.newDocumentBuilder()
503519

504520
Document document = builder.parse(new InputSource(xmlFile.newReader()))
505521
document.getDocumentElement().normalize()
506522

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
523+
// ✅ Create a new cache for this file
524+
Map<String, String> elementCache = new ConcurrentHashMap<>()
525+
526+
// 🚀 Find all elements with an `id` attribute and store them in cache
527+
NodeList elements = document.getElementsByTagName("*")
528+
for (int i = 0; i < elements.length; i++) {
529+
Element element = elements.item(i)
530+
if (element.hasAttribute("id")) {
531+
String elementId = element.getAttribute("id")
532+
String extractedXml = nodeToString(element)
533+
534+
String wrappedXml = "<!--xi:include='${xmlFile.name}' xpointer='${elementId}'-->" +
535+
extractedXml +
536+
"<!-- /xi:include='${xmlFile.name}' xpointer='${elementId}' -->"
537+
538+
elementCache.put(elementId, wrappedXml)
539+
}
512540
}
513541

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)
542+
// ✅ Store parsed elements in the file cache
543+
fileCache.put(xmlFile.getAbsolutePath(), elementCache)
518544

519-
return value
545+
println "✅ Cached ${elementCache.size()} elements from ${xmlFile.name}"
520546

521547
} catch (Exception e) {
522-
println "❌ ERROR: Failed to extract element by id='$elementId' from ${xmlFile.name}: ${e.message}"
523-
cache.put(cacheKey, null)
548+
println "❌ ERROR: Failed to parse ${xmlFile.name}: ${e.message}"
524549
}
525550
}
526551

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-
}
534-
}
535-
return null
536-
}
537-
538552
private static String nodeToString(Node node) {
539553
try {
540554
TransformerFactory transformerFactory = TransformerFactory.newInstance()
@@ -548,6 +562,7 @@ class GenerateDataFromManPages extends DefaultTask {
548562
return ""
549563
}
550564
}
565+
551566
/**
552567
* Transforms the supplied document with the supplied transformer
553568
* @param document - XML Document to transform

src/test/kotlin/net/sjrx/intellij/plugins/systemdunitfiles/semanticdata/SemanticDataDocumentationCompletionTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import net.sjrx.intellij.plugins.systemdunitfiles.AbstractUnitFileTest
44
import java.util.*
55

66
class SemanticDataDocumentationCompletionTest : AbstractUnitFileTest() {
7-
fun testAllOptions() {
7+
fun testAllOptionsHaveDocumentation() {
88
val sdr = SemanticDataRepository.instance
99
val doc: MutableSet<String> = TreeSet()
1010

0 commit comments

Comments
 (0)