source: smart/trunk/p/C0SXPATH.m@ 1742

Last change on this file since 1742 was 1591, checked in by Sam Habiel, 12 years ago

Updated license for routines

File size: 25.5 KB
Line 
1C0SXPATH ; CCDCCR/GPL - XPATH XML manipulation utilities; 6/1/08 ; 9/26/12 10:45am
2 ;;1.0;VISTA SMART CONTAINER;;Sep 26, 2012;Build 5
3 ;Copyright 2008-2012 George Lilly.
4 ;
5 ; This program is free software: you can redistribute it and/or modify
6 ; it under the terms of the GNU Affero General Public License as
7 ; published by the Free Software Foundation, either version 3 of the
8 ; License, or (at your option) any later version.
9 ;
10 ; This program is distributed in the hope that it will be useful,
11 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ; GNU Affero General Public License for more details.
14 ;
15 ; You should have received a copy of the GNU Affero General Public License
16 ; along with this program. If not, see <http://www.gnu.org/licenses/>.
17 ;
18 W "This is an XML XPATH utility library",!
19 W !
20 Q
21 ;
22OUTPUT(OUTARY,OUTNAME,OUTDIR) ; WRITE AN ARRAY TO A FILE
23 ;
24 N Y
25 S Y=$$GTF^%ZISH(OUTARY,$QL(OUTARY),OUTDIR,OUTNAME)
26 I Y Q 1_U_"WROTE FILE: "_OUTNAME_" TO "_OUTDIR
27 I 'Y Q 0_U_"ERROR WRITING FILE"_OUTNAME_" TO "_OUTDIR
28 Q
29 ;
30PUSH(STK,VAL) ; pushs VAL onto STK and updates STK(0)
31 ; VAL IS A STRING AND STK IS PASSED BY NAME
32 ;
33 I '$D(@STK@(0)) S @STK@(0)=0 ; IF THE ARRAY IS EMPTY, INITIALIZE
34 S @STK@(0)=@STK@(0)+1 ; INCREMENT ARRAY DEPTH
35 S @STK@(@STK@(0))=VAL ; PUT VAL A THE END OF THE ARRAY
36 Q
37 ;
38POP(STK,VAL) ; POPS THE LAST VALUE OFF THE STK AND RETURNS IT IN VAL
39 ; VAL AND STK ARE PASSED BY REFERENCE
40 ;
41 I @STK@(0)<1 D ; IF ARRAY IS EMPTY
42 . S VAL=""
43 . S @STK@(0)=0
44 I @STK@(0)>0 D ;
45 . S VAL=@STK@(@STK@(0))
46 . K @STK@(@STK@(0))
47 . S @STK@(0)=@STK@(0)-1 ; NEW DEPTH OF THE ARRAY
48 Q
49 ;
50PUSHA(ADEST,ASRC) ; PUSH ASRC ONTO ADEST, BOTH PASSED BY NAME
51 ;
52 N ZGI
53 F ZGI=1:1:@ASRC@(0) D ; FOR ALL OF THE SOURCE ARRAY
54 . D PUSH(ADEST,@ASRC@(ZGI)) ; PUSH ONE ELEMENT
55 Q
56 ;
57MKMDX(STK,RTN,INREDUX) ; MAKES A MUMPS INDEX FROM THE ARRAY STK
58 ; RTN IS SET TO //FIRST/SECOND/THIRD FOR THREE ARRAY ELEMENTS
59 ; REDUX IS A STRING TO REMOVE FROM THE RESULT
60 S RTN=""
61 N I
62 ; W "STK= ",STK,!
63 I @STK@(0)>0 D ; IF THE ARRAY IS NOT EMPTY
64 . S RTN="//"_@STK@(1) ; FIRST ELEMENT NEEDS NO SEMICOLON
65 . I @STK@(0)>1 D ; SUBSEQUENT ELEMENTS NEED A SEMICOLON
66 . . F I=2:1:@STK@(0) S RTN=RTN_"/"_@STK@(I)
67 I $G(INREDUX)'="" S RTN=$P(RTN,INREDUX,1)_$P(RTN,INREDUX,2)
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,REDUX) ; parse XML in IZXML and produce
203 ; an XPATH index; REDUX is a string to be removed from each xpath
204 ; GPL 7/14/09 OPTIONALLY GENERATE AN XML TEMPLATE IF PASSED BY NAME
205 ; TEMPLATE IS IDENTICAL TO THE PARSED XML LINE BY LINE
206 ; EXCEPT THAT DATA VALUES ARE REPLACED WITH @@XPATH@@ FOR THE XPATH OF THE TAG
207 ; GPL 5/24/09 AND OPTIONALLY PRODUCE THE VDX ARRAY PASSED BY NAME
208 ; @VDX@("XPATH")=VALUE
209 ; ex. @IZXML@("XPATH")=FIRSTLINE^LASTLINE
210 ; WHERE FIRSTLINE AND LASTLINE ARE THE BEGINNING AND ENDING OF THE
211 ; XML SECTION
212 ; IZXML IS PASSED BY NAME
213 ; IF NOINX IS SET TO 1, NO INDEX WILL BE GENERATED, BUT THE VDX WILL BE
214 N I,LINE,FIRST,LAST,CUR,TMP,MDX,FOUND,CURVAL,DVDX,LCNT
215 N C0CSTK ; LEAVE OUT FOR DEBUGGING
216 I '$D(REDUX) S REDUX=""
217 I '$D(NOINX) S NOINX=0 ; IF NOT PASSED, GENERATE AN INDEX
218 N ZXML
219 I NOINX S ZXML=$NA(^TMP("C0CINDEX",$J)) ; TEMP PLACE FOR INDEX TO DISCARD
220 E S ZXML=IZXML ; PLACE FOR INDEX TO KEEP
221 I '$D(@IZXML@(0)) D ; IF COUNT NOT IN NODE 0 COUNT THEM
222 . S I="",LCNT=0
223 . F S I=$O(@IZXML@(I)) Q:I="" S LCNT=LCNT+1
224 E S LCNT=@IZXML@(0) ; LINE COUNT PASSED IN ARRAY
225 I LCNT=0 D Q ; NO XML PASSED
226 . W "ERROR IN XML FILE",!
227 S DVDX=0 ; DEFAULT DO NOT PRODUCE VDX INDEX
228 I $D(VDX) S DVDX=1 ; IF NAME PASSED, DO VDX
229 S C0CSTK(0)=0 ; INITIALIZE STACK
230 K LKASD ; KILL LOOKASIDE ARRAY
231 D MKLASD(.LKASD,IZXML) ;MAKE LOOK ASIDE BUFFER FOR MULTIPLES
232 F I=1:1:LCNT D ; PROCESS THE ENTIRE ARRAY
233 . S LINE=@IZXML@(I)
234 . I $D(TEMPLATE) D ;IF TEMPLATE IS REQUESTED
235 . . S @TEMPLATE@(I)=$$CLEAN(LINE)
236 . ;W LINE,!
237 . S FOUND=0 ; INTIALIZED FOUND FLAG
238 . I LINE?.E1"<!".E S FOUND=1 ; SKIP OVER COMMENTS
239 . I FOUND'=1 D
240 . . I (LINE?.E1"<"1.E1"</".E)!(LINE?.E1"<"1.E1"/>".E) D
241 . . . ; THIS IS THE CASE THERE SECTION BEGINS AND ENDS
242 . . . ; ON THE SAME LINE
243 . . . ; W "FOUND ",LINE,!
244 . . . S FOUND=1 ; SET FOUND FLAG
245 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
246 . . . S CUR=CUR_$G(LKASD(CUR,I)) ; HANDLE MULTIPLES
247 . . . D PUSH("C0CSTK",CUR) ; ADD TO THE STACK
248 . . . D MKMDX("C0CSTK",.MDX,REDUX) ; GENERATE THE M INDEX
249 . . . ; W "MDX=",MDX,!
250 . . . I $D(@ZXML@(MDX)) D ; IN THE INDEX, IS A MULTIPLE
251 . . . . ;I '$D(ZDUP(MDX)) S ZDUP(MDX)=2
252 . . . . ;E S ZDUP(MDX)=ZDUP(MDX)+1
253 . . . . ;W "DUP:",MDX,!
254 . . . . ;I '$D(CURVAL) S CURVAL=""
255 . . . . ;I DVDX S @VDX@(MDX_"["_ZDUP(MDX)_"]")=CURVAL
256 . . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
257 . . . I '$D(@ZXML@(MDX)) D ; NOT IN THE INDEX, NOT A MULTIPLE
258 . . . . S @ZXML@(MDX)=I_"^"_I ; ADD INDEX ENTRY-FIRST AND LAST
259 . . . . S CURVAL=$$XVAL(LINE) ; VALUE
260 . . . . S $P(@ZXML@(MDX),"^",3)=CURVAL ; THIRD PIECE
261 . . . . I DVDX S @VDX@(MDX)=CURVAL ; FILL IN VDX ARRAY IF REQUESTED
262 . . . . I $D(TEMPLATE) D ; IF TEMPLATE IS REQUESTED
263 . . . . . S LINE=$$CLEAN(LINE) ; CLEAN OUT CONTROL CHARACTERS
264 . . . . . S @TEMPLATE@(I)=$P(LINE,">",1)_">@@"_MDX_"@@</"_$P(LINE,"</",2)
265 . . . D POP("C0CSTK",.TMP) ; REMOVE FROM STACK
266 . I FOUND'=1 D ; THE LINE DOESN'T CONTAIN THE START AND END
267 . . I LINE?.E1"</"1.E D ; LINE CONTAINS END OF A SECTION
268 . . . ; W "FOUND ",LINE,!
269 . . . S FOUND=1 ; SET FOUND FLAG
270 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
271 . . . D MKMDX("C0CSTK",.MDX) ; GENERATE THE M INDEX
272 . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
273 . . . D POP("C0CSTK",.TMP) ; REMOVE FROM STACK
274 . . . S TMP=$P(TMP,"[",1) ; REMOVE [X] FROM MULTIPLE
275 . . . I TMP'=CUR D ; MALFORMED XML, END MUST MATCH START
276 . . . . W "MALFORMED XML ",CUR,"LINE "_I_LINE,!
277 . . . . D PARY("C0CSTK") ; PRINT OUT THE STACK FOR DEBUGING
278 . . . . Q
279 . I FOUND'=1 D ; THE LINE MIGHT CONTAIN A SECTION BEGINNING
280 . . I (LINE?.E1"<"1.E)&(LINE'["?>") D ; BEGINNING OF A SECTION
281 . . . ; W "FOUND ",LINE,!
282 . . . S FOUND=1 ; SET FOUND FLAG
283 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
284 . . . S CUR=CUR_$G(LKASD(CUR,I)) ; HANDLE MULTIPLES
285 . . . D PUSH("C0CSTK",CUR) ; ADD TO THE STACK
286 . . . D MKMDX("C0CSTK",.MDX) ; GENERATE THE M INDEX
287 . . . ; W "MDX=",MDX,!
288 . . . I $D(@ZXML@(MDX)) D ; IN THE INDEX, IS A MULTIPLE
289 . . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
290 . . . . ;B
291 . . . I '$D(@ZXML@(MDX)) D ; NOT IN THE INDEX, NOT A MULTIPLE
292 . . . . S @ZXML@(MDX)=I_"^" ; INSERT INTO THE INDEX
293 S @ZXML@("INDEXED")=""
294 S @ZXML@("//")="1^"_LCNT ; ROOT XPATH
295 I NOINX K @ZXML ; DELETE UNWANTED INDEX
296 Q
297 ;
298MKLASD(OUTBUF,INARY) ; CREATE A LOOKASIDE BUFFER FOR MULTILPLES
299 ;
300 N ZI,ZN,ZA,ZLINE,ZLINE2,CUR,CUR2
301 F ZI=1:1:LCNT-1 D ; PROCESS THE ENTIRE ARRAY
302 . S ZLINE=@IZXML@(ZI)
303 . I ZI<LCNT S ZLINE2=@IZXML@(ZI+1)
304 . I ZLINE?.E1"</"1.E D ; NEXT LINE CONTAINS END OF A SECTION
305 . . S CUR=$$XNAME(ZLINE) ; EXTRACT THE NAME
306 . . I (ZLINE2?.E1"<"1.E)&(ZLINE'["?>") D ; BEGINNING OF A SECTION
307 . . . S CUR2=$$XNAME(ZLINE2) ; EXTRACT THE NAME
308 . . . I CUR=CUR2 D ; IF THIS IS A MULTIPLE
309 . . . . S OUTBUF(CUR,ZI+1)=""
310 ;ZWR OUTBUF
311 S ZI=""
312 F S ZI=$O(OUTBUF(ZI)) Q:ZI="" D ; FOR EACH KIND OF MULTIPLE
313 . S ZN=$O(OUTBUF(ZI,"")) ; LINE NUMBER OF SECOND MULTIPLE
314 . F S ZN=$O(@IZXML@(ZN),-1) Q:ZN="" I $E($P(@IZXML@(ZN),"<"_ZI,2),1,1)=">" Q ;
315 . S OUTBUF(ZI,ZN)=""
316 S ZA=1,ZI="",ZN=""
317 F S ZI=$O(OUTBUF(ZI)) Q:ZI="" D ; ADDING THE COUNT FOR THE MULIPLES [x]
318 . S ZN="",ZA=1
319 . F S ZN=$O(OUTBUF(ZI,ZN)) Q:ZN="" D ;
320 . . S OUTBUF(ZI,ZN)="["_ZA_"]"
321 . . S ZA=ZA+1
322 Q
323 ;
324CLEAN(STR,TR) ; extrinsic function; returns string
325 ;; Removes all non printable characters from a string.
326 ;; STR by Value
327 ;; TR IS OPTIONAL TO IMPROVE PERFORMANCE
328 N TR,I
329 I '$D(TR) D ;
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.