Example usage for org.eclipse.jdt.core IBuffer setContents

List of usage examples for org.eclipse.jdt.core IBuffer setContents

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IBuffer setContents.

Prototype

public void setContents(String contents);

Source Link

Document

Sets the contents of this buffer to the given String.

Usage

From source file:org.springframework.tooling.jdt.ls.commons.java.JavaCodeCompletionUtils.java

License:Open Source License

private static void setContents(ICompilationUnit cu, String source) {
    if (cu == null)
        return;/* w w  w  . j  a  va 2s .c o  m*/

    synchronized (cu) {
        IBuffer buffer;
        try {

            buffer = cu.getBuffer();
        } catch (JavaModelException e) {
            e.printStackTrace();
            buffer = null;
        }

        if (buffer != null)
            buffer.setContents(source);
    }
}

From source file:org.ws.eclipse.xassist.editors.java.TypePackageCompletionProcessor.java

License:Open Source License

private void generateProposals(String currentContent, IProject project, final Collection c,
        final int startOffset, final int length, final int typeScope) {

    class TypePackageCompletionRequestor extends CompletionRequestor {

        public TypePackageCompletionRequestor() {
            super(true);
            setIgnored(CompletionProposal.PACKAGE_REF, false);
            setIgnored(CompletionProposal.TYPE_REF, false);
        }//  w  ww  . ja v  a2 s  .co  m

        public void accept(CompletionProposal proposal) {
            if (proposal.getKind() == CompletionProposal.PACKAGE_REF) {
                String pkgName = new String(proposal.getCompletion());
                addProposalToCollection(c, startOffset, length, pkgName, pkgName, null);
            } else {
                boolean isInterface = Flags.isInterface(proposal.getFlags());
                String completion = new String(proposal.getCompletion());
                if (isInterface && typeScope == IJavaSearchConstants.CLASS
                        || (!isInterface && typeScope == IJavaSearchConstants.INTERFACE)
                        || completion.equals("Dummy2")) //$NON-NLS-1$
                    // don't want Dummy class showing up as option.
                    return;
                int period = completion.lastIndexOf('.');
                String cName = null, pName = null;
                if (period == -1) {
                    cName = completion;
                } else {
                    cName = completion.substring(period + 1);
                    pName = completion.substring(0, period);
                }
                /*Image image = isInterface ? PDEPluginImages.get(PDEPluginImages.OBJ_DESC_GENERATE_INTERFACE) : n);*/
                addProposalToCollection(c, startOffset, length, cName + " - " + pName, //$NON-NLS-1$
                        completion, null);
            }
        }

    }

    try {
        ICompilationUnit unit = getWorkingCopy(project);
        if (unit == null) {
            generateTypeProposals(currentContent, project, c, startOffset, length, 1);
            return;
        }
        IBuffer buff = unit.getBuffer();
        buff.setContents("class Dummy2 { " + currentContent); //$NON-NLS-1$

        CompletionRequestor req = new TypePackageCompletionRequestor();
        unit.codeComplete(15 + currentContent.length(), req);
        unit.discardWorkingCopy();
    } catch (JavaModelException e) {
    }
}

From source file:org.wso2.developerstudio.eclipse.artifact.jaxrs.ui.wizard.JaxrsClassWizard.java

License:Open Source License

