1 | <%-- $Id: test_with_file.jsp 5949 2012-02-08 14:35:29Z nicklas $ |
---|
2 | ------------------------------------------------------------------ |
---|
3 | Copyright (C) 2006 Johan Enell, Nicklas Nordborg |
---|
4 | Copyright (C) 2007 Nicklas Nordborg |
---|
5 | |
---|
6 | This file is part of BASE - BioArray Software Environment. |
---|
7 | Available at http://base.thep.lu.se/ |
---|
8 | |
---|
9 | BASE is free software; you can redistribute it and/or |
---|
10 | modify it under the terms of the GNU General Public License |
---|
11 | as published by the Free Software Foundation; either version 3 |
---|
12 | of the License, or (at your option) any later version. |
---|
13 | |
---|
14 | BASE is distributed in the hope that it will be useful, |
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | GNU General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with BASE. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | ------------------------------------------------------------------ |
---|
22 | |
---|
23 | @author Nicklas |
---|
24 | @version 2.0 |
---|
25 | --%> |
---|
26 | <%@ page pageEncoding="UTF-8" session="false" |
---|
27 | contentType="text/html; charset=UTF-8" |
---|
28 | import="net.sf.basedb.core.SessionControl" |
---|
29 | import="net.sf.basedb.core.DbControl" |
---|
30 | import="net.sf.basedb.core.Item" |
---|
31 | import="net.sf.basedb.core.Config" |
---|
32 | import="net.sf.basedb.core.Location" |
---|
33 | import="net.sf.basedb.util.parser.FlatFileParser" |
---|
34 | import="net.sf.basedb.clients.web.Base" |
---|
35 | import="net.sf.basedb.util.Values" |
---|
36 | import="net.sf.basedb.plugins.util.Parameters" |
---|
37 | import="net.sf.basedb.clients.web.util.HTML" |
---|
38 | import="java.nio.charset.Charset" |
---|
39 | %> |
---|
40 | <%@ taglib prefix="base" uri="/WEB-INF/base.tld" %> |
---|
41 | <%@ taglib prefix="m" uri="/WEB-INF/menu.tld" %> |
---|
42 | |
---|
43 | <% |
---|
44 | final SessionControl sc = Base.getExistingSessionControl(pageContext, true); |
---|
45 | final String ID = sc.getId(); |
---|
46 | final DbControl dc = sc.newDbControl(); |
---|
47 | try |
---|
48 | { |
---|
49 | String preQuote = "(?!\"x\"y)"; |
---|
50 | String postQuote = "(?=\"x\"y)"; |
---|
51 | %> |
---|
52 | <base:page type="popup" title="Test with file"> |
---|
53 | <base:head scripts="plugin.js,menu.js,ajax.js,json2.js" styles="menu.css"> |
---|
54 | <script language="JavaScript"> |
---|
55 | var timer = 0; |
---|
56 | var inCall = false; |
---|
57 | function adjustIFrameSize() |
---|
58 | { |
---|
59 | if (inCall) return; |
---|
60 | inCall = true; |
---|
61 | var iframeElement = document.getElementById("idParsed"); |
---|
62 | // Find the top coordinate of the IFrame |
---|
63 | var offsetTop = 1; |
---|
64 | var offsetTrail = iframeElement; |
---|
65 | while (offsetTrail) |
---|
66 | { |
---|
67 | offsetTop += offsetTrail.offsetTop; |
---|
68 | offsetTrail = offsetTrail.offsetParent; |
---|
69 | } |
---|
70 | |
---|
71 | // Find the browser window height |
---|
72 | var windowHeight = Main.getWindowHeight(); |
---|
73 | |
---|
74 | // Set iframe height |
---|
75 | var height = windowHeight-offsetTop - 80 * getScale(); |
---|
76 | iframeElement.height = height; |
---|
77 | |
---|
78 | var iframe = window.frames['parsed']; |
---|
79 | var width = Main.getWindowWidth(iframe); |
---|
80 | var parsedFile = iframe.document.getElementById('parsedFile.content'); |
---|
81 | if (parsedFile) |
---|
82 | { |
---|
83 | parsedFile.style.height = (height-40)+'px'; |
---|
84 | parsedFile.style.width = (width-20)+'px'; |
---|
85 | } |
---|
86 | inCall = false; |
---|
87 | } |
---|
88 | function setTimer() |
---|
89 | { |
---|
90 | if (inCall) return; |
---|
91 | if (timer) clearTimeout(timer); |
---|
92 | timer = setTimeout('adjustIFrameSize()', 150); |
---|
93 | } |
---|
94 | |
---|
95 | function browseOnClick() |
---|
96 | { |
---|
97 | var frm = document.forms['testwithfile']; |
---|
98 | var url = '../../filemanager/index.jsp?ID=<%=ID%>&cmd=SelectOne&title=Test+with+file&callback=setFileCallback'; |
---|
99 | Main.openPopup(url, 'SelectFile', 1050, 700); |
---|
100 | } |
---|
101 | function setFileCallback(fileId, path) |
---|
102 | { |
---|
103 | var frm = document.forms['testwithfile']; |
---|
104 | frm.file_id.value = fileId; |
---|
105 | frm.path.value = path; |
---|
106 | updateCharset(fileId); |
---|
107 | } |
---|
108 | function updateCharset(fileId) |
---|
109 | { |
---|
110 | var request = Ajax.getXmlHttpRequest(); |
---|
111 | if (request != null) |
---|
112 | { |
---|
113 | var url = '../../filemanager/files/ajax.jsp?ID=<%=ID%>&cmd=GetFileInfo&item_id=' + fileId; |
---|
114 | request.open("GET", url, true); |
---|
115 | Ajax.setReadyStateHandler(request, updateCharsetCallback); |
---|
116 | request.send(null); |
---|
117 | } |
---|
118 | return request != null; |
---|
119 | } |
---|
120 | function updateCharsetCallback(request) |
---|
121 | { |
---|
122 | var response = Ajax.parseJsonResponse(request.responseText); |
---|
123 | var charset = response.characterSet; |
---|
124 | if (charset) |
---|
125 | { |
---|
126 | var frm = document.forms['testwithfile']; |
---|
127 | Forms.selectListOption(frm.charset, charset); |
---|
128 | } |
---|
129 | } |
---|
130 | function showPredefined(event, menu) |
---|
131 | { |
---|
132 | Menu.toggleTopMenu(document.getElementById(menu), event.clientX, event.clientY); |
---|
133 | event.cancelBubble = true; |
---|
134 | } |
---|
135 | var preQuote = '(?!")'; |
---|
136 | var postQuote = '(?=")'; |
---|
137 | var whiteSpaceOrQuote = '[\\s\\"]*'; |
---|
138 | var notInsideQoute = '(?=(?:[^"]*"[^"]*")*(?![^"]*"))'; |
---|
139 | function setRegexp(field, regexp) |
---|
140 | { |
---|
141 | var frm = document.forms['testwithfile']; |
---|
142 | frm[field].value = regexp; |
---|
143 | } |
---|
144 | function doParse() |
---|
145 | { |
---|
146 | var frm = document.forms['testwithfile']; |
---|
147 | if (Main.trimString(frm.path.value) == '') |
---|
148 | { |
---|
149 | frm.path.focus(); |
---|
150 | alert('You must select a file'); |
---|
151 | return false; |
---|
152 | } |
---|
153 | else if (Main.trimString(frm.dataSplitter.value) == '') |
---|
154 | { |
---|
155 | frm.dataSplitter.focus(); |
---|
156 | alert('You must specify a data splitter regular expression'); |
---|
157 | return false; |
---|
158 | } |
---|
159 | frm.submit(); |
---|
160 | } |
---|
161 | |
---|
162 | var parameterNames = new Array(); |
---|
163 | function init() |
---|
164 | { |
---|
165 | adjustIFrameSize(); |
---|
166 | var frm = document.forms['testwithfile']; |
---|
167 | var configWin = window.opener; |
---|
168 | frm.path.value = configWin.getParameterValue('file'); |
---|
169 | frm.header.value = configWin.getParameterValue('headerRegexp'); |
---|
170 | frm.dataHeader.value = configWin.getParameterValue('dataHeaderRegexp'); |
---|
171 | frm.dataSplitter.value = configWin.getParameterValue('dataSplitterRegexp'); |
---|
172 | frm.dataFooter.value = configWin.getParameterValue('dataFooterRegexp'); |
---|
173 | frm.ignore.value = configWin.getParameterValue('ignoreRegexp'); |
---|
174 | frm.minDataColumns.value = configWin.getParameterValue('minDataColumns'); |
---|
175 | frm.maxDataColumns.value = configWin.getParameterValue('maxDataColumns'); |
---|
176 | frm.trimQuotes.checked = configWin.getParameterValue('trimQuotes') != 'false'; |
---|
177 | Forms.selectListOption(frm.charset, configWin.getParameterValue('<%=Parameters.CHARSET_PARAMETER%>')); |
---|
178 | |
---|
179 | var allParameters = configWin.Parameters.allParameters; |
---|
180 | for (var i = 0; i < allParameters.length; i++) |
---|
181 | { |
---|
182 | var param = allParameters[i]; |
---|
183 | if (param.name.search('Mapping') >= 0) |
---|
184 | { |
---|
185 | parameterNames[parameterNames.length] = param.name; |
---|
186 | Forms.createHidden(frm, 'mapping.'+param.name+'.label', param.label); |
---|
187 | Forms.createHidden(frm, 'mapping.'+param.name+'.expression', param.values.length == 0 ? '' : param.values[0]); |
---|
188 | } |
---|
189 | } |
---|
190 | if (parameterNames.length > 0) |
---|
191 | { |
---|
192 | Forms.createHidden(frm, 'mappingParameterNames', parameterNames.join(',')); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | function saveSettings() |
---|
197 | { |
---|
198 | var frm = document.forms['testwithfile']; |
---|
199 | var configWin = window.opener; |
---|
200 | configWin.setParameterValue('file', frm.path.value); |
---|
201 | configWin.setParameterValue('headerRegexp', frm.header.value); |
---|
202 | configWin.setParameterValue('dataHeaderRegexp', frm.dataHeader.value); |
---|
203 | configWin.setParameterValue('dataSplitterRegexp', frm.dataSplitter.value); |
---|
204 | configWin.setParameterValue('dataFooterRegexp', frm.dataFooter.value); |
---|
205 | configWin.setParameterValue('ignoreRegexp', frm.ignore.value); |
---|
206 | configWin.setParameterValue('minDataColumns', frm.minDataColumns.value); |
---|
207 | configWin.setParameterValue('maxDataColumns', frm.maxDataColumns.value); |
---|
208 | configWin.setParameterValue('trimQuotes', frm.trimQuotes.checked ? 'true' : 'false'); |
---|
209 | configWin.setParameterValue('<%=Parameters.CHARSET_PARAMETER%>', frm.charset[frm.charset.selectedIndex].value); |
---|
210 | for (var i = 0; i < parameterNames.length; i++) |
---|
211 | { |
---|
212 | var name = parameterNames[i]; |
---|
213 | configWin.setParameterValue(name, frm['mapping.'+name+'.expression'].value); |
---|
214 | } |
---|
215 | window.close(); |
---|
216 | } |
---|
217 | |
---|
218 | function setFormValue(field, value) |
---|
219 | { |
---|
220 | var frm = document.forms['testwithfile']; |
---|
221 | frm.elements[field].value = value; |
---|
222 | } |
---|
223 | |
---|
224 | </script> |
---|
225 | </base:head> |
---|
226 | <base:body attributes="onresize='setTimer();'" onload="init();"> |
---|
227 | |
---|
228 | <m:menu |
---|
229 | id="predefinedSplitters" |
---|
230 | style="display: none;"> |
---|
231 | |
---|
232 | <m:menuitem |
---|
233 | title="Tab" |
---|
234 | onclick="setRegexp('dataSplitter', '\\\\t')" |
---|
235 | tooltip="Splits data on tab" |
---|
236 | /> |
---|
237 | <m:menuitem |
---|
238 | title="Comma" |
---|
239 | onclick="setRegexp('dataSplitter', ',')" |
---|
240 | tooltip="Splits data on comma" |
---|
241 | /> |
---|
242 | <m:menuitem |
---|
243 | title="Semicolon" |
---|
244 | onclick="setRegexp('dataSplitter', ';')" |
---|
245 | tooltip="Splits data on semicolon" |
---|
246 | /> |
---|
247 | <m:menuseparator /> |
---|
248 | <m:menuitem |
---|
249 | title="Tab with quotes" |
---|
250 | onclick="setRegexp('dataSplitter', preQuote+'\\\\t'+postQuote)" |
---|
251 | tooltip="Splits data on tab, but only if it is has quotes before and after" |
---|
252 | /> |
---|
253 | <m:menuitem |
---|
254 | title="Comma with quotes" |
---|
255 | onclick="setRegexp('dataSplitter', preQuote+','+postQuote)" |
---|
256 | tooltip="Splits data on comma, but only if it is has quotes before and after" |
---|
257 | /> |
---|
258 | <m:menuitem |
---|
259 | title="Semicolon with quotes" |
---|
260 | onclick="setRegexp('dataSplitter', preQuote+';'+postQuote)" |
---|
261 | tooltip="Splits data on semicolon, but only if it is has quotes before and after" |
---|
262 | /> |
---|
263 | <m:menuseparator /> |
---|
264 | <m:menuitem |
---|
265 | title="Comma; ignored inside quotes" |
---|
266 | onclick="setRegexp('dataSplitter', ','+notInsideQoute)" |
---|
267 | tooltip="Splits data on comma, but not if they are inside quotes" |
---|
268 | /> |
---|
269 | <m:menuitem |
---|
270 | title="Semicolon; ignored inside quotes" |
---|
271 | onclick="setRegexp('dataSplitter', ';'+notInsideQoute)" |
---|
272 | tooltip="Splits data on semicolon, but not if they are inside quotes" |
---|
273 | /> |
---|
274 | </m:menu> |
---|
275 | |
---|
276 | <m:menu |
---|
277 | id="predefinedHeaders" |
---|
278 | style="display: none;"> |
---|
279 | <m:menuitem |
---|
280 | title="Key = value" |
---|
281 | onclick="setRegexp('header', '(.+)=(.*)')" |
---|
282 | tooltip="Headers have a key and value separated by an equal sign" |
---|
283 | /> |
---|
284 | <m:menuitem |
---|
285 | title="Key = value; remove whitespace and quotes" |
---|
286 | onclick="setRegexp('header', whiteSpaceOrQuote + '(.+?)' + whiteSpaceOrQuote + '=' + whiteSpaceOrQuote + '(.*?)' + whiteSpaceOrQuote)" |
---|
287 | tooltip="Header have a key and value separated by an equal sign; whitespace and quotes are removed" |
---|
288 | /> |
---|
289 | </m:menu> |
---|
290 | |
---|
291 | <m:menu |
---|
292 | id="predefinedIgnore" |
---|
293 | style="display: none;"> |
---|
294 | <m:menuitem |
---|
295 | title="# Comment" |
---|
296 | onclick="setRegexp('ignore', '#.*')" |
---|
297 | tooltip="Ignore all lines starting with #" |
---|
298 | /> |
---|
299 | <m:menuitem |
---|
300 | title="Empty line" |
---|
301 | onclick="setRegexp('ignore', '\\\\s*')" |
---|
302 | tooltip="Ignore all lines that contains nothing but white-space" |
---|
303 | /> |
---|
304 | </m:menu> |
---|
305 | |
---|
306 | <form name="testwithfile" action="parse_file.jsp?ID=<%=ID%>" target="parsed" method="post" onsubmit="return false;"> |
---|
307 | <input type="hidden" name="file_id" value=""> |
---|
308 | |
---|
309 | <h3 class="docked">Test with file <base:help helpid="runplugin.testwithfile" /></h3> |
---|
310 | <div class="boxed"> |
---|
311 | <table class="form"> |
---|
312 | <tr > |
---|
313 | <td class="prompt">File to test</td> |
---|
314 | <td colspan="3"> |
---|
315 | <table border="0" cellspacing="0" cellpadding="0"> |
---|
316 | <tr> |
---|
317 | <td><input type="text" class="text required" name="path" size="60" value=""> </td> |
---|
318 | <td><base:button |
---|
319 | title="Browse…" |
---|
320 | onclick="browseOnClick()" |
---|
321 | /> |
---|
322 | </td> |
---|
323 | <td> </td> |
---|
324 | <td><base:button title="Parse the file" onclick="doParse()" image="gonext.png"/></td> |
---|
325 | </tr> |
---|
326 | </table> |
---|
327 | </td> |
---|
328 | </tr> |
---|
329 | <tr > |
---|
330 | <td class="prompt">Lines to parse</td> |
---|
331 | <td> |
---|
332 | <input type="text" class="text" name="maxLines" size="12" maxlength="10" |
---|
333 | value="<%=FlatFileParser.DEFAULT_MAX_UNKNOWN_LINES%>" |
---|
334 | onkeypress="return Numbers.integerOnly(event)"> |
---|
335 | </td> |
---|
336 | <td class="prompt">Character set</td> |
---|
337 | <td> |
---|
338 | <select name="charset"> |
---|
339 | <% |
---|
340 | String defaultCharset = Config.getCharset(); |
---|
341 | int numCommon = Config.getCommonCharsets().size(); |
---|
342 | int i = 0; |
---|
343 | for (String charset : Config.getAllCharsets()) |
---|
344 | { |
---|
345 | %> |
---|
346 | <option value="<%=charset%>" |
---|
347 | <%=defaultCharset.equalsIgnoreCase(charset) ? "selected" : ""%> |
---|
348 | <%=i==numCommon ? "style=\"border-top: 1px solid #666666;\"" : "" %>><%=charset%> |
---|
349 | <% |
---|
350 | i++; |
---|
351 | } |
---|
352 | %> |
---|
353 | </select> |
---|
354 | </td> |
---|
355 | </tr> |
---|
356 | <tr > |
---|
357 | <td class="prompt">Header regexp</td> |
---|
358 | <td> |
---|
359 | <table border="0" cellspacing="0" cellpadding="0"> |
---|
360 | <tr> |
---|
361 | <td> |
---|
362 | <input type="text" class="text" name="header" size="40"> |
---|
363 | </td> |
---|
364 | <td> |
---|
365 | <base:button title="Predefined…" |
---|
366 | onclick="showPredefined(event, 'predefinedHeaders')" /> |
---|
367 | </td> |
---|
368 | </tr> |
---|
369 | </table> |
---|
370 | </td> |
---|
371 | <td class="prompt">Min data columns</td> |
---|
372 | <td> |
---|
373 | <input type="text" class="text" name="minDataColumns" size="12" maxlength="10" |
---|
374 | onkeypress="return Numbers.integerOnly(event)"> |
---|
375 | </td> |
---|
376 | </tr> |
---|
377 | <tr > |
---|
378 | <td class="prompt">Data splitter regexp</td> |
---|
379 | <td> |
---|
380 | <table border="0" cellspacing="0" cellpadding="0"> |
---|
381 | <tr> |
---|
382 | <td> |
---|
383 | <input type="text" class="text required" name="dataSplitter" size="40"> |
---|
384 | </td> |
---|
385 | <td> |
---|
386 | <base:button title="Predefined…" |
---|
387 | onclick="showPredefined(event, 'predefinedSplitters')" /> |
---|
388 | </td> |
---|
389 | </tr> |
---|
390 | </table> |
---|
391 | </td> |
---|
392 | <td class="prompt">Max data columns</td> |
---|
393 | <td> |
---|
394 | <input type="text" class="text" name="maxDataColumns" size="12" maxlength="10" |
---|
395 | onkeypress="return Numbers.integerOnly(event)"> |
---|
396 | </td> |
---|
397 | </tr> |
---|
398 | <tr > |
---|
399 | <td class="prompt">Ignore regexp</td> |
---|
400 | <td> |
---|
401 | <table border="0" cellspacing="0" cellpadding="0"> |
---|
402 | <tr> |
---|
403 | <td> |
---|
404 | <input type="text" class="text" name="ignore" size="40"> |
---|
405 | </td> |
---|
406 | <td> |
---|
407 | <base:button title="Predefined…" |
---|
408 | onclick="showPredefined(event, 'predefinedIgnore')" /> |
---|
409 | </td> |
---|
410 | </tr> |
---|
411 | </table> |
---|
412 | </td> |
---|
413 | <td class="prompt"><label for="trimQuotes">Remove quotes</label></td> |
---|
414 | <td><input type="checkbox" name="trimQuotes" id="trimQuotes" value="1"></td> |
---|
415 | </tr> |
---|
416 | <tr > |
---|
417 | <td class="prompt">Data header regexp</td> |
---|
418 | <td> |
---|
419 | <input type="text" class="text" name="dataHeader" size="40"> |
---|
420 | </td> |
---|
421 | </tr> |
---|
422 | <tr > |
---|
423 | <td class="prompt">Data footer regexp</td> |
---|
424 | <td> |
---|
425 | <input type="text" class="text" name="dataFooter" size="40"> |
---|
426 | </td> |
---|
427 | </tr> |
---|
428 | </table> |
---|
429 | <div align="right"> <i><base:icon image="required.png" />= required information</i></div> |
---|
430 | |
---|
431 | <iframe name="parsed" id="idParsed" src="parse_file.jsp?ID=<%=ID%>" width="100%" |
---|
432 | frameborder="0" vspace="0" hspace="0" |
---|
433 | marginwidth="0" marginheight="0" scrolling="no" style="overflow: hidden;;"></iframe> |
---|
434 | |
---|
435 | |
---|
436 | </div> |
---|
437 | </form> |
---|
438 | |
---|
439 | <table align="center"> |
---|
440 | <tr> |
---|
441 | <td width="50%"><base:button onclick="saveSettings();" title="Ok" /></td> |
---|
442 | <td width="50%"><base:button onclick="window.close();" title="Cancel" /></td> |
---|
443 | </tr> |
---|
444 | </table> |
---|
445 | </base:body> |
---|
446 | </base:page> |
---|
447 | <% |
---|
448 | } |
---|
449 | finally |
---|
450 | { |
---|
451 | if (dc != null) dc.close(); |
---|
452 | } |
---|
453 | %> |
---|
454 | |
---|