Search in sources :

Example 6 with Variable

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;
}
Also used : Path(org.eclipse.core.runtime.Path) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) Variable(org.talend.commons.runtime.model.expressionbuilder.Variable) Bundle(org.osgi.framework.Bundle) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) URL(java.net.URL) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) File(java.io.File)

Example 7 with Variable

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;
}
Also used : Path(org.eclipse.core.runtime.Path) ErrorHandler(org.xml.sax.ErrorHandler) XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Variable(org.talend.commons.runtime.model.expressionbuilder.Variable) Bundle(org.osgi.framework.Bundle) Element(org.w3c.dom.Element) FileWriter(java.io.FileWriter) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) Document(org.w3c.dom.Document) URL(java.net.URL) Attr(org.w3c.dom.Attr) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) File(java.io.File)

Example 8 with Variable

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();
}
Also used : Variable(org.talend.commons.runtime.model.expressionbuilder.Variable)

Example 9 with Variable

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;
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Variable(org.talend.commons.runtime.model.expressionbuilder.Variable) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IRunProcessService(org.talend.designer.runprocess.IRunProcessService) IOException(java.io.IOException) ITalendProcessJavaProject(org.talend.core.runtime.process.ITalendProcessJavaProject) Function(org.talend.designer.rowgenerator.data.Function) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IFolder(org.eclipse.core.resources.IFolder)

Example 10 with Variable

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);
}
Also used : IExpressionDataBean(org.talend.commons.ui.runtime.expressionbuilder.IExpressionDataBean) Variable(org.talend.commons.runtime.model.expressionbuilder.Variable) ExpressionPersistance(org.talend.expressionbuilder.ExpressionPersistance) ArrayList(java.util.ArrayList)

Aggregations

Variable (org.talend.commons.runtime.model.expressionbuilder.Variable)13 ArrayList (java.util.ArrayList)8 List (java.util.List)4 File (java.io.File)3 Path (org.eclipse.core.runtime.Path)3 IOException (java.io.IOException)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 MouseAdapter (org.eclipse.swt.events.MouseAdapter)2 MouseEvent (org.eclipse.swt.events.MouseEvent)2 Bundle (org.osgi.framework.Bundle)2 IExpressionDataBean (org.talend.commons.ui.runtime.expressionbuilder.IExpressionDataBean)2 Function (org.talend.designer.rowgenerator.data.Function)2 Document (org.w3c.dom.Document)2 SAXException (org.xml.sax.SAXException)2 OutputFormat (com.sun.org.apache.xml.internal.serialize.OutputFormat)1 XMLSerializer (com.sun.org.apache.xml.internal.serialize.XMLSerializer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1