001: /*
002: * RSSBootstrapParser.java
003: *
004: * Created on April 16, 2007, 6:22 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.net.HttpURLConnection;
013: import java.net.URL;
014: import java.sql.Types;
015: import java.util.ArrayList;
016: import java.util.Collections;
017: import java.util.HashMap;
018: import java.util.List;
019: import java.util.Map;
020: import javax.xml.parsers.DocumentBuilderFactory;
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: import org.w3c.dom.Element;
029:
030: /**
031: *
032: * @author karthikeyan s
033: */
034: public class RSSBootstrapParser implements FlatfileBootstrapParser {
035:
036: private static Map<String, String> columnsMap = new HashMap<String, String>();
037:
038: static {
039: columnsMap.put("1", "TITLE");
040: columnsMap.put("2", "LINK");
041: columnsMap.put("3", "DESCRIPTION");
042: columnsMap.put("4", "AUTHOR");
043: columnsMap.put("5", "CATEGORY");
044: columnsMap.put("6", "COMMENTS");
045: columnsMap.put("7", "ENCLOSURE");
046: columnsMap.put("8", "GUID");
047: columnsMap.put("9", "PUBDATE");
048: columnsMap.put("10", "SOURCE");
049: }
050:
051: /** Creates a new instance of RSSBootstrapParser */
052: public RSSBootstrapParser() {
053: }
054:
055: public List buildFlatfileDBColumns(FlatfileDBTable table)
056: throws FlatfileDBException {
057: int defaultPrecision = 200;
058: int jdbcType = SQLUtils.getStdJdbcType(table
059: .getProperty(PropertyKeys.WIZARDDEFAULTSQLTYPE));
060: if (jdbcType == SQLUtils.JDBCSQL_TYPE_UNDEFINED) {
061: jdbcType = Types.VARCHAR;
062: }
063: try {
064: defaultPrecision = Integer
065: .valueOf(
066: table
067: .getProperty(PropertyKeys.WIZARDDEFAULTPRECISION))
068: .intValue();
069: } catch (Exception e) {
070: defaultPrecision = 200;
071: }
072: FlatfileDBColumn[] columns = getColumns(table);
073: List<FlatfileDBColumn> colList = new ArrayList<FlatfileDBColumn>(
074: columns.length);
075: if (!acceptable(table)) {
076: throw new FlatfileDBException("Not acceptable");
077: }
078: for (int i = 1; i <= 10; i++) {
079: FlatfileDBColumn column = null;
080: if (columns != null && i <= columns.length) {
081: column = columns[i - 1];
082: }
083: if (column == null) {
084: column = new FlatfileDBColumnImpl(columnsMap.get(String
085: .valueOf(i)), jdbcType, defaultPrecision, 0,
086: true);
087: column.setCardinalPosition(i);
088: }
089: colList.add(column);
090: }
091: return colList;
092: }
093:
094: public void makeGuess(FlatfileDBTable table)
095: throws FlatfileDBException {
096: }
097:
098: public boolean acceptable(FlatfileDBTable table)
099: throws FlatfileDBException {
100: String url = table.getProperty(PropertyKeys.URL);
101: String[] urls = url.split(",");
102: boolean accept = true;
103: for (String u : urls) {
104: try {
105: getRootElement(u);
106: } catch (Exception ex) {
107: accept = false;
108: }
109: }
110: return accept;
111: }
112:
113: private Element getRootElement(String url) throws Exception {
114: HttpURLConnection conn = null;
115: if (url.indexOf("?") != -1) {
116: String actualUrl = url.substring(0, url.indexOf("?"));
117: conn = (HttpURLConnection) new URL(actualUrl)
118: .openConnection();
119: String tempString = url.substring(url.indexOf("?") + 1);
120: String[] props = tempString.split("&");
121: for (String prop : props) {
122: String[] val = prop.split("=");
123: if (val.length == 2) {
124: conn.setRequestProperty(val[0], val[1]);
125: } else if (val.length == 1) {
126: conn.setRequestProperty(val[0], null);
127: }
128: }
129: conn.connect();
130: } else {
131: conn = (HttpURLConnection) new URL(url).openConnection();
132: }
133: Element elem = DocumentBuilderFactory.newInstance()
134: .newDocumentBuilder().parse(conn.getInputStream())
135: .getDocumentElement();
136: if (!elem.getNodeName().equalsIgnoreCase("rss")) {
137: throw new Exception("Document type not supported.");
138: }
139: return elem;
140: }
141:
142: private FlatfileDBColumn[] getColumns(FlatfileDBTable table) {
143: FlatfileDBColumn[] columns = new FlatfileDBColumn[0];
144: if (table.getColumnList().size() > 0) {
145: columns = (FlatfileDBColumn[]) table.getColumnList()
146: .toArray(columns);
147: }
148: return columns;
149: }
150: }
|