001: /*
002: * WebrowsetBootstrapParser.java
003: *
004: * Created on April 3, 2007, 12:56 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.netbeans.modules.mashup.db.bootstrap;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.InputStream;
015: import java.net.URL;
016: import java.sql.SQLException;
017: import java.sql.Types;
018: import java.util.ArrayList;
019: import java.util.List;
020: import javax.sql.rowset.WebRowSet;
021:
022: import org.netbeans.modules.mashup.db.common.FlatfileDBException;
023: import org.netbeans.modules.mashup.db.common.PropertyKeys;
024: import org.netbeans.modules.mashup.db.common.SQLUtils;
025: import org.netbeans.modules.mashup.db.model.FlatfileDBColumn;
026: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
027: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBColumnImpl;
028:
029: import com.sun.rowset.WebRowSetImpl;
030:
031: import com.sun.sql.framework.utils.StringUtil;
032:
033: /**
034: *
035: * @author ks161616
036: */
037: public class WebrowsetBootstrapParser implements
038: FlatfileBootstrapParser {
039:
040: /** Creates a new instance of WebrowsetBootstrapParser */
041: public WebrowsetBootstrapParser() {
042: }
043:
044: public List buildFlatfileDBColumns(FlatfileDBTable table)
045: throws FlatfileDBException {
046: int defaultPrecision = 60;
047: int jdbcType = SQLUtils.getStdJdbcType(table
048: .getProperty(PropertyKeys.WIZARDDEFAULTSQLTYPE));
049: if (jdbcType == SQLUtils.JDBCSQL_TYPE_UNDEFINED) {
050: jdbcType = Types.VARCHAR;
051: }
052: try {
053: defaultPrecision = Integer
054: .valueOf(
055: table
056: .getProperty(PropertyKeys.WIZARDDEFAULTPRECISION))
057: .intValue();
058: } catch (Exception e) {
059: defaultPrecision = 60;
060: }
061: FlatfileDBColumn[] columns = getColumns(table);
062: List<FlatfileDBColumn> colList = new ArrayList<FlatfileDBColumn>(
063: columns.length);
064: WebRowSet wrs = null;
065: try {
066: wrs = getWebRowset(table.getProperty(PropertyKeys.URL));
067: } catch (Exception ex) {
068: //ignore
069: }
070: if (wrs != null) {
071: try {
072: int count = wrs.getMetaData().getColumnCount();
073: for (int i = 1; i <= count; i++) {
074: FlatfileDBColumn column = null;
075: if (columns != null && i <= columns.length) {
076: column = columns[i - 1];
077: }
078: if (column == null) {
079: String columnName = wrs.getMetaData()
080: .getColumnName(i);
081: if (columnName != null
082: && !columnName.equals("")
083: && columnName.trim().length() != 0) {
084: columnName = StringUtil
085: .escapeNonAlphaNumericCharacters(columnName
086: .trim());
087: columnName = StringUtil
088: .createColumnNameFromFieldName(columnName
089: .trim());
090: column = new FlatfileDBColumnImpl(
091: columnName, jdbcType,
092: defaultPrecision, 0, true);
093: } else {
094: column = new FlatfileDBColumnImpl("FIELD_"
095: + String.valueOf(i), jdbcType,
096: defaultPrecision, 0, true);
097: }
098: column.setCardinalPosition(i);
099: }
100: colList.add(column);
101: }
102: } catch (SQLException ex) {
103: //ignore
104: }
105: }
106: return colList;
107: }
108:
109: private FlatfileDBColumn[] getColumns(FlatfileDBTable table) {
110: FlatfileDBColumn[] columns = new FlatfileDBColumn[0];
111: if (table.getColumnList().size() > 0) {
112: columns = (FlatfileDBColumn[]) table.getColumnList()
113: .toArray(columns);
114: }
115: return columns;
116: }
117:
118: public void makeGuess(FlatfileDBTable table)
119: throws FlatfileDBException {
120: }
121:
122: public boolean acceptable(FlatfileDBTable table)
123: throws FlatfileDBException {
124: try {
125: getWebRowset(table.getProperty(PropertyKeys.URL));
126: } catch (Exception ex) {
127: return false;
128: }
129: return true;
130: }
131:
132: private WebRowSet getWebRowset(String string) throws Exception {
133: try {
134: WebRowSet wrs = new WebRowSetImpl();
135: string = StringUtil.escapeControlChars(string);
136: File f = new File(string);
137: InputStream is = null;
138: if (f.exists()) {
139: is = new FileInputStream(f);
140: } else {
141: is = new URL(string).openStream();
142: }
143: wrs.readXml(is);
144: wrs.getMetaData().getColumnCount();
145: return wrs;
146: } catch (Exception e) {
147: throw new FlatfileDBException("Unable to parse: " + string);
148: }
149: }
150: }
|