Example usage for org.eclipse.jdt.core IPackageFragmentRoot isExternal

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot isExternal

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot isExternal.

Prototype

boolean isExternal();

Source Link

Document

Returns whether this package fragment root is external to the workbench (that is, a local file), and has no underlying resource.

Usage

From source file:ar.com.fluxit.jqa.JQAEclipseRunner.java

License:Open Source License

private Collection<File> getClassPath(IJavaProject javaProject) throws JavaModelException {
    Collection<File> result = new ArrayList<File>();
    for (IPackageFragmentRoot classpathEntry : javaProject.getAllPackageFragmentRoots()) {
        if (classpathEntry.isExternal()) {
            result.add(classpathEntry.getPath().toFile());
        } else {//from w w  w  .j  a  v  a2s .  c o  m
            result.add(Utils.getAbsolutePath(((IJavaProject) classpathEntry.getParent()).getOutputLocation()));
        }

    }
    return result;
}

From source file:at.bestsolution.fxide.jdt.text.javadoc.JavadocContentAccess2.java

License:Open Source License

/**
 * Returns the Javadoc for a package which could be present in package.html, package-info.java
 * or from an attached Javadoc.//from  w w w  .  j  a v  a  2  s.c o m
 *
 * @param packageFragment the package which is requesting for the document
 * @return the document content in HTML format or <code>null</code> if there is no associated
 *         Javadoc
 * @throws CoreException if the Java element does not exists or an exception occurs while
 *             accessing the file containing the package Javadoc
 * @since 3.9
 */
public static String getHTMLContent(IPackageFragment packageFragment) throws CoreException {
    IPackageFragmentRoot root = (IPackageFragmentRoot) packageFragment
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

    //1==> Handle the case when the documentation is present in package-info.java or package-info.class file
    ITypeRoot packageInfo;
    boolean isBinary = root.getKind() == IPackageFragmentRoot.K_BINARY;
    if (isBinary) {
        packageInfo = packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
    } else {
        packageInfo = packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
    }
    if (packageInfo != null && packageInfo.exists()) {
        String cuSource = packageInfo.getSource();
        //the source can be null for some of the class files
        if (cuSource != null) {
            Javadoc packageJavadocNode = getPackageJavadocNode(packageFragment, cuSource);
            if (packageJavadocNode != null) {
                IJavaElement element;
                if (isBinary) {
                    element = ((IClassFile) packageInfo).getType();
                } else {
                    element = packageInfo.getParent(); // parent is the IPackageFragment
                }
                return new JavadocContentAccess2(element, packageJavadocNode, cuSource).toHTML();
            }
        }
    }

    // 2==> Handle the case when the documentation is done in package.html file. The file can be either in normal source folder or coming from a jar file
    else {
        Object[] nonJavaResources = packageFragment.getNonJavaResources();
        // 2.1 ==>If the package.html file is present in the source or directly in the binary jar
        for (Object nonJavaResource : nonJavaResources) {
            if (nonJavaResource instanceof IFile) {
                IFile iFile = (IFile) nonJavaResource;
                if (iFile.exists() && JavaModelUtil.PACKAGE_HTML.equals(iFile.getName())) {
                    return getIFileContent(iFile);
                }
            }
        }

        // 2.2==>The file is present in a binary container
        if (isBinary) {
            for (Object nonJavaResource : nonJavaResources) {
                // The content is from an external binary class folder
                if (nonJavaResource instanceof IJarEntryResource) {
                    IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
                    String encoding = getSourceAttachmentEncoding(root);
                    if (JavaModelUtil.PACKAGE_HTML.equals(jarEntryResource.getName())
                            && jarEntryResource.isFile()) {
                        return getHTMLContent(jarEntryResource, encoding);
                    }
                }
            }
            //2.3 ==>The file is present in the source attachment path.
            String contents = getHTMLContentFromAttachedSource(root, packageFragment);
            if (contents != null)
                return contents;
        }
    }

    //3==> Handle the case when the documentation is coming from the attached Javadoc
    if ((root.isArchive() || root.isExternal())) {
        return packageFragment.getAttachedJavadoc(null);

    }

    return null;
}

