01: /*
02: * LobFileParameterParser.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.util;
13:
14: import java.io.FileNotFoundException;
15: import java.util.regex.Matcher;
16: import java.util.regex.Pattern;
17:
18: /**
19: * A class to analyze the {$blobfile= } and {$clobfile= }
20: * parameters in a SQL statement. This class supports INSERT and UPDATE
21: * statements. To retrieve a blob from the database {@link workbench.sql.wbcommands.WbSelectBlob}
22: * has to be used.
23: * @author support@sql-workbench.net
24: */
25: public class LobFileParameterParser {
26: private final String MARKER = "\\{\\$[cb]lobfile=";
27: private final Pattern MARKER_PATTERN = Pattern.compile(MARKER,
28: Pattern.CASE_INSENSITIVE);
29: private LobFileParameter[] parameters;
30: private int parameterCount = 0;
31:
32: public LobFileParameterParser(String sql)
33: throws FileNotFoundException {
34: Matcher m = MARKER_PATTERN.matcher(sql);
35: if (!m.find())
36: return;
37:
38: // Calculate number of parameters
39: parameterCount++;
40: while (m.find()) {
41: parameterCount++;
42: }
43: m.reset();
44: parameters = new LobFileParameter[parameterCount];
45: int index = 0;
46: WbStringTokenizer tok = new WbStringTokenizer(" \t", false,
47: "\"'", false);
48: int lastStart = 0;
49: while (m.find()) {
50: int start = m.start();
51: int end = sql.indexOf("}", start + 1);
52: if (end > -1) {
53: lastStart = end + 1;
54: String parm = sql.substring(start + 2, end);
55: tok.setSourceString(parm);
56: parameters[index] = new LobFileParameter();
57: while (tok.hasMoreTokens()) {
58: String s = tok.nextToken();
59: String arg = null;
60: String value = null;
61: int pos = s.indexOf("=");
62: if (pos > -1) {
63: arg = s.substring(0, pos);
64: value = s.substring(pos + 1);
65: }
66: if ("encoding".equals(arg)) {
67: parameters[index].setEncoding(value);
68: } else {
69: parameters[index].setFilename(value);
70: parameters[index].setBinary("blobfile"
71: .equals(arg));
72: }
73: }
74: }
75: index++;
76: }
77: }
78:
79: public LobFileParameter[] getParameters() {
80: return parameters;
81: }
82:
83: }
|