source: ccr/trunk/p/C0CXPATH.m@ 690

Last change on this file since 690 was 664, checked in by George Lilly, 15 years ago

bug in PARY

File size: 24.7 KB
RevLine 
[508]1C0CXPATH ; CCDCCR/GPL - XPATH XML manipulation utilities; 6/1/08
2 ;;1.0;C0C;;May 19, 2009;
[391]3 ;Copyright 2008 George Lilly. Licensed under the terms of the GNU
4 ;General Public License See attached copy of the License.
5 ;
6 ;This program is free software; you can redistribute it and/or modify
7 ;it under the terms of the GNU General Public License as published by
8 ;the Free Software Foundation; either version 2 of the License, or
9 ;(at your option) any later version.
10 ;
11 ;This program is distributed in the hope that it will be useful,
12 ;but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;GNU General Public License for more details.
15 ;
16 ;You should have received a copy of the GNU General Public License along
17 ;with this program; if not, write to the Free Software Foundation, Inc.,
18 ;51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ;
20 W "This is an XML XPATH utility library",!
21 W !
22 Q
23 ;
[508]24OUTPUT(OUTARY,OUTNAME,OUTDIR) ; WRITE AN ARRAY TO A FILE
[391]25 ;
26 N Y
27 S Y=$$GTF^%ZISH(OUTARY,$QL(OUTARY),OUTDIR,OUTNAME)
28 I Y Q 1_U_"WROTE FILE: "_OUTNAME_" TO "_OUTDIR
29 I 'Y Q 0_U_"ERROR WRITING FILE"_OUTNAME_" TO "_OUTDIR
30 Q
31 ;
[508]32PUSH(STK,VAL) ; pushs VAL onto STK and updates STK(0)
[391]33 ; VAL IS A STRING AND STK IS PASSED BY NAME
34 ;
35 I '$D(@STK@(0)) S @STK@(0)=0 ; IF THE ARRAY IS EMPTY, INITIALIZE
36 S @STK@(0)=@STK@(0)+1 ; INCREMENT ARRAY DEPTH
37 S @STK@(@STK@(0))=VAL ; PUT VAL A THE END OF THE ARRAY
38 Q
39 ;
[508]40POP(STK,VAL) ; POPS THE LAST VALUE OFF THE STK AND RETURNS IT IN VAL
[391]41 ; VAL AND STK ARE PASSED BY REFERENCE
42 ;
43 I @STK@(0)<1 D ; IF ARRAY IS EMPTY
44 . S VAL=""
45 . S @STK@(0)=0
46 I @STK@(0)>0 D ;
47 . S VAL=@STK@(@STK@(0))
48 . K @STK@(@STK@(0))
49 . S @STK@(0)=@STK@(0)-1 ; NEW DEPTH OF THE ARRAY
50 Q
51 ;
[508]52PUSHA(ADEST,ASRC) ; PUSH ASRC ONTO ADEST, BOTH PASSED BY NAME
[391]53 ;
54 N ZGI
55 F ZGI=1:1:@ASRC@(0) D ; FOR ALL OF THE SOURCE ARRAY
56 . D PUSH(ADEST,@ASRC@(ZGI)) ; PUSH ONE ELEMENT
57 Q
58 ;
[570]59MKMDX(STK,RTN,INREDUX) ; MAKES A MUMPS INDEX FROM THE ARRAY STK
[391]60 ; RTN IS SET TO //FIRST/SECOND/THIRD FOR THREE ARRAY ELEMENTS
[570]61 ; REDUX IS A STRING TO REMOVE FROM THE RESULT
[391]62 S RTN=""
63 N I
64 ; W "STK= ",STK,!
65 I @STK@(0)>0 D ; IF THE ARRAY IS NOT EMPTY
66 . S RTN="//"_@STK@(1) ; FIRST ELEMENT NEEDS NO SEMICOLON
67 . I @STK@(0)>1 D ; SUBSEQUENT ELEMENTS NEED A SEMICOLON
68 . . F I=2:1:@STK@(0) S RTN=RTN_"/"_@STK@(I)
[570]69 I $G(INREDUX)'="" S RTN=$P(RTN,INREDUX,1)_$P(RTN,INREDUX,2)
[391]70 Q
71 ;
[508]72XNAME(ISTR) ; FUNCTION TO EXTRACT A NAME FROM AN XML FRAG
[391]73 ; </NAME> AND <NAME ID=XNAME> WILL RETURN NAME
74 ; ISTR IS PASSED BY VALUE
75 N CUR,TMP
76 I ISTR?.E1"<".E D ; STRIP OFF LEFT BRACKET
77 . S TMP=$P(ISTR,"<",2)
78 I TMP?1"/".E D ; ALSO STRIP OFF SLASH IF PRESENT IE </NAME>
79 . S TMP=$P(TMP,"/",2)
80 S CUR=$P(TMP,">",1) ; EXTRACT THE NAME
81 ; W "CUR= ",CUR,!
82 I CUR?.1"_"1.A1" ".E D ; CONTAINS A BLANK IE NAME ID=TEST>
83 . S CUR=$P(CUR," ",1) ; STRIP OUT BLANK AND AFTER
84 ; W "CUR2= ",CUR,!
85 Q CUR
86 ;
[512]87XVAL(ISTR) ; EXTRACTS THE VALUE FROM A FRAGMENT OF XML
88 ; <NAME>VALUE</NAME> WILL RETURN VALUE
89 N G
90 S G=$P(ISTR,">",2) ;STRIP OFF <NAME>
91 Q $P(G,"<",1) ; STRIP OFF </NAME> LEAVING VALUE
92 ;
93VDX2VDV(OUTVDV,INVDX) ; CONVERT AN VDX ARRAY TO VDV
94 ; VDX: @INVDX@(XPATH)=VALUE
95 ; VDV: @OUTVDV@(X1X2X3X4)=VALUE
96 ; THE VDV DATANAMES MIGHT BE MORE CONVENIENT FOR USE IN CODE
97 ; AN INDEX IS PROVIDED TO GO BACK TO VDX FOR CONVERSIONS
98 ; @VDV@("XPATH",X1X2X3X4)="XPATH"
99 N ZA,ZI,ZW
100 S ZI=""
101 F S ZI=$O(@INVDX@(ZI)) Q:ZI="" D ;
102 . S ZW=$TR(ZI,"/","") ; ELIMINATE ALL SLASHES - CAMEL CASE VARIABLE NAME
103 . W ZW,!
104 . S @OUTVDV@(ZW)=@INVDX@(ZI)
105 . S @OUTVDV@("XPATH",ZW)=ZI
106 Q
107 ;
[513]108VDX2XPG(OUTXPG,INVDX) ; CONVERT AN VDX ARRAY TO XPG
[512]109 ; VDX: @VDX@(XPATH)=VALUE
110 ; XPG: @(VDX(X1,X2,X3,X4))@=VALUE
111 ; THIS IS A STEP TOWARD GENERATING XML FROM A VDX
112 N ZA,ZI,ZW
113 S ZI=""
114 F S ZI=$O(@INVDX@(ZI)) Q:ZI="" D ;
115 . S ZW=$E(ZI,3,$L(ZI)) ; STRIP OFF INITIAL //
116 . S ZW2=$P(ZW,"/",1)
117 . F ZK=1:1:$L(ZW,"/") D PUSH("ZA",$P(ZW,"/",ZK))
[528]118 . ;ZWR ZA
[512]119 . S ZW2=ZA(1)
120 . F ZK=2:1:ZA(0) D ;
121 . . S ZW2=ZW2_""","""_ZA(ZK)
122 . K ZA
123 . S ZW2=""""_ZW2_""""
124 . W ZW2,!
125 . S ZN=OUTXPG_"("_ZW2_")"
126 . S @ZN=@INVDX@(ZI)
127 Q
128 ;
[513]129XML2XPG(OUTXPG,INXML) ; CONVERT AN XML ARRAY, PASSED BY NAME TO AN XPG ARRAY
130 ; XPG MEANS XPATH GLOBAL AND HAS THE FORM @OUTXPG@("X1","X2","X3")=VALUE
131 ;
132 ;N G1
[528]133 D INDEX(INXML,"G1",1) ; PRODUCES A VDX ARRAY IN G1, NO INDEX IS PRODUCED
[513]134 D VDX2XPG(OUTXPG,"G1") ; CONVERTS THE VDX ARRAY TO XPG FORM
135 Q
136 ;
137DO
138 D XPG2XML("^GPL2B","^GPL2A")
139 Q
140 ;
[528]141T1 ; TEST OUT THESE ROUTINES
142 D XML2XPG("G2","^GPL")
143 D XPG2XML("G3","G2")
144 K ^GPLOUT
145 M ^GPLOUT=G3
146 W $$OUTPUT^C0CXPATH("^GPLOUT(1)","GPLTEST.xml","/home/vademo2/EHR/p")
[513]147 Q
148 ;
[528]149XPG2XML(OUTXML,INXPG) ;
150 N C0CN,FWD,ZA,G,GA,ZQ
151 S ZQ=0 ; QUIT FLAG
152 F Q:ZQ=1 D ; LOOP THROUGH EVERYTHING
153 . I '$D(C0CN) D ; FIRST TIME THROUGH
154 . . K @OUTXML ; MAKE SURE OUTPUT ARRAY IS CLEAR
155 . . S FWD=1 ; START OUT GOING FORWARD THROUGH SUBSCRIPTS
156 . . S G=$Q(@INXPG) ; THIS ONE
157 . . S GN=$Q(@G) ; NEXT ONE
158 . . S C0CN=1 ; SUBSCRIPT COUNT
159 . . S ZQ=0 ; QUIT FLAG
160 . . D ZXO("?xml version=""1.0"" encoding=""UTF-8""?") ;MAKE IT REAL XML
161 . . I $QS(G,1)="ContinuityOfCareRecord" D ;
162 . . . D ZXO("?xml-stylesheet type=""text/xsl"" href=""ccr.xsl""?") ; HACK TO MAKE THE CCR STYLESHEET WORK
163 . I FWD D ; GOING FORWARDS
164 . . I C0CN<$QL(G) D ; NOT A DATA NODE
165 . . . S ZA=$QS(G,C0CN) ; PULL OUT THE SUBSCRIPT
166 . . . D ZXO(ZA) ; AND OPEN AN XML ELEMENT
167 . . . I @OUTXML@(@OUTXML@(0))="<ContinuityOfCareRecord>" D ;
168 . . . . S @OUTXML@(@OUTXML@(0))="<ContinuityOfCareRecord xmlns=""urn:astm-org:CCR"">"
169 . . . S C0CN=C0CN+1 ; MOVE TO THE NEXT ONE
170 . . E D ; AT THE DATA NODE
171 . . . S ZA=$QS(G,C0CN) ; PULL OUT THE SUBSCRIPT
172 . . . D ZXVAL(ZA,@G) ; OUTPUT <X>VAL</X> FOR DATA NODE
173 . . . S FWD=0 ; GO BACKWARDS
174 . I 'FWD D ;GOING BACKWARDS
175 . . S GN=$Q(@G) ;NEXT XPATH
176 . . ;W "NEXT!",GN,!
177 . . S C0CN=C0CN-1 ; PREVIOUS SUBSCRIPT
178 . . I GN'="" D ;
179 . . . I $QS(G,C0CN)'=$QS(GN,C0CN) D ; NEED TO CLOSE OFF ELEMENT
180 . . . . D ZXC($QS(G,C0CN)) ;
181 . . . E I GN'="" D ; MORE ELEMENTS AT THIS LEVEL
182 . . . . S G=$Q(@G) ; ADVANCE TO NEW XPATH
183 . . . . S C0CN=C0CN+1 ; GET READY TO PROCESS NEXT SUBSCRIPT
184 . . . . S FWD=1 ; GOING FORWARD NOW
185 . I (GN="")&(C0CN=1) D Q ; WHEN WE ARE ALL DONE
186 . . D ZXC($QS(G,C0CN)) ; LAST ONE
187 . . S ZQ=1 ; QUIT NOW
188 Q
189 ;
[515]190ZXO(WHAT)
[513]191 D PUSH("GA",WHAT)
192 D PUSH(OUTXML,"<"_WHAT_">")
193 Q
194 ;
[515]195ZXC(WHAT)
[513]196 D POP("GA",.TMP)
197 D PUSH(OUTXML,"</"_WHAT_">")
198 Q
199 ;
[515]200ZXVAL(WHAT,VAL)
[513]201 D PUSH(OUTXML,"<"_WHAT_">"_VAL_"</"_WHAT_">")
202 Q
203 ;
[570]204INDEX(IZXML,VDX,NOINX,TEMPLATE,REDUX) ; parse XML in IZXML and produce
205 ; an XPATH index; REDUX is a string to be removed from each xpath
[532]206 ; GPL 7/14/09 OPTIONALLY GENERATE AN XML TEMPLATE IF PASSED BY NAME
207 ; TEMPLATE IS IDENTICAL TO THE PARSED XML LINE BY LINE
208 ; EXCEPT THAT DATA VALUES ARE REPLACED WITH @@XPATH@@ FOR THE XPATH OF THE TAG
[512]209 ; GPL 5/24/09 AND OPTIONALLY PRODUCE THE VDX ARRAY PASSED BY NAME
210 ; @VDX@("XPATH")=VALUE
[528]211 ; ex. @IZXML@("XPATH")=FIRSTLINE^LASTLINE
[391]212 ; WHERE FIRSTLINE AND LASTLINE ARE THE BEGINNING AND ENDING OF THE
213 ; XML SECTION
[528]214 ; IZXML IS PASSED BY NAME
215 ; IF NOINX IS SET TO 1, NO INDEX WILL BE GENERATED, BUT THE VDX WILL BE
[512]216 N I,LINE,FIRST,LAST,CUR,TMP,MDX,FOUND,CURVAL,DVDX,LCNT
[391]217 N C0CSTK ; LEAVE OUT FOR DEBUGGING
[570]218 I '$D(REDUX) S REDUX=""
[528]219 I '$D(NOINX) S NOINX=0 ; IF NOT PASSED, GENERATE AN INDEX
220 N ZXML
221 I NOINX S ZXML=$NA(^TMP("C0CINDEX",$J)) ; TEMP PLACE FOR INDEX TO DISCARD
222 E S ZXML=IZXML ; PLACE FOR INDEX TO KEEP
223 I '$D(@IZXML@(0)) D ; IF COUNT NOT IN NODE 0 COUNT THEM
[512]224 . S I="",LCNT=0
[528]225 . F S I=$O(@IZXML@(I)) Q:I="" S LCNT=LCNT+1
226 E S LCNT=@IZXML@(0) ; LINE COUNT PASSED IN ARRAY
[512]227 I LCNT=0 D Q ; NO XML PASSED
[391]228 . W "ERROR IN XML FILE",!
[512]229 S DVDX=0 ; DEFAULT DO NOT PRODUCE VDX INDEX
230 I $D(VDX) S DVDX=1 ; IF NAME PASSED, DO VDX
[391]231 S C0CSTK(0)=0 ; INITIALIZE STACK
[561]232 K LKASD ; KILL LOOKASIDE ARRAY
[557]233 D MKLASD(.LKASD,IZXML) ;MAKE LOOK ASIDE BUFFER FOR MULTIPLES
[512]234 F I=1:1:LCNT D ; PROCESS THE ENTIRE ARRAY
[528]235 . S LINE=@IZXML@(I)
[532]236 . I $D(TEMPLATE) D ;IF TEMPLATE IS REQUESTED
237 . . S @TEMPLATE@(I)=$$CLEAN(LINE)
[391]238 . ;W LINE,!
239 . S FOUND=0 ; INTIALIZED FOUND FLAG
240 . I LINE?.E1"<!".E S FOUND=1 ; SKIP OVER COMMENTS
241 . I FOUND'=1 D
242 . . I (LINE?.E1"<"1.E1"</".E)!(LINE?.E1"<"1.E1"/>".E) D
243 . . . ; THIS IS THE CASE THERE SECTION BEGINS AND ENDS
244 . . . ; ON THE SAME LINE
245 . . . ; W "FOUND ",LINE,!
246 . . . S FOUND=1 ; SET FOUND FLAG
247 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
[557]248 . . . S CUR=CUR_$G(LKASD(CUR,I)) ; HANDLE MULTIPLES
[391]249 . . . D PUSH("C0CSTK",CUR) ; ADD TO THE STACK
[570]250 . . . D MKMDX("C0CSTK",.MDX,REDUX) ; GENERATE THE M INDEX
[391]251 . . . ; W "MDX=",MDX,!
252 . . . I $D(@ZXML@(MDX)) D ; IN THE INDEX, IS A MULTIPLE
[531]253 . . . . ;I '$D(ZDUP(MDX)) S ZDUP(MDX)=2
254 . . . . ;E S ZDUP(MDX)=ZDUP(MDX)+1
255 . . . . ;W "DUP:",MDX,!
256 . . . . ;I '$D(CURVAL) S CURVAL=""
257 . . . . ;I DVDX S @VDX@(MDX_"["_ZDUP(MDX)_"]")=CURVAL
[391]258 . . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
259 . . . I '$D(@ZXML@(MDX)) D ; NOT IN THE INDEX, NOT A MULTIPLE
260 . . . . S @ZXML@(MDX)=I_"^"_I ; ADD INDEX ENTRY-FIRST AND LAST
[512]261 . . . . S CURVAL=$$XVAL(LINE) ; VALUE
262 . . . . S $P(@ZXML@(MDX),"^",3)=CURVAL ; THIRD PIECE
263 . . . . I DVDX S @VDX@(MDX)=CURVAL ; FILL IN VDX ARRAY IF REQUESTED
[532]264 . . . . I $D(TEMPLATE) D ; IF TEMPLATE IS REQUESTED
265 . . . . . S LINE=$$CLEAN(LINE) ; CLEAN OUT CONTROL CHARACTERS
266 . . . . . S @TEMPLATE@(I)=$P(LINE,">",1)_">@@"_MDX_"@@</"_$P(LINE,"</",2)
[391]267 . . . D POP("C0CSTK",.TMP) ; REMOVE FROM STACK
268 . I FOUND'=1 D ; THE LINE DOESN'T CONTAIN THE START AND END
269 . . I LINE?.E1"</"1.E D ; LINE CONTAINS END OF A SECTION
270 . . . ; W "FOUND ",LINE,!
271 . . . S FOUND=1 ; SET FOUND FLAG
272 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
273 . . . D MKMDX("C0CSTK",.MDX) ; GENERATE THE M INDEX
274 . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
275 . . . D POP("C0CSTK",.TMP) ; REMOVE FROM STACK
[557]276 . . . S TMP=$P(TMP,"[",1) ; REMOVE [X] FROM MULTIPLE
[391]277 . . . I TMP'=CUR D ; MALFORMED XML, END MUST MATCH START
278 . . . . W "MALFORMED XML ",CUR,"LINE "_I_LINE,!
279 . . . . D PARY("C0CSTK") ; PRINT OUT THE STACK FOR DEBUGING
280 . . . . Q
281 . I FOUND'=1 D ; THE LINE MIGHT CONTAIN A SECTION BEGINNING
282 . . I (LINE?.E1"<"1.E)&(LINE'["?>") D ; BEGINNING OF A SECTION
283 . . . ; W "FOUND ",LINE,!
284 . . . S FOUND=1 ; SET FOUND FLAG
285 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
[557]286 . . . S CUR=CUR_$G(LKASD(CUR,I)) ; HANDLE MULTIPLES
[391]287 . . . D PUSH("C0CSTK",CUR) ; ADD TO THE STACK
288 . . . D MKMDX("C0CSTK",.MDX) ; GENERATE THE M INDEX
289 . . . ; W "MDX=",MDX,!
290 . . . I $D(@ZXML@(MDX)) D ; IN THE INDEX, IS A MULTIPLE
291 . . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
[557]292 . . . . ;B
[391]293 . . . I '$D(@ZXML@(MDX)) D ; NOT IN THE INDEX, NOT A MULTIPLE
294 . . . . S @ZXML@(MDX)=I_"^" ; INSERT INTO THE INDEX
295 S @ZXML@("INDEXED")=""
[512]296 S @ZXML@("//")="1^"_LCNT ; ROOT XPATH
[528]297 I NOINX K @ZXML ; DELETE UNWANTED INDEX
[391]298 Q
299 ;
[557]300MKLASD(OUTBUF,INARY) ; CREATE A LOOKASIDE BUFFER FOR MULTILPLES
301 ;
302 N ZI,ZN,ZA,ZLINE,ZLINE2,CUR,CUR2
303 F ZI=1:1:LCNT-1 D ; PROCESS THE ENTIRE ARRAY
304 . S ZLINE=@IZXML@(ZI)
305 . I ZI<LCNT S ZLINE2=@IZXML@(ZI+1)
306 . I ZLINE?.E1"</"1.E D ; NEXT LINE CONTAINS END OF A SECTION
307 . . S CUR=$$XNAME(ZLINE) ; EXTRACT THE NAME
308 . . I (ZLINE2?.E1"<"1.E)&(ZLINE'["?>") D ; BEGINNING OF A SECTION
309 . . . S CUR2=$$XNAME(ZLINE2) ; EXTRACT THE NAME
310 . . . I CUR=CUR2 D ; IF THIS IS A MULTIPLE
311 . . . . S OUTBUF(CUR,ZI+1)=""
312 ;ZWR OUTBUF
313 S ZI=""
314 F S ZI=$O(OUTBUF(ZI)) Q:ZI="" D ; FOR EACH KIND OF MULTIPLE
315 . S ZN=$O(OUTBUF(ZI,"")) ; LINE NUMBER OF SECOND MULTIPLE
[561]316 . F S ZN=$O(@IZXML@(ZN),-1) Q:ZN="" I $E($P(@IZXML@(ZN),"<"_ZI,2),1,1)=">" Q ;
[557]317 . S OUTBUF(ZI,ZN)=""
318 S ZA=1,ZI="",ZN=""
319 F S ZI=$O(OUTBUF(ZI)) Q:ZI="" D ; ADDING THE COUNT FOR THE MULIPLES [x]
320 . S ZN="",ZA=1
321 . F S ZN=$O(OUTBUF(ZI,ZN)) Q:ZN="" D ;
322 . . S OUTBUF(ZI,ZN)="["_ZA_"]"
323 . . S ZA=ZA+1
324 Q
325 ;
[532]326CLEAN(STR) ; extrinsic function; returns string
327 ;; Removes all non printable characters from a string.
328 ;; STR by Value
329 N TR,I
330 F I=0:1:31 S TR=$G(TR)_$C(I)
331 S TR=TR_$C(127)
332 QUIT $TR(STR,TR)
333 ;
[508]334QUERY(IARY,XPATH,OARY) ; RETURNS THE XML ARRAY MATCHING THE XPATH EXPRESSION
[391]335 ; XPATH IS OF THE FORM "//FIRST/SECOND/THIRD"
336 ; IARY AND OARY ARE PASSED BY NAME
337 I '$D(@IARY@("INDEXED")) D ; INDEX IS NOT PRESENT IN IARY
338 . D INDEX(IARY) ; GENERATE AN INDEX FOR THE XML
339 N FIRST,LAST ; FIRST AND LAST LINES OF ARRAY TO RETURN
340 N TMP,I,J,QXPATH
341 S FIRST=1
342 S LAST=@IARY@(0) ; FIRST AND LAST DEFAULT TO ROOT
343 I XPATH'="//" D ; NOT A ROOT QUERY
344 . S TMP=@IARY@(XPATH) ; LOOK UP LINE VALUES
345 . S FIRST=$P(TMP,"^",1)
346 . S LAST=$P(TMP,"^",2)
347 K @OARY
348 S @OARY@(0)=+LAST-FIRST+1
349 S J=1
350 FOR I=FIRST:1:LAST D
351 . S @OARY@(J)=@IARY@(I) ; COPY THE LINE TO OARY
352 . S J=J+1
353 ; ZWR OARY
354 Q
355 ;
[508]356XF(IDX,XPATH) ; EXTRINSIC TO RETURN THE STARTING LINE FROM AN XPATH
[391]357 ; INDEX WITH TWO PIECES START^FINISH
358 ; IDX IS PASSED BY NAME
359 Q $P(@IDX@(XPATH),"^",1)
360 ;
[508]361XL(IDX,XPATH) ; EXTRINSIC TO RETURN THE LAST LINE FROM AN XPATH
[391]362 ; INDEX WITH TWO PIECES START^FINISH
363 ; IDX IS PASSED BY NAME
364 Q $P(@IDX@(XPATH),"^",2)
365 ;
[508]366START(ISTR) ; EXTRINSIC TO RETURN THE STARTING LINE FROM AN INDEX
[391]367 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
368 ; COMPANION TO FINISH ; IDX IS PASSED BY NAME
369 Q $P(ISTR,";",2)
370 ;
[508]371FINISH(ISTR) ; EXTRINSIC TO RETURN THE LAST LINE FROM AN INDEX
[391]372 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
373 Q $P(ISTR,";",3)
374 ;
[508]375ARRAY(ISTR) ; EXTRINSIC TO RETURN THE ARRAY REFERENCE FROM AN INDEX
[391]376 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
377 Q $P(ISTR,";",1)
378 ;
[508]379BUILD(BLIST,BDEST) ; A COPY MACHINE THAT TAKE INSTRUCTIONS IN ARRAY BLIST
[391]380 ; WHICH HAVE ARRAY;START;FINISH AND COPIES THEM TO DEST
381 ; DEST IS CLEARED TO START
382 ; USES PUSH TO DO THE COPY
383 N I
384 K @BDEST
385 F I=1:1:@BLIST@(0) D ; FOR EACH INSTRUCTION IN BLIST
386 . N J,ATMP
387 . S ATMP=$$ARRAY(@BLIST@(I))
[601]388 . I $G(DEBUG) W "ATMP=",ATMP,!
389 . I $G(DEBUG) W @BLIST@(I),!
[391]390 . F J=$$START(@BLIST@(I)):1:$$FINISH(@BLIST@(I)) D ;
391 . . ; FOR EACH LINE IN THIS INSTR
[601]392 . . I $G(DEBUG) W "BDEST= ",BDEST,!
393 . . I $G(DEBUG) W "ATMP= ",@ATMP@(J),!
[391]394 . . D PUSH(BDEST,@ATMP@(J))
395 Q
396 ;
[508]397QUEUE(BLST,ARRAY,FIRST,LAST) ; ADD AN ENTRY TO A BLIST
[391]398 ;
[570]399 I $G(DEBUG) W "QUEUEING ",BLST,!
[391]400 D PUSH(BLST,ARRAY_";"_FIRST_";"_LAST)
401 Q
402 ;
[508]403CP(CPSRC,CPDEST) ; COPIES CPSRC TO CPDEST BOTH PASSED BY NAME
[391]404 ; KILLS CPDEST FIRST
405 N CPINSTR
[601]406 I $G(DEBUG) W "MADE IT TO COPY",CPSRC,CPDEST,!
[391]407 I @CPSRC@(0)<1 D ; BAD LENGTH
408 . W "ERROR IN COPY BAD SOURCE LENGTH: ",CPSRC,!
409 . Q
410 ; I '$D(@CPDEST@(0)) S @CPDEST@(0)=0 ; IF THE DEST IS EMPTY, INIT
411 D QUEUE("CPINSTR",CPSRC,1,@CPSRC@(0)) ; BLIST FOR ENTIRE ARRAY
412 D BUILD("CPINSTR",CPDEST)
413 Q
414 ;
[508]415QOPEN(QOBLIST,QOXML,QOXPATH) ; ADD ALL BUT THE LAST LINE OF QOXML TO QOBLIST
[391]416 ; WARNING NEED TO DO QCLOSE FOR SAME XML BEFORE CALLING BUILD
417 ; QOXPATH IS OPTIONAL - WILL OPEN INSIDE THE XPATH POINT
418 ; USED TO INSERT CHILDREN NODES
419 I @QOXML@(0)<1 D ; MALFORMED XML
420 . W "MALFORMED XML PASSED TO QOPEN: ",QOXML,!
421 . Q
[601]422 I $G(DEBUG) W "DOING QOPEN",!
[391]423 N S1,E1,QOT,QOTMP
424 S S1=1 ; OPEN FROM THE BEGINNING OF THE XML
425 I $D(QOXPATH) D ; XPATH PROVIDED
426 . D QUERY(QOXML,QOXPATH,"QOT") ; INSURE INDEX
427 . S E1=$P(@QOXML@(QOXPATH),"^",2)-1
428 I '$D(QOXPATH) D ; NO XPATH PROVIDED, OPEN AT ROOT
429 . S E1=@QOXML@(0)-1
430 D QUEUE(QOBLIST,QOXML,S1,E1)
431 ; S QOTMP=QOXML_"^"_S1_"^"_E1
432 ; D PUSH(QOBLIST,QOTMP)
433 Q
434 ;
[508]435QCLOSE(QCBLIST,QCXML,QCXPATH) ; CLOSE XML AFTER A QOPEN
[391]436 ; ADDS THE LIST LINE OF QCXML TO QCBLIST
437 ; USED TO FINISH INSERTING CHILDERN NODES
438 ; QCXPATH IS OPTIONAL - IF PROVIDED, WILL CLOSE UNTIL THE END
439 ; IF QOPEN WAS CALLED WITH XPATH, QCLOSE SHOULD BE TOO
440 I @QCXML@(0)<1 D ; MALFORMED XML
441 . W "MALFORMED XML PASSED TO QCLOSE: ",QCXML,!
[601]442 I $G(DEBUG) W "GOING TO CLOSE",!
[391]443 N S1,E1,QCT,QCTMP
444 S E1=@QCXML@(0) ; CLOSE UNTIL THE END OF THE XML
445 I $D(QCXPATH) D ; XPATH PROVIDED
446 . D QUERY(QCXML,QCXPATH,"QCT") ; INSURE INDEX
447 . S S1=$P(@QCXML@(QCXPATH),"^",2) ; REMAINING XML
448 I '$D(QCXPATH) D ; NO XPATH PROVIDED, CLOSE AT ROOT
449 . S S1=@QCXML@(0)
450 D QUEUE(QCBLIST,QCXML,S1,E1)
451 ; D PUSH(QCBLIST,QCXML_";"_S1_";"_E1)
452 Q
453 ;
[508]454INSERT(INSXML,INSNEW,INSXPATH) ; INSERT INSNEW INTO INSXML AT THE
[391]455 ; INSXPATH XPATH POINT INSXPATH IS OPTIONAL - IF IT IS
456 ; OMITTED, INSERTION WILL BE AT THE ROOT
457 ; NOTE INSERT IS NON DESTRUCTIVE AND WILL ADD THE NEW
458 ; XML AT THE END OF THE XPATH POINT
459 ; INSXML AND INSNEW ARE PASSED BY NAME INSXPATH IS A VALUE
460 N INSBLD,INSTMP
[601]461 I $G(DEBUG) W "DOING INSERT ",INSXML,INSNEW,INSXPATH,!
462 I $G(DEBUG) F G1=1:1:@INSXML@(0) W @INSXML@(G1),!
[391]463 I '$D(@INSXML@(0)) D ; INSERT INTO AN EMPTY ARRAY
464 . D CP^C0CXPATH(INSNEW,INSXML) ; JUST COPY INTO THE OUTPUT
465 I $D(@INSXML@(0)) D ; IF ORIGINAL ARRAY IS NOT EMPTY
466 . I $D(INSXPATH) D ; XPATH PROVIDED
467 . . D QOPEN("INSBLD",INSXML,INSXPATH) ; COPY THE BEFORE
[601]468 . . I $G(DEBUG) D PARY^C0CXPATH("INSBLD")
[391]469 . I '$D(INSXPATH) D ; NO XPATH PROVIDED, OPEN AT ROOT
470 . . D QOPEN("INSBLD",INSXML,"//") ; OPEN WITH ROOT XPATH
471 . D QUEUE("INSBLD",INSNEW,1,@INSNEW@(0)) ; COPY IN NEW XML
472 . I $D(INSXPATH) D ; XPATH PROVIDED
473 . . D QCLOSE("INSBLD",INSXML,INSXPATH) ; CLOSE WITH XPATH
474 . I '$D(INSXPATH) D ; NO XPATH PROVIDED, CLOSE AT ROOT
475 . . D QCLOSE("INSBLD",INSXML,"//") ; CLOSE WITH ROOT XPATH
476 . D BUILD("INSBLD","INSTMP") ; PUT RESULTS IN INDEST
477 . D CP^C0CXPATH("INSTMP",INSXML) ; COPY BUFFER TO SOURCE
478 Q
479 ;
[508]480INSINNER(INNXML,INNNEW,INNXPATH) ; INSERT THE INNER XML OF INNNEW
[391]481 ; INTO INNXML AT THE INNXPATH XPATH POINT
482 ;
483 N INNBLD,UXPATH
484 N INNTBUF
485 S INNTBUF=$NA(^TMP($J,"INNTBUF"))
486 I '$D(INNXPATH) D ; XPATH NOT PASSED
487 . S UXPATH="//" ; USE ROOT XPATH
488 I $D(INNXPATH) S UXPATH=INNXPATH ; USE THE XPATH THAT'S PASSED
489 I '$D(@INNXML@(0)) D ; INNXML IS EMPTY
490 . D QUEUE^C0CXPATH("INNBLD",INNNEW,2,@INNNEW@(0)-1) ; JUST INNER
491 . D BUILD("INNBLD",INNXML)
492 I @INNXML@(0)>0 D ; NOT EMPTY
493 . D QOPEN("INNBLD",INNXML,UXPATH) ;
494 . D QUEUE("INNBLD",INNNEW,2,@INNNEW@(0)-1) ; JUST INNER XML
495 . D QCLOSE("INNBLD",INNXML,UXPATH)
496 . D BUILD("INNBLD",INNTBUF) ; BUILD TO BUFFER
497 . D CP(INNTBUF,INNXML) ; COPY BUFFER TO DEST
498 Q
499 ;
[508]500INSB4(XDEST,XNEW) ; INSERT XNEW AT THE BEGINNING OF XDEST
[391]501 ; BUT XDEST AN XNEW ARE PASSED BY NAME
502 N XBLD,XTMP
503 D QUEUE("XBLD",XDEST,1,1) ; NEED TO PRESERVE SECTION ROOT
504 D QUEUE("XBLD",XNEW,1,@XNEW@(0)) ; ALL OF NEW XML FIRST
505 D QUEUE("XBLD",XDEST,2,@XDEST@(0)) ; FOLLOWED BY THE REST OF SECTION
506 D BUILD("XBLD","XTMP") ; BUILD THE RESULT
507 D CP("XTMP",XDEST) ; COPY TO THE DESTINATION
[601]508 I $G(DEBUG) D PARY("XDEST")
[391]509 Q
510 ;
[508]511REPLACE(REXML,RENEW,REXPATH) ; REPLACE THE XML AT THE XPATH POINT
[391]512 ; WITH RENEW - NOTE THIS WILL DELETE WHAT WAS THERE BEFORE
513 ; REXML AND RENEW ARE PASSED BY NAME XPATH IS A VALUE
514 ; THE DELETED XML IS PUT IN ^TMP($J,"REPLACE_OLD")
515 N REBLD,XFIRST,XLAST,OLD,XNODE,RETMP
516 S OLD=$NA(^TMP($J,"REPLACE_OLD"))
517 D QUERY(REXML,REXPATH,OLD) ; CREATE INDEX, TEST XPATH, MAKE OLD
518 S XNODE=@REXML@(REXPATH) ; PULL OUT FIRST AND LAST LINE PTRS
519 S XFIRST=$P(XNODE,"^",1)
520 S XLAST=$P(XNODE,"^",2)
521 I RENEW="" D ; WE ARE DELETING A SECTION, MUST SAVE THE TAG
522 . D QUEUE("REBLD",REXML,1,XFIRST) ; THE BEFORE
523 . D QUEUE("REBLD",REXML,XLAST,@REXML@(0)) ; THE REST
524 I RENEW'="" D ; NEW XML IS NOT NULL
525 . D QUEUE("REBLD",REXML,1,XFIRST-1) ; THE BEFORE
526 . D QUEUE("REBLD",RENEW,1,@RENEW@(0)) ; THE NEW
527 . D QUEUE("REBLD",REXML,XLAST+1,@REXML@(0)) ; THE REST
[601]528 I $G(DEBUG) W "REPLACE PREBUILD",!
529 I $G(DEBUG) D PARY("REBLD")
[391]530 D BUILD("REBLD","RTMP")
531 K @REXML ; KILL WHAT WAS THERE
532 D CP("RTMP",REXML) ; COPY IN THE RESULT
533 Q
534 ;
[508]535MISSING(IXML,OARY) ; SEARTH THROUGH INXLM AND PUT ANY @@X@@ VARS IN OARY
[391]536 ; W "Reporting on the missing",!
537 ; W OARY
538 I '$D(@IXML@(0)) W "MALFORMED XML PASSED TO MISSING",! Q
539 N I
540 S @OARY@(0)=0 ; INITIALIZED MISSING COUNT
541 F I=1:1:@IXML@(0) D ; LOOP THROUGH WHOLE ARRAY
542 . I @IXML@(I)?.E1"@@".E D ; MISSING VARIABLE HERE
543 . . D PUSH^C0CXPATH(OARY,$P(@IXML@(I),"@@",2)) ; ADD TO OUTARY
544 . . Q
545 Q
546 ;
[532]547MAP(IXML,INARY,OXML) ; SUBSTITUTE MULTIPLE @@X@@ VARS WITH VALUES IN INARY
[391]548 ; AND PUT THE RESULTS IN OXML
[532]549 N XCNT
550 I '$D(DEBUG) S DEBUG=0
551 I '$D(IXML) W "MALFORMED XML PASSED TO MAP",! Q
552 I '$D(@IXML@(0)) D ; INITIALIZE COUNT
553 . S XCNT=$O(@IXML@(""),-1)
554 E S XCNT=@IXML@(0) ;COUNT
[391]555 I $O(@INARY@(""))="" W "EMPTY ARRAY PASSED TO MAP",! Q
556 N I,J,TNAM,TVAL,TSTR
[532]557 S @OXML@(0)=XCNT ; TOTAL LINES IN OUTPUT
558 F I=1:1:XCNT D ; LOOP THROUGH WHOLE ARRAY
[391]559 . S @OXML@(I)=@IXML@(I) ; COPY THE LINE TO OUTPUT
560 . I @OXML@(I)?.E1"@@".E D ; IS THERE A VARIABLE HERE?
561 . . S TSTR=$P(@IXML@(I),"@@",1) ; INIT TO PART BEFORE VARS
562 . . F J=2:2:10 D Q:$P(@IXML@(I),"@@",J+2)="" ; QUIT IF NO MORE VARS
563 . . . I DEBUG W "IN MAPPING LOOP: ",TSTR,!
564 . . . S TNAM=$P(@OXML@(I),"@@",J) ; EXTRACT THE VARIABLE NAME
565 . . . S TVAL="@@"_$P(@IXML@(I),"@@",J)_"@@" ; DEFAULT UNCHANGED
566 . . . I $D(@INARY@(TNAM)) D ; IS THE VARIABLE IN THE MAP?
567 . . . . I '$D(@INARY@(TNAM,"F")) D ; NOT A SPECIAL FIELD
568 . . . . . S TVAL=@INARY@(TNAM) ; PULL OUT MAPPED VALUE
569 . . . . E D DOFLD ; PROCESS A FIELD
570 . . . S TVAL=$$SYMENC^MXMLUTL(TVAL) ;MAKE SURE THE VALUE IS XML SAFE
571 . . . S TSTR=TSTR_TVAL_$P(@IXML@(I),"@@",J+1) ; ADD VAR AND PART AFTER
572 . . S @OXML@(I)=TSTR ; COPY LINE WITH MAPPED VALUES
573 . . I DEBUG W TSTR
574 I DEBUG W "MAPPED",!
575 Q
576 ;
[508]577DOFLD ; PROCESS A FILEMAN FIELD REFERENCED BY A VARIABLE
[391]578 ;
579 Q
580 ;
[508]581TRIM(THEXML) ; TAKES OUT ALL NULL ELEMENTS
[391]582 ; THEXML IS PASSED BY NAME
583 N I,J,TMPXML,DEL,FOUND,INTXT
584 S FOUND=0
585 S INTXT=0
[609]586 I $G(DEBUG) W "DELETING EMPTY ELEMENTS",!
[391]587 F I=1:1:(@THEXML@(0)-1) D ; LOOP THROUGH ENTIRE ARRAY
588 . S J=@THEXML@(I)
589 . I J["<text>" D
590 . . S INTXT=1 ; IN HTML SECTION, DON'T TRIM
[609]591 . . I $G(DEBUG) W "IN HTML SECTION",!
[391]592 . N JM,JP,JPX ; JMINUS AND JPLUS
593 . S JM=@THEXML@(I-1) ; LINE BEFORE
594 . I JM["</text>" S INTXT=0 ; LEFT HTML SECTION,START TRIM
595 . S JP=@THEXML@(I+1) ; LINE AFTER
596 . I INTXT=0 D ; IF NOT IN AN HTML SECTION
597 . . S JPX=$TR(JP,"/","") ; REMOVE THE SLASH
598 . . I J=JPX D ; AN EMPTY ELEMENT ON TWO LINES
[609]599 . . . I $G(DEBUG) W I,J,JP,!
[391]600 . . . S FOUND=1 ; FOUND SOMETHING TO BE DELETED
601 . . . S DEL(I)="" ; SET LINE TO DELETE
602 . . . S DEL(I+1)="" ; SET NEXT LINE TO DELETE
603 . . I J["><" D ; AN EMPTY ELEMENT ON ONE LINE
[609]604 . . . I $G(DEBUG) W I,J,!
[391]605 . . . S FOUND=1 ; FOUND SOMETHING TO BE DELETED
606 . . . S DEL(I)="" ; SET THE EMPTY LINE UP TO BE DELETED
607 . . . I JM=JPX D ;
[609]608 . . . . I $G(DEBUG) W I,JM_J_JPX,!
[391]609 . . . . S DEL(I-1)=""
610 . . . . S DEL(I+1)="" ; SET THE SURROUNDING LINES FOR DEL
611 ; . I J'["><" D PUSH("TMPXML",J)
612 I FOUND D ; NEED TO DELETE THINGS
613 . F I=1:1:@THEXML@(0) D ; COPY ARRAY LEAVING OUT DELELTED LINES
614 . . I '$D(DEL(I)) D ; IF THE LINE IS NOT DELETED
615 . . . D PUSH("TMPXML",@THEXML@(I)) ; COPY TO TMPXML ARRAY
616 . D CP("TMPXML",THEXML) ; REPLACE THE XML WITH THE COPY
617 Q FOUND
618 ;
[508]619UNMARK(XSEC) ; REMOVE MARKUP FROM FIRST AND LAST LINE OF XML
[391]620 ; XSEC IS A SECTION PASSED BY NAME
621 N XBLD,XTMP
622 D QUEUE("XBLD",XSEC,2,@XSEC@(0)-1) ; BUILD LIST FOR INNER XML
623 D BUILD("XBLD","XTMP") ; BUILD THE RESULT
624 D CP("XTMP",XSEC) ; REPLACE PASSED XML
625 Q
626 ;
[663]627PARY(GLO,ZN) ;PRINT AN ARRAY
628 ; IF ZN=-1 NO LINE NUMBERS
[391]629 N I
[663]630 F I=1:1:@GLO@(0) D ;
[664]631 . I $G(ZN)=-1 W @GLO@(I),!
[663]632 . E W I_" "_@GLO@(I),!
[391]633 Q
634 ;
[508]635H2ARY(IARYRTN,IHASH,IPRE) ; CONVERT IHASH TO RETURN ARRAY
[391]636 ; IPRE IS OPTIONAL PREFIX FOR THE ELEMENTS. USED FOR MUPTIPLES 1^"VAR"^VALUE
637 I '$D(IPRE) S IPRE=""
638 N H2I S H2I=""
639 ; W $O(@IHASH@(H2I)),!
640 F S H2I=$O(@IHASH@(H2I)) Q:H2I="" D ; FOR EACH ELEMENT OF THE HASH
641 . I $QS(H2I,$QL(H2I))="M" D Q ; SPECIAL CASE FOR MULTIPLES
642 . . ;W H2I_"^"_@IHASH@(H2I),!
643 . . N IH,IHI
644 . . S IH=$NA(@IHASH@(H2I)) ;
645 . . S IH2A=$O(@IH@("")) ; SKIP OVER MULTIPLE DISCRIPTOR
646 . . S IH2=$NA(@IH@(IH2A)) ; PAST THE "M","DIRETIONS" FOR EXAMPLE
647 . . S IHI="" ; INDEX INTO "M" MULTIPLES
648 . . F S IHI=$O(@IH2@(IHI)) Q:IHI="" D ; FOR EACH SUB-MULTIPLE
649 . . . ; W @IH@(IHI)
650 . . . S IH3=$NA(@IH2@(IHI))
651 . . . ; W "HEY",IH3,!
652 . . . D H2ARY(.IARYRTN,IH3,IPRE_";"_IHI) ; RECURSIVE CALL - INDENTED ELEMENTS
653 . . ; W IH,!
654 . . ; W "C0CZZ",!
655 . . ; W $NA(@IHASH@(H2I)),!
656 . . Q ;
657 . D PUSH(IARYRTN,IPRE_"^"_H2I_"^"_@IHASH@(H2I))
658 . ; W @IARYRTN@(0),!
659 Q
660 ;
[508]661XVARS(XVRTN,XVIXML) ; RETURNS AN ARRAY XVRTN OF ALL UNIQUE VARIABLES
[391]662 ; DEFINED IN INPUT XML XVIXML BY @@VAR@@
663 ; XVRTN AND XVIXML ARE PASSED BY NAME
664 ;
665 N XVI,XVTMP,XVT
666 F XVI=1:1:@XVIXML@(0) D ; FOR ALL LINES OF THE XML
667 . S XVT=@XVIXML@(XVI)
668 . I XVT["@@" S XVTMP($P(XVT,"@@",2))=XVI
669 D H2ARY(XVRTN,"XVTMP")
670 Q
671 ;
[508]672DXVARS(DXIN) ;DISPLAY ALL VARIABLES IN A TEMPLATE
[391]673 ; IF PARAMETERS ARE NULL, DEFAULTS TO CCR TEMPLATE
674 ;
675 N DXUSE,DTMP ; DXUSE IS NAME OF VARIABLE, DTMP IS VARIABLE IF NOT SUPPLIED
676 I DXIN="CCR" D ; NEED TO GO GET CCR TEMPLATE
677 . D LOAD^C0CCCR0("DTMP") ; LOAD CCR TEMPLATE INTO DXTMP
678 . S DXUSE="DTMP" ; DXUSE IS NAME
679 E I DXIN="CCD" D ; NEED TO GO GET CCD TEMPLATE
680 . D LOAD^C0CCCD1("DTMP") ; LOAD CCR TEMPLATE INTO DXTMP
681 . S DXUSE="DTMP" ; DXUSE IS NAME
682 E S DXUSE=DXIN ; IF PASSED THE TEMPLATE TO USE
683 N DVARS ; PUT VARIABLE NAME RESULTS IN ARRAY HERE
684 D XVARS("DVARS",DXUSE) ; PULL OUT VARS
685 D PARY^C0CXPATH("DVARS") ;AND DISPLAY THEM
686 Q
687 ;
[508]688TEST ; Run all the test cases
[391]689 D TESTALL^C0CUNIT("C0CXPAT0")
690 Q
691 ;
[508]692ZTEST(WHICH) ; RUN ONE SET OF TESTS
[391]693 N ZTMP
694 S DEBUG=1
695 D ZLOAD^C0CUNIT("ZTMP","C0CXPAT0")
696 D ZTEST^C0CUNIT(.ZTMP,WHICH)
697 Q
698 ;
[508]699TLIST ; LIST THE TESTS
[391]700 N ZTMP
701 D ZLOAD^C0CUNIT("ZTMP","C0CXPAT0")
702 D TLIST^C0CUNIT(.ZTMP)
703 Q
704 ;
Note: See TracBrowser for help on using the repository browser.