Search in sources :

Example 1 with TextTokenElementDescr

use of org.kie.workbench.common.services.datamodeller.parser.descr.TextTokenElementDescr in project kie-wb-common by kiegroup.

the class ParserUtil method populateUnManagedElements.

public static void populateUnManagedElements(int startIndex, int endIndex, ElementDescriptor element) {
    String text;
    TextTokenElementDescr unmanagedToken;
    if (element.getElements().size() > 0) {
        List<ElementDescriptor> originalElements = new ArrayList<ElementDescriptor>();
        originalElements.addAll(element.getElements());
        for (ElementDescriptor child : originalElements) {
            if (startIndex < child.getStart()) {
                unmanagedToken = new TextTokenElementDescr();
                unmanagedToken.setStart(startIndex);
                unmanagedToken.setStop(child.getStart() - 1);
                unmanagedToken.setSourceBuffer(child.getSourceBuffer());
                text = unmanagedToken.getSourceBuffer().substring(unmanagedToken.getStart(), unmanagedToken.getStop() + 1);
                unmanagedToken.setText(text);
                element.getElements().addMemberBefore(child, unmanagedToken);
            }
            startIndex = child.getStop() + 1;
            populateUnManagedElements(child);
        }
        if (startIndex < element.getStop()) {
            unmanagedToken = new TextTokenElementDescr();
            unmanagedToken.setStart(startIndex);
            unmanagedToken.setStop(element.getStop());
            unmanagedToken.setSourceBuffer(element.getSourceBuffer());
            text = unmanagedToken.getSourceBuffer().substring(unmanagedToken.getStart(), unmanagedToken.getStop() + 1);
            unmanagedToken.setText(text);
            element.getElements().add(unmanagedToken);
        }
        if (endIndex > 0 && endIndex > element.getStop() && endIndex < element.getSourceBuffer().length()) {
            unmanagedToken = new TextTokenElementDescr();
            unmanagedToken.setStart(element.getStop() + 1);
            unmanagedToken.setStop(endIndex);
            unmanagedToken.setSourceBuffer(element.getSourceBuffer());
            text = unmanagedToken.getSourceBuffer().substring(unmanagedToken.getStart(), unmanagedToken.getStop() + 1);
            unmanagedToken.setText(text);
            element.getElements().add(unmanagedToken);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ElementDescriptor(org.kie.workbench.common.services.datamodeller.parser.descr.ElementDescriptor) TextTokenElementDescr(org.kie.workbench.common.services.datamodeller.parser.descr.TextTokenElementDescr)

Example 2 with TextTokenElementDescr

use of org.kie.workbench.common.services.datamodeller.parser.descr.TextTokenElementDescr in project kie-wb-common by kiegroup.

the class JavaFileHandler1Test method testMethodRemoval.

@Test
public void testMethodRemoval() {
    try {
        ClassDescr classDescr = fileHandler.getFileDescr().getClassDescr();
        MethodDescr methodDescr = classDescr.getMethod("getField2");
        assertNotNull(methodDescr);
        classDescr.getElements().remove(methodDescr);
        assertStrings(fileContents[0], fileHandler.buildResult());
        methodDescr = classDescr.getMethod("setField1");
        assertNotNull(methodDescr);
        classDescr.getElements().remove(methodDescr);
        assertStrings(fileContents[1], fileHandler.buildResult());
        methodDescr = classDescr.getMethod("getField1");
        assertNotNull(methodDescr);
        classDescr.getElements().remove(methodDescr);
        assertStrings(fileContents[2], fileHandler.buildResult());
        FieldDescr fieldDescr = classDescr.getField("field12");
        assertNotNull(fieldDescr);
        boolean deleted = classDescr.removeField("field12");
        assertEquals(true, deleted);
        assertEquals(fileContents[3], fileHandler.buildResult());
        fieldDescr = DescriptorFactoryImpl.getInstance().createFieldDescr("public int field100 = 12;");
        StringBuilder indentStr = new StringBuilder(System.lineSeparator() + System.lineSeparator() + "    ");
        TextTokenElementDescr indent = new TextTokenElementDescr("", 0, indentStr.length() - 1, 1, 0);
        indent.setSourceBuffer(indentStr);
        classDescr.addField(fieldDescr);
        classDescr.getElements().addMemberBefore(fieldDescr, indent);
        assertEquals(fileContents[4], fileHandler.buildResult());
        methodDescr = DescriptorFactoryImpl.getInstance().createMethodDescr("public java.lang.String getAddress() { return null; }");
        indentStr = new StringBuilder(System.lineSeparator() + System.lineSeparator() + "    ");
        indent = new TextTokenElementDescr("", 0, indentStr.length() - 1, 1, 0);
        indent.setSourceBuffer(indentStr);
        classDescr.addMethod(methodDescr);
        classDescr.getElements().addMemberBefore(methodDescr, indent);
        assertEquals(fileContents[5], fileHandler.buildResult());
        /*

            TODO add more cases

            assertEquals(fileContents[0], fileHandler.build());


            fileHandler.deleteMethod("getField1");
            logger.debug(fileHandler.build());

            fileHandler.deleteField("setField2");
            logger.debug(fileHandler.build());

            fileHandler.deleteMethod("setField2");
            fileHandler.addField("\n\tprotected String surname = null;\n");
            fileHandler.deleteMethod("setField1");
            fileHandler.deleteField("field7");
            fileHandler.addMethod("\n\tpublic static final java.lang.String echo(String msg) {\n\t\treturn msg;\n\t}\n");
            fileHandler.deleteField("field8");
            fileHandler.addField("\n\tprotected int i = 0;\n");
            fileHandler.deleteField("field6");
            fileHandler.deleteField("field9");
            fileHandler.deleteField("field11");
            fileHandler.deleteField("field14");

            fileHandler.addValuePair("\n\tpublic String getUserName() {\n\t\treturn surname;\n\t}\n");
            */
        String result = fileHandler.buildResult();
    // logger.debug(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MethodDescr(org.kie.workbench.common.services.datamodeller.parser.descr.MethodDescr) FieldDescr(org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr) ClassDescr(org.kie.workbench.common.services.datamodeller.parser.descr.ClassDescr) TextTokenElementDescr(org.kie.workbench.common.services.datamodeller.parser.descr.TextTokenElementDescr) Test(org.junit.Test)

Aggregations

TextTokenElementDescr (org.kie.workbench.common.services.datamodeller.parser.descr.TextTokenElementDescr)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 ClassDescr (org.kie.workbench.common.services.datamodeller.parser.descr.ClassDescr)1 ElementDescriptor (org.kie.workbench.common.services.datamodeller.parser.descr.ElementDescriptor)1 FieldDescr (org.kie.workbench.common.services.datamodeller.parser.descr.FieldDescr)1 MethodDescr (org.kie.workbench.common.services.datamodeller.parser.descr.MethodDescr)1