Search in sources :

Example 31 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project che by eclipse.

the class ClasspathEntry method elementDecode.

public static IClasspathEntry elementDecode(Element element, IJavaProject project, Map unknownElements) {
    IPath projectPath = project.getProject().getFullPath();
    NamedNodeMap attributes = element.getAttributes();
    NodeList children = element.getChildNodes();
    boolean[] foundChildren = new boolean[children.getLength()];
    String kindAttr = removeAttribute(TAG_KIND, attributes);
    String pathAttr = removeAttribute(TAG_PATH, attributes);
    // ensure path is absolute
    IPath path = new Path(pathAttr);
    int kind = kindFromString(kindAttr);
    if (kind != IClasspathEntry.CPE_VARIABLE && kind != IClasspathEntry.CPE_CONTAINER && !path.isAbsolute()) {
        if (!(path.segmentCount() > 0 && path.segment(0).equals(org.eclipse.jdt.internal.core.ClasspathEntry.DOT_DOT))) {
            path = projectPath.append(path);
        }
    }
    // source attachment info (optional)
    IPath sourceAttachmentPath = element.hasAttribute(TAG_SOURCEPATH) ? new Path(removeAttribute(TAG_SOURCEPATH, attributes)) : null;
    if (kind != IClasspathEntry.CPE_VARIABLE && sourceAttachmentPath != null && !sourceAttachmentPath.isAbsolute()) {
        sourceAttachmentPath = projectPath.append(sourceAttachmentPath);
    }
    IPath sourceAttachmentRootPath = element.hasAttribute(TAG_ROOTPATH) ? new Path(removeAttribute(TAG_ROOTPATH, attributes)) : null;
    // exported flag (optional)
    //$NON-NLS-1$
    boolean isExported = removeAttribute(TAG_EXPORTED, attributes).equals("true");
    // inclusion patterns (optional)
    IPath[] inclusionPatterns = decodePatterns(attributes, TAG_INCLUDING);
    if (inclusionPatterns == null)
        inclusionPatterns = INCLUDE_ALL;
    // exclusion patterns (optional)
    IPath[] exclusionPatterns = decodePatterns(attributes, TAG_EXCLUDING);
    if (exclusionPatterns == null)
        exclusionPatterns = EXCLUDE_NONE;
    // access rules (optional)
    NodeList attributeList = getChildAttributes(TAG_ACCESS_RULES, children, foundChildren);
    IAccessRule[] accessRules = decodeAccessRules(attributeList);
    // backward compatibility
    if (accessRules == null) {
        accessRules = getAccessRules(inclusionPatterns, exclusionPatterns);
    }
    // combine access rules (optional)
    //$NON-NLS-1$
    boolean combineAccessRestrictions = !removeAttribute(TAG_COMBINE_ACCESS_RULES, attributes).equals("false");
    // extra attributes (optional)
    attributeList = getChildAttributes(TAG_ATTRIBUTES, children, foundChildren);
    IClasspathAttribute[] extraAttributes = decodeExtraAttributes(attributeList);
    // custom output location
    IPath outputLocation = element.hasAttribute(TAG_OUTPUT) ? projectPath.append(removeAttribute(TAG_OUTPUT, attributes)) : null;
    String[] unknownAttributes = null;
    ArrayList unknownChildren = null;
    if (unknownElements != null) {
        // unknown attributes
        int unknownAttributeLength = attributes.getLength();
        if (unknownAttributeLength != 0) {
            unknownAttributes = new String[unknownAttributeLength * 2];
            for (int i = 0; i < unknownAttributeLength; i++) {
                Node attribute = attributes.item(i);
                unknownAttributes[i * 2] = attribute.getNodeName();
                unknownAttributes[i * 2 + 1] = attribute.getNodeValue();
            }
        }
        // unknown children
        for (int i = 0, length = foundChildren.length; i < length; i++) {
            if (!foundChildren[i]) {
                Node node = children.item(i);
                if (node.getNodeType() != Node.ELEMENT_NODE)
                    continue;
                if (unknownChildren == null)
                    unknownChildren = new ArrayList();
                StringBuffer buffer = new StringBuffer();
                decodeUnknownNode(node, buffer, project);
                unknownChildren.add(buffer.toString());
            }
        }
    }
    // recreate the CP entry
    IClasspathEntry entry = null;
    switch(kind) {
        case IClasspathEntry.CPE_PROJECT:
            entry = new org.eclipse.jdt.internal.core.ClasspathEntry(IPackageFragmentRoot.K_SOURCE, IClasspathEntry.CPE_PROJECT, path, // inclusion patterns
            org.eclipse.jdt.internal.core.ClasspathEntry.INCLUDE_ALL, // exclusion patterns
            org.eclipse.jdt.internal.core.ClasspathEntry.EXCLUDE_NONE, // source attachment
            null, // source attachment root
            null, // specific output folder
            null, isExported, accessRules, combineAccessRestrictions, extraAttributes);
            break;
        case IClasspathEntry.CPE_LIBRARY:
            entry = JavaCore.newLibraryEntry(path, sourceAttachmentPath, sourceAttachmentRootPath, accessRules, extraAttributes, isExported);
            break;
        case IClasspathEntry.CPE_SOURCE:
            // must be an entry in this project or specify another project
            String projSegment = path.segment(0);
            if (projSegment != null && path.toOSString().startsWith(project.getProject().getFullPath().toOSString())) {
                // this project
                entry = JavaCore.newSourceEntry(path, inclusionPatterns, exclusionPatterns, outputLocation, extraAttributes);
            } else {
                if (path.segmentCount() == 1 || path.segment(0).equals(project.getProject().getFullPath().segment(0))) {
                    // another project
                    entry = JavaCore.newProjectEntry(path, accessRules, combineAccessRestrictions, extraAttributes, isExported);
                } else {
                    // an invalid source folder
                    entry = JavaCore.newSourceEntry(path, inclusionPatterns, exclusionPatterns, outputLocation, extraAttributes);
                }
            }
            break;
        case IClasspathEntry.CPE_VARIABLE:
            entry = JavaCore.newVariableEntry(path, sourceAttachmentPath, sourceAttachmentRootPath, accessRules, extraAttributes, isExported);
            break;
        case IClasspathEntry.CPE_CONTAINER:
            entry = JavaCore.newContainerEntry(path, accessRules, extraAttributes, isExported);
            break;
        case org.eclipse.jdt.internal.core.ClasspathEntry.K_OUTPUT:
            if (!path.isAbsolute())
                return null;
            entry = new org.eclipse.jdt.internal.core.ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry.K_OUTPUT, IClasspathEntry.CPE_LIBRARY, path, INCLUDE_ALL, EXCLUDE_NONE, // source attachment
            null, // source attachment root
            null, // custom output location
            null, false, // no access rules
            null, // no accessible files to combine
            false, NO_EXTRA_ATTRIBUTES);
            break;
        default:
            throw new AssertionFailedException(Messages.bind(Messages.classpath_unknownKind, kindAttr));
    }
    if (unknownAttributes != null || unknownChildren != null) {
        UnknownXmlElements unknownXmlElements = new UnknownXmlElements();
        unknownXmlElements.attributes = unknownAttributes;
        unknownXmlElements.children = unknownChildren;
        unknownElements.put(path, unknownXmlElements);
    }
    return entry;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NamedNodeMap(org.w3c.dom.NamedNodeMap) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IAccessRule(org.eclipse.jdt.core.IAccessRule)

