-- loading default group and bookeeping files
[bkell-clj.git] / src / xpath_handler.clj
blob9582445236ca943a7f94669ed43ba3d07f0ea929
2 (ns xpath_handler 
3   
4   (:import com.interrupt.cc.xpath.lexer.Lexer) 
5         (:import com.interrupt.cc.xpath.lexer.LexerException) 
6         (:import com.interrupt.cc.xpath.node.Node) 
7         (:import com.interrupt.cc.xpath.node.Start) 
8         (:import com.interrupt.cc.xpath.parser.Parser) 
9         (:import com.interrupt.cc.xpath.parser.ParserException) 
10   (:import com.interrupt.cc.xpath.analysis.DepthFirstAdapter) 
11   (:import com.interrupt.bob.processor.cc.DepthFirstVisitor)
12   
13   (:import java.io.InputStreamReader)
14   (:import java.io.PushbackReader) 
15   (:import java.io.ByteArrayInputStream) 
16   (:import java.util.ArrayList)
17   
18   (:use helpers) 
19   
20   (:require clojure.xml)
21   
25         ;; remove the SableCC added <spaces> and ` 
26         (defn filter-xpath-input [input-string]
27                         (clojure.contrib.string/replace-str " " "" 
28                         (clojure.contrib.string/replace-str "`" "" input-string))
29         )
30         
31         ;; build the XPath parser  
32         (defn get-pushback-parser [xpath-string] 
33                 (Parser. (Lexer. (PushbackReader. (InputStreamReader. 
34                                                                                                                                                                 (ByteArrayInputStream. (. xpath-string getBytes))) 
35                                                                                                                                                                 1024)))
36         )
37         
38         ;; the XPath string that the proxy / adapter will use to figure out context directory & XPath expression 
39         ;; (def XPATH-string (ref ""))
40         (def xpath-data (ref {})) 
41         
42         ;; we're gonna build our eXist URL with this 
43         (def URL-build (ref "")) 
44         
45         ;; pilfered stack ideas and some implementation from: http://programming-puzzler.blogspot.com/2009/04/adts-in-clojure.html 
46         (def stack (ref [])) 
47         (defn stack-peek [] 
48                 (peek (deref stack)))
49         (defn stack-push [e] 
50                 (dosync (alter stack conj e)) 
51                 (println "PUSH stack [" (deref stack) "]" ))
52         (defn stack-pop [] 
53                 (dosync (alter stack pop)))
54         (defn stack-empty? [] 
55                 (dosync (alter stack empty?)))
56         
57         
58         (defn get-url-midpoint [] 
59                 (+ 
60                         (.      (deref URL-build) lastIndexOf                           ;; get the position of substring
61                                 (:context-parent (deref xpath-data)))            
62                         (. (:context-parent (deref xpath-data)) length))        ;; plus the char length of the leaf document name
63         )
64         (defn get-xpath-part-midpoint [] 
65                          
66                         (let [b_index 
67                                         (+ 1 
68                                                 (.      (:xpath-string (deref xpath-data)) indexOf 
69                                                         (. (:context-parent (deref xpath-data)) substring 
70                                                                 0 
71                                                                 (. (:context-parent (deref xpath-data)) indexOf ".")))) ]
72                         
73                                                 (. (:xpath-string (deref xpath-data)) substring 
74                                                         (+ 1 (. (:xpath-string (deref xpath-data)) indexOf "/" (+ 1 b_index)))
75                                                 )
76                         )       
77         )       
78         
79         ;; get DepthFirstAdapter proxy 
80         (defn get-adapter-proxy [] 
81                 
82                 (proxy [DepthFirstAdapter] [] 
83                         
84                         ;; keep a stack with 
85                         ;;      i.      AbbrevRoot 
86                         ;;      ii.     Word 
87                         ;;                              - keep the last/previous token 
88                         ;;      iii.    RelativePath 
89                         ;;      iv.     Predicate 
90                         (caseTAbbrevRootDesc [node] 
91                                 
92                                 (println "caseTAbbrevRootDesc CALLED \t\t\t\t class[" (. node getClass) "] \t\t\t\t" (. node toString))
93                                 (stack-push node)
94                                 
95                         )
96                         (caseTLetter [node] 
97                                 
98                                 (println "caseTLetter CALLED \t\t\t\t\t class[" (. node getClass) "] \t\t\t\t\t" (. node toString))
99                                 (stack-push node)
100                                 
101                         )
102                         (caseAPredicatelist [node] 
103                                 
104                                 (proxy-super inAPredicatelist node)     ;; duplicating adapter 'in' call 
105                     
106                                 (println "caseAPredicatelist CALLED \t\t\t\t class[" (. node getClass) "] \t\t\t\t" (. node toString) "\t\t filtered " (filter-xpath-input (. node toString)))
107                                 (doseq [ each_predicate (java.util.ArrayList. (. node getPredicate)) ] 
108                                         (do
109                                                 
110                                                 (println "DEBUG > each predicate... " each_predicate " predicate expresion[" (. each_predicate getExpr) 
111                                                                         "] getExprsingle[" (.. each_predicate getExpr getExprsingle) "] ugghhhh!! ["    ;; this is where = breaks off: getComparisonexpr -here- getComparisonexprPart
112                                                                                         ;;(.. each_predicate getExpr getExprsingle getOrexpr getAndexpr getComparisonexpr getComparisonexprPart getRangeexpr) "]" )
113                                                                                         (.. each_predicate getExpr getExprsingle getOrexpr getAndexpr getComparisonexpr getRangeexpr) "]" )
114                                                 
115                                                 ;; LATER - DON'T traverse children & evaluate... for now 
116                                                 ;;(. each_predicate apply this)
117                                                 
118                                                 
119                                                 ;; ** here we are assuming there's only one predicate in the list - getting the 'name' and 'value' 
120                                                 (def predicate-name 
121                                                                 (clojure.contrib.string/replace-str "@" "" 
122                                                                         (clojure.contrib.string/replace-str " " "" 
123                                                                                 (.. each_predicate getExpr getExprsingle getOrexpr getAndexpr getComparisonexpr getRangeexpr toString)))) 
124                                                 (def predicate-value 
125                                                                 (clojure.contrib.string/replace-str "'" "" 
126                                                                         (clojure.contrib.string/replace-str " " "" 
127                                                                                 (.. each_predicate getExpr getExprsingle getOrexpr getAndexpr getComparisonexpr getComparisonexprPart getRangeexpr toString))))
128                                                 (println "DEBUG > predicate-name[" predicate-name "] > predicate-value[" predicate-value "]")
129                                                 
130                                                 
131                                                 ;; (peek, then..) pop 'TLetter' & 'RelativePathexpr' 
132                                                 (def top (stack-peek))
133                                                 (stack-pop)     ;; pop the token 
134                                                 (stack-pop)     ;; LATER - pop the relativepathpart - we'll have to assume that there's a relative_path_part... for now  
135                                                 
136                                                 (println "top of stack["top"] > class["(. top getClass)"]")
137                                                 (cond 
138                                                         (instance? com.interrupt.cc.xpath.node.TAbbrevRootDesc top ) 
139                                                                 '() 
140                                                         
141                                                         (instance? com.interrupt.cc.xpath.node.TLetter top ) 
142                                                                 (dosync 
143                                                                         (alter URL-build str (clojure.contrib.string/replace-str " " "" (. top toString) ) "." predicate-value)
144                                                                         (alter URL-build str "/"))
145                                                         
146                                                         ;;(instance? com.interrupt.cc.xpath.node.ARootRelativepathexprPartPart  top ) 
147                                                         ;;      (dosync (alter URL-build str "/"))
148                                                         
149                                                         (instance? com.interrupt.cc.xpath.node.APredicatelist top ) 
150                                                                 '() 
151                                                 )
152                                                 
153                                                 ;; put in a check to see if we are at the leaf document
154                                                 (println "leaf check [" (. (clojure.contrib.string/trim (. top toString)) equals (:leaf-node (deref xpath-data))) "] > top[" (clojure.contrib.string/trim (. top toString)) "] > leaf-node[" (:leaf-node (deref xpath-data)) "]") 
155                                                 
156                                                 (if (. (clojure.contrib.string/trim (. top toString)) equals (:leaf-node (deref xpath-data)))
157                                                         
158                                                         (do     ;; IF portion here 
159                                                                 (def thing 
160                                                                         (. (deref URL-build) substring  ;; get a substring of our long exist URL 
161                                                                                                 0 
162                                                                                                 (get-url-midpoint)
163                                                                                                 ;;(+
164                                                                                                 ;;              (.      (deref URL-build) lastIndexOf                           ;; get the position of substring
165                                                                                                 ;;                      (:context-parent (deref xpath-data)))            
166                                                                                                 ;;              (. (:context-parent (deref xpath-data)) length))        ;; plus the char length of the leaf document name 
167                                                                                         ))
168                                                                 
169                                                                 ;; write out context directory 
170                                                                 (dosync 
171                                                                         (alter xpath-data conj 
172                                                                                 {       :context-dir thing})
173                                                                         
174                                                                         ;; write out leaf document 
175                                                                         (def b_index    (+      (.      (deref URL-build) lastIndexOf 
176                                                                                                                                                                 (:context-parent (deref xpath-data)))   
177                                                                                                                                                                 (. (:context-parent (deref xpath-data)) length))) 
178                                                                         
179                                                                         (println "b_index[" b_index "] > +1[" (+ 1 b_index) "] > :context-parent["(:context-parent (deref xpath-data))"] > URL-build["
180                                                                                         (deref URL-build)"] > indexOf '/' [" (. (deref URL-build) indexOf "/") "] > FINAL["
181                                                                                         (. (deref URL-build) indexOf "/" (+ 1 b_index))"] > 'if' check["(< (. (deref URL-build) indexOf "/" (+ 1 b_index)) 0 )"]")
182                                                                                         
183                                                                                         
184                                                                         (if (> (. (deref URL-build) indexOf "/" (+ 1 b_index)) 0 )
185                                                                                  
186                                                                                 (alter xpath-data conj                  ;; if context directory is NOT the same as leaf document 
187                                                                                         {               :leaf-document-name 
188                                                                                                         (. (deref URL-build) substring 
189                                                                                                                 (+ 1 b_index) 
190                                                                                                                 (. (deref URL-build) indexOf "/" (+ 1 b_index)))
191                                                                                         })
192                                                                                 
193                                                                                 (alter xpath-data conj                  ;; if context directory IS the same as leaf document 
194                                                                                         {               :leaf-document-name 
195                                                                                                         (str (:leaf-node (deref xpath-data)) "." predicate-value )
196                                                                                         }
197                                                                                 )
198                                                                                 
199                                                                         )
200                                                                 )
201                                                                 ;;(println "---> We are at the leaf document[" (deref xpath-data) "]" )
202                                                                 )
203                                                         
204                                                         (dosync (alter xpath-data conj  ;; ELSE, get the child XPath part
205                                                                 {       :xpath-part     (get-xpath-part-midpoint) })) 
206                                                         
207                                                 )
208                                         )
209                                 )
210                                 (println "URL-build[" (deref URL-build) "]")
211                                 (println)
212                                 (println)
213                                 
214                                 (proxy-super outAPredicatelist node)    ;; duplicating adapter 'out' call 
215                                 
216                         )
217                         (caseARootRelativepathexprPartPart [node] 
218                                 
219                                 (println "caseARootRelativepathexprPartPart CALLED \t\t class[" (. node getClass) "] \t\t\t\t" (. node toString)) 
220                                 (stack-push node) 
221                                 
222                         )
223                 )
224         ) 
225         
226         
227 (defn xpath_handler [node handler] 
228    (if (instance? com.interrupt.bookkeeping.cc.node.AXpathCommandInput (. node getCommandInput) ) 
229                 
230                 (do 
231                    
232                    (println "xpath_handler > node[" (.. node getClass) "] > getLoad[" (.. node getLoad getText) "]")
233                    (println "XPATH input[" (.. node getCommandInput toString) "]")
234                    
235                    ;; 1. filter out <spaces> and ` 
236                    (def input-string (filter-xpath-input (.. node getCommandInput toString)))
237                    (println "input-string \t[" input-string "]")
238                    ;;(println "stripped XPath \t[" (clojure.contrib.string/replace-re #"\\[[^\\]]*\\]" "" input-string) "]" )
239                    
240                          ;; 1.1 
241                          (dosync 
242                                         
243                                         ;; put in whole xpath string 
244                                         (alter xpath-data conj { :xpath-string input-string } )
245                                         
246                                         ;; for token substring between last / and [ 
247                                         (alter xpath-data conj { 
248                                                         :leaf-node 
249                                                         (.      (:xpath-string (deref xpath-data)) substring 
250                                                                         (+ (. (:xpath-string (deref xpath-data)) lastIndexOf "/") 1)
251                                                                         (. (:xpath-string (deref xpath-data)) lastIndexOf "[")) 
252                                                 })
253                                         
254                                         ;; with token, lookup context directory 
255                                         (alter xpath-data conj { :context-parent (working-dir-lookup (:leaf-node (deref xpath-data))) } )
256                                         
257                                         (println "LEAF token > " (:leaf-node (deref xpath-data)))
258                                         (println "LEAF context parent > " (:context-parent (deref xpath-data)))
259                          )
260                          
261                          ;; 1.2 build an xpath parser    
262                          (def tree (.parse (get-pushback-parser input-string))) 
263                          
264                          ;; 2. token - find i) leaf document to search ii) root & xpath expression to feed to RESTful  exist 
265                    (. tree apply (get-adapter-proxy ) )
266                          
267                          ;; 3. build RESTful call 
268                    (def db-query (str "_wrap=no&_query=" 
269                             "declare default element namespace '"(namespace-lookup (:leaf-node (deref xpath-data))) "';" 
270                             
271                             ;; TODO - check if we need 'and' conditions 
272                             ;; "**/<token>[ @option='option_value' [ and @option='option_value' ] ]" 
273                             "//"(:leaf-node (deref xpath-data))"[ @" predicate-name "='" predicate-value "']" 
274                             )
275                             
276                    )
277                    
278                    
279                    (def db-full-PARENT (str db-base-URL "rootDir/" (:context-dir (deref xpath-data))))
280                    
281                    ;; 4. make RESTful call  &  5. pass result sequece to handler
282                    
283                    (let [xml_string (execute-http-call db-full-PARENT (:leaf-document-name (deref xpath-data)) db-query)]
284                                 (handler (clojure.xml/parse (ByteArrayInputStream. (.getBytes xml_string "UTF-8"))))
285                    )
286                    
287                    ;;
288                    
289                    ;;(handler (xml-seq 
290                    ;;                   (execute-http-call db-full-PARENT (:leaf-document-name (deref xpath-data)) db-query)))
291                    
292                    
293                    ;;(with-out-str (clojure.xml/emit
294                    ;;))
295                    
296                 )
297    )