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

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

non-multiples will be "1" not "0" in array:

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