001: /*
002: * XMLBootstrapParser.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 com.sun.sql.framework.utils.StringUtil;
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.InputStream;
016: import java.net.URL;
017: import java.sql.SQLException;
018: import java.sql.Types;
019: import java.util.ArrayList;
020: import java.util.List;
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023:
024: import org.netbeans.modules.mashup.db.common.FlatfileDBException;
025: import org.netbeans.modules.mashup.db.common.PropertyKeys;
026: import org.netbeans.modules.mashup.db.common.SQLUtils;
027: import org.netbeans.modules.mashup.db.model.FlatfileDBColumn;
028: import org.netbeans.modules.mashup.db.model.FlatfileDBTable;
029: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBColumnImpl;
030: import org.netbeans.modules.mashup.db.model.impl.FlatfileDBTableImpl;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.NodeList;
035:
036: /**
037: *
038: * @author ks161616
039: */
040: public class XMLBootstrapParser implements FlatfileBootstrapParser {
041:
042: /** Creates a new instance of XMLBootstrapParser */
043: public XMLBootstrapParser() {
044: }
045:
046: public List buildFlatfileDBColumns(FlatfileDBTable table)
047: throws FlatfileDBException {
048: int defaultPrecision = 60;
049: int jdbcType = SQLUtils.getStdJdbcType(table
050: .getProperty(PropertyKeys.WIZARDDEFAULTSQLTYPE));
051: if (jdbcType == SQLUtils.JDBCSQL_TYPE_UNDEFINED) {
052: jdbcType = Types.VARCHAR;
053: }
054: try {
055: defaultPrecision = Integer
056: .valueOf(
057: table
058: .getProperty(PropertyKeys.WIZARDDEFAULTPRECISION))
059: .intValue();
060: } catch (Exception e) {
061: defaultPrecision = 60;
062: }
063: FlatfileDBColumn[] columns = getColumns(table);
064: List<FlatfileDBColumn> colList = new ArrayList<FlatfileDBColumn>(
065: columns.length);
066: Element root = null;
067: try {
068: root = getRootElement(table.getProperty(PropertyKeys.URL));
069: } catch (Exception ex) {
070: //ignore
071: }
072: if (root != null) {
073: Element row = getRowElement(root);
074: if (row != null) {
075: int count = getColumnCount(row);
076: for (int i = 1; i <= count; i++) {
077: FlatfileDBColumn column = null;
078: if (columns != null && i <= columns.length) {
079: column = columns[i - 1];
080: }
081: if (column == null) {
082: String columnName = getColumnName(row, i);
083: if (columnName != null
084: && !columnName.equals("")
085: && !StringUtil.isNullString(columnName)) {
086: columnName = StringUtil
087: .escapeNonAlphaNumericCharacters(columnName
088: .trim());
089: columnName = StringUtil
090: .createColumnNameFromFieldName(columnName
091: .trim());
092: column = new FlatfileDBColumnImpl(
093: columnName, jdbcType,
094: defaultPrecision, 0, true);
095: } else {
096: column = new FlatfileDBColumnImpl("FIELD_"
097: + String.valueOf(i), jdbcType,
098: defaultPrecision, 0, true);
099: }
100: column.setCardinalPosition(i);
101: }
102: colList.add(column);
103: }
104: }
105: }
106: return colList;
107: }
108:
109: public void makeGuess(FlatfileDBTable table)
110: throws FlatfileDBException {
111: }
112:
113: public boolean acceptable(FlatfileDBTable table)
114: throws FlatfileDBException {
115: try {
116: getRootElement(table.getProperty(PropertyKeys.URL));
117: } catch (Exception ex) {
118: return false;
119: }
120: return true;
121: }
122:
123: private FlatfileDBColumn[] getColumns(FlatfileDBTable table) {
124: FlatfileDBColumn[] columns = new FlatfileDBColumn[0];
125: if (table.getColumnList().size() > 0) {
126: columns = (FlatfileDBColumn[]) table.getColumnList()
127: .toArray(columns);
128: }
129: return columns;
130: }
131:
132: private Element getRootElement(String file) throws Exception {
133: file = StringUtil.escapeControlChars(file.trim());
134: File f = new File(file);
135: InputStream is = null;
136: if (f.exists()) {
137: is = new FileInputStream(f);
138: } else {
139: is = new URL(file).openStream();
140: }
141: DocumentBuilderFactory factory = DocumentBuilderFactory
142: .newInstance();
143: DocumentBuilder builder = factory.newDocumentBuilder();
144: Document document = builder.parse(is);
145: return document.getDocumentElement();
146: }
147:
148: private Element getRowElement(Element root) {
149: NodeList children = root.getChildNodes();
150: for (int i = 0; i < children.getLength(); i++) {
151: Node nd = children.item(i);
152: if (nd.getNodeType() == Node.ELEMENT_NODE) {
153: return (Element) nd;
154: }
155: }
156: return null;
157: }
158:
159: private int getColumnCount(Element row) {
160: int count = 0;
161: NodeList children = row.getChildNodes();
162: for (int i = 0; i < children.getLength(); i++) {
163: Node nd = children.item(i);
164: if (nd.getNodeType() == Node.ELEMENT_NODE) {
165: count++;
166: }
167: }
168: return count;
169: }
170:
171: private String getColumnName(Element row, int i) {
172: NodeList children = row.getChildNodes();
173: int count = 0;
174: Element column = null;
175: for (int j = 0; j < children.getLength(); j++) {
176: Node nd = children.item(j);
177: if (nd.getNodeType() == Node.ELEMENT_NODE) {
178: count++;
179: if (count == i) {
180: column = (Element) nd;
181: break;
182: }
183: }
184: }
185: String name = "";
186: if (column != null) {
187: name = column.getNodeName();
188: }
189: return name;
190: }
191: }
|