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

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

updates to XML processing

File size: 25.6 KB
Line 
1C0CXPATH ; CCDCCR/GPL - XPATH XML manipulation utilities; 6/1/08
2 ;;1.0;C0C;;May 19, 2009;
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 ;
24OUTPUT(OUTARY,OUTNAME,OUTDIR) ; WRITE AN ARRAY TO A FILE
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 ;
32PUSH(STK,VAL) ; pushs VAL onto STK and updates STK(0)
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 ;
40POP(STK,VAL) ; POPS THE LAST VALUE OFF THE STK AND RETURNS IT IN VAL
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 ;
52PUSHA(ADEST,ASRC) ; PUSH ASRC ONTO ADEST, BOTH PASSED BY NAME
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 ;
59MKMDX(STK,RTN,INREDUX) ; MAKES A MUMPS INDEX FROM THE ARRAY STK
60 ; RTN IS SET TO //FIRST/SECOND/THIRD FOR THREE ARRAY ELEMENTS
61 ; REDUX IS A STRING TO REMOVE FROM THE RESULT
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)
69 I $G(INREDUX)'="" S RTN=$P(RTN,INREDUX,1)_$P(RTN,INREDUX,2)
70 Q
71 ;
72XNAME(ISTR) ; FUNCTION TO EXTRACT A NAME FROM AN XML FRAG
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 ;
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 ;
108VDX2XPG(OUTXPG,INVDX) ; CONVERT AN VDX ARRAY TO XPG
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))
118 . ;ZWR ZA
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 ;
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
133 D INDEX(INXML,"G1",1) ; PRODUCES A VDX ARRAY IN G1, NO INDEX IS PRODUCED
134 D VDX2XPG(OUTXPG,"G1") ; CONVERTS THE VDX ARRAY TO XPG FORM
135 Q
136 ;
137DO
138 D XPG2XML("^GPL2B","^GPL2A")
139 Q
140 ;
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")
147 Q
148 ;
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 ;
190ZXO(WHAT)
191 D PUSH("GA",WHAT)
192 D PUSH(OUTXML,"<"_WHAT_">")
193 Q
194 ;
195ZXC(WHAT)
196 D POP("GA",.TMP)
197 D PUSH(OUTXML,"</"_WHAT_">")
198 Q
199 ;
200ZXVAL(WHAT,VAL)
201 D PUSH(OUTXML,"<"_WHAT_">"_VAL_"</"_WHAT_">")
202 Q
203 ;
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
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
209 ; GPL 5/24/09 AND OPTIONALLY PRODUCE THE VDX ARRAY PASSED BY NAME
210 ; @VDX@("XPATH")=VALUE
211 ; ex. @IZXML@("XPATH")=FIRSTLINE^LASTLINE
212 ; WHERE FIRSTLINE AND LASTLINE ARE THE BEGINNING AND ENDING OF THE
213 ; XML SECTION
214 ; IZXML IS PASSED BY NAME
215 ; IF NOINX IS SET TO 1, NO INDEX WILL BE GENERATED, BUT THE VDX WILL BE
216 N I,LINE,FIRST,LAST,CUR,TMP,MDX,FOUND,CURVAL,DVDX,LCNT
217 N C0CSTK ; LEAVE OUT FOR DEBUGGING
218 I '$D(REDUX) S REDUX=""
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
224 . S I="",LCNT=0
225 . F S I=$O(@IZXML@(I)) Q:I="" S LCNT=LCNT+1
226 E S LCNT=@IZXML@(0) ; LINE COUNT PASSED IN ARRAY
227 I LCNT=0 D Q ; NO XML PASSED
228 . W "ERROR IN XML FILE",!
229 S DVDX=0 ; DEFAULT DO NOT PRODUCE VDX INDEX
230 I $D(VDX) S DVDX=1 ; IF NAME PASSED, DO VDX
231 S C0CSTK(0)=0 ; INITIALIZE STACK
232 K LKASD ; KILL LOOKASIDE ARRAY
233 D MKLASD(.LKASD,IZXML) ;MAKE LOOK ASIDE BUFFER FOR MULTIPLES
234 F I=1:1:LCNT D ; PROCESS THE ENTIRE ARRAY
235 . S LINE=@IZXML@(I)
236 . I $D(TEMPLATE) D ;IF TEMPLATE IS REQUESTED
237 . . S @TEMPLATE@(I)=$$CLEAN(LINE)
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
248 . . . S CUR=CUR_$G(LKASD(CUR,I)) ; HANDLE MULTIPLES
249 . . . D PUSH("C0CSTK",CUR) ; ADD TO THE STACK
250 . . . D MKMDX("C0CSTK",.MDX,REDUX) ; GENERATE THE M INDEX
251 . . . ; W "MDX=",MDX,!
252 . . . I $D(@ZXML@(MDX)) D ; IN THE INDEX, IS A MULTIPLE
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
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
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
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)
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
276 . . . S TMP=$P(TMP,"[",1) ; REMOVE [X] FROM MULTIPLE
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
286 . . . S CUR=CUR_$G(LKASD(CUR,I)) ; HANDLE MULTIPLES
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
292 . . . . ;B
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")=""
296 S @ZXML@("//")="1^"_LCNT ; ROOT XPATH
297 I NOINX K @ZXML ; DELETE UNWANTED INDEX
298 Q
299 ;
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
316 . F S ZN=$O(@IZXML@(ZN),-1) Q:ZN="" I $E($P(@IZXML@(ZN),"<"_ZI,2),1,1)=">" Q ;
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 ;
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 ;
334QUERY(IARY,XPATH,OARY) ; RETURNS THE XML ARRAY MATCHING THE XPATH EXPRESSION
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 I '$D(@IARY@(0)) D ; LINE COUNT NOT IN ZERO NODE
343 . S @IARY@(0)=$O(@IARY@("//"),-1) ; THIS SHOULD USUALLY WORK
344 S LAST=@IARY@(0) ; FIRST AND LAST DEFAULT TO ROOT
345 I XPATH'="//" D ; NOT A ROOT QUERY
346 . S TMP=@IARY@(XPATH) ; LOOK UP LINE VALUES
347 . S FIRST=$P(TMP,"^",1)
348 . S LAST=$P(TMP,"^",2)
349 K @OARY
350 S @OARY@(0)=+LAST-FIRST+1
351 S J=1
352 FOR I=FIRST:1:LAST D
353 . S @OARY@(J)=@IARY@(I) ; COPY THE LINE TO OARY
354 . S J=J+1
355 ; ZWR OARY
356 Q
357 ;
358XF(IDX,XPATH) ; EXTRINSIC TO RETURN THE STARTING LINE FROM AN XPATH
359 ; INDEX WITH TWO PIECES START^FINISH
360 ; IDX IS PASSED BY NAME
361 Q $P(@IDX@(XPATH),"^",1)
362 ;
363XL(IDX,XPATH) ; EXTRINSIC TO RETURN THE LAST LINE FROM AN XPATH
364 ; INDEX WITH TWO PIECES START^FINISH
365 ; IDX IS PASSED BY NAME
366 Q $P(@IDX@(XPATH),"^",2)
367 ;
368START(ISTR) ; EXTRINSIC TO RETURN THE STARTING LINE FROM AN INDEX
369 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
370 ; COMPANION TO FINISH ; IDX IS PASSED BY NAME
371 Q $P(ISTR,";",2)
372 ;
373FINISH(ISTR) ; EXTRINSIC TO RETURN THE LAST LINE FROM AN INDEX
374 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
375 Q $P(ISTR,";",3)
376 ;
377ARRAY(ISTR) ; EXTRINSIC TO RETURN THE ARRAY REFERENCE FROM AN INDEX
378 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
379 Q $P(ISTR,";",1)
380 ;
381BUILD(BLIST,BDEST) ; A COPY MACHINE THAT TAKE INSTRUCTIONS IN ARRAY BLIST
382 ; WHICH HAVE ARRAY;START;FINISH AND COPIES THEM TO DEST
383 ; DEST IS CLEARED TO START
384 ; USES PUSH TO DO THE COPY
385 N I
386 K @BDEST
387 F I=1:1:@BLIST@(0) D ; FOR EACH INSTRUCTION IN BLIST
388 . N J,ATMP
389 . S ATMP=$$ARRAY(@BLIST@(I))
390 . I $G(DEBUG) W "ATMP=",ATMP,!
391 . I $G(DEBUG) W @BLIST@(I),!
392 . F J=$$START(@BLIST@(I)):1:$$FINISH(@BLIST@(I)) D ;
393 . . ; FOR EACH LINE IN THIS INSTR
394 . . I $G(DEBUG) W "BDEST= ",BDEST,!
395 . . I $G(DEBUG) W "ATMP= ",@ATMP@(J),!
396 . . D PUSH(BDEST,@ATMP@(J))
397 Q
398 ;
399QUEUE(BLST,ARRAY,FIRST,LAST) ; ADD AN ENTRY TO A BLIST
400 ;
401 I $G(DEBUG) W "QUEUEING ",BLST,!
402 D PUSH(BLST,ARRAY_";"_FIRST_";"_LAST)
403 Q
404 ;
405CP(CPSRC,CPDEST) ; COPIES CPSRC TO CPDEST BOTH PASSED BY NAME
406 ; KILLS CPDEST FIRST
407 N CPINSTR
408 I $G(DEBUG) W "MADE IT TO COPY",CPSRC,CPDEST,!
409 I @CPSRC@(0)<1 D ; BAD LENGTH
410 . W "ERROR IN COPY BAD SOURCE LENGTH: ",CPSRC,!
411 . Q
412 ; I '$D(@CPDEST@(0)) S @CPDEST@(0)=0 ; IF THE DEST IS EMPTY, INIT
413 D QUEUE("CPINSTR",CPSRC,1,@CPSRC@(0)) ; BLIST FOR ENTIRE ARRAY
414 D BUILD("CPINSTR",CPDEST)
415 Q
416 ;
417QOPEN(QOBLIST,QOXML,QOXPATH) ; ADD ALL BUT THE LAST LINE OF QOXML TO QOBLIST
418 ; WARNING NEED TO DO QCLOSE FOR SAME XML BEFORE CALLING BUILD
419 ; QOXPATH IS OPTIONAL - WILL OPEN INSIDE THE XPATH POINT
420 ; USED TO INSERT CHILDREN NODES
421 I @QOXML@(0)<1 D ; MALFORMED XML
422 . W "MALFORMED XML PASSED TO QOPEN: ",QOXML,!
423 . Q
424 I $G(DEBUG) W "DOING QOPEN",!
425 N S1,E1,QOT,QOTMP
426 S S1=1 ; OPEN FROM THE BEGINNING OF THE XML
427 I $D(QOXPATH) D ; XPATH PROVIDED
428 . D QUERY(QOXML,QOXPATH,"QOT") ; INSURE INDEX
429 . S E1=$P(@QOXML@(QOXPATH),"^",2)-1
430 I '$D(QOXPATH) D ; NO XPATH PROVIDED, OPEN AT ROOT
431 . S E1=@QOXML@(0)-1
432 D QUEUE(QOBLIST,QOXML,S1,E1)
433 ; S QOTMP=QOXML_"^"_S1_"^"_E1
434 ; D PUSH(QOBLIST,QOTMP)
435 Q
436 ;
437QCLOSE(QCBLIST,QCXML,QCXPATH) ; CLOSE XML AFTER A QOPEN
438 ; ADDS THE LIST LINE OF QCXML TO QCBLIST
439 ; USED TO FINISH INSERTING CHILDERN NODES
440 ; QCXPATH IS OPTIONAL - IF PROVIDED, WILL CLOSE UNTIL THE END
441 ; IF QOPEN WAS CALLED WITH XPATH, QCLOSE SHOULD BE TOO
442 I @QCXML@(0)<1 D ; MALFORMED XML
443 . W "MALFORMED XML PASSED TO QCLOSE: ",QCXML,!
444 I $G(DEBUG) W "GOING TO CLOSE",!
445 N S1,E1,QCT,QCTMP
446 S E1=@QCXML@(0) ; CLOSE UNTIL THE END OF THE XML
447 I $D(QCXPATH) D ; XPATH PROVIDED
448 . D QUERY(QCXML,QCXPATH,"QCT") ; INSURE INDEX
449 . S S1=$P(@QCXML@(QCXPATH),"^",2) ; REMAINING XML
450 I '$D(QCXPATH) D ; NO XPATH PROVIDED, CLOSE AT ROOT
451 . S S1=@QCXML@(0)
452 D QUEUE(QCBLIST,QCXML,S1,E1)
453 ; D PUSH(QCBLIST,QCXML_";"_S1_";"_E1)
454 Q
455 ;
456INSERT(INSXML,INSNEW,INSXPATH) ; INSERT INSNEW INTO INSXML AT THE
457 ; INSXPATH XPATH POINT INSXPATH IS OPTIONAL - IF IT IS
458 ; OMITTED, INSERTION WILL BE AT THE ROOT
459 ; NOTE INSERT IS NON DESTRUCTIVE AND WILL ADD THE NEW
460 ; XML AT THE END OF THE XPATH POINT
461 ; INSXML AND INSNEW ARE PASSED BY NAME INSXPATH IS A VALUE
462 N INSBLD,INSTMP
463 I $G(DEBUG) W "DOING INSERT ",INSXML,INSNEW,INSXPATH,!
464 I $G(DEBUG) F G1=1:1:@INSXML@(0) W @INSXML@(G1),!
465 I '$D(@INSXML@(1)) D ; INSERT INTO AN EMPTY ARRAY
466 . D CP^C0CXPATH(INSNEW,INSXML) ; JUST COPY INTO THE OUTPUT
467 I $D(@INSXML@(1)) D ; IF ORIGINAL ARRAY IS NOT EMPTY
468 . I '$D(@INSXML@(0)) S @INSXML@(0)=$O(@INSXML@(""),-1) ;SET LENGTH
469 . I $D(INSXPATH) D ; XPATH PROVIDED
470 . . D QOPEN("INSBLD",INSXML,INSXPATH) ; COPY THE BEFORE
471 . . I $G(DEBUG) D PARY^C0CXPATH("INSBLD")
472 . I '$D(INSXPATH) D ; NO XPATH PROVIDED, OPEN AT ROOT
473 . . D QOPEN("INSBLD",INSXML,"//") ; OPEN WITH ROOT XPATH
474 . I '$D(@INSNEW@(0)) S @INSNEW@(0)=$O(@INSNEW@(""),-1) ;SIZE OF XML
475 . D QUEUE("INSBLD",INSNEW,1,@INSNEW@(0)) ; COPY IN NEW XML
476 . I $D(INSXPATH) D ; XPATH PROVIDED
477 . . D QCLOSE("INSBLD",INSXML,INSXPATH) ; CLOSE WITH XPATH
478 . I '$D(INSXPATH) D ; NO XPATH PROVIDED, CLOSE AT ROOT
479 . . D QCLOSE("INSBLD",INSXML,"//") ; CLOSE WITH ROOT XPATH
480 . D BUILD("INSBLD","INSTMP") ; PUT RESULTS IN INDEST
481 . D CP^C0CXPATH("INSTMP",INSXML) ; COPY BUFFER TO SOURCE
482 Q
483 ;
484INSINNER(INNXML,INNNEW,INNXPATH) ; INSERT THE INNER XML OF INNNEW
485 ; INTO INNXML AT THE INNXPATH XPATH POINT
486 ;
487 N INNBLD,UXPATH
488 N INNTBUF
489 S INNTBUF=$NA(^TMP($J,"INNTBUF"))
490 I '$D(INNXPATH) D ; XPATH NOT PASSED
491 . S UXPATH="//" ; USE ROOT XPATH
492 I $D(INNXPATH) S UXPATH=INNXPATH ; USE THE XPATH THAT'S PASSED
493 I '$D(@INNXML@(0)) D ; INNXML IS EMPTY
494 . D QUEUE^C0CXPATH("INNBLD",INNNEW,2,@INNNEW@(0)-1) ; JUST INNER
495 . D BUILD("INNBLD",INNXML)
496 I @INNXML@(0)>0 D ; NOT EMPTY
497 . D QOPEN("INNBLD",INNXML,UXPATH) ;
498 . D QUEUE("INNBLD",INNNEW,2,@INNNEW@(0)-1) ; JUST INNER XML
499 . D QCLOSE("INNBLD",INNXML,UXPATH)
500 . D BUILD("INNBLD",INNTBUF) ; BUILD TO BUFFER
501 . D CP(INNTBUF,INNXML) ; COPY BUFFER TO DEST
502 Q
503 ;
504INSB4(XDEST,XNEW) ; INSERT XNEW AT THE BEGINNING OF XDEST
505 ; BUT XDEST AN XNEW ARE PASSED BY NAME
506 N XBLD,XTMP
507 D QUEUE("XBLD",XDEST,1,1) ; NEED TO PRESERVE SECTION ROOT
508 D QUEUE("XBLD",XNEW,1,@XNEW@(0)) ; ALL OF NEW XML FIRST
509 D QUEUE("XBLD",XDEST,2,@XDEST@(0)) ; FOLLOWED BY THE REST OF SECTION
510 D BUILD("XBLD","XTMP") ; BUILD THE RESULT
511 D CP("XTMP",XDEST) ; COPY TO THE DESTINATION
512 I $G(DEBUG) D PARY("XDEST")
513 Q
514 ;
515REPLACE(REXML,RENEW,REXPATH) ; REPLACE THE XML AT THE XPATH POINT
516 ; WITH RENEW - NOTE THIS WILL DELETE WHAT WAS THERE BEFORE
517 ; REXML AND RENEW ARE PASSED BY NAME XPATH IS A VALUE
518 ; THE DELETED XML IS PUT IN ^TMP($J,"REPLACE_OLD")
519 N REBLD,XFIRST,XLAST,OLD,XNODE,RETMP
520 S OLD=$NA(^TMP($J,"REPLACE_OLD"))
521 D QUERY(REXML,REXPATH,OLD) ; CREATE INDEX, TEST XPATH, MAKE OLD
522 S XNODE=@REXML@(REXPATH) ; PULL OUT FIRST AND LAST LINE PTRS
523 S XFIRST=$P(XNODE,"^",1)
524 S XLAST=$P(XNODE,"^",2)
525 I RENEW="" D ; WE ARE DELETING A SECTION, MUST SAVE THE TAG
526 . D QUEUE("REBLD",REXML,1,XFIRST) ; THE BEFORE
527 . D QUEUE("REBLD",REXML,XLAST,@REXML@(0)) ; THE REST
528 I RENEW'="" D ; NEW XML IS NOT NULL
529 . D QUEUE("REBLD",REXML,1,XFIRST-1) ; THE BEFORE
530 . D QUEUE("REBLD",RENEW,1,@RENEW@(0)) ; THE NEW
531 . D QUEUE("REBLD",REXML,XLAST+1,@REXML@(0)) ; THE REST
532 I $G(DEBUG) W "REPLACE PREBUILD",!
533 I $G(DEBUG) D PARY("REBLD")
534 D BUILD("REBLD","RTMP")
535 K @REXML ; KILL WHAT WAS THERE
536 D CP("RTMP",REXML) ; COPY IN THE RESULT
537 Q
538 ;
539DELETE(REXML,REXPATH) ; DELETE THE XML AT THE XPATH POINT
540 ; REXML IS PASSED BY NAME XPATH IS A VALUE
541 N REBLD,XFIRST,XLAST,OLD,XNODE,RETMP
542 S OLD=$NA(^TMP($J,"REPLACE_OLD"))
543 D QUERY(REXML,REXPATH,OLD) ; CREATE INDEX, TEST XPATH, MAKE OLD
544 S XNODE=@REXML@(REXPATH) ; PULL OUT FIRST AND LAST LINE PTRS
545 S XFIRST=$P(XNODE,"^",1)
546 S XLAST=$P(XNODE,"^",2)
547 D QUEUE("REBLD",REXML,1,XFIRST-1) ; THE BEFORE
548 D QUEUE("REBLD",REXML,XLAST+1,@REXML@(0)) ; THE REST
549 I $G(DEBUG) D PARY("REBLD")
550 D BUILD("REBLD","RTMP")
551 K @REXML ; KILL WHAT WAS THERE
552 D CP("RTMP",REXML) ; COPY IN THE RESULT
553 Q
554 ;
555MISSING(IXML,OARY) ; SEARTH THROUGH INXLM AND PUT ANY @@X@@ VARS IN OARY
556 ; W "Reporting on the missing",!
557 ; W OARY
558 I '$D(@IXML@(0)) W "MALFORMED XML PASSED TO MISSING",! Q
559 N I
560 S @OARY@(0)=0 ; INITIALIZED MISSING COUNT
561 F I=1:1:@IXML@(0) D ; LOOP THROUGH WHOLE ARRAY
562 . I @IXML@(I)?.E1"@@".E D ; MISSING VARIABLE HERE
563 . . D PUSH^C0CXPATH(OARY,$P(@IXML@(I),"@@",2)) ; ADD TO OUTARY
564 . . Q
565 Q
566 ;
567MAP(IXML,INARY,OXML) ; SUBSTITUTE MULTIPLE @@X@@ VARS WITH VALUES IN INARY
568 ; AND PUT THE RESULTS IN OXML
569 N XCNT
570 I '$D(DEBUG) S DEBUG=0
571 I '$D(IXML) W "MALFORMED XML PASSED TO MAP",! Q
572 I '$D(@IXML@(0)) D ; INITIALIZE COUNT
573 . S XCNT=$O(@IXML@(""),-1)
574 E S XCNT=@IXML@(0) ;COUNT
575 I $O(@INARY@(""))="" W "EMPTY ARRAY PASSED TO MAP",! Q
576 N I,J,TNAM,TVAL,TSTR
577 S @OXML@(0)=XCNT ; TOTAL LINES IN OUTPUT
578 F I=1:1:XCNT D ; LOOP THROUGH WHOLE ARRAY
579 . S @OXML@(I)=@IXML@(I) ; COPY THE LINE TO OUTPUT
580 . I @OXML@(I)?.E1"@@".E D ; IS THERE A VARIABLE HERE?
581 . . S TSTR=$P(@IXML@(I),"@@",1) ; INIT TO PART BEFORE VARS
582 . . F J=2:2:10 D Q:$P(@IXML@(I),"@@",J+2)="" ; QUIT IF NO MORE VARS
583 . . . I DEBUG W "IN MAPPING LOOP: ",TSTR,!
584 . . . S TNAM=$P(@OXML@(I),"@@",J) ; EXTRACT THE VARIABLE NAME
585 . . . S TVAL="@@"_$P(@IXML@(I),"@@",J)_"@@" ; DEFAULT UNCHANGED
586 . . . I $D(@INARY@(TNAM)) D ; IS THE VARIABLE IN THE MAP?
587 . . . . I '$D(@INARY@(TNAM,"F")) D ; NOT A SPECIAL FIELD
588 . . . . . S TVAL=@INARY@(TNAM) ; PULL OUT MAPPED VALUE
589 . . . . E D DOFLD ; PROCESS A FIELD
590 . . . S TVAL=$$SYMENC^MXMLUTL(TVAL) ;MAKE SURE THE VALUE IS XML SAFE
591 . . . S TSTR=TSTR_TVAL_$P(@IXML@(I),"@@",J+1) ; ADD VAR AND PART AFTER
592 . . S @OXML@(I)=TSTR ; COPY LINE WITH MAPPED VALUES
593 . . I DEBUG W TSTR
594 I DEBUG W "MAPPED",!
595 Q
596 ;
597DOFLD ; PROCESS A FILEMAN FIELD REFERENCED BY A VARIABLE
598 ;
599 Q
600 ;
601TRIM(THEXML) ; TAKES OUT ALL NULL ELEMENTS
602 ; THEXML IS PASSED BY NAME
603 N I,J,TMPXML,DEL,FOUND,INTXT
604 S FOUND=0
605 S INTXT=0
606 I $G(DEBUG) W "DELETING EMPTY ELEMENTS",!
607 F I=1:1:(@THEXML@(0)-1) D ; LOOP THROUGH ENTIRE ARRAY
608 . S J=@THEXML@(I)
609 . I J["<text>" D
610 . . S INTXT=1 ; IN HTML SECTION, DON'T TRIM
611 . . I $G(DEBUG) W "IN HTML SECTION",!
612 . N JM,JP,JPX ; JMINUS AND JPLUS
613 . S JM=@THEXML@(I-1) ; LINE BEFORE
614 . I JM["</text>" S INTXT=0 ; LEFT HTML SECTION,START TRIM
615 . S JP=@THEXML@(I+1) ; LINE AFTER
616 . I INTXT=0 D ; IF NOT IN AN HTML SECTION
617 . . S JPX=$TR(JP,"/","") ; REMOVE THE SLASH
618 . . I J=JPX D ; AN EMPTY ELEMENT ON TWO LINES
619 . . . I $G(DEBUG) W I,J,JP,!
620 . . . S FOUND=1 ; FOUND SOMETHING TO BE DELETED
621 . . . S DEL(I)="" ; SET LINE TO DELETE
622 . . . S DEL(I+1)="" ; SET NEXT LINE TO DELETE
623 . . I J["><" D ; AN EMPTY ELEMENT ON ONE LINE
624 . . . I $G(DEBUG) W I,J,!
625 . . . S FOUND=1 ; FOUND SOMETHING TO BE DELETED
626 . . . S DEL(I)="" ; SET THE EMPTY LINE UP TO BE DELETED
627 . . . I JM=JPX D ;
628 . . . . I $G(DEBUG) W I,JM_J_JPX,!
629 . . . . S DEL(I-1)=""
630 . . . . S DEL(I+1)="" ; SET THE SURROUNDING LINES FOR DEL
631 ; . I J'["><" D PUSH("TMPXML",J)
632 I FOUND D ; NEED TO DELETE THINGS
633 . F I=1:1:@THEXML@(0) D ; COPY ARRAY LEAVING OUT DELELTED LINES
634 . . I '$D(DEL(I)) D ; IF THE LINE IS NOT DELETED
635 . . . D PUSH("TMPXML",@THEXML@(I)) ; COPY TO TMPXML ARRAY
636 . D CP("TMPXML",THEXML) ; REPLACE THE XML WITH THE COPY
637 Q FOUND
638 ;
639UNMARK(XSEC) ; REMOVE MARKUP FROM FIRST AND LAST LINE OF XML
640 ; XSEC IS A SECTION PASSED BY NAME
641 N XBLD,XTMP
642 D QUEUE("XBLD",XSEC,2,@XSEC@(0)-1) ; BUILD LIST FOR INNER XML
643 D BUILD("XBLD","XTMP") ; BUILD THE RESULT
644 D CP("XTMP",XSEC) ; REPLACE PASSED XML
645 Q
646 ;
647PARY(GLO,ZN) ;PRINT AN ARRAY
648 ; IF ZN=-1 NO LINE NUMBERS
649 N I
650 F I=1:1:@GLO@(0) D ;
651 . I $G(ZN)=-1 W @GLO@(I),!
652 . E W I_" "_@GLO@(I),!
653 Q
654 ;
655H2ARY(IARYRTN,IHASH,IPRE) ; CONVERT IHASH TO RETURN ARRAY
656 ; IPRE IS OPTIONAL PREFIX FOR THE ELEMENTS. USED FOR MUPTIPLES 1^"VAR"^VALUE
657 I '$D(IPRE) S IPRE=""
658 N H2I S H2I=""
659 ; W $O(@IHASH@(H2I)),!
660 F S H2I=$O(@IHASH@(H2I)) Q:H2I="" D ; FOR EACH ELEMENT OF THE HASH
661 . I $QS(H2I,$QL(H2I))="M" D Q ; SPECIAL CASE FOR MULTIPLES
662 . . ;W H2I_"^"_@IHASH@(H2I),!
663 . . N IH,IHI
664 . . S IH=$NA(@IHASH@(H2I)) ;
665 . . S IH2A=$O(@IH@("")) ; SKIP OVER MULTIPLE DISCRIPTOR
666 . . S IH2=$NA(@IH@(IH2A)) ; PAST THE "M","DIRETIONS" FOR EXAMPLE
667 . . S IHI="" ; INDEX INTO "M" MULTIPLES
668 . . F S IHI=$O(@IH2@(IHI)) Q:IHI="" D ; FOR EACH SUB-MULTIPLE
669 . . . ; W @IH@(IHI)
670 . . . S IH3=$NA(@IH2@(IHI))
671 . . . ; W "HEY",IH3,!
672 . . . D H2ARY(.IARYRTN,IH3,IPRE_";"_IHI) ; RECURSIVE CALL - INDENTED ELEMENTS
673 . . ; W IH,!
674 . . ; W "C0CZZ",!
675 . . ; W $NA(@IHASH@(H2I)),!
676 . . Q ;
677 . D PUSH(IARYRTN,IPRE_"^"_H2I_"^"_@IHASH@(H2I))
678 . ; W @IARYRTN@(0),!
679 Q
680 ;
681XVARS(XVRTN,XVIXML) ; RETURNS AN ARRAY XVRTN OF ALL UNIQUE VARIABLES
682 ; DEFINED IN INPUT XML XVIXML BY @@VAR@@
683 ; XVRTN AND XVIXML ARE PASSED BY NAME
684 ;
685 N XVI,XVTMP,XVT
686 F XVI=1:1:@XVIXML@(0) D ; FOR ALL LINES OF THE XML
687 . S XVT=@XVIXML@(XVI)
688 . I XVT["@@" S XVTMP($P(XVT,"@@",2))=XVI
689 D H2ARY(XVRTN,"XVTMP")
690 Q
691 ;
692DXVARS(DXIN) ;DISPLAY ALL VARIABLES IN A TEMPLATE
693 ; IF PARAMETERS ARE NULL, DEFAULTS TO CCR TEMPLATE
694 ;
695 N DXUSE,DTMP ; DXUSE IS NAME OF VARIABLE, DTMP IS VARIABLE IF NOT SUPPLIED
696 I DXIN="CCR" D ; NEED TO GO GET CCR TEMPLATE
697 . D LOAD^C0CCCR0("DTMP") ; LOAD CCR TEMPLATE INTO DXTMP
698 . S DXUSE="DTMP" ; DXUSE IS NAME
699 E I DXIN="CCD" D ; NEED TO GO GET CCD TEMPLATE
700 . D LOAD^C0CCCD1("DTMP") ; LOAD CCR TEMPLATE INTO DXTMP
701 . S DXUSE="DTMP" ; DXUSE IS NAME
702 E S DXUSE=DXIN ; IF PASSED THE TEMPLATE TO USE
703 N DVARS ; PUT VARIABLE NAME RESULTS IN ARRAY HERE
704 D XVARS("DVARS",DXUSE) ; PULL OUT VARS
705 D PARY^C0CXPATH("DVARS") ;AND DISPLAY THEM
706 Q
707 ;
708TEST ; Run all the test cases
709 D TESTALL^C0CUNIT("C0CXPAT0")
710 Q
711 ;
712ZTEST(WHICH) ; RUN ONE SET OF TESTS
713 N ZTMP
714 S DEBUG=1
715 D ZLOAD^C0CUNIT("ZTMP","C0CXPAT0")
716 D ZTEST^C0CUNIT(.ZTMP,WHICH)
717 Q
718 ;
719TLIST ; LIST THE TESTS
720 N ZTMP
721 D ZLOAD^C0CUNIT("ZTMP","C0CXPAT0")
722 D TLIST^C0CUNIT(.ZTMP)
723 Q
724 ;
Note: See TracBrowser for help on using the repository browser.