From source file:at.bestsolution.fxide.jdt.text.viewersupport.JavaElementLabelComposer.java

License:Open Source License

private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) {
    try {/*from  ww w . ja  v  a 2 s .  c  om*/
        IClasspathEntry rawEntry = root.getRawClasspathEntry();
        if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            IClasspathEntry entry = JavaModelUtil.getClasspathEntry(root);
            if (entry.getReferencingEntry() != null) {
                return false; // not the variable entry itself, but a referenced entry
            }
            IPath path = rawEntry.getPath().makeRelative();

            if (getFlag(flags, JavaElementLabels.REFERENCED_ROOT_POST_QUALIFIED)) {
                int segements = path.segmentCount();
                if (segements > 0) {
                    fBuffer.append(path.segment(segements - 1));
                    if (segements > 1) {
                        int offset = fBuffer.length();
                        fBuffer.append(JavaElementLabels.CONCAT_STRING);
                        fBuffer.append(path.removeLastSegments(1).toOSString());
                        //                     if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                        //                        fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
                        //                     }
                    }
                } else {
                    fBuffer.append(path.toString());
                }
            } else {
                fBuffer.append(path.toString());
            }
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            if (root.isExternal())
                fBuffer.append(root.getPath().toOSString());
            else
                fBuffer.append(root.getPath().makeRelative().toString());

            //            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
            //               fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            //            }
            return true;
        }
    } catch (JavaModelException e) {
        // problems with class path, ignore (bug 202792)
        return false;
    }
    return false;
}

From source file:at.bestsolution.fxide.jdt.text.viewersupport.JavaElementLabelComposer.java

License:Open Source License

private void appendArchiveLabel(IPackageFragmentRoot root, long flags) {
    boolean external = root.isExternal();
    if (external)
        appendExternalArchiveLabel(root, flags);
    else/*  www.  j a v  a 2s  .co  m*/
        appendInternalArchiveLabel(root, flags);
}

From source file:cn.ieclipse.pde.explorer.java.JavaAdapterFactory.java

License:Apache License

