Author: DartPeng
Date: 2009-07-06 05:02:08 -0400 (Mon, 06 Jul 2009)
New Revision: 16392
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataParser.java
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/META-INF/MANIFEST.MF
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SelectoreSelectionDialog.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataConfigurationWizardPage.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataPathWizardPage.java
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVInputDataWizard.java
Log:
JBIDE-4551
Done
Modified: trunk/smooks/plugins/org.jboss.tools.smooks.ui/META-INF/MANIFEST.MF
===================================================================
--- trunk/smooks/plugins/org.jboss.tools.smooks.ui/META-INF/MANIFEST.MF 2009-07-06
02:16:20 UTC (rev 16391)
+++ trunk/smooks/plugins/org.jboss.tools.smooks.ui/META-INF/MANIFEST.MF 2009-07-06
09:02:08 UTC (rev 16392)
@@ -25,7 +25,8 @@
org.eclipse.wst.wsdl,
org.apache.xerces,
javax.wsdl,
- org.jboss.tools.smooks.runtime;bundle-version="1.1.2"
+ org.jboss.tools.smooks.runtime;bundle-version="1.1.2",
+ org.eclipse.gef
Bundle-ActivationPolicy: lazy
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SelectoreSelectionDialog.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SelectoreSelectionDialog.java 2009-07-06
02:16:20 UTC (rev 16391)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/SelectoreSelectionDialog.java 2009-07-06
09:02:08 UTC (rev 16392)
@@ -43,6 +43,7 @@
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.jboss.tools.smooks.configuration.SmooksConfigurationActivator;
+import org.jboss.tools.smooks.configuration.editors.csv.CSVDataParser;
import org.jboss.tools.smooks.configuration.editors.javabean.JavaBeanModel;
import org.jboss.tools.smooks.configuration.editors.javabean.JavaBeanModelFactory;
import org.jboss.tools.smooks.configuration.editors.uitls.JsonInputDataParser;
@@ -253,6 +254,17 @@
String path = SmooksModelUtils.getInputPath(inputType);
if (type != null && path != null) {
path = path.trim();
+ if (SmooksModelUtils.INPUT_TYPE_CSV.equals(type)) {
+ CSVDataParser parser = new CSVDataParser();
+ try {
+ TagList tl = parser.parseCSV(path, inputType, smooksResourceListType);
+ if (tl != null) {
+ list.addAll(((TagList) tl).getChildren());
+ }
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ }
if (SmooksModelUtils.INPUT_TYPE_JSON.equals(type)) {
try {
JsonInputDataParser parser = new JsonInputDataParser();
@@ -265,7 +277,7 @@
}
} catch (Throwable tt) {
// ignore
-// SmooksConfigurationActivator.getDefault().log(tt);
+ // SmooksConfigurationActivator.getDefault().log(tt);
}
}
if (SmooksModelUtils.INPUT_TYPE_JAVA.equals(type)) {
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataConfigurationWizardPage.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataConfigurationWizardPage.java 2009-07-06
02:16:20 UTC (rev 16391)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataConfigurationWizardPage.java 2009-07-06
09:02:08 UTC (rev 16392)
@@ -3,14 +3,20 @@
*/
package org.jboss.tools.smooks.configuration.editors.csv;
+import java.io.BufferedReader;
+import java.io.FileReader;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
+import org.eclipse.draw2d.ColorConstants;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.ITableColorProvider;
+import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
@@ -22,6 +28,9 @@
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
@@ -30,8 +39,8 @@
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
-import
org.jboss.tools.smooks.configuration.editors.json.JsonDataConfiguraitonWizardPage.KeyValueModel;
import org.jboss.tools.smooks.configuration.editors.uitls.SmooksUIUtils;
import org.jboss.tools.smooks.model.csv.CsvReader;
import org.jboss.tools.smooks.model.csv.impl.CsvReaderImpl;
@@ -57,13 +66,15 @@
private Button removeButton;
private Button createCSVReaderButton;
+ private String filePath = null;
+
private boolean useAvailabelReader = false;
private boolean hasReader = false;
private boolean createCSVReader = true;
- private String speartor;
+ private String separator;
private String skipLines;
@@ -178,16 +189,28 @@
fieldsComposite.setLayout(kgl);
gd = new GridData(GridData.FILL_BOTH);
+ gd.heightHint = 200;
- fieldsViewer = new TableViewer(fieldsComposite, SWT.BORDER | SWT.MULTI);
+ fieldsViewer = new TableViewer(fieldsComposite, SWT.BORDER | SWT.MULTI |
SWT.FULL_SELECTION);
fieldsViewer.getControl().setLayoutData(gd);
-// fieldsViewer.getTable().setHeaderVisible(true);
+ // fieldsViewer.getTable().setHeaderVisible(true);
fieldsViewer.getTable().setLinesVisible(true);
fieldsViewer.setContentProvider(new FieldsContentProvider());
fieldsViewer.setLabelProvider(new FieldsLabelProvider());
CellEditor fieldCellEditor = new TextCellEditor(fieldsViewer.getTable(), SWT.BORDER);
+ TableColumn fieldColumn = new TableColumn(fieldsViewer.getTable(), SWT.NONE);
+ fieldColumn.setText("Field");
+ fieldColumn.setWidth(150);
+
+ TableColumn exampleValueColumn = new TableColumn(fieldsViewer.getTable(), SWT.NONE);
+ exampleValueColumn.setText("Example Value");
+ exampleValueColumn.setWidth(150);
+
+ fieldsViewer.getTable().setHeaderVisible(true);
+ fieldsViewer.getTable().setLinesVisible(true);
+
fieldsViewer.setCellEditors(new CellEditor[] { fieldCellEditor });
fieldsViewer.setColumnProperties(new String[] { "field" });
@@ -207,6 +230,7 @@
}
fieldsViewer.refresh(el);
}
+ changePageStatus();
}
public Object getValue(Object element, String property) {
@@ -243,7 +267,9 @@
Composite buttonComposite = new Composite(fieldsComposite, SWT.NONE);
gd = new GridData(GridData.FILL_VERTICAL);
+ gd.widthHint = 0;
buttonComposite.setLayoutData(gd);
+ buttonComposite.setVisible(false);
GridLayout bgl = new GridLayout();
buttonComposite.setLayout(bgl);
@@ -272,10 +298,10 @@
createCSVReaderButton.setEnabled(false);
setConfigCompositeStates(false);
}
-
+
changePageStatus();
hookControls();
-
+
this.setControl(mainComposite);
}
@@ -313,7 +339,8 @@
this.separatorText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
- speartor = separatorText.getText();
+ separator = separatorText.getText();
+ resetViewerContent();
changePageStatus();
}
});
@@ -341,6 +368,33 @@
}
});
+ this.createCSVReaderButton.addSelectionListener(new SelectionListener() {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.swt.events.SelectionListener#widgetDefaultSelected
+ * (org.eclipse.swt.events.SelectionEvent)
+ */
+ public void widgetDefaultSelected(SelectionEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
+ * .swt.events.SelectionEvent)
+ */
+ public void widgetSelected(SelectionEvent e) {
+ createCSVReader = createCSVReaderButton.getSelection();
+ }
+
+ });
+
this.addButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
@@ -358,7 +412,7 @@
this.removeButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
- IStructuredSelection s = (IStructuredSelection)fieldsViewer.getSelection();
+ IStructuredSelection s = (IStructuredSelection) fieldsViewer.getSelection();
fieldsList.removeAll(s.toList());
fieldsViewer.refresh();
}
@@ -369,26 +423,70 @@
});
}
+ private void resetViewerContent() {
+ try {
+ fieldsList.clear();
+ if (filePath != null && separator != null && separator.length() == 1)
{
+
+ filePath = SmooksUIUtils.parseFilePath(filePath);
+
+ FileReader fr = new FileReader(filePath);
+ BufferedReader br = new BufferedReader(fr);
+
+ String content = br.readLine();
+ if (content != null) {
+ String[] exampleValues = content.split(separator);
+ if (exampleValues != null) {
+ List<FieldString> list = new ArrayList<FieldString>();
+ for (int i = 0; i < exampleValues.length; i++) {
+ String s = exampleValues[i];
+ if (s != null) {
+ if (s.length() == 0)
+ s = "<Null>";
+ FieldString fs = new FieldString("");
+ fs.setExampleValue(s);
+ list.add(fs);
+ }
+ }
+ if(!list.isEmpty()){
+ fieldsList.addAll(list);
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ this.setErrorMessage("Occurs an error when parse CSV file");
+ }
+ fieldsViewer.refresh();
+ }
+
private void changePageStatus() {
if (useAvailabelReader) {
+ setErrorMessage(null);
+ setPageComplete(true);
return;
}
String error = null;
- if (speartor == null || speartor.length() == 0) {
+ if (separator == null || separator.length() == 0) {
error = "Sperator can't be null";
}
-
+ if (separator != null && separator.length() > 1) {
+ error = "Sperator needs only one char";
+ }
if (quoteChar == null || quoteChar.length() == 0) {
- error = "Quote char can't be null";
+ error = "QuoteChar can't be null";
}
-
+ if (quoteChar != null && quoteChar.length() > 1) {
+ error = "QuoteChar needs only one char";
+ }
+
if (encoding == null || encoding.length() == 0) {
error = "Encoding can't be null";
}
if (skipLines == null || skipLines.length() == 0) {
- error = "Skip lines can't be null";
+// error = "Skip lines can't be null";
} else {
try {
Integer.parseInt(skipLines);
@@ -396,6 +494,13 @@
error = "Skip lines text must be the number";
}
}
+
+ for (Iterator<?> iterator = fieldsList.iterator(); iterator.hasNext();) {
+ FieldString field = (FieldString) iterator.next();
+ if(field.getText() == null || field.getText().length() == 0 ){
+ error = "Fields can't be null";
+ }
+ }
setErrorMessage(error);
setPageComplete(error == null);
@@ -423,8 +528,115 @@
}
}
+ /**
+ * @return the separator
+ */
+ public String getSeparator() {
+ return separator;
+ }
+
+ /**
+ * @param separator
+ * the separator to set
+ */
+ public void setSeparator(String separator) {
+ this.separator = separator;
+ }
+
+ /**
+ * @return the fieldsList
+ */
+ public List<FieldString> getFieldsList() {
+ return fieldsList;
+ }
+
+ /**
+ * @param fieldsList
+ * the fieldsList to set
+ */
+ public void setFieldsList(List<FieldString> fieldsList) {
+ this.fieldsList = fieldsList;
+ }
+
+ /**
+ * @return the useAvailabelReader
+ */
+ public boolean isUseAvailabelReader() {
+ return useAvailabelReader;
+ }
+
+ /**
+ * @param useAvailabelReader
+ * the useAvailabelReader to set
+ */
+ public void setUseAvailabelReader(boolean useAvailabelReader) {
+ this.useAvailabelReader = useAvailabelReader;
+ }
+
+ /**
+ * @return the createCSVReader
+ */
+ public boolean isCreateCSVReader() {
+ return createCSVReader;
+ }
+
+ /**
+ * @param createCSVReader
+ * the createCSVReader to set
+ */
+ public void setCreateCSVReader(boolean createCSVReader) {
+ this.createCSVReader = createCSVReader;
+ }
+
+ /**
+ * @return the skipLines
+ */
+ public String getSkipLines() {
+ return skipLines;
+ }
+
+ /**
+ * @param skipLines
+ * the skipLines to set
+ */
+ public void setSkipLines(String skipLines) {
+ this.skipLines = skipLines;
+ }
+
+ /**
+ * @return the quoteChar
+ */
+ public String getQuoteChar() {
+ return quoteChar;
+ }
+
+ /**
+ * @param quoteChar
+ * the quoteChar to set
+ */
+ public void setQuoteChar(String quoteChar) {
+ this.quoteChar = quoteChar;
+ }
+
+ /**
+ * @return the encoding
+ */
+ public String getEncoding() {
+ return encoding;
+ }
+
+ /**
+ * @param encoding
+ * the encoding to set
+ */
+ public void setEncoding(String encoding) {
+ this.encoding = encoding;
+ }
+
private void initValue() {
useAvailabelReader = false;
+
+ filePath = null;
hasReader = false;
@@ -437,7 +649,7 @@
encoding = "UTF-8";
- speartor = null;
+ separator = null;
skipLines = null;
@@ -455,13 +667,45 @@
return smooksResourceList;
}
+ /**
+ * @return the filePath
+ */
+ public String getFilePath() {
+ return filePath;
+ }
+
+ /**
+ * @param filePath
+ * the filePath to set
+ */
+ public void setFilePath(String filePath) {
+ this.filePath = filePath;
+ }
+
public void setSmooksResourceList(SmooksResourceListType smooksResourceList) {
this.smooksResourceList = smooksResourceList;
}
- private class FieldString {
+ public class FieldString {
private String text = null;
+ private String exampleValue;
+
+ /**
+ * @return the exampleValue
+ */
+ public String getExampleValue() {
+ return exampleValue;
+ }
+
+ /**
+ * @param exampleValue
+ * the exampleValue to set
+ */
+ public void setExampleValue(String exampleValue) {
+ this.exampleValue = exampleValue;
+ }
+
public FieldString(String text) {
this.setText(text);
}
@@ -475,19 +719,63 @@
}
}
- private class FieldsLabelProvider extends LabelProvider implements ITableLabelProvider
{
+ private class FieldsLabelProvider extends LabelProvider implements ITableLabelProvider,
ITableFontProvider,
+ ITableColorProvider {
+ private Font font = null;
+
+ public void dispose() {
+ if (font != null) {
+ font.dispose();
+ }
+ super.dispose();
+ }
+
+ public FieldsLabelProvider() {
+ FontData fd = new FontData();
+ fd.setName("Arial");
+ fd.setStyle(SWT.BOLD);
+ font = new Font(null, fd);
+ }
+
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
- if(element instanceof FieldString){
- return ((FieldString)element).getText();
+ if (element instanceof FieldString) {
+ switch(columnIndex){
+ case 0 :
+ return ((FieldString) element).getText();
+ case 1:
+ return ((FieldString) element).getExampleValue();
+ }
+ return ((FieldString) element).getText();
}
return getText(element);
}
+ public Font getFont(Object element, int columnIndex) {
+ if (columnIndex == 0) {
+ return font;
+ }
+ return null;
+ }
+
+ public Color getBackground(Object element, int columnIndex) {
+ if (columnIndex == 1) {
+ return ColorConstants.lightGray;
+ }
+ return null;
+ }
+
+ public Color getForeground(Object element, int columnIndex) {
+ if (columnIndex == 1) {
+ return org.eclipse.draw2d.ColorConstants.darkGray;
+ }
+ return null;
+ }
+
}
private class FieldsContentProvider implements IStructuredContentProvider {
Added:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataParser.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataParser.java
(rev 0)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataParser.java 2009-07-06
09:02:08 UTC (rev 16392)
@@ -0,0 +1,211 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.smooks.configuration.editors.csv;
+
+import java.io.ByteArrayInputStream;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.dom4j.DocumentException;
+import org.jboss.tools.smooks.configuration.editors.uitls.SmooksUIUtils;
+import org.jboss.tools.smooks.configuration.editors.xml.TagList;
+import org.jboss.tools.smooks.configuration.editors.xml.XMLObjectAnalyzer;
+import org.jboss.tools.smooks.model.csv.CsvReader;
+import org.jboss.tools.smooks.model.graphics.ext.InputType;
+import org.jboss.tools.smooks.model.graphics.ext.ParamType;
+import org.jboss.tools.smooks.model.smooks.AbstractReader;
+import org.jboss.tools.smooks.model.smooks.SmooksResourceListType;
+import org.milyn.Smooks;
+import org.milyn.SmooksUtil;
+import org.milyn.cdr.SmooksResourceConfiguration;
+import org.milyn.csv.CSVReader;
+import org.milyn.xml.XmlUtil;
+import org.w3c.dom.Document;
+
+/**
+ * @author Dart (dpeng(a)redhat.com)
+ *
+ */
+public class CSVDataParser {
+
+ public static final String LINK_CSV_READER = "linkCSVReader";
+
+ public static final String SEPARATOR = "separator";
+
+ public static final String QUOTECHAR = "quoteChar";
+
+ public static final String SKIPLINES = "skiplines";
+
+ public static final String ENCODING = "encoding";
+
+ public static final String FIELDS = "fields";
+
+ public TagList parseCSV(String filePath, InputType inputType, SmooksResourceListType
resourceList)
+ throws FileNotFoundException, DocumentException, InvocationTargetException,
ParserConfigurationException {
+ return parseCSV(new FileInputStream(SmooksUIUtils.parseFilePath(filePath)), inputType,
resourceList);
+ }
+
+ public TagList parseCSV(InputStream inputStream, CsvReader reader)
+ throws ParserConfigurationException, DocumentException {
+ String fields = null;
+ String separator = null;
+ String quoteChar = null;
+ String skiplines = null;
+ String encoding = null;
+ if (reader == null)
+ return null;
+ fields = reader.getFields();
+ separator = reader.getSeparator();
+ skiplines = reader.getSkipLines().toString();
+ quoteChar = reader.getQuote();
+ encoding = reader.getEncoding();
+ return this.parseCSV(inputStream, fields, separator, quoteChar, skiplines, encoding);
+ }
+
+ public TagList parseCSV(InputStream stream, InputType inputType, SmooksResourceListType
resourceList)
+ throws DocumentException, ParserConfigurationException {
+ List<ParamType> paramList = inputType.getParam();
+ String fields = null;
+ String separator = null;
+ String quoteChar = null;
+ String skiplines = null;
+ String encoding = null;
+
+ for (Iterator<?> iterator = paramList.iterator(); iterator.hasNext();) {
+ ParamType paramType = (ParamType) iterator.next();
+ if (paramType.getName().equals(LINK_CSV_READER)) {
+ if (paramType.getValue().equalsIgnoreCase("true") && resourceList
!= null) {
+ List<AbstractReader> readers = resourceList.getAbstractReader();
+ int count = 0;
+ int index = -1;
+ for (Iterator<?> iterator2 = readers.iterator(); iterator2.hasNext();) {
+ AbstractReader abstractReader = (AbstractReader) iterator2.next();
+ if (abstractReader instanceof CsvReader) {
+ count++;
+ if (index == -1) {
+ index = readers.indexOf(abstractReader);
+ }
+ }
+ }
+
+ if (count > 1) {
+ // throw new
+ // RuntimeException("The smooks config file should have only one JSON
reader");
+ }
+ if (index != -1) {
+ return parseCSV(stream, (CsvReader)readers.get(index));
+ // return parseJsonFile(stream, (JsonReader)
+ // readers.get(index));
+ }
+
+ }
+ }
+ if (paramType.getName().equals(FIELDS)) {
+ fields = paramType.getValue();
+ try {
+// fields = fields.replace(';', ',');
+ } catch (Throwable t) {
+
+ }
+ }
+ if (paramType.getName().equals(SEPARATOR)) {
+ separator = paramType.getValue();
+ }
+ if (paramType.getName().equals(SKIPLINES)) {
+ skiplines = paramType.getValue();
+ }
+ if (paramType.getName().equals(QUOTECHAR)) {
+ quoteChar = paramType.getValue();
+ }
+ if (paramType.getName().equals(ENCODING)) {
+ encoding = paramType.getValue();
+ }
+ }
+
+ return this.parseCSV(stream, fields, separator, quoteChar, skiplines, encoding);
+ }
+
+ public TagList parseCSV(String filePath, String fields, String separator, String
quoteChar, String skiplines,
+ String encoding) throws DocumentException, FileNotFoundException {
+ return parseCSV(new FileInputStream(filePath), fields, separator, quoteChar, skiplines,
encoding);
+ }
+
+ public TagList parseCSV(InputStream stream, String fields, String separator, String
quoteChar, String skiplines,
+ String encoding) throws DocumentException {
+
+ Smooks smooks = new Smooks();
+ SmooksResourceConfiguration readerConfig = new
SmooksResourceConfiguration("org.xml.sax.driver",
+ CSVReader.class.getName());
+// if(quoteChar == null) quoteChar = "\"";
+// if(skiplines == null) skiplines = "0";
+// if(encoding == null) encoding = "UTF-8";
+ readerConfig.setParameter("fields", fields);
+ readerConfig.setParameter("separator", separator);
+ readerConfig.setParameter("quote-char", quoteChar);
+ readerConfig.setParameter("skip-line-count", skiplines);
+ readerConfig.setParameter("encoding", encoding);
+
+ SmooksUtil.registerResource(readerConfig, smooks);
+
+ DOMResult domResult = new DOMResult();
+
+ // Filter the message through Smooks and capture the result as a DOM in
+ // the domResult instance...
+ smooks.filter(new StreamSource(stream), domResult);
+
+ // Get the Document object from the domResult. This is the message
+ // model!!!...
+ Document model = (Document) domResult.getNode();
+
+ // So using the model Document, you can construct a tree structure for
+ // the editor.
+
+ StringWriter modelWriter = new StringWriter();
+ XmlUtil.serialize(model, true, modelWriter);
+
+ XMLObjectAnalyzer analyzer = new XMLObjectAnalyzer();
+ ByteArrayInputStream byteinputStream = new
ByteArrayInputStream(modelWriter.toString().getBytes());
+ TagList tagList = analyzer.analyze(byteinputStream, null);
+
+ try {
+ if (byteinputStream != null) {
+ byteinputStream.close();
+ byteinputStream = null;
+ }
+ if (modelWriter != null) {
+ modelWriter.close();
+ modelWriter = null;
+ }
+ if (smooks != null) {
+ smooks.close();
+ smooks = null;
+ }
+ if (stream != null) {
+ stream.close();
+ stream = null;
+ }
+ model = null;
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+
+ return tagList;
+ }
+}
Property changes on:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataParser.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataPathWizardPage.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataPathWizardPage.java 2009-07-06
02:16:20 UTC (rev 16391)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVDataPathWizardPage.java 2009-07-06
09:02:08 UTC (rev 16392)
@@ -10,34 +10,66 @@
******************************************************************************/
package org.jboss.tools.smooks.configuration.editors.csv;
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.eclipse.jface.viewers.ViewerFilter;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.jboss.tools.smooks.configuration.editors.uitls.SmooksUIUtils;
import org.jboss.tools.smooks.configuration.editors.xml.AbstractFileSelectionWizardPage;
/**
* @author Dart (dpeng(a)redhat.com)
- *
+ *
*/
public class CSVDataPathWizardPage extends AbstractFileSelectionWizardPage {
+ private CSVDataConfigurationWizardPage configPage;
+
public CSVDataPathWizardPage(String pageName, boolean multiSelect, Object[]
initSelections,
- List<ViewerFilter> filters) {
+ List<ViewerFilter> filters,CSVDataConfigurationWizardPage configPage) {
super(pageName, multiSelect, initSelections, filters);
+ this.configPage = configPage;
}
- public CSVDataPathWizardPage(String pageName, String[] fileExtensionNames) {
+ public CSVDataPathWizardPage(String pageName, String[]
fileExtensionNames,CSVDataConfigurationWizardPage configPage) {
super(pageName, fileExtensionNames);
this.setTitle("CSV file selection");
this.setDescription("Select a CSV data file");
+ this.configPage = configPage;
}
+
-
/* (non-Javadoc)
- * @see
org.jboss.tools.smooks.configuration.editors.xml.AbstractFileSelectionWizardPage#loadedTheObject(java.lang.String)
+ * @see
org.jboss.tools.smooks.configuration.editors.xml.AbstractFileSelectionWizardPage#hookFileTextModifyListener()
*/
@Override
+ protected void hookFileTextModifyListener() {
+ super.hookFileTextModifyListener();
+ this.fileText.addModifyListener(new ModifyListener(){
+
+ /* (non-Javadoc)
+ * @see
org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
+ */
+ public void modifyText(ModifyEvent e) {
+ if(configPage != null){
+ configPage.setFilePath(fileText.getText());
+ }
+ }
+
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seeorg.jboss.tools.smooks.configuration.editors.xml.
+ * AbstractFileSelectionWizardPage#loadedTheObject(java.lang.String)
+ */
+ @Override
protected Object loadedTheObject(String path) throws Exception {
return null;
}
@@ -45,18 +77,32 @@
@Override
protected void changeWizardPageStatus() {
super.changeWizardPageStatus();
+ String text = this.fileText.getText();
+ String error = null;
+ String filePath;
+ try {
+ filePath = SmooksUIUtils.parseFilePath(text);
+ if (!new File(filePath).exists()) {
+ error = "Can't find the file : " + filePath;
+ }
+ } catch (InvocationTargetException e) {
+ error = "Error file path";
+ }
+
+ this.setErrorMessage(error);
+ this.setPageComplete(error == null);
}
@Override
public boolean canFlipToNextPage() {
return super.canFlipToNextPage();
-// String filePath = this.getFilePath();
-// try {
-// filePath = SmooksUIUtils.parseFilePath(filePath);
-// if(filePath == null) return false;
-// return new File(filePath).exists();
-// } catch (InvocationTargetException e) {
-// return false;
-// }
+ // String filePath = this.getFilePath();
+ // try {
+ // filePath = SmooksUIUtils.parseFilePath(filePath);
+ // if(filePath == null) return false;
+ // return new File(filePath).exists();
+ // } catch (InvocationTargetException e) {
+ // return false;
+ // }
}
}
Modified:
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVInputDataWizard.java
===================================================================
---
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVInputDataWizard.java 2009-07-06
02:16:20 UTC (rev 16391)
+++
trunk/smooks/plugins/org.jboss.tools.smooks.ui/src/org/jboss/tools/smooks/configuration/editors/csv/CSVInputDataWizard.java 2009-07-06
09:02:08 UTC (rev 16392)
@@ -3,8 +3,13 @@
*/
package org.jboss.tools.smooks.configuration.editors.csv;
+import java.math.BigInteger;
+import java.util.Iterator;
+import java.util.List;
import java.util.Properties;
+import org.eclipse.emf.common.command.Command;
+import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
@@ -14,97 +19,228 @@
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.jboss.tools.smooks.configuration.editors.SmooksMultiFormEditor;
+import
org.jboss.tools.smooks.configuration.editors.csv.CSVDataConfigurationWizardPage.FieldString;
import
org.jboss.tools.smooks.configuration.editors.wizard.IStructuredDataSelectionWizard;
+import org.jboss.tools.smooks.model.csv.CsvFactory;
+import org.jboss.tools.smooks.model.csv.CsvReader;
import org.jboss.tools.smooks.model.smooks.DocumentRoot;
+import org.jboss.tools.smooks.model.smooks.SmooksPackage;
import org.jboss.tools.smooks.model.smooks.SmooksResourceListType;
+import org.jboss.tools.smooks10.model.smooks.util.SmooksModelUtils;
/**
* @author Dart
- *
+ *
*/
public class CSVInputDataWizard extends Wizard implements IStructuredDataSelectionWizard,
INewWizard {
-
- private SmooksResourceListType resourceList ;
-
+
+ private SmooksResourceListType resourceList;
+
private EditingDomain editingDomain;
-
+
private CSVDataConfigurationWizardPage configPage;
-
+
private CSVDataPathWizardPage pathPage;
-
public CSVInputDataWizard() {
super();
this.setWindowTitle("CSV Input Data Wizard");
}
-
-
@Override
public void addPages() {
- if(pathPage == null){
- pathPage = new CSVDataPathWizardPage("CSV Path Page", new String[]{});
- }
- if(configPage == null){
+ if (configPage == null) {
configPage = new CSVDataConfigurationWizardPage("CSV Configurations Page");
configPage.setSmooksResourceList(resourceList);
}
+
+ if (pathPage == null) {
+ pathPage = new CSVDataPathWizardPage("CSV Path Page", new String[]
{},configPage);
+ }
+
this.addPage(pathPage);
this.addPage(configPage);
super.addPages();
}
-
-
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.eclipse.jface.wizard.Wizard#performFinish()
*/
@Override
public boolean performFinish() {
- if(configPage != null){
-
+ if (configPage != null) {
+ boolean createCSVReader = configPage.isCreateCSVReader();
+ if (createCSVReader) {
+ CsvReader reader = CsvFactory.eINSTANCE.createCsvReader();
+
+ String encoding = configPage.getEncoding();
+ reader.setEncoding(encoding);
+
+ String separator = configPage.getSeparator();
+ reader.setSeparator(separator);
+
+ String skipLines = configPage.getSkipLines();
+ long skip = -1;
+ try {
+ skip = Long.parseLong(skipLines);
+ } catch (Throwable t) {
+
+ }
+ if (skip >= 0) {
+ reader.setSkipLines(BigInteger.valueOf(skip));
+ }
+
+ String quoteChar = configPage.getQuoteChar();
+ reader.setQuote(quoteChar);
+
+ String fields = null;
+ List<FieldString> fieldList = configPage.getFieldsList();
+ if (fieldList != null && !fieldList.isEmpty()) {
+ fields = "";
+ for (Iterator<?> iterator = fieldList.iterator(); iterator.hasNext();) {
+ FieldString fieldString = (FieldString) iterator.next();
+ String f = fieldString.getText();
+ fields += (f + ",");
+ }
+ if (fields.length() > 1) {
+ fields = fields.substring(0, fields.length() - 1);
+ }
+ }
+ reader.setFields(fields);
+
+ Command command = AddCommand.create(editingDomain, resourceList,
SmooksPackage.eINSTANCE
+ .getSmooksResourceListType_AbstractReader(), reader);
+ editingDomain.getCommandStack().execute(command);
+
+ }
}
return true;
}
+
+ public boolean canFinish() {
+ if (configPage != null && pathPage != null) {
+ if (configPage.isPageComplete() && pathPage.isPageComplete())
+ return true;
+ }
+ return false;
+ }
- /* (non-Javadoc)
- * @see
org.jboss.tools.smooks.configuration.editors.wizard.IStructuredDataSelectionWizard#complate(org.jboss.tools.smooks.configuration.editors.SmooksMultiFormEditor)
+ /*
+ * (non-Javadoc)
+ *
+ * @seeorg.jboss.tools.smooks.configuration.editors.wizard.
+ * IStructuredDataSelectionWizard
+ * #complate(org.jboss.tools.smooks.configuration
+ * .editors.SmooksMultiFormEditor)
*/
public void complate(SmooksMultiFormEditor formEditor) {
}
- /* (non-Javadoc)
- * @see
org.jboss.tools.smooks.configuration.editors.wizard.IStructuredDataSelectionWizard#getInputDataTypeID()
+ /*
+ * (non-Javadoc)
+ *
+ * @seeorg.jboss.tools.smooks.configuration.editors.wizard.
+ * IStructuredDataSelectionWizard#getInputDataTypeID()
*/
public String getInputDataTypeID() {
- return null;
+ return SmooksModelUtils.INPUT_TYPE_CSV;
}
- /* (non-Javadoc)
- * @see
org.jboss.tools.smooks.configuration.editors.wizard.IStructuredDataSelectionWizard#getProperties()
+ /*
+ * (non-Javadoc)
+ *
+ * @seeorg.jboss.tools.smooks.configuration.editors.wizard.
+ * IStructuredDataSelectionWizard#getProperties()
*/
public Properties getProperties() {
- return null;
+ Properties pro = new Properties();
+ fillProperties(pro);
+ return pro;
}
- /* (non-Javadoc)
- * @see
org.jboss.tools.smooks.configuration.editors.wizard.IStructuredDataSelectionWizard#getReturnData()
+ private void fillProperties(Properties pro) {
+ if (configPage != null) {
+ boolean createCSVReader = configPage.isCreateCSVReader();
+ boolean useAvailabelReader = configPage.isUseAvailabelReader();
+
+ if (useAvailabelReader || createCSVReader) {
+ pro.put(CSVDataParser.LINK_CSV_READER, "true");
+ return;
+ }
+
+ String fields = null;
+ List<FieldString> fieldList = configPage.getFieldsList();
+ if (fieldList != null && !fieldList.isEmpty()) {
+ fields = "";
+ for (Iterator<?> iterator = fieldList.iterator(); iterator.hasNext();) {
+ FieldString fieldString = (FieldString) iterator.next();
+ String f = fieldString.getText();
+ fields += (f + ",");
+ }
+ if (fields.length() > 1) {
+ fields = fields.substring(0, fields.length() - 1);
+ }
+ }
+
+ if (fields != null && fields.length() != 0) {
+ pro.put(CSVDataParser.FIELDS, fields);
+ }
+
+ String encoding = configPage.getEncoding();
+ if (encoding != null && encoding.length() != 0) {
+ pro.put(CSVDataParser.ENCODING, encoding);
+ }
+
+ String separator = configPage.getSeparator();
+ if (separator != null && separator.length() != 0) {
+ pro.put(CSVDataParser.SEPARATOR, separator);
+ }
+
+ String quoteChar = configPage.getQuoteChar();
+ if (quoteChar != null && quoteChar.length() != 0) {
+ pro.put(CSVDataParser.QUOTECHAR, quoteChar);
+ }
+
+ String skiplines = configPage.getSkipLines();
+ if (skiplines != null && skiplines.length() != 0) {
+ pro.put(CSVDataParser.SKIPLINES, skiplines);
+ }
+
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seeorg.jboss.tools.smooks.configuration.editors.wizard.
+ * IStructuredDataSelectionWizard#getReturnData()
*/
public Object getReturnData() {
return null;
}
- /* (non-Javadoc)
- * @see
org.jboss.tools.smooks.configuration.editors.wizard.IStructuredDataSelectionWizard#getStructuredDataSourcePath()
+ /*
+ * (non-Javadoc)
+ *
+ * @seeorg.jboss.tools.smooks.configuration.editors.wizard.
+ * IStructuredDataSelectionWizard#getStructuredDataSourcePath()
*/
public String getStructuredDataSourcePath() {
- // TODO Auto-generated method stub
+ if (pathPage != null) {
+ return pathPage.getFilePath();
+ }
return null;
}
- /* (non-Javadoc)
- * @see
org.jboss.tools.smooks.configuration.editors.wizard.IStructuredDataSelectionWizard#init(org.eclipse.ui.IEditorSite,
org.eclipse.ui.IEditorInput)
+ /*
+ * (non-Javadoc)
+ *
+ * @seeorg.jboss.tools.smooks.configuration.editors.wizard.
+ * IStructuredDataSelectionWizard#init(org.eclipse.ui.IEditorSite,
+ * org.eclipse.ui.IEditorInput)
*/
public void init(IEditorSite site, IEditorInput input) {
IEditorPart editorPart = site.getWorkbenchWindow().getActivePage().findEditor(input);
@@ -116,14 +252,17 @@
}
editingDomain = formEditor.getEditingDomain();
}
- if(configPage != null){
+ if (configPage != null) {
configPage.setSmooksResourceList(resourceList);
}
}
- /* (non-Javadoc)
- * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
org.eclipse.jface.viewers.IStructuredSelection)
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
+ * org.eclipse.jface.viewers.IStructuredSelection)
*/
public void init(IWorkbench workbench, IStructuredSelection selection) {
}