1 | package eu.clarin.cmdi.mdservice.model; |
---|
2 | |
---|
3 | import java.io.StringWriter; |
---|
4 | |
---|
5 | import javax.xml.transform.OutputKeys; |
---|
6 | import javax.xml.transform.Transformer; |
---|
7 | import javax.xml.transform.TransformerConfigurationException; |
---|
8 | import javax.xml.transform.TransformerException; |
---|
9 | import javax.xml.transform.TransformerFactory; |
---|
10 | import javax.xml.transform.dom.DOMSource; |
---|
11 | import javax.xml.transform.stream.StreamResult; |
---|
12 | |
---|
13 | import org.w3c.dom.Document; |
---|
14 | import org.w3c.dom.Element; |
---|
15 | import org.w3c.dom.Node; |
---|
16 | import org.w3c.dom.NodeList; |
---|
17 | |
---|
18 | import eu.clarin.cmdi.mdservice.internal.Admin; |
---|
19 | import eu.clarin.cmdi.mdservice.action.DCRProxyAction; |
---|
20 | |
---|
21 | |
---|
22 | public class SearchClauses{ |
---|
23 | private String xml_string; |
---|
24 | private Document xml_document; |
---|
25 | private SearchClause[][] list; |
---|
26 | |
---|
27 | |
---|
28 | public SearchClauses(Document _doc){ |
---|
29 | list = new SearchClause[20][20]; |
---|
30 | |
---|
31 | for(int i=0;i<20;i++){ |
---|
32 | for(int j=0;j<20;j++){ |
---|
33 | list[i][j]=null; |
---|
34 | } |
---|
35 | } |
---|
36 | xml_document= _doc; |
---|
37 | Admin.notifyUser("SCCCCC: constuctor"); |
---|
38 | deserialize(); |
---|
39 | // do processing |
---|
40 | } |
---|
41 | |
---|
42 | // create array from xml_document |
---|
43 | private int getLastAnd(){ |
---|
44 | for(int i=0; i< 20;i++){ |
---|
45 | if (list[i][0] == null) return i; |
---|
46 | } |
---|
47 | return 0; |
---|
48 | } |
---|
49 | private int getLastOr(int i){ |
---|
50 | for(int j=0;j< 20;j++){ |
---|
51 | if (list[i][j] == null) return j; |
---|
52 | } |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | private void deserialize(Node node, int level){ |
---|
57 | NodeList nodeList; |
---|
58 | String b; |
---|
59 | SearchClause sc; |
---|
60 | int or_last; |
---|
61 | |
---|
62 | if (node.getNodeName().equals("tripple")){ |
---|
63 | nodeList = node.getChildNodes(); |
---|
64 | b = nodeList.item(0).getFirstChild().getNodeValue(); |
---|
65 | deserialize(b,nodeList.item(1), nodeList.item(2), level); |
---|
66 | }else{ |
---|
67 | if (node.getNodeName().equals("searchClause")){ |
---|
68 | nodeList = node.getChildNodes(); |
---|
69 | sc = new SearchClause(nodeList.item(0).getNodeValue(), nodeList.item(1).getFirstChild().getNodeValue(), nodeList.item(2).getNodeValue()); |
---|
70 | or_last = getLastOr(level); |
---|
71 | list[level][or_last] = sc; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|
76 | private void deserialize(String rel, Node left, Node right, int and_level){ |
---|
77 | String b; |
---|
78 | NodeList nodeList; |
---|
79 | SearchClause sc; |
---|
80 | int and_last; |
---|
81 | |
---|
82 | if (rel.equals("and")) { |
---|
83 | deserialize(left, and_level); |
---|
84 | and_last = getLastAnd(); |
---|
85 | deserialize(right, and_last); |
---|
86 | } |
---|
87 | if (rel.equals("or")) { |
---|
88 | deserialize(left, and_level); |
---|
89 | deserialize(right, and_level); |
---|
90 | } |
---|
91 | |
---|
92 | } |
---|
93 | private void deserialize(){ |
---|
94 | Node node; |
---|
95 | NodeList nodeList; |
---|
96 | String b; |
---|
97 | SearchClause sc; |
---|
98 | |
---|
99 | node = xml_document.getFirstChild(); |
---|
100 | if (node.getNodeName().equals("tripple")){ |
---|
101 | nodeList = node.getChildNodes(); |
---|
102 | b = nodeList.item(0).getFirstChild().getNodeValue(); |
---|
103 | deserialize(b,nodeList.item(1), nodeList.item(2),0); |
---|
104 | }else{ |
---|
105 | Admin.notifyUser("SCCCCC: deserialize"); |
---|
106 | if (node.getNodeName().equals("searchClause")){ |
---|
107 | |
---|
108 | nodeList = node.getChildNodes(); |
---|
109 | sc = new SearchClause(nodeList.item(1).getTextContent().trim(), nodeList.item(3).getTextContent().trim(), nodeList.item(5).getTextContent().trim()); |
---|
110 | list[0][0] = sc; |
---|
111 | } |
---|
112 | } |
---|
113 | Admin.notifyUser("SCCCCC: list " + list[0][0].getIndex() + list[0][0].getValue()); |
---|
114 | |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | // create xml_document from array-list |
---|
119 | // create xml-string from xml_document |
---|
120 | private Element serialize(SearchClause sc){ |
---|
121 | Element e, root, e_rel; |
---|
122 | |
---|
123 | root = xml_document.createElement("searchClause"); |
---|
124 | e = xml_document.createElement("index"); |
---|
125 | e.setNodeValue(sc.getIndex()); |
---|
126 | root.appendChild(e); |
---|
127 | e_rel = xml_document.createElement("relation"); |
---|
128 | //e.setNodeValue(list[0][0].getRelation()); |
---|
129 | root.appendChild(e_rel); |
---|
130 | e = xml_document.createElement("term"); |
---|
131 | e.setNodeValue(sc.getValue()); |
---|
132 | root.appendChild(e); |
---|
133 | |
---|
134 | e = xml_document.createElement("value"); |
---|
135 | e.setNodeValue(sc.getValue()); |
---|
136 | e_rel.appendChild(e); |
---|
137 | |
---|
138 | return root; |
---|
139 | } |
---|
140 | |
---|
141 | private Element serialize(Element e1, Element e2, String operand){ |
---|
142 | Element e, root, e_rel; |
---|
143 | |
---|
144 | root = xml_document.createElement("tripple"); |
---|
145 | e_rel = xml_document.createElement("boolean"); |
---|
146 | e = xml_document.createElement("value"); |
---|
147 | e.setNodeValue(operand); |
---|
148 | e_rel.appendChild(e); |
---|
149 | root.appendChild(e_rel); |
---|
150 | |
---|
151 | e = xml_document.createElement("leftOperand"); |
---|
152 | e.appendChild(e1); |
---|
153 | root.appendChild(e); |
---|
154 | |
---|
155 | e = xml_document.createElement("rightOperand"); |
---|
156 | e.appendChild(e2); |
---|
157 | root.appendChild(e); |
---|
158 | |
---|
159 | return root; |
---|
160 | } |
---|
161 | |
---|
162 | private void serialize(){ |
---|
163 | Node node; |
---|
164 | Element root, e, e_rel, ej, ej_last = null; |
---|
165 | |
---|
166 | // remove old document |
---|
167 | Admin.notifyUser("SCCCCC: serialize"); |
---|
168 | Admin.notifyUser("SCCCCC: " + xml_document.getChildNodes().getLength()); |
---|
169 | xml_document.removeChild(xml_document.getFirstChild()); |
---|
170 | Admin.notifyUser("SCCCCC: CLEARED " + xml_document.getChildNodes().getLength()); |
---|
171 | |
---|
172 | if ((getLastAnd() == 1) && (getLastOr(0) == 1)){ |
---|
173 | Admin.notifyUser("SCCCCC: searchClause "); |
---|
174 | root = xml_document.createElement("searchClause"); |
---|
175 | e = xml_document.createElement("index"); |
---|
176 | e.setTextContent(list[0][0].getIndex()); |
---|
177 | root.appendChild(e); |
---|
178 | e_rel = xml_document.createElement("relation"); |
---|
179 | root.appendChild(e_rel); |
---|
180 | e = xml_document.createElement("term"); |
---|
181 | e.setTextContent(list[0][0].getValue()); |
---|
182 | root.appendChild(e); |
---|
183 | |
---|
184 | e = xml_document.createElement("value"); |
---|
185 | e.setTextContent(list[0][0].getRelation()); |
---|
186 | e_rel.appendChild(e); |
---|
187 | |
---|
188 | } else { |
---|
189 | root = xml_document.createElement("tripple"); |
---|
190 | |
---|
191 | for(int i=0;i<20;i++){ |
---|
192 | if (list[i][0] == null){ |
---|
193 | break; |
---|
194 | } |
---|
195 | ej = serialize(list[i][0]); |
---|
196 | for(int j=1;j<20;j++){ |
---|
197 | if (list[i][j] == null){ |
---|
198 | break; |
---|
199 | } |
---|
200 | e = serialize(list[i][j]); |
---|
201 | ej = serialize(ej,e,"or"); |
---|
202 | } |
---|
203 | if (ej_last != null){ |
---|
204 | ej_last = serialize(ej_last, ej, "and"); |
---|
205 | } |
---|
206 | ej_last = ej; |
---|
207 | } |
---|
208 | } |
---|
209 | xml_document.appendChild(root); |
---|
210 | |
---|
211 | |
---|
212 | ////////////////// to XML-string |
---|
213 | |
---|
214 | |
---|
215 | //set up a transformer |
---|
216 | TransformerFactory transfac = TransformerFactory.newInstance(); |
---|
217 | Transformer trans; |
---|
218 | try { |
---|
219 | trans = transfac.newTransformer(); |
---|
220 | trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
---|
221 | trans.setOutputProperty(OutputKeys.INDENT, "yes"); |
---|
222 | |
---|
223 | //create string from xml tree |
---|
224 | StringWriter sw = new StringWriter(); |
---|
225 | StreamResult result = new StreamResult(sw); |
---|
226 | DOMSource source = new DOMSource(xml_document); |
---|
227 | try { |
---|
228 | trans.transform(source, result); |
---|
229 | } catch (TransformerException e1) { |
---|
230 | // TODO Auto-generated catch block |
---|
231 | e1.printStackTrace(); |
---|
232 | } |
---|
233 | xml_string = sw.toString(); |
---|
234 | Admin.notifyUser("SERIALIZED-XML: " + xml_string); |
---|
235 | } catch (TransformerConfigurationException e1) { |
---|
236 | // TODO Auto-generated catch block |
---|
237 | e1.printStackTrace(); |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | } |
---|
242 | |
---|
243 | public String toXML(){ |
---|
244 | serialize(); |
---|
245 | return xml_string; |
---|
246 | } |
---|
247 | |
---|
248 | //TODO processing |
---|
249 | // replace ISOCAT - category by elements |
---|
250 | // optimize |
---|
251 | // remove duplicates |
---|
252 | |
---|
253 | public void process(){ |
---|
254 | DCRProxyAction a = new DCRProxyAction(); |
---|
255 | //TODO format, actionkey - |
---|
256 | |
---|
257 | try { |
---|
258 | //a.execute(); |
---|
259 | //TODO update list structure according ELEMENTS |
---|
260 | } catch (Exception e) { |
---|
261 | // TODO Auto-generated catch block |
---|
262 | e.printStackTrace(); |
---|
263 | } |
---|
264 | //insert into a[i] |
---|
265 | |
---|
266 | |
---|
267 | } |
---|
268 | |
---|
269 | } |
---|