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

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

updates to web services support

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