1+ package org.sdpi.asciidoc.extension
2+
3+ import org.apache.logging.log4j.kotlin.Logging
4+ import org.asciidoctor.ast.StructuralNode
5+ import org.asciidoctor.extension.BlockMacroProcessor
6+ import org.asciidoctor.extension.Name
7+ import org.sdpi.asciidoc.DeprecationAttributes
8+ import org.sdpi.asciidoc.model.DeprecatedOid
9+ import org.sdpi.asciidoc.model.WellKnownOid
10+ import org.sdpi.asciidoc.model.makeConformityVersionOid
11+
12+ const val BLOCK_MACRO_NAME_DEPRECATE = " Deprecate"
13+ const val BLOCK_MACRO_NAME_DEPRECATE_REQUIREMENT = " DeprecateRequirement"
14+ const val BLOCK_MACRO_NAME_DEPRECATE_TRANSACTION = " DeprecateTransaction"
15+
16+ @Name(BLOCK_MACRO_NAME_DEPRECATE_REQUIREMENT )
17+ class DeprecateRequirementProcessor : BlockMacroProcessor (BLOCK_MACRO_NAME_DEPRECATE_REQUIREMENT ) {
18+ private companion object : Logging
19+
20+ val entries: Map <String , DeprecatedOid >
21+ field = mutableMapOf ()
22+
23+ fun isDeprecated (strRequirementOid : String ) : DeprecatedOid ? {
24+ return entries[strRequirementOid]
25+ }
26+
27+ override fun process (parent : StructuralNode , strTarget : String , attributes : MutableMap <String , Any >): Any? {
28+
29+ val strVersion = attributes[DeprecationAttributes .VERSION .key]?.toString()
30+ checkNotNull(strVersion) {
31+ logger.error(" $BLOCK_MACRO_NAME_DEPRECATE_REQUIREMENT missing required attribute '${DeprecationAttributes .VERSION .key} '" )
32+ }
33+
34+ val strVersionOid = makeConformityVersionOid(strVersion)
35+
36+ val requirementNumber = parseRequirementNumber(strTarget)
37+ val strRequirementId = getRequirementOid(requirementNumber)
38+
39+ checkNotNull(! entries.containsKey(strRequirementId)) {
40+ logger.error(" Requirement $strRequirementId is already marked deprecated" )
41+ }
42+
43+ entries[strRequirementId] = DeprecatedOid (strRequirementId, strVersionOid)
44+
45+ return null
46+ }
47+ }
48+ @Name(BLOCK_MACRO_NAME_DEPRECATE )
49+ class DeprecateProcessor : BlockMacroProcessor (BLOCK_MACRO_NAME_DEPRECATE ) {
50+ private companion object : Logging
51+
52+ val entries: Map <String , DeprecatedOid >
53+ field = mutableMapOf ()
54+
55+ fun isDeprecated (strOid : String ) : DeprecatedOid ? {
56+ return entries[strOid]
57+ }
58+
59+ override fun process (parent : StructuralNode , strTarget : String , attributes : MutableMap <String , Any >): Any? {
60+
61+ val strVersion = attributes[DeprecationAttributes .VERSION .key]?.toString()
62+ checkNotNull(strVersion) {
63+ " $BLOCK_MACRO_NAME_DEPRECATE_REQUIREMENT missing required attribute '${DeprecationAttributes .VERSION .key} '" .also {
64+ logger.error{it}
65+ }
66+ }
67+ println (" **Deprecating $strTarget " )
68+ val strVersionOid = makeConformityVersionOid(strVersion)
69+
70+
71+ check(strTarget.startsWith(WellKnownOid .DEV_SDPi .oid)) {
72+ " Oid `$strTarget ` must begin with ${WellKnownOid .DEV_SDPi .oid} " .also {
73+ logger.error{it}
74+ }
75+ }
76+
77+ val reOid = Regex (""" ^(?:(?:[01]\.(?:[0-9]|[1-3][0-9]))|(?:2\.(?:0|[1-9]\d*)))(?:\.(?:0|[1-9]\d*))*$""" )
78+ check(reOid.matches(strTarget)) {
79+ " Oid `$strTarget ` must be a valid oid" .also {
80+ logger.error{it}
81+ }
82+ }
83+
84+ checkNotNull(! entries.containsKey(strTarget)) {
85+ " Oid $strTarget is already marked deprecated" .also {
86+ logger.error{it}
87+ }
88+ }
89+
90+
91+ entries[strTarget] = DeprecatedOid (strTarget, strVersionOid)
92+
93+ return null
94+ }
95+ }
96+
97+ @Name(BLOCK_MACRO_NAME_DEPRECATE_TRANSACTION )
98+ class DeprecateTransactionProcessor : BlockMacroProcessor (BLOCK_MACRO_NAME_DEPRECATE_TRANSACTION ) {
99+ private companion object : Logging
100+
101+ val entries: Map <String , DeprecatedOid >
102+ field = mutableMapOf ()
103+
104+ fun isDeprecated (strTransactionOid : String ) : DeprecatedOid ? {
105+ return entries[strTransactionOid]
106+ }
107+
108+ override fun process (parent : StructuralNode , strTarget : String , attributes : MutableMap <String , Any >): Any? {
109+
110+ val strVersion = attributes[DeprecationAttributes .VERSION .key]?.toString()
111+ checkNotNull(strVersion) {
112+ " $BLOCK_MACRO_NAME_DEPRECATE_TRANSACTION missing required attribute '${DeprecationAttributes .VERSION .key} '" .also {
113+ logger.error{it}
114+ }
115+ }
116+
117+ val strVersionOid = makeConformityVersionOid(strVersion)
118+
119+ val strTransactionId = getTransactionOid(strTarget)
120+
121+ checkNotNull(! entries.containsKey(strTransactionId)) {
122+ " Transaction $strTransactionId is already marked deprecated" .also {
123+ logger.error{it}
124+ }
125+ }
126+
127+ entries[strTransactionId] = DeprecatedOid (strTransactionId, strVersionOid)
128+
129+ return null
130+ }
131+
132+ private fun getTransactionOid (strTransactionId : String ): String {
133+ if (strTransactionId.startsWith(" DEV-" )) {
134+ val strLeaf = strTransactionId.substring(4 )
135+ if (strLeaf.toIntOrNull() != null ) {
136+ return " ${WellKnownOid .DEV_TRANSACTION .oid} .$strLeaf "
137+ }
138+ }
139+ throw IllegalArgumentException (" '$strTransactionId ' is not a valid transaction id" )
140+ }
141+ }
0 commit comments