-
Notifications
You must be signed in to change notification settings - Fork 2
/
DocumentTemplate.xml
2019 lines (1808 loc) · 59 KB
/
DocumentTemplate.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="Cache" version="25" zv="Cache for Windows (x86-64) 2018.1.2 (Build 309U_SU)" ts="2019-10-17 16:55:17">
<Class name="DocumentTemplate.DocXmlRenderer">
<Description><![CDATA[
Render document template (docx or odt) unpacked in direcotry <var>Directory</var>
Lexical parser successively passes through character of the xml document
On each character it chooses a new state according to current state and table of states transitions
Initial state is 1 - between tags. On last character the state must be equal to 1, otherwise error occured
<p>Lexical parser states: </p>
<p> 1 Between tags </p>
<p> "{" - 2 </p>
<p> - 1 </p>
<p> 2 First tag character was found "{" </p>
<p> "{" - 3 </p>
<p> "%" - 4 </p>
<p> - 1 </p>
<p> 3 Inside the tag {{ }} </p>
<p> "}" - 5 </p>
<p> - 3 </p>
<p> 4 Inside the tag {% %} </p>
<p> "%" - 6 </p>
<p> - 4 </p>
<p> 5 First character } of closing braces {{ }} </p>
<p> "}" - 1 </p>
<p> - 3 </p>
<p> 6 First character % of closing braces {% %} </p>
<p> "}" - 1 </p>
<p> - 4 </p>]]></Description>
<IncludeCode>DocumentTemplate.General</IncludeCode>
<Super>%RegisteredObject</Super>
<TimeChanged>65303,60810.311491</TimeChanged>
<TimeCreated>65269,53530.911081</TimeCreated>
<Property name="Directory">
<Description>
Unpacked document directory</Description>
<Type>%String</Type>
</Property>
<Property name="XMLDocument">
<Description>
XML.Document</Description>
<Type>%XML.Document</Type>
</Property>
<Property name="XMLDocumentMainNodeId">
<Type>%String</Type>
</Property>
<Property name="XMLWriter">
<Type>%XML.Writer</Type>
</Property>
<Property name="LexicalStates">
<Description>
States map </Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="LexicalCurrentState">
<Description>
Current state</Description>
</Property>
<Property name="LexicalStateHistory">
<Description>
States history</Description>
<Type>%List</Type>
</Property>
<Property name="CurrentTagNodes">
<Description>
Store xml node ids of current tag
Whole tag wil be moved to the first node</Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="CurrentTagNodesList">
<Description>
Store xml node ids of current tag as a list</Description>
<Type>%List</Type>
</Property>
<Property name="ToDeleteNodes">
<Description>
Nodes marked for deleting</Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="IfTags">
<Description>
store 'if' tags
IfTags(startTagNodeId) = endTagNodeId
IfTags(startTagNodeId, "expression") = "conditional expression" </Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="IfTagsStack">
<Description>
Stack for nested 'if' tags</Description>
<Type>%List</Type>
</Property>
<Property name="OrderTags">
<Description>
Store order-tags
OrderTags(startTagNodeId) = endTagNodeId
OrderTags(startTagNodeId, "mas") = "Name of the global"
OrderTags(startTagNodeId, "index") = "index variable for passing through global nodes"</Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="OrderTagsStack">
<Description>
Stack for nested 'order' tags</Description>
<Type>%List</Type>
</Property>
<Property name="TagTypeStack">
<Description>
Store 'order' or 'if' tags
Using for catching error</Description>
<Type>%String</Type>
</Property>
<Property name="SetTags">
<Description>
Store set-tag
setTags(setTagNodeId) = ""
setTags(setTagNodeId, "variable") = "variable for assignment"
setTags(setTagNodeId, "expression") = "expression"</Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="IfSiblings">
<Description>
Store if-siblings </Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="OrderSiblings">
<Description>
Store order sibling</Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Property name="SkipIfEmptyNodes">
<Description>
Store nodes marked for deletion if they are empty</Description>
<MultiDimensional>1</MultiDimensional>
</Property>
<Method name="%OnNew">
<FormalSpec>directory:%String</FormalSpec>
<ReturnType>%Status</ReturnType>
<Implementation><![CDATA[
set $this.Directory = directory
set ..LexicalStates(1) = 1
set ..LexicalStates(1,"{") = 2
set ..LexicalStates(2) = 1
set ..LexicalStates(2,"{") = 3
set ..LexicalStates(2,"%") = 4
set ..LexicalStates(3) = 3
set ..LexicalStates(3,"}") = 5
set ..LexicalStates(4) = 4
set ..LexicalStates(4,"%") = 6
set ..LexicalStates(5) = 3
set ..LexicalStates(5,"}") = 1
set ..LexicalStates(6) = 4
set ..LexicalStates(6,"}") = 1
quit $$$OK
]]></Implementation>
</Method>
<Method name="ClearState">
<Implementation><![CDATA[
set ..LexicalCurrentState = 1
set ..LexicalStateHistory = $LB()
set ..CurrentTagNodesList=""
set ..IfTagsStack = ""
set ..OrderTagsStack = ""
set ..TagTypeStack = ""
kill ..CurrentTagNodes, ..ToDeleteNodes, ..IfTags, ..OrderTags
kill ..IfSiblings, ..OrderSiblings, ..SkipIfEmptyNodes
]]></Implementation>
</Method>
<Method name="Render">
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
set documentFileDocx = ##class(%File).NormalizeFilename("word/document.xml", $this.Directory)
set documentFileOdt = ##class(%File).NormalizeFilename("content.xml", $this.Directory)
if ##class(%File).Exists(documentFileDocx){
quit ..RenderDocx()
}
elseif ##class(%File).Exists(documentFileOdt){
quit ..RenderOdt()
}
else{
quit "Unsupported file format"
}
]]></Implementation>
</Method>
<Method name="RenderDocx">
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
set documentFile = ##class(%File).NormalizeFilename("word/document.xml", $this.Directory)
// Render main document
set error = ..RenderXml(documentFile)
if error'="" quit error
do ..WriteDebug("Main document is rendered")
// Render footers
set footerExist = 1
set footerCounter = 1
while footerExist{
set footerFile = ##class(%File).NormalizeFilename("word/footer"_footerCounter_".xml", $this.Directory)
if ##class(%File).Exists(footerFile){
set error = ..RenderXml(footerFile)
if error'="" quit
do ..WriteDebug("Footer is rendered")
set footerCounter = $I(footerCounter)
}
else{
set footerExist = 0
}
}
if error'="" quit error
// Render headers
set headerExist = 1
set headerCounter = 1
while headerExist{
set headerFile = ##class(%File).NormalizeFilename("word/header"_headerCounter_".xml", $this.Directory)
if ##class(%File).Exists(headerFile){
set error = ..RenderXml(headerFile)
if error'="" quit
do ..WriteDebug("Header rendered")
set headerCounter = $I(headerCounter)
}
else{
set headerExist = 0
}
}
if error'="" quit error
do ..WriteDebug("render complete")
quit ""
]]></Implementation>
</Method>
<Method name="RenderOdt">
<Implementation><![CDATA[
set documentFile = ##class(%File).NormalizeFilename("content.xml", $this.Directory)
// Render main document
set error = ..RenderXml(documentFile)
if error'="" quit error
do ..WriteDebug("Main document is rendered")
set styleFile = ##class(%File).NormalizeFilename("styles.xml", $this.Directory)
set error = ..RenderXml(styleFile)
if error'="" quit error
do ..WriteDebug("Style documnet is rendered")
quit ""
]]></Implementation>
</Method>
<Method name="RenderXml">
<Description>
Render xml
Return error string or "" if no errors occured</Description>
<FormalSpec>fullPathToXml:%String</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
do ..ClearState()
set docFullFileName = fullPathToXml
set xmlReader = ##class(%XML.Reader).%New()
set sc = xmlReader.OpenFile(docFullFileName)
if $$$ISERR(sc) quit sc
set ..XMLDocument = xmlReader.Document
set mainDocumentNode = ..XMLDocument.GetNode(..XMLDocument.GetDocumentNode())
set ..XMLDocumentMainNodeId = mainDocumentNode.NodeId
d ..TraverseXmlTree(.mainDocumentNode,"WriteXmlNode")
set error = ..TraverseXmlTree(.mainDocumentNode,"SpecialTreatment")
if error'="" quit error
// First passing through the tree - check the syntax and move lexem in one xml node
// {{ sometext }} {% sometext %}
set error = ..TraverseXmlTree(.mainDocumentNode,"LexicalParse")
if error'="" quit error
// If state is not equal to 1 then error occured
if "3,5" [..LexicalCurrentState quit "Unclosed tag {{ }}"
if "4,6" [..LexicalCurrentState quit "Unclosed tag {% %}"
do ..WriteDebug("Lexical parsing is completed")
d ..DeleteMarkedNodes()
do ..WriteDebug("Nodes was deleted")
do ..WriteDebug("Tree after parsing: ")
d ..TraverseXmlTree(.mainDocumentNode,"WriteXmlNode")
// Separate tags in one xml node into individual nodes
set error = ..TraverseXmlTree(.mainDocumentNode,"SeparateTags")
if error'="" quit error
do ..WriteDebug("Separating is completed")
// Check syntax and find tags order,if, set
set error = ..TraverseXmlTree(.mainDocumentNode,"SyntaxParse")
if error'="" quit error
do ..WriteDebug("Syntax parsing is completed")
// Check unclosed tags
set unclosedIf = 0, unclosedOrder=0, nodeId=""
if $length(..OrderTagsStack)'=0{
set unclosedOrder = 1
set nodeId = ..PopFromOrderStack()
}
if $length(..IfTagsStack)'=0{
set unclosedIf = 1
set nodeId = ..PopFromIfStack()
}
if unclosedIf||unclosedOrder{
set node = ..XMLDocument.GetNode(nodeId)
set tagText = ""
try{
d node.GetText(.tagText, 0)
}
catch{}
quit "Unclosed tag "_$S(unclosedOrder:"Order",1:"If")_": "_ tagText
}
// Processing IfTags and OrderTags
// Find siblings for repetition in order loop
do ..FindSiblings($Name(..OrderTags), $Name(..OrderSiblings))
// Find siblings for repetition in if loop
do ..FindSiblings($Name(..IfTags), $Name(..IfSiblings))
// Write result
d xmlReader.Close()
set ..XMLWriter = ##class(%XML.Writer).%New()
set ..XMLWriter.Charset="UTF-8"
set sc = ..XMLWriter.OutputToFile(docFullFileName)
if $$$ISERR(sc) quit $system.OBJ.DisplayError(sc)
set error = ..Processing(.mainDocumentNode)
if error'="" quit error
quit ""
]]></Implementation>
</Method>
<Method name="TraverseXmlTree">
<Description><![CDATA[
Pass through <var>xmlNode</var> from left to right calling <var> method </var on each node
<var>method </var> takes xml node as an argument
<var>method1</var> - calling before child elements
<var>method2</var> - calling after child elements
Return string with error or "" if no error occured]]></Description>
<FormalSpec><![CDATA[&xmlNode:%XML.Node,method1:%String,method2:%String=""]]></FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
set error = $method($this, method1, .xmlNode)
if error'="" quit error
if xmlNode.HasChildNodes(){
d xmlNode.MoveToFirstChild()
set siblingsExist = 1
while siblingsExist{
set error = ..TraverseXmlTree(.xmlNode, method1, method2)
if error'="" quit
set siblingsExist = xmlNode.MoveToNextSibling()
}
d xmlNode.MoveToParent()
}
if $G(error)'="" quit error
if method2'=""{
set error = $method($this, method2, .xmlNode)
if error'="" quit error
}
quit ""
]]></Implementation>
</Method>
<Method name="LexicalParse">
<Description>
Method is used as TraverseXmlTree argument
Find tags, store in stack their position
Return string with error or "" if no error occured</Description>
<FormalSpec>xmlNode</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
if xmlNode.NodeType'= $$$xmlELEMENTNODE quit ""
set text = ""
set textExist = 0
try{
set textExist = xmlNode.GetText(.text, 0 )
}
catch{ }
if textExist && ($G(text)'=""){
for i=1:1:$length(text){
set char = $E(text,i)
d ..NextLexicalState(char)
if ..LexicalCurrentState=1{
// if end of the tag was found assemble the tag in one xml node
if ($D(..CurrentTagNodes) && (char="}") ) {
// if something else in the node put tag characters before
// to ensure that there is no different tags parts in one node
if (i <$length(text) && ($LISTLENGTH(..CurrentTagNodesList)>1)){
set newNodeId = ..InsertNodeBefore(.xmlNode,$E(text, 1, i))
do ..DeleteLastNodeFromTagNodeList(xmlNode.NodeId)
do ..PushToCurrentTagNodesList(newNodeId)
// All the rest stay in current node
do ..ReplaceNode(.xmlNode,$E(text,i+1,*))
}
else{
do ..PushToCurrentTagNodesList(xmlNode.NodeId)
}
do ..StoreTagInOneNode()
}
}
else {
if ..LexicalCurrentState=2{
do ..DeleteCurrentTagNodes()
}
do ..PushToCurrentTagNodesList(xmlNode.NodeId)
}
}
}
quit ""
]]></Implementation>
</Method>
<Method name="SpecialTreatment">
<FormalSpec><![CDATA[&node]]></FormalSpec>
<Implementation><![CDATA[
set nodeId = node.NodeId
if node.QName="text:p"{
// Loop through child nodes
// characer nodes replaced with <text:span>
if node.HasChildNodes(1){
d node.MoveToFirstChild(1)
set siblingsExist = 1
while siblingsExist{
if node.NodeType=$$$xmlTEXTNODE{
set childNodeId = node.NodeId
set nodeData = node.NodeData
do node.MoveToParent()
set newNodeId = node.InsertElement("span", "", .childNodeId, nodeData, "text:span")
set node = ..XMLDocument.GetNode(childNodeId)
d node.Remove()
set node = ..XMLDocument.GetNode(newNodeId)
//do node.ReplaceElement("span", "", node.NodeData, "text:span")
}
set siblingsExist = node.MoveToNextSibling(1)
}
d node.MoveToParent()
}
}
if node.QName="text:s"{
// <text:s> replaced with spaces
set spaceCount = node.GetAttributeValue("c")
if spaceCount'>0 set spaceCount=1
set replaceStr = ""
for i=1:1:spaceCount set replaceStr=replaceStr_" "
do node.ReplaceCharacter(replaceStr)
}
quit ""
]]></Implementation>
</Method>
<Method name="NextLexicalState">
<FormalSpec>char</FormalSpec>
<Implementation><![CDATA[
// Take next state from state table
if $D(..LexicalStates(..LexicalCurrentState,char)){
set ..LexicalCurrentState = ..LexicalStates(..LexicalCurrentState,char)
}
// If there is no state then go to the default state
else {
set ..LexicalCurrentState = ..LexicalStates(..LexicalCurrentState)
}
]]></Implementation>
</Method>
<Method name="PushToCurrentTagNodesList">
<FormalSpec>value</FormalSpec>
<Implementation><![CDATA[
if '$D(..CurrentTagNodes(value)){
s ..CurrentTagNodes(value) = ""
$$$PushToStack(..CurrentTagNodesList, value)
}
]]></Implementation>
</Method>
<Method name="DeleteLastNodeFromTagNodeList">
<FormalSpec>value</FormalSpec>
<Implementation><![CDATA[
if $LI(..CurrentTagNodesList,*)=value{
set value = $$$PopFromStack(..CurrentTagNodesList)
kill ..CurrentTagNodes(value)
}
]]></Implementation>
</Method>
<Method name="DeleteCurrentTagNodes">
<Implementation><![CDATA[
k ..CurrentTagNodes
s ..CurrentTagNodesList = ""
]]></Implementation>
</Method>
<Method name="StoreTagInOneNode">
<Description>
Remote tag in first xml node
Other nodes will be deleted</Description>
<Implementation><![CDATA[
if '$D(..CurrentTagNodes) quit
// If tag in one xml node there's nothing to do
if $LISTLENGTH(..CurrentTagNodesList)=1 d ..DeleteCurrentTagNodes() quit
do ..WriteDebug("Begin tag moving")
set ptr = 0
set nodeId=""
set isNotEnd = $LISTNEXT(..CurrentTagNodesList,ptr,nodeId)
if 'isNotEnd d ..DeleteCurrentTagNodes() quit
set mainNodeId = nodeId
set mainNode = ..XMLDocument.GetNode(nodeId)
set nodeText=""
try{
d mainNode.GetText(.nodeText)
}
catch{}
// tag assemle in nodeText
while $LISTNEXT(..CurrentTagNodesList, ptr, nodeId){
set node = ..XMLDocument.GetNode(nodeId)
try{
d node.GetText(.nodeText)
}
catch{
}
do ..WriteDebug("Node marked for deletion: "_node.NodeId)
set ..ToDeleteNodes(node.NodeId)=""
}
do ..ReplaceNode(.mainNode, nodeText)
do ..WriteDebug("Assembling tag in one node is completed. Tag: "_nodeText)
k testText
k nodeText
d ..DeleteCurrentTagNodes()
]]></Implementation>
</Method>
<Method name="DeleteMarkedNodes">
<Implementation><![CDATA[
set nodeId=""
for{
set nodeId = $Order(..ToDeleteNodes(nodeId),-1)
quit:nodeId=""
try{
set node = ..XMLDocument.GetNode(nodeId)
do node.ReplaceCharacter("")
do ..WriteDebug("Node "_node.NodeId_" "_nodeId_ "deleted")
}
catch error{
do ..WriteDebug(error.Name_" "_error.Location)
}
}
]]></Implementation>
</Method>
<Method name="Processing">
<Description>
Traverse XML and writing nodes
Processing 'if' 'order' and 'set' tags</Description>
<FormalSpec><![CDATA[&node:%XML.Node]]></FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
// processing if
set error =""
set expression=1
if ($D(..IfSiblings(node.NodeId))){
try{
set expression = ..EvaluateExpression(..IfSiblings(node.NodeId,"expression"))
d ..WriteDebug("Condition "_..IfSiblings(node.NodeId,"expression")_" "_expression)
}
catch exeption{
set error = "Erroe while 'if' condition evaluating : "_..IfSiblings(node.NodeId,"expression")_$C(13,10)_exeption.DisplayString()
}
if error'="" quit error
// Mark node with closing tag for deleting if it empty
set ..SkipIfEmptyNodes(..IfSiblings(node.NodeId))=""
// If condition is false go to last sibling
// i.e skip block inside 'if'
if 'expression{
set node = ..XMLDocument.GetNode(..IfSiblings(node.NodeId))
quit ""
}else{
if ..IsNodeEmpty(node){
d node.MoveToNextSibling()
}
}
}
if error'="" quit error
// processing 'order'
// ..OrderSiblings(node.NodeId,"calculating")=1 - mark that node under processing
// otherwise infinite loop occour
if ($D(..OrderSiblings(node.NodeId))&&($G(..OrderSiblings(node.NodeId,"calculating"))'=1)){
set endSiblingNodeId = ..OrderSiblings(node.NodeId)
set startSiblingNodeId = node.NodeId
set indexRef = ..OrderSiblings(node.NodeId,"index")
set masRef = ..OrderSiblings(node.NodeId,"mas")
s ..OrderSiblings(startSiblingNodeId, "calculating")=1
do ..WriteDebug("Loop through array "_masRef_" "_indexRef)
do ..DeclareIndex(indexRef)
//Loop through array using declared index
set infiniteLoopProtector = 0
for{
try{
set index = ..OrderMas(masRef, indexRef)
set infiniteLoopProtector = infiniteLoopProtector + 1
do ..WriteDebug("Inside the array "_masRef_" index "_indexRef_" "_index)
}
catch exeption{
set error = "Error while order processing: "_..OrderSiblings(node.NodeId,"mas")_$C(13,10)_exeption.DisplayString()
}
quit:index=""
quit:error'=""
if infiniteLoopProtector > $G(^DocumentPatternSettings("maxOrderIterations"),"128000"){
set error="Infinite loop protection, number of iterations is more than "_$G(^DocumentPatternSettings("maxOrderIterations"),"128000")
quit
}
set siblingsExist = 1
// Skip first sibling if it is empty
if ..IsNodeEmpty(node){
set siblingsExist = node.MoveToNextSibling()
}
// loop through siblings
while siblingsExist{
// Skip last sibling if it is empty
if node.NodeId=endSiblingNodeId && ..IsNodeEmpty(node){
quit
}
set error = ..Processing(.node)
if error'="" quit
set siblingsExist = node.MoveToNextSibling()
}
quit:error'=""
// Before the next repetition go to the first sibling
set node = ..XMLDocument.GetNode(startSiblingNodeId)
}
quit:error'="" error
s ..OrderSiblings(startSiblingNodeId, "calculating")=0
do ..KillIndex(indexRef)
// After the order the last sibling became current node
set node = ..XMLDocument.GetNode(endSiblingNodeId)
quit ""
}
if error'="" quit error
// Processing set
if ($D(..SetTags(node.NodeId)))
{
try{
set value = ..EvaluateExpression(..SetTags(node.NodeId,"expression"))
}
catch exeption{
set error = "Error while evaluating 'set' expression : "_..SetTags(node.NodeId,"expression")_$C(13,10)_exeption.DisplayString()
}
quit:error'="" error
try{
d ..SetVariable(..SetTags(node.NodeId,"variable"),value)
}
catch exeption{
set error = "Error while variable assignment: "_..SetTags(node.NodeId,"variable")_$C(13,10)_exeption.DisplayString()
}
}
// Skip nodes marked as empty
if $D(..SkipIfEmptyNodes(node.NodeId))&&..IsNodeEmpty(node) quit ""
set error = ..WriteAndEvaluateNode1(node)
if error'="" quit error
if node.HasChildNodes(){
d node.MoveToFirstChild()
set siblingsExist = 1
while siblingsExist{
set error = ..Processing(.node)
if error'="" quit
set siblingsExist = node.MoveToNextSibling()
}
d node.MoveToParent()
}
if $G(error)'="" quit error
set error = ..WriteAndEvaluateNode2(node)
if error'="" quit error
quit ""
]]></Implementation>
</Method>
<Method name="WriteAndEvaluateNode1">
<Description>
Write xml node and evaluate it. Used as TraverseXml argument</Description>
<FormalSpec>node:%XML.Node</FormalSpec>
<Implementation><![CDATA[
if node.QName="" quit ""
set error = ""
if node.NodeId=..XMLDocumentMainNodeId{
set sc = ..XMLWriter.RootElement(node.QName)
}
else{
set sc = ..XMLWriter.Element(node.QName)
}
if $$$ISERR(sc) quit $system.OBJ.DisplayError(sc)
set attributeName = node.FirstAttributeName()
//Write all attributes
while attributeName'=""{
set name = node.GetAttributeQName(attributeName)
set value = node.GetAttributeValue(attributeName)
set namespace = node.GetAttributeNamespace(attributeName)
d ..XMLWriter.WriteAttribute(name, value, namespace)
set attributeName = node.NextAttributeName(.attributeName)
}
set textExist = 0
set text=""
try{
set textExist = node.GetText(.text)
}
catch{ }
if textExist{
// if expression then evaluate it
if $match(text,"\{\{.+\}\}"){
set expression= $ZSTRIP($P($P(text,"{{",2),"}}",1),"<>W")
try{
set result = ..EvaluateExpression(expression)
}
catch exeption{
set error="Error while evaluating expression: "_expression_$C(13,10)_exeption.DisplayString()
}
if error'="" quit error
if $E(result,1,6)="XMLMOD"{
set result=$E(result,7,*)
do ..XMLWriter.Write(result)
}
else{
do ..XMLWriter.WriteChars(result)
}
}
else{
do ..XMLWriter.WriteChars(text)
}
}
quit error
]]></Implementation>
</Method>
<Method name="WriteAndEvaluateNode2">
<Description>
End of writing xml node. Used as TraverseXml argument</Description>
<FormalSpec>node:%XML.Node</FormalSpec>
<Implementation><![CDATA[
if node.QName="" quit ""
if node.NodeId=..XMLDocumentMainNodeId{
d ..XMLWriter.EndRootElement()
}
else{
do ..XMLWriter.EndElement()
}
quit ""
]]></Implementation>
</Method>
<Method name="SeparateTags">
<FormalSpec>node:%XML.Node</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
set textExist=0
set text = ""
try{
set textExist = node.GetText(.text, 0 )
}
catch{ }
if 'textExist quit ""
set start = $LOCATE(text,"\{[%\{].*[%\}]\}")
if start=0 quit ""
set tagExist=1
while tagExist{
//finding tag
set start = 0
set end = 0
set tag = ""
set start = $LOCATE(text,"\{[%\{].*[%\}]\}", start, end, tag)
if start=0 quit
// .* - greedy qualifier
// find inside text closing lexem
set closeBracket = $S($E(text,start+1)="{":"\}",1:"%")_"\}" // closeBracket = %} or }}
set closeBracketIndex = $Locate(text, closeBracket,start)
// if closing lexem was found shorten it
if (closeBracketIndex'=0) && ((closeBracketIndex + 2) < end){
set end = closeBracketIndex + 2
set tag = $E(text,start, end-1)
}
// insert text before tag into the xml node before
// all the rest remains in current node
if start>1{
set newNodeText = $E(text,1,start-1)
do ..InsertNodeBefore(.node, newNodeText)
do ..WriteDebug("Text1: "_text_" splitted on "_newNodeText_" and "_$E(text,start,*))
set text=$E(text,start,*)
continue
}
// if characters after tag exists then insert the copy of xml node with tag before the current node
// all the rest remains in current node
if end '> $L(text){
do ..InsertNodeBefore(.node, tag)
do ..WriteDebug("Text2: "_text_" splitted on "_tag_" and "_$E(text,end,*))
set text = $E(text,end,*)
continue
}
set tagExist=0
}
do ..ReplaceNode(.node, text)
quit ""
]]></Implementation>
</Method>
<Method name="InsertNodeBefore">
<Description>
Insert copy of xml node before with given text
Return id of the new node</Description>
<FormalSpec><![CDATA[&node:%XML.Node,text:%String]]></FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
do ..WriteDebug("Insert node before, node name "_node.QName_" , text "_text)
set nodeId = node.NodeId
set QName = node.QName
set localName = node.LocalName
set namespace = node.Namespace
set nodeAttributes = ""
do ..SaveNodeAttributes(.node, .nodeAttributes)
d node.MoveToParent()
set text = ..Replaceon160(text)
set newNodeId = node.InsertElement(localName, namespace, nodeId, text, QName)
// Back on current node
set node = ..XMLDocument.GetNode(newNodeId)
set attributeName = ""
do ..SetAttributesToNode(.node, .nodeAttributes)
// Node wil be the same as before
d node.MoveToNextSibling()
do ..SetAttributesToNode(.node, .nodeAttributes)
quit newNodeId
]]></Implementation>
</Method>
<Method name="SetAttributesToNode">
<Description>
Set node attributes
nodeAttributes - array of node attributes</Description>
<FormalSpec><![CDATA[&node:%XML.Node,&nodeAttributes]]></FormalSpec>
<Implementation><![CDATA[
set attributeName = ""
for{
set attributeName = $O(nodeAttributes(attributeName))
quit:attributeName=""
set name = nodeAttributes(attributeName,"name")
set value = nodeAttributes(attributeName,"value")
set namespace = nodeAttributes(attributeName,"namespace")
do node.SetAttribute(name, namespace, value)
}
]]></Implementation>
</Method>
<Method name="SaveNodeAttributes">
<Description>
Save node attributes in array nodeAttributes</Description>
<FormalSpec><![CDATA[&node:%XML.Node,&nodeAttributes]]></FormalSpec>
<Implementation><![CDATA[
set attributeName = node.FirstAttributeName()
set nodeAttributes = ""
while attributeName'=""{
set name = node.GetAttributeQName(attributeName)
set value = node.GetAttributeValue(attributeName)
set namespace = node.GetAttributeNamespace(attributeName)
set nodeAttributes(attributeName, "name") = name
set nodeAttributes(attributeName, "value") = value
set nodeAttributes(attributeName, "namespace") = namespace
set attributeName = node.NextAttributeName(attributeName)
}
]]></Implementation>
</Method>
<Method name="ReplaceNode">
<FormalSpec><![CDATA[&node:%XML.Node,text:%String=""]]></FormalSpec>
<Implementation><![CDATA[
set nodeAttributes = ""
do ..SaveNodeAttributes(.node, .nodeAttributes)
// ending space replaced whith nonbreaking space
set text = ..Replaceon160(text)
do node.ReplaceElement(node.LocalName, node.Namespace, text, node.QName)
do ..SetAttributesToNode(.node, .nodeAttributes)
]]></Implementation>
</Method>
<Method name="Replaceon160">
<FormalSpec>text</FormalSpec>
<Implementation><![CDATA[
if $E(text)=" "{
set $E(text) = $C(160)
}
if $E(text,*)=" "{
set $E(text, *) = $C(160)
}
quit text
]]></Implementation>
</Method>
<Method name="SyntaxParse">
<Description>
Syntax parsers. Used as TraverseXml argument
Check the sytax, saves tag information</Description>
<FormalSpec>node:%XML.Node</FormalSpec>
<ReturnType>%String</ReturnType>
<Implementation><![CDATA[
set textExist = 0
set text = ""
try{
set textExist = node.GetText(.text, 0 )
}
catch{ }
if 'textExist quit ""
set isFound = $match(text,"\{%.*%\}")
if 'isFound quit ""
set variableRegExp = "([A-Za-z]+[A-Za-z0-9]*"_"(\(.+\)){0,1})"
set variableRegExpWithDots = "([A-Za-z]+[A-Za-z0-9\.]*"_"(\(.+\)){0,1})"
set globalVariableRegExp = "((\^)|(\^\|\|)|(\^\|\"".+\""\|))"_variableRegExpWithDots
set anyVariableRegExp = "(("_globalVariableRegExp_")|("_variableRegExp_"))"
set numberRegExp = "\d+(\.\d+)?"
set beginOfEndOrderRegExp = "\{%"_"\s*"_"(?i)endorder(?-i)"
set beginOfOrderRegExp = "\{%"_"\s*"_"(?i)order(?-i)"
set beginOfEndIfRegExp = "\{%"_"\s*"_"(?i)endif(?-i)"
set beginOfIfRegExp = "\{%"_"\s*"_"(?i)if(?-i)"
set beginOfSetRegExp = "\{%"_"\s*"_"(?i)set(?-i)\s+"
// Check 'endorder' tag
if $locate(text, beginOfEndOrderRegExp)'=0{
set fullEndOrderRegExp = beginOfEndOrderRegExp_"\s*"_"%\}"
set isEndOrderCorrect = $match(text, fullEndOrderRegExp)
if 'isEndOrderCorrect quit "Incorrect sytax of 'endorder' tag: "_text
set currentTagType = $$$PopFromStack(..TagTypeStack)
if currentTagType="" quit "Unexpected 'endorder', must be preceded by 'order'"
if currentTagType'="order" quit "Unexpected 'endorder', expecting closing of "_currentTagType
set orderTagNodeId = ..PopFromOrderStack()
set ..OrderTags(orderTagNodeId) = node.NodeId