public boolean performFinish() {
    String id = "";
    String address = "";
    String serviceClass = "";

    try {//from   www . j ava  2 s . c o  m

        IProject project = getSelectedProject();
        IFolder sourceFolder = ProjectUtils.getWorkspaceFolder(project, "src", "main", "java");
        IFolder webINF = ProjectUtils.getWorkspaceFolder(project, "src", "main", "webapp", "WEB-INF");
        IFile cxfServletXML = webINF.getFile("cxf-servlet.xml");
        IJavaProject javaProject = JavaCore.create(project);
        IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);

        if (classWizardPage.getIsCreateIfClass()) {
            String ifPkg = classWizardPage.getIfPkg();
            String ifClass = classWizardPage.getIfClass();

            IPackageFragment ifSourcePackage = root.getPackageFragment(ifPkg);
            if (!ifSourcePackage.exists()) {
                ifSourcePackage = root.createPackageFragment(ifPkg, false, null);
            }
            ICompilationUnit compilationUnit = ifSourcePackage.createCompilationUnit(ifClass + ".java",
                    JaxUtil.getServiceClassSource(ifPkg, ifClass, classWizardPage.isCreateStubs()), false,
                    null);
            project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

            List superInterfaces = classWizardPage.getSuperInterfaces();
            superInterfaces.add(compilationUnit.getTypes()[0].getFullyQualifiedName());
            classWizardPage.setSuperInterfaces(superInterfaces, false);

            id = compilationUnit.getTypes()[0].getElementName();
            id = Character.toLowerCase(id.charAt(0)) + id.substring(1);
            serviceClass = "";
            address = "/" + compilationUnit.getTypes()[0].getElementName();

        }

        classWizardPage.createType(new NullProgressMonitor());
        IType classSource = classWizardPage.getCreatedType();
        ICompilationUnit unit = classSource.getCompilationUnit();
        unit.getJavaProject().getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

        unit.becomeWorkingCopy(new NullProgressMonitor());
        unit.createImport("javax.ws.rs.Path", null, new NullProgressMonitor());
        String source = unit.getSource();
        String searchFor = "public class " + classSource.getTypeQualifiedName();
        int pos = source.indexOf(searchFor);
        source = (source.substring(0, pos) + "@Path(\"/" + "\")" + System.getProperty("line.separator")
                + source.substring(pos));
        IBuffer workingCopyBuffer = unit.getBuffer();
        workingCopyBuffer.setContents(source);
        unit.commitWorkingCopy(false, new NullProgressMonitor());

        try {

            if (!classWizardPage.getIsCreateIfClass()) {
                id = unit.getTypes()[0].getElementName();
                id = Character.toLowerCase(id.charAt(0)) + id.substring(1);
                serviceClass = unit.getTypes()[0].getFullyQualifiedName();
                address = "/" + unit.getTypes()[0].getElementName();
            }

            JaxUtil.CxfServlet cxfServlet = new JaxUtil.CxfServlet();
            cxfServlet.deserialize(cxfServletXML);
            address = address.replaceAll("([A-Z])", "_$1"); // split CamelCase
            address = address.replaceAll("^/_", "/");
            address = address.toLowerCase();
            String beanClass = unit.getTypes()[0].getFullyQualifiedName();
            cxfServlet.addServer(id, serviceClass, address, beanClass);
            String content = cxfServlet.toString().replaceAll("\\ xmlns=\"\"", "");
            cxfServletXML.setContents(new ByteArrayInputStream(content.getBytes()), IResource.FORCE, null);
            project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

            try {
                IEditorPart javaEditor = JavaUI.openInEditor(unit);
                JavaUI.revealInEditor(javaEditor, (IJavaElement) unit);
            } catch (Exception e) {/* ignore */
                log.error("Exception has occurred", e);
            }

        } catch (Exception e) {
            log.error("cannot update cxf-servlet.xml", e);
        }

    } catch (CoreException e) {
        log.error("CoreException has occurred", e);
    } catch (InterruptedException e) {
        log.error("An InterruptedException has occurred", e);
    }
    return true;
}

From source file:org.wso2.developerstudio.eclipse.artifact.jaxws.ui.wizard.JaxwsClassWizard.java

License:Open Source License