Example 32 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project che by eclipse.

the class ClasspathEntry method encodeExtraAttributes.

void encodeExtraAttributes(XMLWriter writer, boolean indent, boolean newLine) {
    writer.startTag(TAG_ATTRIBUTES, indent);
    for (int i = 0; i < this.extraAttributes.length; i++) {
        IClasspathAttribute attribute = this.extraAttributes[i];
        HashMap parameters = new HashMap();
        parameters.put(TAG_ATTRIBUTE_NAME, attribute.getName());
        parameters.put(TAG_ATTRIBUTE_VALUE, attribute.getValue());
        writer.printTag(TAG_ATTRIBUTE, parameters, indent, newLine, true);
    }
    writer.endTag(TAG_ATTRIBUTES, indent, true);
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) HashMap(java.util.HashMap)

Example 33 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project azure-tools-for-java by Microsoft.

the class SDKJarsFilter method configureAzureSDK.

private void configureAzureSDK(IJavaProject proj) {
    try {
        IClasspathEntry[] classpath = proj.getRawClasspath();
        for (IClasspathEntry iClasspathEntry : classpath) {
            final IPath containerPath = iClasspathEntry.getPath();
            if (containerPath.toString().contains(Messages.azureSDKcontainerID)) {
                return;
            }
        }
        List<IClasspathEntry> list = new ArrayList<IClasspathEntry>(java.util.Arrays.asList(classpath));
        IClasspathAttribute[] attr = new IClasspathAttribute[1];
        attr[0] = JavaCore.newClasspathAttribute(Messages.jstDep, "/WEB-INF/lib");
        IClasspathEntry jarEntry = JavaCore.newContainerEntry(new Path(Messages.azureSDKcontainerID).append(getLatestSDKVersion()), null, attr, false);
        list.add(jarEntry);
        IClasspathEntry[] newClasspath = (IClasspathEntry[]) list.toArray(new IClasspathEntry[0]);
        proj.setRawClasspath(newClasspath, null);
        // Azure SDK configured - application insights configured for the first time for specific project
        Bundle bundle = Activator.getDefault().getBundle();
        if (bundle != null) {
            PluginUtil.showBusy(true, getShell());
            AppInsightsClient.create("Application Insights", bundle.getVersion().toString());
            PluginUtil.showBusy(false, getShell());
        }
    } catch (Exception e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) CoreException(org.eclipse.core.runtime.CoreException)

Example 34 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project azure-tools-for-java by Microsoft.

the class SDKJarsFilter method getClasspathEntries.

/**
     * Returns the classpath entries.
     */
@Override
public IClasspathEntry[] getClasspathEntries() {
    Bundle bundle = Platform.getBundle(Messages.sdkID);
    //Search the available SDKs
    Bundle[] bundles = Platform.getBundles(Messages.sdkID, null);
    List<IClasspathEntry> listEntries = new ArrayList<IClasspathEntry>();
    if (bundles != null) {
        for (Bundle bundle2 : bundles) {
            if (bundle2.getVersion().toString().startsWith(containerPath.segment(1))) {
                bundle = bundle2;
                break;
            }
        }
        //Get the SDK jar.
        URL sdkJar = FileLocator.find(bundle, new Path(Messages.sdkJar), null);
        URL resSdkJar = null;
        IClasspathAttribute[] attr = null;
        try {
            if (sdkJar != null) {
                resSdkJar = FileLocator.resolve(sdkJar);
            //create classpath attribute for java doc, if present
            }
            if (resSdkJar == null) {
                /* if sdk jar is not present then create an place holder
                	for sdk jar so that it would be shown as missing file */
                URL bundleLoc = new URL(bundle.getLocation());
                StringBuffer strBfr = new StringBuffer(bundleLoc.getPath());
                strBfr.append(File.separator).append(Messages.sdkJar);
                URL jarLoc = new URL(strBfr.toString());
                IPath jarPath = new Path(FileLocator.resolve(jarLoc).getPath());
                File jarFile = jarPath.toFile();
                listEntries.add(JavaCore.newLibraryEntry(new Path(jarFile.getAbsolutePath()), null, null, null, attr, true));
            } else {
                File directory = new File(resSdkJar.getPath());
                //create the library entry for sdk jar
                listEntries.add(JavaCore.newLibraryEntry(new Path(directory.getAbsolutePath()), null, null, null, attr, true));
                FilenameFilter sdkJarsFilter = new SDKJarsFilter();
                File[] jars = new File(String.format("%s%s%s", directory.getParent(), File.separator, Messages.depLocation)).listFiles(sdkJarsFilter);
                for (int i = 0; i < jars.length; i++) {
                    if (jars[i].getName().contains(Messages.appInsightMng) || jars[i].getName().contains(Messages.adAuth) || jars[i].getName().contains(Messages.srvExp)) {
                    /*
                			 * Do not add them as they are not part of Azure SDK.
                			 * They are just used for coding purpose.
                			 */
                    } else {
                        listEntries.add(JavaCore.newLibraryEntry(new Path(jars[i].getAbsolutePath()), null, null, null, attr, true));
                    }
                }
            }
        } catch (Exception e) {
            listEntries = new ArrayList<IClasspathEntry>();
            Activator.getDefault().log(Messages.excp, e);
        }
    }
    IClasspathEntry[] entries = new IClasspathEntry[listEntries.size()];
    //Return the classpath entries.
    return listEntries.toArray(entries);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) URL(java.net.URL) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Example 35 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project webtools.servertools by eclipse.

the class RuntimeClasspathProviderDelegate method resolveClasspathContainerImpl.

/**
 * Resolve the classpath container.
 *
 * @param project a project
 * @param runtime a runtime
 * @return a possibly empty array of classpath entries
 */
public IClasspathEntry[] resolveClasspathContainerImpl(IProject project, IRuntime runtime) {
    if (runtime == null)
        return new IClasspathEntry[0];
    runtimePathMap.put(runtime.getId(), runtime.getLocation());
    IClasspathEntry[] entries = resolveClasspathContainer(project, runtime);
    if (entries == null)
        entries = resolveClasspathContainer(runtime);
    if (entries == null)
        entries = new IClasspathEntry[0];
    synchronized (this) {
        if (sourceAttachments == null)
            load();
    }
    List<SourceAttachmentUpdate> srcAttachments = sourceAttachments;
    if (srcAttachments != null) {
        int size = entries.length;
        int size2 = srcAttachments.size();
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size2; j++) {
                SourceAttachmentUpdate sau = srcAttachments.get(j);
                if (sau.runtimeId.equals(runtime.getId()) && sau.entry.equals(entries[i].getPath())) {
                    IClasspathAttribute[] consolidatedClasspathAttributes = consolidateClasspathAttributes(sau.attributes, entries[i].getExtraAttributes());
                    entries[i] = JavaCore.newLibraryEntry(entries[i].getPath(), sau.sourceAttachmentPath, sau.sourceAttachmentRootPath, entries[i].getAccessRules(), consolidatedClasspathAttributes, false);
                    break;
                }
            }
        }
    }
    String key = project.getName() + "/" + runtime.getId();
    if (!previousClasspath.containsKey(key))
        previousClasspath.put(key, entries);
    else {
        IClasspathEntry[] previousClasspathEntries = previousClasspath.get(key);
        if (previousClasspathEntries == null || previousClasspathEntries.length != entries.length || entriesChanged(previousClasspathEntries, entries)) {
            if (Trace.FINEST) {
                Trace.trace(Trace.STRING_FINEST, "Classpath update: " + key + " " + entries);
            }
            previousClasspath.put(key, entries);
            IPath path = new Path(RuntimeClasspathContainer.SERVER_CONTAINER);
            path = path.append(extensionId).append(runtime.getId());
            try {
                IJavaProject javaProject = JavaCore.create(project);
                JavaCore.setClasspathContainer(path, new IJavaProject[] { javaProject }, new IClasspathContainer[] { null }, new NullProgressMonitor());
            } catch (Exception e) {
                if (Trace.WARNING) {
                    Trace.trace(Trace.STRING_WARNING, "Error updating classpath", e);
                }
            }
        }
    }
    return entries;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IJavaProject(org.eclipse.jdt.core.IJavaProject)

Aggregations

IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)52 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)37 IPath (org.eclipse.core.runtime.IPath)26 ArrayList (java.util.ArrayList)17 IJavaProject (org.eclipse.jdt.core.IJavaProject)16 Path (org.eclipse.core.runtime.Path)11 HashMap (java.util.HashMap)9 CoreException (org.eclipse.core.runtime.CoreException)9 IAccessRule (org.eclipse.jdt.core.IAccessRule)9 File (java.io.File)6 LinkedHashMap (java.util.LinkedHashMap)6 IProject (org.eclipse.core.resources.IProject)6 Map (java.util.Map)5 IFolder (org.eclipse.core.resources.IFolder)5 Bundle (org.osgi.framework.Bundle)5 Iterator (java.util.Iterator)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 URL (java.net.URL)3 IClasspathContainer (org.eclipse.jdt.core.IClasspathContainer)3