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

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

bug fix in xpath lib

File size: 20.5 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") ; PRODUCES A VDX ARRAY IN G1
132 D VDX2XPG(OUTXPG,"G1") ; CONVERTS THE VDX ARRAY TO XPG FORM
133 Q
134 ;
135DO
136 D XPG2XML("^GPL2B","^GPL2A")
137 Q
138 ;
139XPG2XML(OUTXML,INXPG,NUM) ;
140 ; OUTPUT XML FROM AN XPG FILE
141 I '$D(NUM) D ; FIRST TIME THROUGH
142 . K @OUTXML
143 . S NUM=1
144 . S ZA=""
145 S G=INXPG
146 S G=@G I G="" Q ;
147 F D ; S G=$Q(@G) Q:G="" D ;
148 . F ZI=1:1:$QL(G) D ;
149 . . I ZI<$QL(G) D ;
150 . . . S ZB=$QS(G,ZI)
151 . . . I ZA(ZA(0))'=ZB D ZXO(ZB)
152 . . . E D ZXC(ZB)
153 . . S ZV=""
154 . . S G2=$QS(G,ZI)
155 . . I ZI=$QL(G) S ZV=@G
156 . . W G2," ",ZV
157 . S G=@G I G="" Q ;
158 . W !
159 Q
160 ;
161ZXO(WHAT)
162 D PUSH("GA",WHAT)
163 D PUSH(OUTXML,"<"_WHAT_">")
164 Q
165 ;
166ZXC(WHAT)
167 D POP("GA",.TMP)
168 D PUSH(OUTXML,"</"_WHAT_">")
169 Q
170 ;
171ZXVAL(WHAT,VAL)
172 D PUSH(OUTXML,"<"_WHAT_">"_VAL_"</"_WHAT_">")
173 Q
174 ;
175INDEX(ZXML,VDX) ; parse the XML in ZXML and produce an XPATH index
176 ; GPL 5/24/09 AND OPTIONALLY PRODUCE THE VDX ARRAY PASSED BY NAME
177 ; @VDX@("XPATH")=VALUE
178 ; ex. @ZXML@("XPATH")=FIRSTLINE^LASTLINE
179 ; WHERE FIRSTLINE AND LASTLINE ARE THE BEGINNING AND ENDING OF THE
180 ; XML SECTION
181 ; ZXML IS PASSED BY NAME
182 N I,LINE,FIRST,LAST,CUR,TMP,MDX,FOUND,CURVAL,DVDX,LCNT
183 N C0CSTK ; LEAVE OUT FOR DEBUGGING
184 I '$D(@ZXML@(0)) D ; IF COUNT NOT IN NODE 0 COUNT THEM
185 . S I="",LCNT=0
186 . F S I=$O(@ZXML@(I)) Q:I="" S LCNT=LCNT+1
187 E S LCNT=@ZXML@(0) ; LINE COUNT PASSED IN ARRAY
188 I LCNT=0 D Q ; NO XML PASSED
189 . W "ERROR IN XML FILE",!
190 S DVDX=0 ; DEFAULT DO NOT PRODUCE VDX INDEX
191 I $D(VDX) S DVDX=1 ; IF NAME PASSED, DO VDX
192 S C0CSTK(0)=0 ; INITIALIZE STACK
193 F I=1:1:LCNT D ; PROCESS THE ENTIRE ARRAY
194 . S LINE=@ZXML@(I)
195 . ;W LINE,!
196 . S FOUND=0 ; INTIALIZED FOUND FLAG
197 . I LINE?.E1"<!".E S FOUND=1 ; SKIP OVER COMMENTS
198 . I FOUND'=1 D
199 . . I (LINE?.E1"<"1.E1"</".E)!(LINE?.E1"<"1.E1"/>".E) D
200 . . . ; THIS IS THE CASE THERE SECTION BEGINS AND ENDS
201 . . . ; ON THE SAME LINE
202 . . . ; W "FOUND ",LINE,!
203 . . . S FOUND=1 ; SET FOUND FLAG
204 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
205 . . . D PUSH("C0CSTK",CUR) ; ADD TO THE STACK
206 . . . D MKMDX("C0CSTK",.MDX) ; GENERATE THE M INDEX
207 . . . ; W "MDX=",MDX,!
208 . . . I $D(@ZXML@(MDX)) D ; IN THE INDEX, IS A MULTIPLE
209 . . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
210 . . . I '$D(@ZXML@(MDX)) D ; NOT IN THE INDEX, NOT A MULTIPLE
211 . . . . S @ZXML@(MDX)=I_"^"_I ; ADD INDEX ENTRY-FIRST AND LAST
212 . . . . S CURVAL=$$XVAL(LINE) ; VALUE
213 . . . . S $P(@ZXML@(MDX),"^",3)=CURVAL ; THIRD PIECE
214 . . . . I DVDX S @VDX@(MDX)=CURVAL ; FILL IN VDX ARRAY IF REQUESTED
215 . . . D POP("C0CSTK",.TMP) ; REMOVE FROM STACK
216 . I FOUND'=1 D ; THE LINE DOESN'T CONTAIN THE START AND END
217 . . I LINE?.E1"</"1.E D ; LINE CONTAINS END OF A SECTION
218 . . . ; W "FOUND ",LINE,!
219 . . . S FOUND=1 ; SET FOUND FLAG
220 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
221 . . . D MKMDX("C0CSTK",.MDX) ; GENERATE THE M INDEX
222 . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
223 . . . D POP("C0CSTK",.TMP) ; REMOVE FROM STACK
224 . . . I TMP'=CUR D ; MALFORMED XML, END MUST MATCH START
225 . . . . W "MALFORMED XML ",CUR,"LINE "_I_LINE,!
226 . . . . D PARY("C0CSTK") ; PRINT OUT THE STACK FOR DEBUGING
227 . . . . Q
228 . I FOUND'=1 D ; THE LINE MIGHT CONTAIN A SECTION BEGINNING
229 . . I (LINE?.E1"<"1.E)&(LINE'["?>") D ; BEGINNING OF A SECTION
230 . . . ; W "FOUND ",LINE,!
231 . . . S FOUND=1 ; SET FOUND FLAG
232 . . . S CUR=$$XNAME(LINE) ; EXTRACT THE NAME
233 . . . D PUSH("C0CSTK",CUR) ; ADD TO THE STACK
234 . . . D MKMDX("C0CSTK",.MDX) ; GENERATE THE M INDEX
235 . . . ; W "MDX=",MDX,!
236 . . . I $D(@ZXML@(MDX)) D ; IN THE INDEX, IS A MULTIPLE
237 . . . . S $P(@ZXML@(MDX),"^",2)=I ; UPDATE LAST LINE NUMBER
238 . . . I '$D(@ZXML@(MDX)) D ; NOT IN THE INDEX, NOT A MULTIPLE
239 . . . . S @ZXML@(MDX)=I_"^" ; INSERT INTO THE INDEX
240 S @ZXML@("INDEXED")=""
241 S @ZXML@("//")="1^"_LCNT ; ROOT XPATH
242 Q
243 ;
244QUERY(IARY,XPATH,OARY) ; RETURNS THE XML ARRAY MATCHING THE XPATH EXPRESSION
245 ; XPATH IS OF THE FORM "//FIRST/SECOND/THIRD"
246 ; IARY AND OARY ARE PASSED BY NAME
247 I '$D(@IARY@("INDEXED")) D ; INDEX IS NOT PRESENT IN IARY
248 . D INDEX(IARY) ; GENERATE AN INDEX FOR THE XML
249 N FIRST,LAST ; FIRST AND LAST LINES OF ARRAY TO RETURN
250 N TMP,I,J,QXPATH
251 S FIRST=1
252 S LAST=@IARY@(0) ; FIRST AND LAST DEFAULT TO ROOT
253 I XPATH'="//" D ; NOT A ROOT QUERY
254 . S TMP=@IARY@(XPATH) ; LOOK UP LINE VALUES
255 . S FIRST=$P(TMP,"^",1)
256 . S LAST=$P(TMP,"^",2)
257 K @OARY
258 S @OARY@(0)=+LAST-FIRST+1
259 S J=1
260 FOR I=FIRST:1:LAST D
261 . S @OARY@(J)=@IARY@(I) ; COPY THE LINE TO OARY
262 . S J=J+1
263 ; ZWR OARY
264 Q
265 ;
266XF(IDX,XPATH) ; EXTRINSIC TO RETURN THE STARTING LINE FROM AN XPATH
267 ; INDEX WITH TWO PIECES START^FINISH
268 ; IDX IS PASSED BY NAME
269 Q $P(@IDX@(XPATH),"^",1)
270 ;
271XL(IDX,XPATH) ; EXTRINSIC TO RETURN THE LAST LINE FROM AN XPATH
272 ; INDEX WITH TWO PIECES START^FINISH
273 ; IDX IS PASSED BY NAME
274 Q $P(@IDX@(XPATH),"^",2)
275 ;
276START(ISTR) ; EXTRINSIC TO RETURN THE STARTING LINE FROM AN INDEX
277 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
278 ; COMPANION TO FINISH ; IDX IS PASSED BY NAME
279 Q $P(ISTR,";",2)
280 ;
281FINISH(ISTR) ; EXTRINSIC TO RETURN THE LAST LINE FROM AN INDEX
282 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
283 Q $P(ISTR,";",3)
284 ;
285ARRAY(ISTR) ; EXTRINSIC TO RETURN THE ARRAY REFERENCE FROM AN INDEX
286 ; TYPE STRING WITH THREE PIECES ARRAY;START;FINISH
287 Q $P(ISTR,";",1)
288 ;
289BUILD(BLIST,BDEST) ; A COPY MACHINE THAT TAKE INSTRUCTIONS IN ARRAY BLIST
290 ; WHICH HAVE ARRAY;START;FINISH AND COPIES THEM TO DEST
291 ; DEST IS CLEARED TO START
292 ; USES PUSH TO DO THE COPY
293 N I
294 K @BDEST
295 F I=1:1:@BLIST@(0) D ; FOR EACH INSTRUCTION IN BLIST
296 . N J,ATMP
297 . S ATMP=$$ARRAY(@BLIST@(I))
298 . I DEBUG W "ATMP=",ATMP,!
299 . I DEBUG W @BLIST@(I),!
300 . F J=$$START(@BLIST@(I)):1:$$FINISH(@BLIST@(I)) D ;
301 . . ; FOR EACH LINE IN THIS INSTR
302 . . I DEBUG W "BDEST= ",BDEST,!
303 . . I DEBUG W "ATMP= ",@ATMP@(J),!
304 . . D PUSH(BDEST,@ATMP@(J))
305 Q
306 ;
307QUEUE(BLST,ARRAY,FIRST,LAST) ; ADD AN ENTRY TO A BLIST
308 ;
309 I DEBUG W "QUEUEING ",BLST,!
310 D PUSH(BLST,ARRAY_";"_FIRST_";"_LAST)
311 Q
312 ;
313CP(CPSRC,CPDEST) ; COPIES CPSRC TO CPDEST BOTH PASSED BY NAME
314 ; KILLS CPDEST FIRST
315 N CPINSTR
316 I DEBUG W "MADE IT TO COPY",CPSRC,CPDEST,!
317 I @CPSRC@(0)<1 D ; BAD LENGTH
318 . W "ERROR IN COPY BAD SOURCE LENGTH: ",CPSRC,!
319 . Q
320 ; I '$D(@CPDEST@(0)) S @CPDEST@(0)=0 ; IF THE DEST IS EMPTY, INIT
321 D QUEUE("CPINSTR",CPSRC,1,@CPSRC@(0)) ; BLIST FOR ENTIRE ARRAY
322 D BUILD("CPINSTR",CPDEST)
323 Q
324 ;
325QOPEN(QOBLIST,QOXML,QOXPATH) ; ADD ALL BUT THE LAST LINE OF QOXML TO QOBLIST
326 ; WARNING NEED TO DO QCLOSE FOR SAME XML BEFORE CALLING BUILD
327 ; QOXPATH IS OPTIONAL - WILL OPEN INSIDE THE XPATH POINT
328 ; USED TO INSERT CHILDREN NODES
329 I @QOXML@(0)<1 D ; MALFORMED XML
330 . W "MALFORMED XML PASSED TO QOPEN: ",QOXML,!
331 . Q
332 I DEBUG W "DOING QOPEN",!
333 N S1,E1,QOT,QOTMP
334 S S1=1 ; OPEN FROM THE BEGINNING OF THE XML
335 I $D(QOXPATH) D ; XPATH PROVIDED
336 . D QUERY(QOXML,QOXPATH,"QOT") ; INSURE INDEX
337 . S E1=$P(@QOXML@(QOXPATH),"^",2)-1
338 I '$D(QOXPATH) D ; NO XPATH PROVIDED, OPEN AT ROOT
339 . S E1=@QOXML@(0)-1
340 D QUEUE(QOBLIST,QOXML,S1,E1)
341 ; S QOTMP=QOXML_"^"_S1_"^"_E1
342 ; D PUSH(QOBLIST,QOTMP)
343 Q
344 ;
345QCLOSE(QCBLIST,QCXML,QCXPATH) ; CLOSE XML AFTER A QOPEN
346 ; ADDS THE LIST LINE OF QCXML TO QCBLIST
347 ; USED TO FINISH INSERTING CHILDERN NODES
348 ; QCXPATH IS OPTIONAL - IF PROVIDED, WILL CLOSE UNTIL THE END
349 ; IF QOPEN WAS CALLED WITH XPATH, QCLOSE SHOULD BE TOO
350 I @QCXML@(0)<1 D ; MALFORMED XML
351 . W "MALFORMED XML PASSED TO QCLOSE: ",QCXML,!
352 I DEBUG W "GOING TO CLOSE",!
353 N S1,E1,QCT,QCTMP
354 S E1=@QCXML@(0) ; CLOSE UNTIL THE END OF THE XML
355 I $D(QCXPATH) D ; XPATH PROVIDED
356 . D QUERY(QCXML,QCXPATH,"QCT") ; INSURE INDEX
357 . S S1=$P(@QCXML@(QCXPATH),"^",2) ; REMAINING XML
358 I '$D(QCXPATH) D ; NO XPATH PROVIDED, CLOSE AT ROOT
359 . S S1=@QCXML@(0)
360 D QUEUE(QCBLIST,QCXML,S1,E1)
361 ; D PUSH(QCBLIST,QCXML_";"_S1_";"_E1)
362 Q
363 ;
364INSERT(INSXML,INSNEW,INSXPATH) ; INSERT INSNEW INTO INSXML AT THE
365 ; INSXPATH XPATH POINT INSXPATH IS OPTIONAL - IF IT IS
366 ; OMITTED, INSERTION WILL BE AT THE ROOT
367 ; NOTE INSERT IS NON DESTRUCTIVE AND WILL ADD THE NEW
368 ; XML AT THE END OF THE XPATH POINT
369 ; INSXML AND INSNEW ARE PASSED BY NAME INSXPATH IS A VALUE
370 N INSBLD,INSTMP
371 I DEBUG W "DOING INSERT ",INSXML,INSNEW,INSXPATH,!
372 I DEBUG F G1=1:1:@INSXML@(0) W @INSXML@(G1),!
373 I '$D(@INSXML@(0)) D ; INSERT INTO AN EMPTY ARRAY
374 . D CP^C0CXPATH(INSNEW,INSXML) ; JUST COPY INTO THE OUTPUT
375 I $D(@INSXML@(0)) D ; IF ORIGINAL ARRAY IS NOT EMPTY
376 . I $D(INSXPATH) D ; XPATH PROVIDED
377 . . D QOPEN("INSBLD",INSXML,INSXPATH) ; COPY THE BEFORE
378 . . I DEBUG D PARY^C0CXPATH("INSBLD")
379 . I '$D(INSXPATH) D ; NO XPATH PROVIDED, OPEN AT ROOT
380 . . D QOPEN("INSBLD",INSXML,"//") ; OPEN WITH ROOT XPATH
381 . D QUEUE("INSBLD",INSNEW,1,@INSNEW@(0)) ; COPY IN NEW XML
382 . I $D(INSXPATH) D ; XPATH PROVIDED
383 . . D QCLOSE("INSBLD",INSXML,INSXPATH) ; CLOSE WITH XPATH
384 . I '$D(INSXPATH) D ; NO XPATH PROVIDED, CLOSE AT ROOT
385 . . D QCLOSE("INSBLD",INSXML,"//") ; CLOSE WITH ROOT XPATH
386 . D BUILD("INSBLD","INSTMP") ; PUT RESULTS IN INDEST
387 . D CP^C0CXPATH("INSTMP",INSXML) ; COPY BUFFER TO SOURCE
388 Q
389 ;
390INSINNER(INNXML,INNNEW,INNXPATH) ; INSERT THE INNER XML OF INNNEW
391 ; INTO INNXML AT THE INNXPATH XPATH POINT
392 ;
393 N INNBLD,UXPATH
394 N INNTBUF
395 S INNTBUF=$NA(^TMP($J,"INNTBUF"))
396 I '$D(INNXPATH) D ; XPATH NOT PASSED
397 . S UXPATH="//" ; USE ROOT XPATH
398 I $D(INNXPATH) S UXPATH=INNXPATH ; USE THE XPATH THAT'S PASSED
399 I '$D(@INNXML@(0)) D ; INNXML IS EMPTY
400 . D QUEUE^C0CXPATH("INNBLD",INNNEW,2,@INNNEW@(0)-1) ; JUST INNER
401 . D BUILD("INNBLD",INNXML)
402 I @INNXML@(0)>0 D ; NOT EMPTY
403 . D QOPEN("INNBLD",INNXML,UXPATH) ;
404 . D QUEUE("INNBLD",INNNEW,2,@INNNEW@(0)-1) ; JUST INNER XML
405 . D QCLOSE("INNBLD",INNXML,UXPATH)
406 . D BUILD("INNBLD",INNTBUF) ; BUILD TO BUFFER
407 . D CP(INNTBUF,INNXML) ; COPY BUFFER TO DEST
408 Q
409 ;
410INSB4(XDEST,XNEW) ; INSERT XNEW AT THE BEGINNING OF XDEST
411 ; BUT XDEST AN XNEW ARE PASSED BY NAME
412 N XBLD,XTMP
413 D QUEUE("XBLD",XDEST,1,1) ; NEED TO PRESERVE SECTION ROOT
414 D QUEUE("XBLD",XNEW,1,@XNEW@(0)) ; ALL OF NEW XML FIRST
415 D QUEUE("XBLD",XDEST,2,@XDEST@(0)) ; FOLLOWED BY THE REST OF SECTION
416 D BUILD("XBLD","XTMP") ; BUILD THE RESULT
417 D CP("XTMP",XDEST) ; COPY TO THE DESTINATION
418 I DEBUG D PARY("XDEST")
419 Q
420 ;
421REPLACE(REXML,RENEW,REXPATH) ; REPLACE THE XML AT THE XPATH POINT
422 ; WITH RENEW - NOTE THIS WILL DELETE WHAT WAS THERE BEFORE
423 ; REXML AND RENEW ARE PASSED BY NAME XPATH IS A VALUE
424 ; THE DELETED XML IS PUT IN ^TMP($J,"REPLACE_OLD")
425 N REBLD,XFIRST,XLAST,OLD,XNODE,RETMP
426 S OLD=$NA(^TMP($J,"REPLACE_OLD"))
427 D QUERY(REXML,REXPATH,OLD) ; CREATE INDEX, TEST XPATH, MAKE OLD
428 S XNODE=@REXML@(REXPATH) ; PULL OUT FIRST AND LAST LINE PTRS
429 S XFIRST=$P(XNODE,"^",1)
430 S XLAST=$P(XNODE,"^",2)
431 I RENEW="" D ; WE ARE DELETING A SECTION, MUST SAVE THE TAG
432 . D QUEUE("REBLD",REXML,1,XFIRST) ; THE BEFORE
433 . D QUEUE("REBLD",REXML,XLAST,@REXML@(0)) ; THE REST
434 I RENEW'="" D ; NEW XML IS NOT NULL
435 . D QUEUE("REBLD",REXML,1,XFIRST-1) ; THE BEFORE
436 . D QUEUE("REBLD",RENEW,1,@RENEW@(0)) ; THE NEW
437 . D QUEUE("REBLD",REXML,XLAST+1,@REXML@(0)) ; THE REST
438 I DEBUG W "REPLACE PREBUILD",!
439 I DEBUG D PARY("REBLD")
440 D BUILD("REBLD","RTMP")
441 K @REXML ; KILL WHAT WAS THERE
442 D CP("RTMP",REXML) ; COPY IN THE RESULT
443 Q
444 ;
445MISSING(IXML,OARY) ; SEARTH THROUGH INXLM AND PUT ANY @@X@@ VARS IN OARY
446 ; W "Reporting on the missing",!
447 ; W OARY
448 I '$D(@IXML@(0)) W "MALFORMED XML PASSED TO MISSING",! Q
449 N I
450 S @OARY@(0)=0 ; INITIALIZED MISSING COUNT
451 F I=1:1:@IXML@(0) D ; LOOP THROUGH WHOLE ARRAY
452 . I @IXML@(I)?.E1"@@".E D ; MISSING VARIABLE HERE
453 . . D PUSH^C0CXPATH(OARY,$P(@IXML@(I),"@@",2)) ; ADD TO OUTARY
454 . . Q
455 Q
456 ;
457MAP(IXML,INARY,OXML) ; SUBSTITUTE MULTIPLE @@X@@ VARS WITH VALUES IN INARY
458 ; AND PUT THE RESULTS IN OXML
459 I '$D(@IXML@(0)) W "MALFORMED XML PASSED TO MAP",! Q
460 I $O(@INARY@(""))="" W "EMPTY ARRAY PASSED TO MAP",! Q
461 N I,J,TNAM,TVAL,TSTR
462 S @OXML@(0)=@IXML@(0) ; TOTAL LINES IN OUTPUT
463 F I=1:1:@OXML@(0) D ; LOOP THROUGH WHOLE ARRAY
464 . S @OXML@(I)=@IXML@(I) ; COPY THE LINE TO OUTPUT
465 . I @OXML@(I)?.E1"@@".E D ; IS THERE A VARIABLE HERE?
466 . . S TSTR=$P(@IXML@(I),"@@",1) ; INIT TO PART BEFORE VARS
467 . . F J=2:2:10 D Q:$P(@IXML@(I),"@@",J+2)="" ; QUIT IF NO MORE VARS
468 . . . I DEBUG W "IN MAPPING LOOP: ",TSTR,!
469 . . . S TNAM=$P(@OXML@(I),"@@",J) ; EXTRACT THE VARIABLE NAME
470 . . . S TVAL="@@"_$P(@IXML@(I),"@@",J)_"@@" ; DEFAULT UNCHANGED
471 . . . I $D(@INARY@(TNAM)) D ; IS THE VARIABLE IN THE MAP?
472 . . . . I '$D(@INARY@(TNAM,"F")) D ; NOT A SPECIAL FIELD
473 . . . . . S TVAL=@INARY@(TNAM) ; PULL OUT MAPPED VALUE
474 . . . . E D DOFLD ; PROCESS A FIELD
475 . . . S TVAL=$$SYMENC^MXMLUTL(TVAL) ;MAKE SURE THE VALUE IS XML SAFE
476 . . . S TSTR=TSTR_TVAL_$P(@IXML@(I),"@@",J+1) ; ADD VAR AND PART AFTER
477 . . S @OXML@(I)=TSTR ; COPY LINE WITH MAPPED VALUES
478 . . I DEBUG W TSTR
479 I DEBUG W "MAPPED",!
480 Q
481 ;
482DOFLD ; PROCESS A FILEMAN FIELD REFERENCED BY A VARIABLE
483 ;
484 Q
485 ;
486TRIM(THEXML) ; TAKES OUT ALL NULL ELEMENTS
487 ; THEXML IS PASSED BY NAME
488 N I,J,TMPXML,DEL,FOUND,INTXT
489 S FOUND=0
490 S INTXT=0
491 I DEBUG W "DELETING EMPTY ELEMENTS",!
492 F I=1:1:(@THEXML@(0)-1) D ; LOOP THROUGH ENTIRE ARRAY
493 . S J=@THEXML@(I)
494 . I J["<text>" D
495 . . S INTXT=1 ; IN HTML SECTION, DON'T TRIM
496 . . I DEBUG W "IN HTML SECTION",!
497 . N JM,JP,JPX ; JMINUS AND JPLUS
498 . S JM=@THEXML@(I-1) ; LINE BEFORE
499 . I JM["</text>" S INTXT=0 ; LEFT HTML SECTION,START TRIM
500 . S JP=@THEXML@(I+1) ; LINE AFTER
501 . I INTXT=0 D ; IF NOT IN AN HTML SECTION
502 . . S JPX=$TR(JP,"/","") ; REMOVE THE SLASH
503 . . I J=JPX D ; AN EMPTY ELEMENT ON TWO LINES
504 . . . I DEBUG W I,J,JP,!
505 . . . S FOUND=1 ; FOUND SOMETHING TO BE DELETED
506 . . . S DEL(I)="" ; SET LINE TO DELETE
507 . . . S DEL(I+1)="" ; SET NEXT LINE TO DELETE
508 . . I J["><" D ; AN EMPTY ELEMENT ON ONE LINE
509 . . . I DEBUG W I,J,!
510 . . . S FOUND=1 ; FOUND SOMETHING TO BE DELETED
511 . . . S DEL(I)="" ; SET THE EMPTY LINE UP TO BE DELETED
512 . . . I JM=JPX D ;
513 . . . . I DEBUG W I,JM_J_JPX,!
514 . . . . S DEL(I-1)=""
515 . . . . S DEL(I+1)="" ; SET THE SURROUNDING LINES FOR DEL
516 ; . I J'["><" D PUSH("TMPXML",J)
517 I FOUND D ; NEED TO DELETE THINGS
518 . F I=1:1:@THEXML@(0) D ; COPY ARRAY LEAVING OUT DELELTED LINES
519 . . I '$D(DEL(I)) D ; IF THE LINE IS NOT DELETED
520 . . . D PUSH("TMPXML",@THEXML@(I)) ; COPY TO TMPXML ARRAY
521 . D CP("TMPXML",THEXML) ; REPLACE THE XML WITH THE COPY
522 Q FOUND
523 ;
524UNMARK(XSEC) ; REMOVE MARKUP FROM FIRST AND LAST LINE OF XML
525 ; XSEC IS A SECTION PASSED BY NAME
526 N XBLD,XTMP
527 D QUEUE("XBLD",XSEC,2,@XSEC@(0)-1) ; BUILD LIST FOR INNER XML
528 D BUILD("XBLD","XTMP") ; BUILD THE RESULT
529 D CP("XTMP",XSEC) ; REPLACE PASSED XML
530 Q
531 ;
532PARY(GLO) ;PRINT AN ARRAY
533 N I
534 F I=1:1:@GLO@(0) W I_" "_@GLO@(I),!
535 Q
536 ;
537H2ARY(IARYRTN,IHASH,IPRE) ; CONVERT IHASH TO RETURN ARRAY
538 ; IPRE IS OPTIONAL PREFIX FOR THE ELEMENTS. USED FOR MUPTIPLES 1^"VAR"^VALUE
539 I '$D(IPRE) S IPRE=""
540 N H2I S H2I=""
541 ; W $O(@IHASH@(H2I)),!
542 F S H2I=$O(@IHASH@(H2I)) Q:H2I="" D ; FOR EACH ELEMENT OF THE HASH
543 . I $QS(H2I,$QL(H2I))="M" D Q ; SPECIAL CASE FOR MULTIPLES
544 . . ;W H2I_"^"_@IHASH@(H2I),!
545 . . N IH,IHI
546 . . S IH=$NA(@IHASH@(H2I)) ;
547 . . S IH2A=$O(@IH@("")) ; SKIP OVER MULTIPLE DISCRIPTOR
548 . . S IH2=$NA(@IH@(IH2A)) ; PAST THE "M","DIRETIONS" FOR EXAMPLE
549 . . S IHI="" ; INDEX INTO "M" MULTIPLES
550 . . F S IHI=$O(@IH2@(IHI)) Q:IHI="" D ; FOR EACH SUB-MULTIPLE
551 . . . ; W @IH@(IHI)
552 . . . S IH3=$NA(@IH2@(IHI))
553 . . . ; W "HEY",IH3,!
554 . . . D H2ARY(.IARYRTN,IH3,IPRE_";"_IHI) ; RECURSIVE CALL - INDENTED ELEMENTS
555 . . ; W IH,!
556 . . ; W "C0CZZ",!
557 . . ; W $NA(@IHASH@(H2I)),!
558 . . Q ;
559 . D PUSH(IARYRTN,IPRE_"^"_H2I_"^"_@IHASH@(H2I))
560 . ; W @IARYRTN@(0),!
561 Q
562 ;
563XVARS(XVRTN,XVIXML) ; RETURNS AN ARRAY XVRTN OF ALL UNIQUE VARIABLES
564 ; DEFINED IN INPUT XML XVIXML BY @@VAR@@
565 ; XVRTN AND XVIXML ARE PASSED BY NAME
566 ;
567 N XVI,XVTMP,XVT
568 F XVI=1:1:@XVIXML@(0) D ; FOR ALL LINES OF THE XML
569 . S XVT=@XVIXML@(XVI)
570 . I XVT["@@" S XVTMP($P(XVT,"@@",2))=XVI
571 D H2ARY(XVRTN,"XVTMP")
572 Q
573 ;
574DXVARS(DXIN) ;DISPLAY ALL VARIABLES IN A TEMPLATE
575 ; IF PARAMETERS ARE NULL, DEFAULTS TO CCR TEMPLATE
576 ;
577 N DXUSE,DTMP ; DXUSE IS NAME OF VARIABLE, DTMP IS VARIABLE IF NOT SUPPLIED
578 I DXIN="CCR" D ; NEED TO GO GET CCR TEMPLATE
579 . D LOAD^C0CCCR0("DTMP") ; LOAD CCR TEMPLATE INTO DXTMP
580 . S DXUSE="DTMP" ; DXUSE IS NAME
581 E I DXIN="CCD" D ; NEED TO GO GET CCD TEMPLATE
582 . D LOAD^C0CCCD1("DTMP") ; LOAD CCR TEMPLATE INTO DXTMP
583 . S DXUSE="DTMP" ; DXUSE IS NAME
584 E S DXUSE=DXIN ; IF PASSED THE TEMPLATE TO USE
585 N DVARS ; PUT VARIABLE NAME RESULTS IN ARRAY HERE
586 D XVARS("DVARS",DXUSE) ; PULL OUT VARS
587 D PARY^C0CXPATH("DVARS") ;AND DISPLAY THEM
588 Q
589 ;
590TEST ; Run all the test cases
591 D TESTALL^C0CUNIT("C0CXPAT0")
592 Q
593 ;
594ZTEST(WHICH) ; RUN ONE SET OF TESTS
595 N ZTMP
596 S DEBUG=1
597 D ZLOAD^C0CUNIT("ZTMP","C0CXPAT0")
598 D ZTEST^C0CUNIT(.ZTMP,WHICH)
599 Q
600 ;
601TLIST ; LIST THE TESTS
602 N ZTMP
603 D ZLOAD^C0CUNIT("ZTMP","C0CXPAT0")
604 D TLIST^C0CUNIT(.ZTMP)
605 Q
606 ;
Note: See TracBrowser for help on using the repository browser.