public IExplorable getFromJava(IJavaElement obj) {
    String path = null;//from   w w  w  .  ja  v a 2s  . com
    // java project.
    if (obj instanceof IJavaProject) {
        path = ((IJavaProject) obj).getProject().getLocation().toOSString();
        return new Explorer(path, null);
    }
    // jar resource is null
    else if (obj instanceof JarPackageFragmentRoot) {
        IPackageFragmentRoot lib = ((IPackageFragmentRoot) obj);
        if (!lib.isExternal()) {
            IWorkspaceRoot root = lib.getJavaProject().getProject().getWorkspace().getRoot();
            IResource res = root.findMember(lib.getPath());
            if (res != null) {
                return ExplorerPlugin.getFromResource(res);
            }
        }
        // external lib
        String file = lib.getPath().toOSString();
        return new Explorer(null, file);
    } else if (obj instanceof IPackageFragmentRoot) {
        // src folder
        IPackageFragmentRoot src = ((IPackageFragmentRoot) obj);
        IProject p = src.getJavaProject().getProject();
        String prjPath = p.getLocation().toOSString();
        IResource res = src.getResource();
        // Fix multi-folder source folder issue
        String srcospath = res.getProjectRelativePath().toOSString();
        path = new File(prjPath, srcospath).getAbsolutePath();
        // end fix
        return new Explorer(path, null);
        // System.out.println(path);
    } else if (obj instanceof IPackageFragment) {// other : package
        IResource resource = ((IPackageFragment) obj).getResource();
        path = resource.getLocation().toOSString();
        return new Explorer(path, null);
    } else {// member:filed:
        IResource resource = ((IJavaElement) obj).getResource();
        String file = resource.getLocation().toOSString();
        // get folder
        return new Explorer(null, file);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.NameLookup.java

License:Open Source License

/**
 * Returns the package fragment whose path matches the given
 * (absolute) path, or <code>null</code> if none exist. The domain of
 * the search is bounded by the classpath of the <code>IJavaProject</code>
 * this <code>NameLookup</code> was obtained from.
 * The path can be:/*w  w  w  .  ja  v  a 2  s  . c o m*/
 * - internal to the workbench: "/Project/src"
 * - external to the workbench: "c:/jdk/classes.zip/java/lang"
 */
public IPackageFragment findPackageFragment(IPath path) {
    if (!path.isAbsolute()) {
        throw new IllegalArgumentException(Messages.path_mustBeAbsolute);
    }
    /*
     * TODO (jerome) this code should rather use the package fragment map to find the candidate package, then
     * check if the respective enclosing root maps to the one on this given IPath.
     */
    IResource possibleFragment = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
    if (possibleFragment == null) {
        //external jar
        for (int i = 0; i < this.packageFragmentRoots.length; i++) {
            IPackageFragmentRoot root = this.packageFragmentRoots[i];
            if (!root.isExternal()) {
                continue;
            }
            IPath rootPath = root.getPath();
            if (rootPath.isPrefixOf(path)) {
                String name = path.toOSString();
                // + 1 is for the File.separatorChar
                name = name.substring(rootPath.toOSString().length() + 1, name.length());
                name = name.replace(File.separatorChar, '.');
                IJavaElement[] list = null;
                try {
                    list = root.getChildren();
                } catch (JavaModelException npe) {
                    continue; // the package fragment root is not present;
                }
                int elementCount = list.length;
                for (int j = 0; j < elementCount; j++) {
                    IPackageFragment packageFragment = (IPackageFragment) list[j];
                    if (nameMatches(name, packageFragment, false)) {
                        return packageFragment;
                    }
                }
            }
        }
    } else {
        IJavaElement fromFactory = JavaCore.create(possibleFragment);
        if (fromFactory == null) {
            return null;
        }
        switch (fromFactory.getElementType()) {
        case IJavaElement.PACKAGE_FRAGMENT:
            return (IPackageFragment) fromFactory;
        case IJavaElement.JAVA_PROJECT:
            // default package in a default root
            JavaProject project = (JavaProject) fromFactory;
            try {
                IClasspathEntry entry = project.getClasspathEntryFor(path);
                if (entry != null) {
                    IPackageFragmentRoot root = project.getPackageFragmentRoot(project.getResource());
                    Object defaultPkgRoot = this.packageFragments.get(CharOperation.NO_STRINGS);
                    if (defaultPkgRoot == null) {
                        return null;
                    }
                    if (defaultPkgRoot instanceof PackageFragmentRoot && defaultPkgRoot.equals(root))
                        return ((PackageFragmentRoot) root).getPackageFragment(CharOperation.NO_STRINGS);
                    else {
                        IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) defaultPkgRoot;
                        for (int i = 0; i < roots.length; i++) {
                            if (roots[i].equals(root)) {
                                return ((PackageFragmentRoot) root)
                                        .getPackageFragment(CharOperation.NO_STRINGS);
                            }
                        }
                    }
                }
            } catch (JavaModelException e) {
                return null;
            }
            return null;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return ((PackageFragmentRoot) fromFactory).getPackageFragment(CharOperation.NO_STRINGS);
        }
    }
    return null;
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.ui.JarExportWizardPage.java

License:Open Source License

@Override
public void createControl(Composite parent) {

    Composite container = new Composite(parent, SWT.NONE);
    container.setLayout(new GridLayout());
    container.setLayoutData(new GridData(GridData.FILL_BOTH));
    new Label(container, SWT.NONE).setText(
            "Select the resources to export.\nThe following projects are the project you selected and the projects it depends on.");

    int labelFlags = JavaElementLabelProvider.SHOW_BASICS | JavaElementLabelProvider.SHOW_OVERLAY_ICONS
            | JavaElementLabelProvider.SHOW_SMALL_ICONS;

    ITreeContentProvider treeContentProvider = new StandardJavaElementContentProvider() {
        @Override//from  ww  w.  ja  v  a  2s.  c  o  m
        public boolean hasChildren(Object element) {
            return !(element instanceof IPackageFragment) && super.hasChildren(element);
        }

        @Override
        public Object[] getElements(Object parent) {
            if (parent instanceof IWorkspaceRoot) {
                List<IJavaProject> result = JavaUtils
                        .getJavaProjectDependencies(JarExportWizardPage.this.selectedProject);
                return result.toArray();
            }
            return super.getElements(parent);
        }
    };

    final DecoratingLabelProvider provider = new DecoratingLabelProvider(
            new JavaElementLabelProvider(labelFlags), new ProblemsLabelDecorator(null));

    this.javaSelectionViewer = new CheckboxTreeAndListGroup(container, ResourcesPlugin.getWorkspace().getRoot(),
            treeContentProvider, provider, new StandardJavaElementContentProvider(), provider, SWT.NONE, 420,
            150) {

        /*
         * (non-Javadoc)
         * @see com.ebmwebsourcing.petals.common.internal.provisional.ui.jdt.CheckboxTreeAndListGroup
         * #setTreeChecked(java.lang.Object, boolean)
         */
        @Override
        protected void setTreeChecked(final Object element, final boolean state) {
            if (element instanceof IResource) {
                final IResource resource = (IResource) element;
                if (resource.getName().charAt(0) == '.')
                    return;
            }
            super.setTreeChecked(element, state);
        }
    };

    this.javaSelectionViewer.addTreeFilter(new ViewerFilter() {
        @Override
        public boolean select(Viewer viewer, Object parent, Object element) {

            boolean result = true;
            if (element instanceof IPackageFragment) {
                IPackageFragment pkg = (IPackageFragment) element;
                try {
                    if (pkg.isDefaultPackage())
                        result = pkg.hasChildren();
                    else
                        result = !pkg.hasSubpackages() || pkg.hasChildren()
                                || pkg.getNonJavaResources().length > 0;

                } catch (JavaModelException e) {
                    result = false;
                }
            }

            return result;
        }
    });

    this.javaSelectionViewer.setTreeComparator(new JavaElementComparator());
    this.javaSelectionViewer.setListComparator(new JavaElementComparator());
    this.javaSelectionViewer.addTreeFilter(new ContainerFilter(ContainerFilter.FILTER_NON_CONTAINERS));
    this.javaSelectionViewer.addTreeFilter(new ViewerFilter() {
        @Override
        public boolean select(Viewer viewer, Object p, Object element) {
            if (element instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) element;
                return !root.isArchive() && !root.isExternal();
            }
            return true;
        }
    });

    this.javaSelectionViewer.addListFilter(new ContainerFilter(ContainerFilter.FILTER_CONTAINERS));
    this.javaSelectionViewer.expandTreeToLevel(this.selectedProject, 1);
    this.javaSelectionViewer.addCheckStateListener(new ICheckStateListener() {
        @Override
        public void checkStateChanged(CheckStateChangedEvent event) {
            validate();
        }
    });

    // Options
    final Button exportSourcesButton = new Button(container, SWT.CHECK);
    exportSourcesButton.setText("Export sources");
    exportSourcesButton.setSelection(this.exportSources);
    exportSourcesButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            JarExportWizardPage.this.exportSources = exportSourcesButton.getSelection();
            validate();
        }
    });

    final Button compressJarButton = new Button(container, SWT.CHECK);
    compressJarButton.setText("Compress the content of the JAR file");
    compressJarButton.setSelection(this.compressJar);
    compressJarButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            JarExportWizardPage.this.compressJar = compressJarButton.getSelection();
            validate();
        }
    });

    final Button exportWarningsAndErrorsButton = new Button(container, SWT.CHECK);
    exportWarningsAndErrorsButton.setText("Export class files with compile warnings or errors");
    exportWarningsAndErrorsButton.setSelection(this.exportWarningsAndErrors);
    exportWarningsAndErrorsButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            JarExportWizardPage.this.exportWarningsAndErrors = exportWarningsAndErrorsButton.getSelection();
            validate();
        }
    });

    validate();
    setErrorMessage(null);
    setControl(container);
}

