Search in sources :

Example 1 with XPathFunc

use of com.amalto.workbench.models.XPathFunc in project tmdm-studio-se by Talend.

the class SchematronExpressBuilder method parseFunxml.

private void parseFunxml() throws Exception {
    InputStream in = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document document;
        if (isSchematron) {
            // $NON-NLS-1$
            in = SchematronExpressBuilder.class.getResourceAsStream("XPathFunc.xml");
        } else {
            // $NON-NLS-1$
            in = SchematronExpressBuilder.class.getResourceAsStream("StandardXPathFunc.xml");
        }
        document = builder.parse(in);
        // $NON-NLS-1$
        NodeList list = document.getElementsByTagName("category");
        categories = new ArrayList<XPathFunc>();
        for (int i = 0; i < list.getLength(); i++) {
            XPathFunc xpathFunc = new XPathFunc();
            // get the number i node
            Node node = list.item(i);
            NamedNodeMap map = node.getAttributes();
            // $NON-NLS-1$
            Node nameNode = map.getNamedItem("name");
            xpathFunc.setCategory(nameNode.getTextContent());
            java.util.List<KeyValue> keylist = new ArrayList<KeyValue>();
            for (int j = 0; j < node.getChildNodes().getLength(); j++) {
                Node n = node.getChildNodes().item(j);
                NamedNodeMap fmap = n.getAttributes();
                if (fmap != null && fmap.getLength() > 0) {
                    // $NON-NLS-1$
                    Node n1 = fmap.getNamedItem("name");
                    // $NON-NLS-1$
                    Node n2 = fmap.getNamedItem("help");
                    String help = n2.getTextContent();
                    // $NON-NLS-1$//$NON-NLS-2$
                    help = help.replaceAll("\\n", "\n");
                    KeyValue kv = new KeyValue(n1.getTextContent(), help);
                    keylist.add(kv);
                }
            }
            Collections.sort(keylist, new Comparator<KeyValue>() {

                public int compare(KeyValue o1, KeyValue o2) {
                    if (o1 != null && o2 != null) {
                        return o1.key.compareTo(o2.key);
                    }
                    return 0;
                }
            });
            xpathFunc.setFuncs(keylist);
            categories.add(xpathFunc);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) KeyValue(com.amalto.workbench.models.KeyValue) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) XPathFunc(com.amalto.workbench.models.XPathFunc) Point(org.eclipse.swt.graphics.Point) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 2 with XPathFunc

use of com.amalto.workbench.models.XPathFunc in project tmdm-studio-se by Talend.

the class SchematronExpressBuilder method create.

