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)
13 (:import java.io.InputStreamReader)
14 (:import java.io.PushbackReader)
15 (:import java.io.ByteArrayInputStream)
16 (:import java.util.ArrayList)
23 ;; remove the SableCC added <spaces> and `
24 (defn filter-xpath-input [input-string]
25 (clojure.contrib.string/replace-str " " ""
26 (clojure.contrib.string/replace-str "`" "" input-string))
29 ;; build the XPath parser
30 (defn get-pushback-parser [xpath-string]
31 (Parser. (Lexer. (PushbackReader. (InputStreamReader.
32 (ByteArrayInputStream. (. xpath-string getBytes)))
36 ;; the XPath string that the proxy / adapter will use to figure out context directory & XPath expression
37 ;; (def XPATH-string (ref ""))
38 (def xpath-data (ref {}))
40 ;; we're gonna build our eXist URL with this
41 (def URL-build (ref ""))
43 ;; pilfered stack ideas and some implementation from: http://programming-puzzler.blogspot.com/2009/04/adts-in-clojure.html
48 (dosync (alter stack conj e))
49 (println "PUSH stack [" (deref stack) "]" ))
51 (dosync (alter stack pop)))
53 (dosync (alter stack empty?)))
56 ;; get DepthFirstAdapter proxy
57 (defn get-adapter-proxy []
59 (proxy [DepthFirstAdapter] []
64 ;; - keep the last/previous token
67 (caseTAbbrevRootDesc [node]
69 (println "caseTAbbrevRootDesc CALLED \t\t\t\t class[" (. node getClass) "] \t\t\t\t" (. node toString))
75 (println "caseTLetter CALLED \t\t\t\t\t class[" (. node getClass) "] \t\t\t\t\t" (. node toString))
79 (caseAPredicatelist [node]
81 (proxy-super inAPredicatelist node) ;; duplicating adapter 'in' call
83 (println "caseAPredicatelist CALLED \t\t\t\t class[" (. node getClass) "] \t\t\t\t" (. node toString) "\t\t filtered " (filter-xpath-input (. node toString)))
84 (doseq [ each_predicate (java.util.ArrayList. (. node getPredicate)) ]
87 (println "DEBUG > each predicate... " each_predicate " predicate expresion[" (. each_predicate getExpr)
88 "] getExprsingle[" (.. each_predicate getExpr getExprsingle) "] ugghhhh!! [" ;; this is where = breaks off: getComparisonexpr -here- getComparisonexprPart
89 ;;(.. each_predicate getExpr getExprsingle getOrexpr getAndexpr getComparisonexpr getComparisonexprPart getRangeexpr) "]" )
90 (.. each_predicate getExpr getExprsingle getOrexpr getAndexpr getComparisonexpr getRangeexpr) "]" )
92 ;; LATER - DON'T traverse children & evaluate... for now
93 ;;(. each_predicate apply this)
96 ;; ** here we are assuming there's only one predicate in the list
98 (clojure.contrib.string/replace-str "@" ""
99 (clojure.contrib.string/replace-str " " ""
100 (.. each_predicate getExpr getExprsingle getOrexpr getAndexpr getComparisonexpr getRangeexpr toString))))
102 (clojure.contrib.string/replace-str "'" ""
103 (clojure.contrib.string/replace-str " " ""
104 (.. each_predicate getExpr getExprsingle getOrexpr getAndexpr getComparisonexpr getComparisonexprPart getRangeexpr toString))))
105 (println "DEBUG > predicate-name[" predicate-name "] > predicate-value[" predicate-value "]")
108 ;; (peek, then..) pop 'TLetter' & 'RelativePathexpr'
109 (def top (stack-peek))
111 ;; put in a check to see if we are at the leaf document
112 (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)) "]")
113 (if (. (clojure.contrib.string/trim (. top toString)) equals (:leaf-node (deref xpath-data)))
115 (println "---> We are at the leaf document " )
116 ;; TODO - write out context directory
117 ;; TODO - write out leaf document
118 ;; TODO - begin writing out XPath expression
122 (stack-pop) ;; pop the token
123 (stack-pop) ;; LATER - pop the relativepathpart - we'll have to assume that there's a relative_path_part... for now
125 (println "top of stack["top"] > class["(. top getClass)"]")
127 (instance? com.interrupt.cc.xpath.node.TAbbrevRootDesc top )
130 (instance? com.interrupt.cc.xpath.node.TLetter top )
132 (alter URL-build str (clojure.contrib.string/replace-str " " "" (. top toString) ) "." predicate-value)
133 (alter URL-build str "/"))
135 ;;(instance? com.interrupt.cc.xpath.node.ARootRelativepathexprPartPart top )
136 ;; (dosync (alter URL-build str "/"))
138 (instance? com.interrupt.cc.xpath.node.APredicatelist top )
143 (println "URL-build[" (deref URL-build) "]")
148 ;;* at the end of processing the XPath
149 ;; - capture leaf document
150 ;; - build XPath expr to feed to RESTful exist
153 (proxy-super outAPredicatelist node) ;; duplicating adapter 'out' call
156 (caseARootRelativepathexprPartPart [node]
158 (println "caseARootRelativepathexprPartPart CALLED \t\t class[" (. node getClass) "] \t\t\t\t" (. node toString))
166 (defn xpath_handler [node handler]
167 (if (instance? com.interrupt.bookkeeping.cc.node.AXpathCommandInput (. node getCommandInput) )
171 (println "xpath_handler > node[" (.. node getClass) "] > getLoad[" (.. node getLoad getText) "]")
172 (println "XPATH input[" (.. node getCommandInput toString) "]")
174 ;; 1. filter out <spaces> and `
175 (def input-string (filter-xpath-input (.. node getCommandInput toString)))
176 (println "input-string \t[" input-string "]")
177 (println "stripped XPath \t[" (clojure.contrib.string/replace-re #"\\[[^\\]]*\\]" "" input-string) "]" )
182 ;; put in whole xpath string
183 (alter xpath-data conj { :xpath-string input-string } )
185 ;; for token substring between last / and [
186 (alter xpath-data conj {
188 (. (:xpath-string (deref xpath-data)) substring
189 (+ (. (:xpath-string (deref xpath-data)) lastIndexOf "/") 1)
190 (. (:xpath-string (deref xpath-data)) lastIndexOf "["))
193 ;; with token, lookup context directory
194 (alter xpath-data conj { :context-dir (working-dir-lookup (:leaf-node (deref xpath-data))) } )
196 (println "LEAF token > " (:leaf-node (deref xpath-data)))
197 (println "LEAF context directory > " (:context-dir (deref xpath-data)))
200 ;; 1.2 build an xpath parser
201 (def tree (.parse (get-pushback-parser input-string)))
202 (. tree apply (get-adapter-proxy ) )
204 ;; 2. token - find i) leaf document to search ii) root & xpath expression to feed to RESTful exist
205 ;; 3. build RESTful call
206 ;; 4. make RESTful call
207 ;; 5. pass result sequece to handler
209 ;; extract the context
210 (let [ result_seq []]
212 ;; operate with handler