source: ccr/trunk/p/C0CMXP.m@ 727

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

update to template compiler

File size: 8.6 KB
Line 
1C0CMXP ; GPL - MXML based XPath utilities;12/04/09 17:05
2 ;;0.1;C0C;nopatch;noreleasedate
3 ;Copyright 2009 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 Q
21 ;
22INITXPF(ARY) ;INITIAL XML/XPATH FILE ARRAY
23 ; DON'T USE THIS ONE ... USE INITFARY^C0CSOAP("FARY") INSTEAD
24 D INITFARY^C0CSOAP(ARY) ;
25 Q
26 S @ARY@("XML FILE NUMBER")=178.101
27 S @ARY@("XML SOURCE FIELD")=2.1
28 S @ARY@("XML TEMPLATE FIELD")=3
29 S @ARY@("XPATH BINDING SUBFILE")=178.1014
30 S @ARY@("REDUX FIELD")=2.5
31 Q
32 ;
33SETXPF(ARY) ; SET FILE AND FIELD VARIABLES FROM XPF ARRAY
34 ;
35 S C0CXPF=@ARY@("XML FILE NUMBER")
36 S C0CXFLD=@ARY@("XML")
37 S C0CXTFLD=@ARY@("TEMPLATE XML")
38 S C0CXPBF=@ARY@("BINDING SUBFILE NUMBER")
39 S C0CRDUXF=@ARY@("XPATH REDUCTION STRING")
40 Q
41 ;
42ADDXP(INARY,TID,FARY) ;ADD XPATH .01 FIELD TO BINDING SUBFILE OF TEMPLATE TID
43 I '$D(FARY) D ;
44 . S FARY="FARY" ; FILE ARRAY
45 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
46 D SETXPF(FARY) ;SET FILE VARIABLES
47 N C0CA,C0CB
48 S C0CA="" S C0CB=0
49 F S C0CA=$O(@INARY@(C0CA)) Q:C0CA="" D ; FOR EACH XPATH
50 . S C0CB=C0CB+1 ; COUNT OF XPATHS
51 . S C0CFDA(C0CXPBF,"?+"_C0CB_","_TID_",",.01)=C0CA
52 . D UPDIE ; CREATE THE BINDING SUBFILE FOR THIS XPATH
53 Q
54 ;
55FIXICD9 ; FIX THE ICD9RESULT XML
56 D GETXML("GPL","ICD9RESULT") ; GET SOME BAD XML OUT OF THE FILE
57 S ZI=""
58 S G=""
59 F S ZI=$O(GPL(ZI)) Q:ZI="" D ; FOR EACH LINE
60 . S G=G_GPL(ZI) ; MAKE ONE BIG STRING OF XML
61 D NORMAL^C0CSOAP("G2","G") ;NO NORMALIZE IT BACK INTO AN ARRAY
62 D ADDXML("G2","ICD9RESULT") ; AND PUT IT BACK
63 Q
64ADDXML(INXML,TEMPID,INFARY) ;ADD XML TO A TEMPLATE ID TEMPID
65 ; INXML IS PASSED BY NAME
66 I '$D(INFARY) D ;
67 . S INFARY="FARY" ; FILE ARRAY
68 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
69 I +TEMPID=0 S TEMPID=$$RESTID^C0CSOAP(TEMPID,INFARY) ;RESOLVE TEMPLATE NAME
70 D SETXPF(INFARY) ;SET FILE VARIABLES
71 D WP^DIE(C0CXPF,TEMPID_",",C0CXFLD,,INXML)
72 Q
73 ;
74ADDTEMP(INXML,TEMPID,INFARY) ;ADD XML TEMPLATE TO TEMPLATE RECORD TEMPID
75 ;
76 I '$D(INFARY) D ;
77 . S INFARY="FARY" ; FILE ARRAY
78 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
79 I +TEMPID=0 S TEMPID=$$RESTID^C0CSOAP(TEMPID,INFARY) ;RESOLVE TEMPLATE NAME
80 D SETXPF(INFARY) ;SET FILE VARIABLES
81 D WP^DIE(C0CXPF,TEMPID_",",C0CXTFLD,,INXML)
82 Q
83 ;
84GETXML(OUTXML,TEMPID,INFARY) ;GET THE XML FROM TEMPLATE TEMPID
85 ;
86 I '$D(INFARY) D ;
87 . S INFARY="FARY" ; FILE ARRAY
88 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
89 D SETXPF(INFARY) ;SET FILE VARIABLES
90 I +TEMPID=0 S TEMPID=$$RESTID^C0CSOAP(TEMPID,INFARY) ;RESOLVE TEMPLATE NAME
91 I $$GET1^DIQ(C0CXPF,TEMPID_",",C0CXFLD,,OUTXML)'=OUTXML D Q ;
92 . W "ERROR RETRIEVING TEMPLATE",!
93 Q
94 ;
95GETTEMP(OUTXML,TEMPID,FARY) ;GET THE TEMPLATE XML FROM TEMPLATE TEMPID
96 ;
97 I '$D(FARY) D ;
98 . S FARY="FARY" ; FILE ARRAY
99 . D INITXPF("FARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
100 D SETXPF(FARY) ;SET FILE VARIABLES
101 I +TEMPID=0 S TEMPID=$$RESTID^C0CSOAP(TEMPID,FARY) ;RESOLVE TEMPLATE NAME
102 I $$GET1^DIQ(C0CXPF,TEMPID_",",C0CXTFLD,,OUTXML)'=OUTXML D Q ;
103 . W "ERROR RETRIEVING TEMPLATE",!
104 Q
105 ;
106COMPILE(TID,UFARY) ; COMPILES AN XML TEMPLATE AND GENERATES XPATH BINDINGS
107 ; UFARY IF SPECIFIED WILL REDIRECT THE XML FILE TO USE
108 ; INTID IS THE IEN OF THE RECORD TO USE IN THE XML FILE
109 ; XML IS PULLED FROM THE "XML" FIELD AND THE COMPILED RESULT PUT
110 ; IN THE "XML TEMPLATE" FIELD. ALL XPATHS USED IN THE TEMPLATE
111 ; WILL BE POPULATED TO THE XPATH BINDINGS SUBFILE AS .01
112 I '$D(UFARY) D ;
113 . S UFARY="DEFFARY" ; FILE ARRAY
114 . ;D INITXPF("UFARY") ;IF FILE ARRAY NOT PASSED, INITIALIZE
115 . D INITFARY^C0CSOAP(UFARY)
116 D SETXPF(UFARY) ;SET FILE VARIABLES
117 I +TID=0 S INTID=$$RESTID^C0CSOAP(TID,UFARY)
118 E S INTID=TID
119 ;B
120 ;N C0CXML,C0CREDUX,C0CTEMP,C0CIDX
121 D GETXML("C0CXML",INTID,UFARY)
122 S C0CREDUX=$$GET1^DIQ(C0CXPF,INTID_",",C0CRDUXF,"E") ;XPATH REDUCTION STRING
123 D MKTPLATE("C0CTEMP","C0CIDX","C0CXML",C0CREDUX) ; CREATE TEMPLATE AND IDX
124 D ADDTEMP("C0CTEMP",INTID,UFARY) ; WRITE THE TEMPLATE TO FILE
125 D ADDXP("C0CIDX",INTID,UFARY) ;CREATE XPATH SUBFILE ENTRIES FOR EVERY XPATH
126 Q
127 ;
128MKTPLATE(OUTT,OUTIDX,INXML,REDUX) ;MAKE A TEMPLATE FROM INXML, RETURNED IN OUTT
129 ; BOTH PASSED BY NAME. THE REDUX XPATH REDUCTION STRING IS USED IF PASSED
130 ; OUTIDX IS AN ARRAY OF THE XPATHS USED IN MAKING THE TEMPLATE
131 ;
132 S C0CXLOC=$NA(^TMP("C0CXML",$J))
133 K @C0CXLOC
134 M @C0CXLOC=@INXML
135 S C0CDOCID=$$PARSE^C0CMXML(C0CXLOC,"C0CMKT")
136 K @C0CXLOC
137 S C0CDOM=$NA(^TMP("MXMLDOM",$J,C0CDOCID))
138 ;N GIDX,GIDX2,GARY,GARY2
139 I '$D(REDUX) S REDUX=""
140 D XPATH^C0CMXML(1,"/","GIDX","GARY",,REDUX)
141 D INVERT("GIDX2","GIDX") ;MAKE ARRAY TO LOOK UP XPATH BY NODE
142 N ZI,ZD S ZI=""
143 F S ZI=$O(@C0CDOM@(ZI)) Q:ZI="" D ; FOR EACH NODE IN THE DOM
144 . K ZD ;FOR DATA
145 . D DATA^C0CMXML("ZD",ZI) ;SEE IF THERE IS DATA FOR THIS NODE
146 . ;I $D(ZD(1)) D ; IF YES
147 . I $$FIRST^C0CMXML(ZI)=0 D ; IF THERE ARE NO CHILDREN TO THIS NODE
148 . . ;I ZI<3 B ;W !,ZD(1)
149 . . K @C0CDOM@(ZI,"T") ; KILL THE DATA
150 . . N ZXPATH
151 . . S ZXPATH=$G(GIDX2(ZI)) ;FIND AN XPATH FOR THIS NODE
152 . . S @C0CDOM@(ZI,"T",1)="@@"_ZXPATH_"@@"
153 . . I ZXPATH'="" S @OUTIDX@(ZXPATH)="" ; PASS BACK XPATH USED IN IDX
154 D OUTXML^C0CMXML(OUTT,C0CDOCID)
155 Q
156 ;
157INVERT(OUTX,INX) ;INVERTS AN XPATH INDEX RETURNING @OUTX@(x)=XPath from
158 ; @INX@(XPath)=x
159 N ZI S ZI=""
160 F S ZI=$O(@INX@(ZI)) Q:ZI="" D ;FOR EACH XPATH IN THE INPUT
161 . S @OUTX@(@INX@(ZI))=ZI ; SET INVERTED ENTRY
162 Q
163 ;
164DEMUX(OUTX,INX) ;PARSES XPATH PASSED BY VALUE IN INX TO REMOVE [x] MULTIPLES
165 ; RETURNS OUTX: MULTIPLE^SUBMULTIPLE^XPATH
166 N ZX,ZY,ZZ,ZZ1,ZMULT,ZSUB
167 S (ZMULT,ZSUB)=""
168 S ZX=$P(INX,"[",2)
169 I ZX'="" D ; THERE IS A [x] MULTIPLE
170 . S ZY=$P(INX,"[",1) ;FIRST PART OF XPATH
171 . S ZMULT=$P(ZX,"]",1) ; NUMBER OF THE MULTIPLE
172 . S ZX=ZY_$P(ZX,"]",2) ; REST OF THE XPATH
173 . I $P(ZX,"[",2)'="" D ; A SUB MULTIPLE EXISTS
174 . . S ZZ=$P(ZX,"[",1) ; FIRST PART OF XPATH
175 . . S ZX=$P(ZX,"[",2) ; DELETE THE [
176 . . S ZSUB=$P(ZX,"]",1) ; NUMBER OF THE SUBMULTIPLE
177 . . S ZX=ZZ_$P(ZX,"]",2) ; REST OF THE XPATH
178 E S ZX=INX ;NO MULTIPLE HERE
179 S @OUTX=ZMULT_"^"_ZSUB_"^"_ZX ;RETURN MULTIPLE^SUBMULTIPLE^XPATH
180 Q
181 ;
182DEMUXARY(OARY,IARY,DEPTH) ;CONVERT AN XPATH ARRAY PASSED AS IARY TO
183 ; FORMAT @OARY@(x,variablename) where x is the first multiple
184 ; IF DEPTH=2, THE LAST 2 PARTS OF THE XPATH WILL BE USED
185 N ZI,ZJ,ZK,ZL,ZM S ZI=""
186 F S ZI=$O(@IARY@(ZI)) Q:ZI="" D ;
187 . D DEMUX^C0CMXP("ZJ",ZI)
188 . S ZK=$P(ZJ,"^",3)
189 . S ZM=$RE($P($RE(ZK),"/",1))
190 . I $G(DEPTH)=2 D ;LAST TWO PARTS OF XPATH USED FOR THE VARIABLE NAME
191 . . S ZM=$RE($P($RE(ZK),"/",2))_ZM
192 . S ZL=$P(ZJ,"^",1)
193 . I ZL="" S ZL=1
194 . I $D(@OARY@(ZL,ZM)) D ;IT'S A DUP
195 . . S @OARY@(ZL,ZM_"[2]")=@IARY@(ZI)
196 . E S @OARY@(ZL,ZM)=@IARY@(ZI)
197 Q
198 ;
199DEMUXXP1(OARY,IARY) ;IARY IS INCOMING XPATH ARRAY
200 ; BOTH IARY AND OARY ARE PASSED BY NAME
201 ; RETURNS A SIMPLE XPATH ARRAY WITHOUT MULTIPLES. DUPLICATES ARE REMOVED
202 N ZI,ZJ,ZK
203 S ZI=""
204 F S ZI=$O(@IARY@(ZI)) Q:ZI="" D ; FOR EACH XPATH IN IARY
205 . D DEMUX^C0CMXP("ZJ",ZI)
206 . S ZK=$P(ZJ,"^",3) ;THE XPATH
207 . S @OARY@(ZK)=@IARY@(ZI) ;THE RESULT. DUPLICATES WILL NOT SHOW
208 . ; CAUTION, IF THERE ARE MULTIPLES, ONLY THE DATA FOR THE LAST
209 . ; MULTIPLE WILL BE INCLUDED IN THE OUTPUT ARRAY, ASSIGNED TO THE
210 . ; COMMON XPATH
211 Q
212 ;
213DEMUXXP2(OARY,IARY) ; IARY AND OARY ARE PASSED BY NAME
214 ; IARY IS AN XPATH ARRAY THAT MAY CONTAIN MULTIPLES
215 ; OARY IS THE OUTPUT ARRAY WHERE MULTIPLES ARE RETURNED IN THE FORM
216 ; @OARY@(x,Xpath)=data or @OARY@(x,y,Xpath)=data WHERE x AND y ARE
217 ; THE MULTIPLES AND Xpath IS THE BASE XPATH WITHOUT [x] AND [y]
218 ;
219 N ZI,ZJ,ZK,ZX,ZY,ZP
220 S ZI=""
221 F S ZI=$O(@IARY@(ZI)) Q:ZI="" D ; FOR EACH INPUT XPATH
222 . D DEMUX("ZJ",ZI) ; PULL OUT THE MULTIPLES
223 . S ZX=$P(ZJ,"^",1) ;x
224 . S ZY=$P(ZJ,"^",2) ;y
225 . S ZP=$P(ZJ,"^",3) ;Xpath
226 . I ZX="" S ZX=1 ; NO MULTIPLE WILL STORE IN x=1
227 . I ZY'="" D ;IS THERE A y?
228 . . S @OARY@(ZX,ZY,ZP)=@IARY@(ZI)
229 . E D ;NO y
230 . . S @OARY@(ZX,ZP)=@IARY@(ZI)
231 Q
232 ;
233UPDIE ; INTERNAL ROUTINE TO CALL UPDATE^DIE AND CHECK FOR ERRORS
234 K ZERR
235 D CLEAN^DILF
236 D UPDATE^DIE("","C0CFDA","","ZERR")
237 I $D(ZERR) D ;
238 . W "ERROR",!
239 . ZWR ZERR
240 . B
241 K C0CFDA
242 Q
243 ;
Note: See TracBrowser for help on using the repository browser.