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

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

fixing a bug

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