From source file:com.ifedorenko.m2e.sourcelookup.internal.JavaProjectSources.java

License:Open Source License

private void addJavaProject(IJavaProject project) throws CoreException {
    if (project != null) {
        final Map<File, IPackageFragmentRoot> classpath = new LinkedHashMap<File, IPackageFragmentRoot>();
        for (IPackageFragmentRoot fragment : project.getPackageFragmentRoots()) {
            if (fragment.getKind() == IPackageFragmentRoot.K_BINARY
                    && fragment.getSourceAttachmentPath() != null) {
                File classpathLocation;
                if (fragment.isExternal()) {
                    classpathLocation = fragment.getPath().toFile();
                } else {
                    classpathLocation = toFile(fragment.getPath());
                }/* w ww .  jav a2  s .  c o m*/
                if (classpathLocation != null) {
                    classpath.put(classpathLocation, fragment);
                }
            }
        }

        final JavaProjectInfo projectInfo = new JavaProjectInfo(project, classpath);

        final Set<File> projectLocations = new HashSet<File>();

        final String jarLocation = project.getProject().getPersistentProperty(BinaryProjectPlugin.QNAME_JAR);
        if (jarLocation != null) {
            // maven binary project
            projectLocations.add(new File(jarLocation));
        } else {
            // regular project
            projectLocations.add(toFile(project.getOutputLocation()));
            for (IClasspathEntry cpe : project.getRawClasspath()) {
                if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    projectLocations.add(toFile(cpe.getOutputLocation()));
                }
            }
        }

        synchronized (lock) {
            projects.put(project, projectLocations);
            for (File projectLocation : projectLocations) {
                locations.put(projectLocation, projectInfo);
            }
        }
    }
}

