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

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

addition of multiples in XPATH generation

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