private void create() {
    com = new Composite(parent, SWT.NONE);
    com.setLayout(getLayout(3));
    // expression
    Group expressG = new Group(com, SWT.NONE);
    expressG.setText(Messages.SchematronExpressBuilder_Expression);
    expressG.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 2));
    expressG.setLayout(getLayout(1));
    // top button group
    Composite topCom = new Composite(expressG, 0);
    Button clearBtn = new Button(topCom, SWT.PUSH);
    topCom.setLayout(getLayout(4));
    clearBtn.setText(Messages.SchematronExpressBuilder_Clear);
    clearBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1));
    clearBtn.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // $NON-NLS-1$
            sourceViewer.setDocument(new Document(""));
        }
    });
    org.eclipse.jface.text.Document doc = new org.eclipse.jface.text.Document(value);
    sourceViewer = new SourceViewer(expressG, new VerticalRuler(5), SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 6);
    gd.heightHint = 150;
    sourceViewer.getControl().setLayoutData(gd);
    WidgetUtils.initRedoUndo(sourceViewer);
    sourceViewer.setDocument(doc);
    // bottom button group
    Composite bottomCom = new Composite(expressG, 0);
    GridLayout ly = getLayout(4);
    bottomCom.setLayout(ly);
    Composite com1 = new Composite(bottomCom, 0);
    com1.setLayout(getLayout(4));
    // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
    String[] strs1 = { "+", "-", "*", "div" };
    for (String str : strs1) {
        final Button btn = new Button(com1, SWT.PUSH);
        btn.setText(str);
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // getTextWidget().setText(getText() + btn.getText());
                insertText(btn.getText());
            }
        });
    }
    Composite com2 = new Composite(bottomCom, 0);
    com2.setLayout(getLayout(4));
    // $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
    String[] strs2 = { "=", "!=", ">", "<" };
    for (String str : strs2) {
        final Button btn = new Button(com2, SWT.PUSH);
        btn.setText(str);
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // getTextWidget().setText(getText() + btn.getText());
                insertText(btn.getText());
            }
        });
    }
    Composite com3 = new Composite(bottomCom, 0);
    com3.setLayout(getLayout(4));
    // $NON-NLS-1$//$NON-NLS-2$
    String[] strs3 = { "and", "or" };
    for (int i = 0; i < strs3.length; i++) {
        final Button btn = new Button(com3, SWT.PUSH);
        btn.setText(strs3[i]);
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // getTextWidget().setText(getText() + btn.getText());
                insertText(btn.getText());
            }
        });
    }
    Composite com4 = new Composite(bottomCom, 0);
    com4.setLayout(getLayout(3));
    // $NON-NLS-1$//$NON-NLS-2$
    String[] strs4 = { ")", "(" };
    for (int i = 0; i < strs4.length; i++) {
        final Button btn = new Button(com4, SWT.PUSH);
        btn.setText(strs4[i]);
        btn.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // getTextWidget().setText(getText() + btn.getText());
                insertText(btn.getText());
            }
        });
    }
    Button xpathButton = new Button(com4, SWT.PUSH);
    // $NON-NLS-1$
    xpathButton.setText("xpath");
    xpathButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            XpathSelectDialog dlg = getXPathSelectDialog();
            dlg.setConceptName(conceptName);
            // dlg.setContext(context);
            dlg.setBlockOnOpen(true);
            dlg.open();
            if (dlg.getReturnCode() == Window.OK) {
                if (getTextWidget().getSelectionText().length() > 0) {
                    // getTextWidget().setText(getText().replace(getTextWidget().getSelectionText(),
                    // dlg.getXpath()));
                    getTextWidget().replaceTextRange(getTextWidget().getSelectionRanges()[0], getTextWidget().getSelectionRanges()[1], dlg.getXpath());
                } else {
                    // getTextWidget().setText(getText()+dlg.getXpath());
                    insertText(dlg.getXpath());
                }
            }
        }
    });
    // //test
    // Group testG=new Group(com,SWT.NONE);
    // testG.setText("Test");
    // GridData gd2=new GridData(SWT.FILL,SWT.FILL,true,true,1,2);
    // gd2.widthHint=190;
    // testG.setLayoutData(gd2);
    // testG.setLayout(getLayout(2));
    // Button btnStartTest=new Button(testG,SWT.PUSH);
    // btnStartTest.setText("Start Test");
    // btnStartTest.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,1,1));
    // 
    // Button btnCleartTest=new Button(testG,SWT.PUSH);
    // btnCleartTest.setText("Clear");
    // btnCleartTest.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false,1,1));
    // 
    // testResult=new StyledText(testG,SWT.V_SCROLL|SWT.BORDER);
    // testResult.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,3));
    // btnCleartTest.addSelectionListener(new SelectionAdapter(){
    // @Override
    // public void widgetSelected(SelectionEvent e) {
    // testResult.setText("");
    // }
    // });
    // categories
    Group categoryG = new Group(com, SWT.NONE);
    categoryG.setText(Messages.SchematronExpressBuilder_Categories);
    GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd1.heightHint = 200;
    gd1.widthHint = 140;
    categoryG.setLayoutData(gd1);
    categoryG.setLayout(getLayout(1));
    categoryList = new List(categoryG, SWT.BORDER | SWT.V_SCROLL);
    categoryList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    for (XPathFunc c : categories) {
        categoryList.add(c.getCategory());
    }
    categoryList.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
        }

        public void widgetSelected(SelectionEvent e) {
            funcList.removeAll();
            String c = categoryList.getSelection()[0];
            for (XPathFunc func : categories) {
                if (func.getCategory().equals(c)) {
                    curfc = func;
                    for (KeyValue kv : func.getFuncs()) {
                        funcList.add(kv.key);
                    }
                    break;
                }
            }
            if (funcList.getItems().length > 0) {
                funcList.select(0);
                helpTxt.setText(curfc.getFuncs().get(0).value);
            }
        }
    });
    // funcations
    Group functionG = new Group(com, SWT.NONE);
    functionG.setText(Messages.SchematronExpressBuilder_Functions);
    functionG.setLayout(getLayout(1));
    GridData gd3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd3.widthHint = 220;
    functionG.setLayoutData(gd3);
    funcList = new List(functionG, SWT.BORDER | SWT.V_SCROLL);
    funcList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    funcList.addSelectionListener(new SelectionListener() {

        public void widgetDefaultSelected(SelectionEvent e) {
            getTextWidget().setText(getText() + funcList.getItem(funcList.getSelectionIndex()));
        }

        public void widgetSelected(SelectionEvent e) {
            String c = funcList.getSelection()[0];
            for (KeyValue kv : curfc.getFuncs()) {
                if (kv.key.equals(c)) {
                    helpTxt.setText(kv.value);
                    break;
                }
            }
        }
    });
    // help
    Group helpG = new Group(com, SWT.NONE);
    helpG.setText(Messages.SchematronExpressBuilder_Help);
    helpG.setLayout(getLayout(1));
    helpG.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    helpTxt = new StyledText(helpG, SWT.BORDER | SWT.WRAP);
    GridData gd4 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
    gd4.widthHint = 210;
    helpTxt.setLayoutData(gd4);
}
Also used : Group(org.eclipse.swt.widgets.Group) SourceViewer(org.eclipse.jface.text.source.SourceViewer) KeyValue(com.amalto.workbench.models.KeyValue) XpathSelectDialog(com.amalto.workbench.dialogs.XpathSelectDialog) Document(org.eclipse.jface.text.Document) Document(org.eclipse.jface.text.Document) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(org.eclipse.swt.widgets.List) VerticalRuler(org.eclipse.jface.text.source.VerticalRuler) StyledText(org.eclipse.swt.custom.StyledText) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) XPathFunc(com.amalto.workbench.models.XPathFunc) Point(org.eclipse.swt.graphics.Point) GridData(org.eclipse.swt.layout.GridData) SelectionListener(org.eclipse.swt.events.SelectionListener)

Aggregations

KeyValue (com.amalto.workbench.models.KeyValue)2 XPathFunc (com.amalto.workbench.models.XPathFunc)2 ArrayList (java.util.ArrayList)2 Point (org.eclipse.swt.graphics.Point)2 NodeList (org.w3c.dom.NodeList)2 XpathSelectDialog (com.amalto.workbench.dialogs.XpathSelectDialog)1 InputStream (java.io.InputStream)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.eclipse.jface.text.Document)1 SourceViewer (org.eclipse.jface.text.source.SourceViewer)1 VerticalRuler (org.eclipse.jface.text.source.VerticalRuler)1 StyledText (org.eclipse.swt.custom.StyledText)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 SelectionListener (org.eclipse.swt.events.SelectionListener)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Button (org.eclipse.swt.widgets.Button)1 Composite (org.eclipse.swt.widgets.Composite)1