public boolean performFinish() {
    try {//from  www.j  ava2  s  . c  o m
        String ifPkg = jaxwsClassWizardPage.getIfPkg();
        String ifClass = jaxwsClassWizardPage.getIfClass();
        IProject project = getSelectedProject();
        IFolder sourceFolder = ProjectUtils.getWorkspaceFolder(project, "src", "main", "java");
        IFolder webINF = ProjectUtils.getWorkspaceFolder(project, "src", "main", "webapp", "WEB-INF");
        IFile cxfServletXML = webINF.getFile("cxf-servlet.xml");
        IJavaProject javaProject = JavaCore.create(project);
        IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);

        String id = "";
        String serviceClass = null;
        String address = "";

        List superInterfaces = jaxwsClassWizardPage.getSuperInterfaces();

        if (jaxwsClassWizardPage.isCreateServiceInterface()) {

            IPackageFragment ifSourcePackage = root.getPackageFragment(ifPkg);
            if (!ifSourcePackage.exists()) {
                ifSourcePackage = root.createPackageFragment(ifPkg, false, null);
            }

            ICompilationUnit cu = ifSourcePackage.createCompilationUnit(ifClass + ".java",
                    JaxUtil.getServiceClassSource(ifPkg, ifClass, jaxwsClassWizardPage.isCreateStubs()), false,
                    null);
            project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

            superInterfaces.add(cu.getTypes()[0].getFullyQualifiedName());

            id = cu.getTypes()[0].getElementName();
            id = Character.toLowerCase(id.charAt(0)) + id.substring(1);
            serviceClass = cu.getTypes()[0].getFullyQualifiedName();
            address = "/" + cu.getTypes()[0].getElementName();
        }

        jaxwsClassWizardPage.setSuperInterfaces(superInterfaces, false);
        jaxwsClassWizardPage.createType(new NullProgressMonitor());
        IType classSource = jaxwsClassWizardPage.getCreatedType();
        ICompilationUnit unit = classSource.getCompilationUnit();
        unit.becomeWorkingCopy(new NullProgressMonitor());
        unit.createImport("javax.jws.WebService", null, new NullProgressMonitor());
        String source = unit.getSource();
        String searchFor = "public class " + classSource.getTypeQualifiedName();
        int pos = source.indexOf(searchFor);
        source = (source.substring(0, pos) + "@WebService(serviceName = \"" + classSource.getTypeQualifiedName()
                + "\")" + System.getProperty("line.separator") + source.substring(pos));
        IBuffer workingCopyBuffer = unit.getBuffer();
        workingCopyBuffer.setContents(source);
        unit.commitWorkingCopy(false, new NullProgressMonitor());
        unit.getJavaProject().getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

        if (!jaxwsClassWizardPage.isCreateServiceInterface()) {
            id = classSource.getElementName();
            id = Character.toLowerCase(id.charAt(0)) + id.substring(1);
            //serviceClass = classSource.getFullyQualifiedName();
            address = "/" + classSource.getElementName();
        }

        JaxUtil.CxfServlet cxfServlet = new JaxUtil.CxfServlet();

        try {

            cxfServlet = new JaxUtil.CxfServlet();
            cxfServlet.deserialize(cxfServletXML);

            address = address.replaceAll("([A-Z])", "_$1"); // split CamelCase
            address = address.replaceAll("^/_", "/");
            address = address.toLowerCase();
            String beanClass = unit.getTypes()[0].getFullyQualifiedName();
            cxfServlet.addServer(id, serviceClass, address, beanClass);
            /*to drop empty NS, due to https://issues.apache.org/jira/browse/AXIOM-97 (was fixed in AXIOM 1.2.10)*/
            String content = cxfServlet.toString().replaceAll("xmlns=\"\"", "");
            cxfServletXML.setContents(new ByteArrayInputStream(content.getBytes()), IResource.FORCE, null);
            project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

            IEditorPart javaEditor = JavaUI.openInEditor(unit);
            JavaUI.revealInEditor(javaEditor, (IJavaElement) unit);
        } catch (Exception e) {
            log.error("cannot update cxf-servlet.xml", e);
        }

    } catch (CoreException e) {
        log.error("CoreException has occurred", e);
    } catch (InterruptedException e) {
        log.error("An InterruptedException has occurred", e);
    }
    return true;
}