use of org.talend.commons.runtime.model.expressionbuilder.Variable in project tdi-studio-se by Talend.
the class ExpressionFileOperation method importExpressionFromFile.
/**
* yzhang Comment method "getExpressionFromFile".
*
* @param file
* @return
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
*/
public List importExpressionFromFile(File file, Shell shell) throws IOException, ParserConfigurationException, SAXException {
if (file != null) {
List list = new ArrayList();
if (!file.isFile()) {
openDialog(shell);
return list;
} else {
final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
final Bundle b = Platform.getBundle(PLUGIN_ID);
final URL url = FileLocator.toFileURL(FileLocator.find(b, new Path(SCHEMA_XSD), null));
final File schema = new File(url.getPath());
final Document document = XSDValidator.checkXSD(file, schema);
final NodeList expressionNodes = document.getElementsByTagName(Messages.getString(//$NON-NLS-1$
"ExpressionFileOperation.expression"));
if (expressionNodes.getLength() == 1) {
Node expressionNode = expressionNodes.item(0);
NamedNodeMap epxressionAttrs = expressionNode.getAttributes();
Node contentNode = epxressionAttrs.getNamedItem(Messages.getString(//$NON-NLS-1$
"ExpressionFileOperation.content"));
list.add(contentNode.getNodeValue());
}
final NodeList variableNodes = document.getElementsByTagName(Messages.getString(//$NON-NLS-1$
"ExpressionFileOperation.variable"));
for (int i = 0; i < variableNodes.getLength(); i++) {
Node variableNode = variableNodes.item(i);
NamedNodeMap varAttrs = variableNode.getAttributes();
//$NON-NLS-1$
Node nameNode = varAttrs.getNamedItem(Messages.getString("ExpressionFileOperation.name"));
//$NON-NLS-1$
Node valueNode = varAttrs.getNamedItem(Messages.getString("ExpressionFileOperation.value"));
Node talendTypeNode = varAttrs.getNamedItem(Messages.getString(//$NON-NLS-1$
"ExpressionFileOperation.talendType"));
//$NON-NLS-1$
Node nullableNode = varAttrs.getNamedItem(Messages.getString("ExpressionFileOperation.nullable"));
Variable variable = new Variable();
variable.setName(nameNode.getNodeValue());
variable.setValue(valueNode.getNodeValue());
variable.setTalendType(talendTypeNode.getNodeValue());
String s = nullableNode.getNodeValue();
Boolean boo = Boolean.valueOf(s);
if (boo == null) {
boo = false;
}
variable.setNullable(boo);
list.add(variable);
}
return list;
}
}
return null;
}
use of org.talend.commons.runtime.model.expressionbuilder.Variable in project tdi-studio-se by Talend.
the class ExpressionFileOperation method saveExpressionToFile.
/**
* yzhang Comment method "savingExpression".
*
* @return
* @throws IOException
* @throws ParserConfigurationException
*/
public boolean saveExpressionToFile(File file, List<Variable> variables, String expressionContent) throws IOException, ParserConfigurationException {
final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
final Bundle b = Platform.getBundle(PLUGIN_ID);
final URL url = FileLocator.toFileURL(FileLocator.find(b, new Path(SCHEMA_XSD), null));
final File schema = new File(url.getPath());
//$NON-NLS-1$
fabrique.setAttribute(SCHEMA_LANGUAGE, "http://www.w3.org/2001/XMLSchema");
fabrique.setAttribute(SCHEMA_VALIDATOR, schema);
fabrique.setValidating(true);
final DocumentBuilder analyseur = fabrique.newDocumentBuilder();
analyseur.setErrorHandler(new ErrorHandler() {
public void error(final SAXParseException exception) throws SAXException {
throw exception;
}
public void fatalError(final SAXParseException exception) throws SAXException {
throw exception;
}
public void warning(final SAXParseException exception) throws SAXException {
throw exception;
}
});
Document document = analyseur.newDocument();
//$NON-NLS-1$
Element expressionElement = document.createElement("expression");
document.appendChild(expressionElement);
//$NON-NLS-1$
Attr content = document.createAttribute(Messages.getString("ExpressionFileOperation.content"));
content.setNodeValue(expressionContent);
expressionElement.setAttributeNode(content);
for (Variable variable : variables) {
//$NON-NLS-1$
Element variableElement = document.createElement("variable");
expressionElement.appendChild(variableElement);
//$NON-NLS-1$
Attr name = document.createAttribute(Messages.getString("ExpressionFileOperation.name"));
name.setNodeValue(variable.getName());
variableElement.setAttributeNode(name);
//$NON-NLS-1$
Attr value = document.createAttribute(Messages.getString("ExpressionFileOperation.value"));
value.setNodeValue(variable.getValue());
variableElement.setAttributeNode(value);
//$NON-NLS-1$
Attr talendType = document.createAttribute(Messages.getString("ExpressionFileOperation.talendType"));
//$NON-NLS-1$
talendType.setNodeValue(variable.getTalendType());
variableElement.setAttributeNode(talendType);
//$NON-NLS-1$
Attr nullable = document.createAttribute(Messages.getString("ExpressionFileOperation.nullable"));
//$NON-NLS-1$
nullable.setNodeValue(String.valueOf(variable.isNullable()));
variableElement.setAttributeNode(nullable);
}
// use specific Xerces class to write DOM-data to a file:
XMLSerializer serializer = new XMLSerializer();
OutputFormat outputFormat = new OutputFormat();
outputFormat.setIndenting(true);
serializer.setOutputFormat(outputFormat);
FileWriter writer = new FileWriter(file);
serializer.setOutputCharStream(writer);
serializer.serialize(document);
writer.close();
return true;
}
use of org.talend.commons.runtime.model.expressionbuilder.Variable in project tdi-studio-se by Talend.
the class ExpressionGenerator method generate.
public String generate(Object argument) {
final StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(TEXT_1);
ExpressionArguments args = (ExpressionArguments) argument;
String expression = args.getExpression();
List<Variable> variables = args.getVariables();
stringBuffer.append(TEXT_2);
for (Variable var : variables) {
stringBuffer.append(TEXT_3);
stringBuffer.append(var.getName());
stringBuffer.append(TEXT_4);
stringBuffer.append(var.getValue());
stringBuffer.append(TEXT_5);
}
stringBuffer.append(TEXT_6);
stringBuffer.append(expression);
stringBuffer.append(TEXT_7);
stringBuffer.append(TEXT_8);
return stringBuffer.toString();
}
use of org.talend.commons.runtime.model.expressionbuilder.Variable in project tdi-studio-se by Talend.
the class JavaTestShadow method process.
public void process(Text testResultText, TableViewer tableViewer) {
Map<String, String> map = RoutineFunctionParser.getTypePackgeMethods();
Function function = CategoryComposite.getSelectedFunction();
String expression = ExpressionBuilderDialog.getExpressionComposite().getExpression();
if (function != null && expression != null && !"".equals(expression)) {
if (serverThread == null || !serverThread.isAlive()) {
server = ExpressionTestServer.getInstance(Display.getCurrent(), testResultText);
serverThread = new Thread(server);
serverThread.start();
}
//$NON-NLS-1$
String methodFullName = map.get(function.getTalendType().getName() + "." + function.getName());
//$NON-NLS-1$
String[] fullClassAndMethod = methodFullName.split("\\.");
String methodName = fullClassAndMethod[fullClassAndMethod.length - 1];
expression = expression.replaceAll(methodName, methodFullName);
ExpressionArguments arguments = new ExpressionArguments();
arguments.setExpression(expression);
arguments.setVariables((List<Variable>) tableViewer.getInput());
ExpressionGenerator codeGenerator = new ExpressionGenerator();
String fileContent = codeGenerator.generate(arguments);
ITalendProcessJavaProject talendProcessJavaProject = null;
if (GlobalServiceRegister.getDefault().isServiceRegistered(IRunProcessService.class)) {
IRunProcessService processService = (IRunProcessService) GlobalServiceRegister.getDefault().getService(IRunProcessService.class);
talendProcessJavaProject = processService.getTalendProcessJavaProject();
}
if (talendProcessJavaProject == null) {
return;
}
IFolder srcFolder = talendProcessJavaProject.getSrcFolder();
IFolder outputFolder = talendProcessJavaProject.getOutputFolder();
//$NON-NLS-1$
IFile file = srcFolder.getFile(new Path(JavaUtils.JAVA_ROUTINES_DIRECTORY + "/ExpressionVariableTest.java"));
//$NON-NLS-1$
IFile classFile = outputFolder.getFile(new Path(JavaUtils.JAVA_ROUTINES_DIRECTORY + "/ExpressionVariableTest.class"));
InputStream in = new ByteArrayInputStream(fileContent.getBytes());
try {
if (!file.exists()) {
file.create(in, true, null);
} else {
file.delete(true, null);
classFile.delete(true, null);
file.create(in, true, null);
}
while (!classFile.exists()) {
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
RuntimeExceptionHandler.process(e1);
}
}
} catch (CoreException e1) {
RuntimeExceptionHandler.process(e1);
}
try {
in.close();
in = null;
} catch (IOException e2) {
RuntimeExceptionHandler.process(e2);
}
String javaInterpreter = CorePlugin.getDefault().getPreferenceStore().getString(ITalendCorePrefConstants.JAVA_INTERPRETER);
IPath path = outputFolder.getLocation();
//$NON-NLS-1$ //$NON-NLS-2$
String[] str = new String[] { javaInterpreter, "-cp", path.toOSString(), "routines.ExpressionVariableTest" };
InputStreamReader reader = null;
try {
Process exec = Runtime.getRuntime().exec(str);
reader = new InputStreamReader(exec.getErrorStream());
char[] buffer = new char[1024];
int i = 0;
StringBuffer bufferString = new StringBuffer();
while ((i = reader.read(buffer)) != -1) {
bufferString.append(buffer, 0, i);
}
reader.close();
if (bufferString.length() > 0) {
testResultText.setText(bufferString.toString());
}
} catch (IOException e1) {
RuntimeExceptionHandler.process(e1);
} finally {
reader = null;
}
}
}
use of org.talend.commons.runtime.model.expressionbuilder.Variable in project tdi-studio-se by Talend.
the class PigExpressionBuilderDialog method openDialog.
/*
* (non-Javadoc)
*
* @see org.talend.expressionbuilder.ui.IExpressionBuilderDialogController#openDialog()
*/
@Override
public void openDialog(Object obj) {
if (obj instanceof IExpressionDataBean) {
List<Variable> vars = new ArrayList<Variable>();
IExpressionDataBean bean = (IExpressionDataBean) obj;
setDefaultExpression(bean.getExpression());
if (bean.getVariables() != null) {
vars.addAll(bean.getVariables());
}
ExpressionPersistance persistance = ExpressionPersistance.getInstance();
persistance.setOwnerId(bean.getOwnerId());
persistance.setPath(getExpressionStorePath());
for (Variable var1 : persistance.loadExpression().getVariables()) {
boolean needAdd = true;
for (Variable var2 : vars) {
if (var1.getName() != null && var1.getName().equals(var2.getName())) {
needAdd = false;
break;
}
}
if (var1.getName() != null && needAdd) {
vars.add(var1);
}
}
}
open();
setBlockOnOpen(true);
}
Aggregations