001: /*
002: * WebBootstrapParser.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.FileNotFoundException;
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.net.URL;
018: import java.sql.Types;
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.List;
022: import javax.swing.text.BadLocationException;
023: import javax.swing.text.EditorKit;
024: import javax.swing.text.ElementIterator;
025: import javax.swing.text.html.HTMLDocument;
026: import javax.swing.text.html.HTMLEditorKit;
027:
028: import org.netbeans.modules.mashup.db.common.FlatfileDBException;
029: import org.netbeans.modules.mashup.db.common.PropertyKeys;
030: import org.netbeans.modules.mashup.db.common.SQLUtils;
031: import org.netbeans.modules.mashup.db.model.FlatfileDBColumn;
032: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
033: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBColumnImpl;
034:
035: import com.sun.sql.framework.utils.StringUtil;
036:
037: /**
038: *
039: * @author ks161616
040: */
041: public class WebBootstrapParser implements FlatfileBootstrapParser {
042:
043: /** Creates a new instance of WebBootstrapParser */
044: public WebBootstrapParser() {
045: }
046:
047: public List buildFlatfileDBColumns(FlatfileDBTable table)
048: throws FlatfileDBException {
049: if (table == null || table.getProperties() == null
050: || table.getProperties().size() == 0) {
051: return Collections.EMPTY_LIST;
052: }
053:
054: String fieldSep = table
055: .getProperty(PropertyKeys.FIELDDELIMITER);
056: if (fieldSep.equalsIgnoreCase("UserDefined")) {
057: fieldSep = table
058: .getProperty(PropertyKeys.WIZARDCUSTOMFIELDDELIMITER);
059: table.setProperty(PropertyKeys.FIELDDELIMITER, fieldSep);
060: if (StringUtil.isNullString(fieldSep)) {
061: throw new FlatfileDBException(
062: "Please supply valid custom delimiter.");
063: }
064: }
065:
066: final String recordSep = table
067: .getProperty(PropertyKeys.RECORDDELIMITER);
068: final String qualifier = table
069: .getProperty(PropertyKeys.QUALIFIER);
070: boolean isFirstLineHeader = Boolean.valueOf(
071: table.getProperty(PropertyKeys.ISFIRSTLINEHEADER))
072: .booleanValue();
073: int defaultPrecision = 60;
074: int jdbcType = SQLUtils.getStdJdbcType(table
075: .getProperty(PropertyKeys.WIZARDDEFAULTSQLTYPE));
076: if (jdbcType == SQLUtils.JDBCSQL_TYPE_UNDEFINED) {
077: jdbcType = Types.VARCHAR;
078: }
079: try {
080: defaultPrecision = Integer
081: .valueOf(
082: table
083: .getProperty(PropertyKeys.WIZARDDEFAULTPRECISION))
084: .intValue();
085: } catch (Exception e) {
086: defaultPrecision = 60;
087: }
088:
089: FlatfileDBColumn[] columns = getColumns(table);
090: List<FlatfileDBColumn> colList = new ArrayList<FlatfileDBColumn>(
091: columns.length);
092:
093: javax.swing.text.Element element = null;
094: try {
095: element = getElement(table.getProperty(PropertyKeys.URL),
096: Integer.parseInt(table.getProperty("TABLENUMBER")));
097: } catch (Exception ex) {
098: ex.printStackTrace();
099: }
100: if (element != null) {
101: ElementIterator it = new ElementIterator(element);
102: javax.swing.text.Element elem = null;
103: while ((elem = it.next()) != null) {
104: if (elem.getName().equalsIgnoreCase("tr")) {
105: break;
106: }
107: }
108: ElementIterator rowIt = new ElementIterator(elem);
109: HTMLDocument doc = (HTMLDocument) elem.getDocument();
110: int count = 1;
111: while ((element = rowIt.next()) != null) {
112: if (element.getName().equalsIgnoreCase("td")) {
113: String field = "FIELD_" + count;
114: try {
115: if (isFirstLineHeader) {
116: field = doc.getText(
117: element.getStartOffset(),
118: (element.getEndOffset() - element
119: .getStartOffset())).trim();
120: field = StringUtil
121: .escapeNonAlphaNumericCharacters(field);
122: field = StringUtil
123: .createColumnNameFromFieldName(field);
124: }
125: } catch (BadLocationException ex) {
126: //ignore
127: }
128: FlatfileDBColumn column = null;
129: if (columns != null && count <= columns.length) {
130: column = columns[count - 1];
131: }
132: if (column == null) {
133: column = new FlatfileDBColumnImpl(field,
134: jdbcType, defaultPrecision, 0, true);
135: } else {
136: if (isFirstLineHeader) {
137: column.setName(field);
138: }
139: }
140: column.setCardinalPosition(count++);
141: colList.add(column);
142: }
143: }
144: }
145: return colList;
146: }
147:
148: public void makeGuess(FlatfileDBTable table)
149: throws FlatfileDBException {
150: }
151:
152: public boolean acceptable(FlatfileDBTable table)
153: throws FlatfileDBException {
154: String url = table.getProperty(PropertyKeys.URL).toLowerCase();
155: if (url.startsWith("http://") || url.startsWith("https://")
156: || url.startsWith("ftp://") || url.endsWith(".html")
157: || url.endsWith(".htm")) {
158: try {
159: getElement(table.getProperty(PropertyKeys.URL), 1);
160: } catch (Exception e) {
161: return false;
162: }
163: return true;
164: }
165: return false;
166: }
167:
168: private FlatfileDBColumn[] getColumns(FlatfileDBTable table) {
169: FlatfileDBColumn[] columns = new FlatfileDBColumn[0];
170: if (table.getColumnList().size() > 0) {
171: columns = (FlatfileDBColumn[]) table.getColumnList()
172: .toArray(columns);
173: }
174: return columns;
175: }
176:
177: private javax.swing.text.Element getElement(String url, int depth)
178: throws Exception {
179: InputStream in = null;
180: url = StringUtil.escapeControlChars(url);
181: File f = new File(url);
182: if (f.exists()) {
183: in = new FileInputStream(f);
184: } else {
185: in = new URL(url).openStream();
186: }
187:
188: EditorKit kit = new HTMLEditorKit();
189: HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
190: doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
191: kit.read(in, doc, 0);
192: int tableCount = 1;
193: ElementIterator it = new ElementIterator(doc);
194: javax.swing.text.Element element = null;
195: while ((element = it.next()) != null) {
196: // read all table elements.
197: if ("table".equalsIgnoreCase(element.getName())) {
198: if (tableCount++ == depth) {
199: return element;
200: }
201: }
202: }
203: return null;
204: }
205: }
|