From: Santi B. <san...@us...> - 2008-01-14 23:40:34
|
Update of /cvsroot/babeldoc/babeldoc/modules/scanner/src/com/babeldoc/scanner/worker In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv8326/modules/scanner/src/com/babeldoc/scanner/worker Modified Files: SqlScanner.java Log Message: Revision on SqlScanner for multi-document processing, each row in a separate document. Added internationalization for catalan(ca). Index: SqlScanner.java =================================================================== RCS file: /cvsroot/babeldoc/babeldoc/modules/scanner/src/com/babeldoc/scanner/worker/SqlScanner.java,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** SqlScanner.java 21 Dec 2004 17:03:06 -0000 1.19 --- SqlScanner.java 14 Jan 2008 23:40:34 -0000 1.20 *************** *** 87,101 **** import com.babeldoc.scanner.ScannerWorkerInfo; - /** ! * Scanner that scans SQL databases. The document can be constructed from the ! * select results in a number of ways: * <ul> ! * <li>One document as csv document made of all results returned</li> ! * <li>One document as xml document made of all results returned</li> ! * <li>One document for every returned row (only first column will be included)</li> * </ul> ! * After selecting results an update statement can be executed (if provided) so ! * rows can be marked as processed (thanks Klaas Eikelboom!) * * @author dejank --- 87,101 ---- import com.babeldoc.scanner.ScannerWorkerInfo; /** ! * Scanner that scans SQL databases. The document can be constructed from the ! * select results in a number of ways: * <ul> ! * <li>One document as csv document made of all results returned</li> ! * <li>One document as xml document made of all results returned</li> ! * <li>One document for every returned row (only first column will be included)</li> ! * <li>Multiple documents, one for each result, in CSV or XML types</li> * </ul> ! * After selecting results an update statement can be executed (if provided) so ! * rows can be marked as processed (thanks Klaas Eikelboom!) * * @author dejank *************** *** 103,136 **** */ public class SqlScanner extends ScannerWorker { ! //Constants ! public static final String RESOURCE_NAME = "resourceName"; ! public static final String SQL_STATEMENT = "sqlStatement"; ! public static final String UPDATE_STATEMENT = "updateStatement"; ! public static final String DOCUMENT_TYPE = "documentType"; ! public static final String FIELD_SEPARATOR = "cvsFieldSeparator"; ! public static final String ROW_SEPARATOR = "csvRowSeparator"; ! public static final String XML_HEADING_TAG = "xmlHeadingTag"; ! public static final String XML_ROW_TAG = "xmlRowTag"; ! ! //legal values for document type ! public static final String DOC_TYPE_SIMPLE = "simple"; ! public static final String DOC_TYPE_XML = "xml"; ! public static final String DOC_TYPE_CSV = "csv"; ! //csv separators. This could be config option too ! public static final String DEFAULT_CSV_FIELD_SEP = ","; ! public static final String DEFAULT_CSV_ROW_SEP = "\n"; ! public static final String DEFAULT_XML_HEADING_TAG = "document"; ! public static final String DEFAULT_XML_ROW_TAG = "row"; ! private IResource resource; ! private String documentType = null; ! private String fieldSeparator = DEFAULT_CSV_FIELD_SEP; ! private String headingTag = DEFAULT_XML_HEADING_TAG; ! private String resourceName = null; ! private String rowSeparator = DEFAULT_CSV_ROW_SEP; ! private String rowTag = DEFAULT_XML_ROW_TAG; ! private String sqlStatement = null; ! private String updateStatement = null; public SqlScanner() { --- 103,138 ---- */ public class SqlScanner extends ScannerWorker { ! // Constants ! public static final String RESOURCE_NAME = "resourceName"; ! public static final String SQL_STATEMENT = "sqlStatement"; ! public static final String UPDATE_STATEMENT = "updateStatement"; ! public static final String DOCUMENT_TYPE = "documentType"; ! // TODO: DOCUMENT-ME! multidocument option ! public static final String MULTIDOCUMENT = "multiDocument"; ! public static final String FIELD_SEPARATOR = "cvsFieldSeparator"; ! public static final String ROW_SEPARATOR = "csvRowSeparator"; ! public static final String XML_HEADING_TAG = "xmlHeadingTag"; ! public static final String XML_ROW_TAG = "xmlRowTag"; ! // legal values for document type ! public static final String DOC_TYPE_SIMPLE = "simple"; ! public static final String DOC_TYPE_XML = "xml"; ! public static final String DOC_TYPE_CSV = "csv"; + // csv separators. This could be config option too + public static final String DEFAULT_CSV_FIELD_SEP = ","; + public static final String DEFAULT_CSV_ROW_SEP = "\n"; + public static final String DEFAULT_XML_HEADING_TAG = "document"; + public static final String DEFAULT_XML_ROW_TAG = "row"; + private IResource resource; + private String documentType = null; + private String fieldSeparator = DEFAULT_CSV_FIELD_SEP; + private String headingTag = DEFAULT_XML_HEADING_TAG; + private String resourceName = null; + private String rowSeparator = DEFAULT_CSV_ROW_SEP; + private String rowTag = DEFAULT_XML_ROW_TAG; + private String sqlStatement = null; + private String updateStatement = null; + private boolean multiDocument = false; public SqlScanner() { *************** *** 138,305 **** } ! /** ! * Make one csv document from all rows ! * ! * @param rs ! * ! * @throws Exception ! */ ! public void handleCsvDocument(ResultSet rs) throws Exception { ! int columnCount = rs.getMetaData().getColumnCount(); ! StringBuffer sb = new StringBuffer(); ! ! if (columnCount > 0) { ! while (rs.next()) { ! for (int i = 1; i <= columnCount; i++) { ! sb.append(rs.getObject(i).toString()); ! if (i != columnCount) { ! sb.append(fieldSeparator); ! } else { ! sb.append(rowSeparator); ! } ! } ! } ! this.enqueue(sb.toString().getBytes(), ! new NameValuePair[] {new NameValuePair(SCAN_MIMETYPE_KEY, "text/plain")}); ! } ! } ! /** ! * Get ! * ! * @param rs ! * ! * @throws Exception ! */ ! public void handleXmlDocuments(ResultSet rs) throws Exception { ! int columnCount = rs.getMetaData().getColumnCount(); ! StringBuffer sb = new StringBuffer( ! "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ! int rowCount = 0; ! if (columnCount > 0) { ! sb.append("<" + headingTag + ">\n"); ! while (rs.next()) { ! sb.append("\t<" + rowTag + ">\n"); ! for (int i = 1; i <= columnCount; i++) { ! sb.append("\t\t<" + rs.getMetaData().getColumnLabel(i) + ">"); ! String data = ""; ! if (rs.getObject(i)!=null) { ! data = rs.getObject(i).toString(); ! } ! sb.append("<![CDATA[" + data + "]]>"); ! sb.append("</" + rs.getMetaData().getColumnLabel(i) + ">\n"); ! } ! sb.append("\t</" + rowTag + ">\n"); ! rowCount++; ! } ! sb.append("</" + headingTag + ">\n"); ! // only submit the document if we have any rows in the result set ! if (rowCount > 0) { ! enqueue(sb.toString().getBytes("UTF-8"), ! new NameValuePair[] {new NameValuePair(SCAN_MIMETYPE_KEY, "text/xml")}); ! } ! } ! } ! /** ! * Get the documents from the sql ! * ! * @throws ScannerException DOCUMENT ME! ! */ ! public void doScan() throws ScannerException { ! getDocumentsFromSql(); ! } ! /** ! * Set-up the sql scanner code. ! * ! * @throws ScannerConfigurationException ! */ ! public void initialize() throws ScannerConfigurationException { ! resourceName = this.getInfo().getStrValue(RESOURCE_NAME); ! if (resourceName.equals("")) { ! throw new ScannerConfigurationException(I18n.get("scanner.056"), null); ! } ! sqlStatement = this.getInfo().getStrValue(SQL_STATEMENT); updateStatement = this.getInfo().getStrValue(UPDATE_STATEMENT); ! if (sqlStatement.equals("")) { ! throw new ScannerConfigurationException(I18n.get("scanner.057"), null); ! } ! documentType = this.getInfo().getStrValue(DOCUMENT_TYPE); ! if (DOC_TYPE_CSV.equalsIgnoreCase(documentType)) { ! //getChild cvs options ! fieldSeparator = this.getInfo().getStrValue(FIELD_SEPARATOR); ! rowSeparator = this.getInfo().getStrValue(ROW_SEPARATOR); ! } else if (DOC_TYPE_XML.equalsIgnoreCase(documentType)) { ! //getChild xml options ! headingTag = this.getInfo().getStrValue(XML_HEADING_TAG); ! rowTag = this.getInfo().getStrValue(XML_ROW_TAG); ! } ! } ! /** ! * release the held resource. Do nothing - no held resources. ! */ ! public void relinquishResources() { ! LogService.getInstance().logInfo("relinquishResources"); ! } ! /** ! * Get a connection using the resource name ! * ! * @return ! * ! * @throws ScannerException ! */ ! private Connection getConnection() throws ScannerException { ! try { ! if (resource == null) { ! resource = ResourceFactory.getResource(resourceName); ! } ! return ((Connection) resource.checkOut()); ! } catch (ResourceException re) { ! throw new ScannerException(I18n.get("scanner.055"), re); ! } ! } ! /** ! * This is main method which generates documents from underlying database. ! * ! * @throws ScannerException DOCUMENT ME! ! */ ! private void getDocumentsFromSql() throws ScannerException { ! Connection conn = null; ! Statement stmt = null; ! Statement updstmt = null; ! ResultSet rs = null; ! try { ! conn = getConnection(); ! stmt = conn.createStatement(); ! rs = stmt.executeQuery(sqlStatement); ! ! if (DOC_TYPE_CSV.equalsIgnoreCase(documentType)) { ! handleCsvDocument(rs); ! } else if (DOC_TYPE_XML.equalsIgnoreCase(documentType)) { ! handleXmlDocuments(rs); ! } else { ! handleSimpleDocuments(rs); ! } ! if(updateStatement != null) { updstmt = conn.createStatement(); updstmt.execute(updateStatement); --- 140,330 ---- } ! /** ! * Make one csv document from all rows ! * ! * @param rs ! * ! * @throws Exception ! */ ! public void handleCsvDocument(ResultSet rs) throws Exception { ! int columnCount = rs.getMetaData().getColumnCount(); ! StringBuffer sb = new StringBuffer(); ! if (columnCount > 0) { ! while (rs.next()) { ! for (int i = 1; i <= columnCount; i++) { ! sb.append(rs.getObject(i).toString()); ! if (i != columnCount) { ! sb.append(fieldSeparator); ! } else { ! sb.append(rowSeparator); ! } ! } ! if (multiDocument) { ! this.enqueue(sb.toString().getBytes(), ! new NameValuePair[] { new NameValuePair( ! SCAN_MIMETYPE_KEY, "text/plain") }); ! } ! } + if (!multiDocument) { + this.enqueue(sb.toString().getBytes(), + new NameValuePair[] { new NameValuePair( + SCAN_MIMETYPE_KEY, "text/plain") }); + } + } + } ! /** ! * Make one xml document from all rows ! * ! * @param rs ! * ! * @throws Exception ! */ ! public void handleXmlDocuments(ResultSet rs) throws Exception { ! int columnCount = rs.getMetaData().getColumnCount(); ! StringBuffer sb = new StringBuffer( ! "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ! int rowCount = 0; ! if (columnCount > 0) { ! sb.append("<" + headingTag + ">\n"); ! while (rs.next()) { ! sb.append("\t<" + rowTag + ">\n"); ! for (int i = 1; i <= columnCount; i++) { ! sb.append("\t\t<" + rs.getMetaData().getColumnLabel(i) ! + ">"); ! String data = ""; ! if (rs.getObject(i) != null) { ! data = rs.getObject(i).toString(); ! } ! sb.append("<![CDATA[" + data + "]]>"); ! sb ! .append("</" + rs.getMetaData().getColumnLabel(i) ! + ">\n"); ! } ! sb.append("\t</" + rowTag + ">\n"); ! if (multiDocument) { ! enqueue(sb.toString().getBytes("UTF-8"), ! new NameValuePair[] { new NameValuePair( ! SCAN_MIMETYPE_KEY, "text/xml") }); ! } ! rowCount++; ! } ! sb.append("</" + headingTag + ">\n"); ! // only submit the document if we have any rows in the result set ! if (rowCount > 0 && !multiDocument) { ! enqueue(sb.toString().getBytes("UTF-8"), ! new NameValuePair[] { new NameValuePair( ! SCAN_MIMETYPE_KEY, "text/xml") }); ! } ! } ! } ! /** ! * Get the documents from the sql ! * ! * @throws ScannerException ! * DOCUMENT ME! ! */ ! public void doScan() throws ScannerException { ! getDocumentsFromSql(); ! } ! /** ! * Set-up the sql scanner code. ! * ! * @throws ScannerConfigurationException ! */ ! public void initialize() throws ScannerConfigurationException { ! resourceName = this.getInfo().getStrValue(RESOURCE_NAME); ! if (resourceName.equals("")) { ! throw new ScannerConfigurationException(I18n.get("scanner.056"), ! null); ! } ! sqlStatement = this.getInfo().getStrValue(SQL_STATEMENT); updateStatement = this.getInfo().getStrValue(UPDATE_STATEMENT); ! if (sqlStatement.equals("")) { ! throw new ScannerConfigurationException(I18n.get("scanner.057"), ! null); ! } ! documentType = this.getInfo().getStrValue(DOCUMENT_TYPE); ! multiDocument = this.getInfo().getBooleanValue(MULTIDOCUMENT); ! if (DOC_TYPE_CSV.equalsIgnoreCase(documentType)) { ! // getChild cvs options ! fieldSeparator = this.getInfo().getStrValue(FIELD_SEPARATOR); ! rowSeparator = this.getInfo().getStrValue(ROW_SEPARATOR); ! } else if (DOC_TYPE_XML.equalsIgnoreCase(documentType)) { ! // getChild xml options ! headingTag = this.getInfo().getStrValue(XML_HEADING_TAG); ! rowTag = this.getInfo().getStrValue(XML_ROW_TAG); ! } ! } ! /** ! * release the held resource. Do nothing - no held resources. ! */ ! public void relinquishResources() { ! LogService.getInstance().logInfo("relinquishResources"); ! } ! /** ! * Get a connection using the resource name ! * ! * @return ! * ! * @throws ScannerException ! */ ! private Connection getConnection() throws ScannerException { ! try { ! if (resource == null) { ! resource = ResourceFactory.getResource(resourceName); ! } ! return ((Connection) resource.checkOut()); ! } catch (ResourceException re) { ! throw new ScannerException(I18n.get("scanner.055"), re); ! } ! } ! /** ! * This is main method which generates documents from underlying database. ! * ! * @throws ScannerException ! * DOCUMENT ME! ! */ ! private void getDocumentsFromSql() throws ScannerException { ! Connection conn = null; ! Statement stmt = null; ! Statement updstmt = null; ! ResultSet rs = null; ! ! try { ! conn = getConnection(); ! stmt = conn.createStatement(); ! rs = stmt.executeQuery(sqlStatement); ! ! if (DOC_TYPE_CSV.equalsIgnoreCase(documentType)) { ! handleCsvDocument(rs); ! } else if (DOC_TYPE_XML.equalsIgnoreCase(documentType)) { ! handleXmlDocuments(rs); ! } else { ! handleSimpleDocuments(rs); ! } ! ! if (updateStatement != null) { updstmt = conn.createStatement(); updstmt.execute(updateStatement); *************** *** 307,422 **** } ! } catch (Exception e) { ! throw new ScannerException(I18n.get("scanner.059"), e); ! } finally { ! try { ! if (rs != null) { ! rs.close(); ! } ! } catch (Exception ignoreThis) { ! } ! try { ! if (stmt != null) { ! stmt.close(); ! } ! } catch (Exception ignoreThis) { ! } ! ! try { if (updstmt != null) { updstmt.close(); } ! } catch (Exception ignoreThis) { ! } ! ! try { ! if (conn != null) { ! resource.checkIn(conn); ! } ! } catch (Exception ignoreThis) { ! } ! } ! } ! /** ! * Get simple documents using resultsets ! * ! * @param rs ! * ! * @throws Exception ! */ ! private void handleSimpleDocuments(ResultSet rs) ! throws Exception { ! while (rs.next()) { ! String contnet = rs.getObject(1).toString(); ! enqueue(contnet.getBytes("UTF8"), new HashMap()); ! } ! } } - /** * Get the sql scanner info ! * * @author $author$ * @version $Revision$ */ class SqlScannerInfo extends ScannerWorkerInfo { ! /** ! * @return get the description ! */ ! public String getDescription() { ! return com.babeldoc.core.I18n.get("scanner.061"); ! } ! /** ! * @return get the name ! */ ! public String getName() { ! return "SqlScanner"; ! } ! /** ! * @return get the type specific options ! */ ! public Collection getTypeSpecificOptions() { ! ArrayList options = new ArrayList(); ! //add specific options ! options.add(new ConfigOption(SqlScanner.RESOURCE_NAME, ! IConfigOptionType.STRING, null, true, I18n.get("scanner.062"))); - options.add(new ConfigOption(SqlScanner.SQL_STATEMENT, - IConfigOptionType.STRING, null, true, I18n.get("scanner.063"))); - options.add(new ConfigOption(SqlScanner.UPDATE_STATEMENT, ! IConfigOptionType.STRING, null, false, ! I18n.get("scanner.SqlScanner.option.updateStatement"))); ! ! String[] docTypes = { SqlScanner.DOC_TYPE_SIMPLE, SqlScanner.DOC_TYPE_CSV, SqlScanner.DOC_TYPE_XML }; ! options.add(new ConfigOption(SqlScanner.DOCUMENT_TYPE, ! new ValueListConfigOptionType(docTypes), SqlScanner.DOC_TYPE_SIMPLE, ! false, I18n.get("scanner.064"))); ! options.add(new ConfigOption(SqlScanner.FIELD_SEPARATOR, ! IConfigOptionType.STRING, SqlScanner.DEFAULT_CSV_FIELD_SEP, false, ! I18n.get("scanner.065"))); ! options.add(new ConfigOption(SqlScanner.ROW_SEPARATOR, ! IConfigOptionType.STRING, SqlScanner.DEFAULT_CSV_ROW_SEP, false, ! I18n.get("scanner.066"))); ! options.add(new ConfigOption(SqlScanner.XML_HEADING_TAG, ! IConfigOptionType.STRING, SqlScanner.DEFAULT_XML_HEADING_TAG, false, ! I18n.get("scanner.067"))); ! options.add(new ConfigOption(SqlScanner.XML_ROW_TAG, ! IConfigOptionType.STRING, SqlScanner.DEFAULT_XML_ROW_TAG, false, ! I18n.get("scanner.068"))); ! return options; ! } } --- 332,447 ---- } ! } catch (Exception e) { ! throw new ScannerException(I18n.get("scanner.059"), e); ! } finally { ! try { ! if (rs != null) { ! rs.close(); ! } ! } catch (Exception ignoreThis) { ! } ! try { ! if (stmt != null) { ! stmt.close(); ! } ! } catch (Exception ignoreThis) { ! } ! ! try { if (updstmt != null) { updstmt.close(); } ! } catch (Exception ignoreThis) { ! } ! try { ! if (conn != null) { ! resource.checkIn(conn); ! } ! } catch (Exception ignoreThis) { ! } ! } ! } ! /** ! * Get simple documents using resultsets ! * ! * @param rs ! * ! * @throws Exception ! */ ! private void handleSimpleDocuments(ResultSet rs) throws Exception { ! while (rs.next()) { ! String contnet = rs.getObject(1).toString(); ! enqueue(contnet.getBytes("UTF8"), new HashMap()); ! } ! } } /** * Get the sql scanner info ! * * @author $author$ * @version $Revision$ */ class SqlScannerInfo extends ScannerWorkerInfo { ! /** ! * @return get the description ! */ ! public String getDescription() { ! return com.babeldoc.core.I18n.get("scanner.061"); ! } ! /** ! * @return get the name ! */ ! public String getName() { ! return "SqlScanner"; ! } ! /** ! * @return get the type specific options ! */ ! public Collection getTypeSpecificOptions() { ! ArrayList options = new ArrayList(); ! // add specific options ! options.add(new ConfigOption(SqlScanner.RESOURCE_NAME, ! IConfigOptionType.STRING, null, true, I18n.get("scanner.062"))); ! ! options.add(new ConfigOption(SqlScanner.SQL_STATEMENT, ! IConfigOptionType.STRING, null, true, I18n.get("scanner.063"))); options.add(new ConfigOption(SqlScanner.UPDATE_STATEMENT, ! IConfigOptionType.STRING, null, false, I18n ! .get("scanner.SqlScanner.option.updateStatement"))); ! String[] docTypes = { SqlScanner.DOC_TYPE_SIMPLE, ! SqlScanner.DOC_TYPE_CSV, SqlScanner.DOC_TYPE_XML }; ! options.add(new ConfigOption(SqlScanner.DOCUMENT_TYPE, ! new ValueListConfigOptionType(docTypes), ! SqlScanner.DOC_TYPE_SIMPLE, false, I18n.get("scanner.064"))); ! options.add(new ConfigOption(SqlScanner.FIELD_SEPARATOR, ! IConfigOptionType.STRING, SqlScanner.DEFAULT_CSV_FIELD_SEP, ! false, I18n.get("scanner.065"))); ! options.add(new ConfigOption(SqlScanner.ROW_SEPARATOR, ! IConfigOptionType.STRING, SqlScanner.DEFAULT_CSV_ROW_SEP, ! false, I18n.get("scanner.066"))); ! options.add(new ConfigOption(SqlScanner.XML_HEADING_TAG, ! IConfigOptionType.STRING, SqlScanner.DEFAULT_XML_HEADING_TAG, ! false, I18n.get("scanner.067"))); ! options.add(new ConfigOption(SqlScanner.XML_ROW_TAG, ! IConfigOptionType.STRING, SqlScanner.DEFAULT_XML_ROW_TAG, ! false, I18n.get("scanner.068"))); ! options.add(new ConfigOption(SqlScanner.MULTIDOCUMENT, ! IConfigOptionType.BOOLEAN, "false", false, ! I18n.get("scanner.SqlScanner.option.multiDocument"))); ! return options; ! } } |