Search in sources :

Example 1 with Variable

use of org.talend.commons.runtime.model.expressionbuilder.Variable in project tdi-studio-se by Talend.

the class CategoryComposite method getProposals.

/**
     * yzhang Comment method "getProposals".
     * 
     * @return
     */
public IContentProposal[] getProposals(String categoryFunction, int position) {
    String category = null;
    String function = null;
    boolean displayFunction = false;
    if (categoryFunction.indexOf(".") != -1) {
        //$NON-NLS-1$
        //$NON-NLS-1$
        String[] cf = categoryFunction.split("\\.");
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < cf.length - 1; i++) {
            buffer.append(cf[i]);
            if (i != cf.length - 2) {
                //$NON-NLS-1$
                buffer.append(".");
            }
        }
        if (cf.length == 1) {
            category = cf[cf.length - 1];
        } else {
            function = cf[cf.length - 1];
            category = buffer.toString();
        }
        displayFunction = true;
    } else {
        category = categoryFunction;
    }
    java.util.List<IContentProposal> proposals = new LinkedList<IContentProposal>();
    java.util.List<Category> categories = manager.getInputCategory("java");
    //$NON-NLS-1$
    boolean addAllCategory = category.equals("*C") ? true : false;
    if (!displayFunction) {
        for (Category cg : categories) {
            if (!cg.getName().startsWith("*") && (addAllCategory || cg.getName().startsWith(category))) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                proposals.add(new ExpressionContentProposal(cg.getName(), "", position));
            }
        }
        java.util.List<Variable> vars = ExpressionBuilderDialog.getTestComposite().getVariableList();
        for (Variable var : vars) {
            if (addAllCategory || var.getName().startsWith(category)) {
                proposals.add(new ExpressionContentProposal(var.getName(), var.getValue(), position));
            }
        }
    } else {
        for (Category cg : categories) {
            if (cg.getName().equals(category)) {
                java.util.List<Function> funs = cg.getFunctions();
                boolean addAll = (function == null ? true : false);
                for (Function fun : funs) {
                    if (addAll || fun.getName().startsWith(function)) {
                        proposals.add(new //$NON-NLS-1$
                        ExpressionContentProposal(//$NON-NLS-1$
                        fun.getName() + "()", //$NON-NLS-1$
                        fun.getDescription(), position));
                    }
                }
            }
        }
    }
    Collections.sort(proposals, new CategoryFunctionCompartor());
    String replaceString;
    if (displayFunction) {
        //$NON-NLS-1$
        replaceString = function == null ? "" : function;
    } else {
        replaceString = category;
    }
    ExpressionBuilderDialog.getExpressionComposite().setReplacedText(replaceString);
    return proposals.toArray(new IContentProposal[proposals.size()]);
}
Also used : Category(org.talend.expressionbuilder.model.Category) Variable(org.talend.commons.runtime.model.expressionbuilder.Variable) ExpressionContentProposal(org.talend.expressionbuilder.ui.proposal.ExpressionContentProposal) LinkedList(java.util.LinkedList) IContentProposal(org.eclipse.jface.fieldassist.IContentProposal) Function(org.talend.designer.rowgenerator.data.Function)

Example 2 with Variable

use of org.talend.commons.runtime.model.expressionbuilder.Variable in project tdi-studio-se by Talend.

the class ExpressionBuilderDialog method createButtonsForButtonBar.

/**
     * Create contents of the button bar
     * 
     * @param parent
     */
@Override
protected void createButtonsForButtonBar(Composite parent) {
    ((GridLayout) parent.getLayout()).numColumns++;
    Composite buttons = new Composite(parent, SWT.NONE);
    buttons.setLayout(new GridLayout(2, false));
    final Button importButton = new Button(buttons, SWT.PUSH);
    //$NON-NLS-1$
    importButton.setToolTipText(Messages.getString("ExpressionBuilderDialog.import"));
    importButton.setImage(ImageProvider.getImage(EImage.IMPORT_ICON));
    final Button exportButton = new Button(buttons, SWT.PUSH);
    //$NON-NLS-1$
    exportButton.setToolTipText(Messages.getString("ExpressionBuilderDialog.export"));
    exportButton.setImage(ImageProvider.getImage(EImage.EXPORT_ICON));
    //$NON-NLS-1$
    createButton(parent, IDialogConstants.OK_ID, Messages.getString("ExpressionBuilderDialog.ok.button"), true);
    //$NON-NLS-1$
    createButton(parent, IDialogConstants.CANCEL_ID, Messages.getString("ExpressionBuilderDialog.cancel.button"), false);
    exportButton.addMouseListener(new MouseAdapter() {

        /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events.MouseEvent)
             */
        @Override
        public void mouseUp(MouseEvent e) {
            FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
            //$NON-NLS-1$
            dialog.setFilterExtensions(new String[] { "*.xml" });
            String filePath = dialog.open();
            if (filePath != null) {
                String expresionContent = expressionComposite.getExpression();
                List<Variable> variables = new ArrayList<Variable>();
                variables = testComposite.getVariableList();
                File file = new File(filePath);
                ExpressionFileOperation operation = new ExpressionFileOperation();
                try {
                    if (file != null) {
                        file.createNewFile();
                    }
                    operation.saveExpressionToFile(file, variables, expresionContent);
                } catch (IOException e1) {
                    RuntimeExceptionHandler.process(e1);
                } catch (ParserConfigurationException e1) {
                    RuntimeExceptionHandler.process(e1);
                }
            }
        }
    });
    importButton.addMouseListener(new MouseAdapter() {

        /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.MouseAdapter#mouseUp(org.eclipse.swt.events.MouseEvent)
             */
        @Override
        public void mouseUp(MouseEvent e) {
            FileDialog dialog = new FileDialog(getShell(), SWT.OPEN);
            //$NON-NLS-1$
            dialog.setFilterExtensions(new String[] { "*.xml" });
            String filePath = dialog.open();
            if (filePath != null) {
                File file = new File(filePath);
                ExpressionFileOperation operation = new ExpressionFileOperation();
                try {
                    List list = operation.importExpressionFromFile(file, getShell());
                    if (list != null && list.size() != 0) {
                        expressionComposite.setExpression((String) list.get(0), false);
                        list.remove(0);
                        if (list.size() > 0) {
                            testComposite.addVariables(list);
                        }
                    }
                } catch (IOException e1) {
                    RuntimeExceptionHandler.process(e1);
                } catch (ParserConfigurationException e1) {
                    RuntimeExceptionHandler.process(e1);
                } catch (SAXException e1) {
                    RuntimeExceptionHandler.process(e1);
                }
            }
        }
    });
}
Also used : MouseEvent(org.eclipse.swt.events.MouseEvent) Variable(org.talend.commons.runtime.model.expressionbuilder.Variable) ExpressionFileOperation(org.talend.expressionbuilder.ExpressionFileOperation) Composite(org.eclipse.swt.widgets.Composite) MouseAdapter(org.eclipse.swt.events.MouseAdapter) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) ArrayList(java.util.ArrayList) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File)

Example 3 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 4 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 5 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)

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