From source file:com.microsoft.javapkgsrv.JavaElementLabelComposer.java

License:Open Source License

private boolean appendVariableLabel(IPackageFragmentRoot root, long flags) {
    try {//from   w w w .j  a v  a  2s  .  co  m
        IClasspathEntry rawEntry = root.getRawClasspathEntry();
        if (rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            IClasspathEntry entry = getClasspathEntry(root);
            if (entry.getReferencingEntry() != null) {
                return false; // not the variable entry itself, but a referenced entry
            }
            IPath path = rawEntry.getPath().makeRelative();

            if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) {
                int segements = path.segmentCount();
                if (segements > 0) {
                    fBuffer.append(path.segment(segements - 1));
                    if (segements > 1) {
                        int offset = fBuffer.length();
                        fBuffer.append(CONCAT_STRING);
                        fBuffer.append(path.removeLastSegments(1).toOSString());
                    }
                } else {
                    fBuffer.append(path.toString());
                }
            } else {
                fBuffer.append(path.toString());
            }
            int offset = fBuffer.length();
            fBuffer.append(CONCAT_STRING);
            if (root.isExternal())
                fBuffer.append(root.getPath().toOSString());
            else
                fBuffer.append(root.getPath().makeRelative().toString());
            return true;
        }
    } catch (JavaModelException e) {
        // problems with class path, ignore (bug 202792)
        return false;
    }
    return false;
}

From source file:com.mountainminds.eclemma.autoMerge.OldFileAnalyzer.java

License:Open Source License

public AnalyzedNodes analyze(final IPackageFragmentRoot root) throws CoreException {
    AnalyzedNodes nodes;//w w w .j  a  va2 s .  com
    if (root.isExternal()) {
        nodes = analyzeExternal(root);

    } else {
        nodes = analyzeInternal(root);

    }
    return nodes;

}