List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot isArchive
public boolean isArchive();
From source file:org.dyno.visual.swing.base.ResourceImage.java
License:Open Source License
public ResourceImage(String p) { this.path = p; IJavaProject prj = VisualSwingPlugin.getCurrentProject(); IProject project = prj.getProject(); IResource resource = project.findMember(new Path(p)); if (resource == null) { IPackageFragmentRoot[] roots;//from www.ja v a2 s . com try { roots = prj.getPackageFragmentRoots(); for (IPackageFragmentRoot root : roots) { if (!root.isArchive()) { String src = root.getElementName(); src = "/" + src + p; //$NON-NLS-1$ resource = project.findMember(new Path(src)); if (resource != null) { String ext = resource.getFileExtension(); if (ext.equals("gif") || ext.equals("png") || ext.equals("jpg")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ IPath fullPath = project.getWorkspace().getRoot().getRawLocation() .append(resource.getFullPath()); String fullpath = fullPath.toString(); image = Toolkit.getDefaultToolkit().getImage(fullpath); } else { break; } } } } } catch (JavaModelException e) { VisualSwingPlugin.getLogger().error(e); } } if (image == null) throw new IllegalArgumentException(Messages.RESOURCE_IMAGE_CANNOT_FIND_IMG_PATH + p); }
From source file:org.dyno.visual.swing.parser.listener.StandaloneClassModel.java
License:Open Source License
private IType findDeclaringType(WidgetAdapter adapter) { ICompilationUnit unit = adapter.getCompilationUnit(); if (unit == null) return null; IJavaProject javaPrj = unit.getJavaProject(); try {// w ww . j a v a2 s . c o m IJavaElement[] children = javaPrj.getChildren(); for (IJavaElement child : children) { if (child instanceof IPackageFragmentRoot) { IPackageFragmentRoot root = (IPackageFragmentRoot) child; if (!root.isArchive()) { IJavaElement[] children2 = root.getChildren(); for (IJavaElement child2 : children2) { if (child2 instanceof IPackageFragment) { IPackageFragment pkg = (IPackageFragment) child2; IJavaElement[] children3 = pkg.getChildren(); for (IJavaElement child3 : children3) { if (child3 instanceof ICompilationUnit) { ICompilationUnit icu = (ICompilationUnit) child3; IType type2 = icu.getType(className); if (icu.getElementName().equals((className + ".java")) && type2.exists()) { return type2; } } } } } } } } } catch (Exception e) { ParserPlugin.getLogger().error(e); } return null; }
From source file:org.dyno.visual.swing.types.IconCellEditor.java
License:Open Source License
private IPackageFragmentRoot getSourceRoot(IJavaProject prj) { try {/*from ww w . ja va 2 s . com*/ IJavaElement[] children = prj.getChildren(); for (IJavaElement child : children) { if (child instanceof IPackageFragmentRoot) { IPackageFragmentRoot childRoot = (IPackageFragmentRoot) child; if (!childRoot.isArchive()) return childRoot; } } } catch (JavaModelException e) { VisualSwingPlugin.getLogger().error(e); } return null; }
From source file:org.dyno.visual.swing.types.ImageIconValidator.java
License:Open Source License
public String isValid(Object value) { if (value == null || value.equals("")) //$NON-NLS-1$ return null; StringTokenizer tokenizer = new StringTokenizer((String) value, "/"); //$NON-NLS-1$ if (!tokenizer.hasMoreTokens()) { return Messages.ImageIconValidator_Incorrect_Icon_Image_Format; }//from w ww . j a v a 2s . c o m do { String token = tokenizer.nextToken().trim(); if (token.length() == 0) return Messages.ImageIconValidator_Incorrect_Icon_Image_Format; if (tokenizer.hasMoreTokens()) { char c = token.charAt(0); if (!Character.isJavaIdentifierStart(c)) { return Messages.ImageIconValidator_Incorrect_Icon_Image_Format_Segment_Id; } int i = 0; while (true) { c = token.charAt(i++); if (!Character.isJavaIdentifierPart(c) && c != '.') return Messages.ImageIconValidator_Incorrect_Icon_Image_Format_Segment_Id; if (i >= token.length()) break; } } } while (tokenizer.hasMoreTokens()); IJavaProject prj = VisualSwingPlugin.getCurrentProject(); IProject project = prj.getProject(); IResource resource = project.findMember(new Path((String) value)); if (resource == null) { IPackageFragmentRoot[] roots; try { roots = prj.getPackageFragmentRoots(); for (IPackageFragmentRoot root : roots) { if (!root.isArchive()) { String src = root.getElementName(); src = "/" + src + value; //$NON-NLS-1$ resource = project.findMember(new Path(src)); if (resource != null) { String ext = resource.getFileExtension(); if (ext != null && (ext.equalsIgnoreCase("gif") || ext.equalsIgnoreCase("png") //$NON-NLS-1$//$NON-NLS-2$ || ext.equalsIgnoreCase("jpg"))) //$NON-NLS-1$ return null; else return Messages.ImageIconValidator_Not_Image_File + value; } } } } catch (JavaModelException e) { VisualSwingPlugin.getLogger().error(e); return e.getLocalizedMessage(); } return Messages.ImageIconValidator_Cannot_Find_Such_Image_File + value + "!"; //$NON-NLS-2$ } return null; }
From source file:org.eclipse.ajdt.internal.buildpath.ConfigureAJBuildPathAction.java
License:Open Source License
private IProject getProjectFromSelectedElement(Object firstElement) { if (firstElement instanceof IJavaElement) { IJavaElement element = (IJavaElement) firstElement; IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element); if (root != null && root != element && root.isArchive()) { return null; }/*from w w w . j a v a 2 s .c o m*/ IJavaProject project = element.getJavaProject(); if (project != null) { return project.getProject(); } return null; } else if (firstElement instanceof ClassPathContainer) { return ((ClassPathContainer) firstElement).getJavaProject().getProject(); } else if (firstElement instanceof IAdaptable) { IResource res = (IResource) ((IAdaptable) firstElement).getAdapter(IResource.class); if (res != null) { return res.getProject(); } } return null; }
From source file:org.eclipse.ajdt.internal.ui.wizards.exports.AJJarFileExportOperation.java
License:Open Source License
private void exportResource(IProgressMonitor progressMonitor, IPackageFragmentRoot pkgRoot, boolean isInJavaProject, IResource resource, IPath destinationPath, boolean isInOutputFolder) { // Handle case where META-INF/MANIFEST.MF is part of the exported files if (fJarPackage.areClassFilesExported() && destinationPath.toString().equals("META-INF/MANIFEST.MF")) {//$NON-NLS-1$ if (fJarPackage.isManifestGenerated()) addWarning(Messages.format(JarPackagerMessages.JarFileExportOperation_didNotAddManifestToJar, resource.getFullPath()), null); return;//from ww w. ja v a 2s. c o m } boolean isNonJavaResource = !isInJavaProject || pkgRoot == null; boolean isInClassFolder = false; try { isInClassFolder = pkgRoot != null && !pkgRoot.isArchive() && pkgRoot.getKind() == IPackageFragmentRoot.K_BINARY; } catch (JavaModelException ex) { addWarning(Messages.format(JarPackagerMessages.JarFileExportOperation_cantGetRootKind, resource.getFullPath()), ex); } if ((fJarPackage.areClassFilesExported() && ((isNonJavaResource || (pkgRoot != null && !isJavaFile(resource) && !isClassFile(resource))) || isInClassFolder && isClassFile(resource))) || (fJarPackage.areJavaFilesExported() && (isNonJavaResource || (pkgRoot != null && !isClassFile(resource)) || (isInClassFolder && isClassFile(resource) && !fJarPackage.areClassFilesExported())))) { try { progressMonitor.subTask(Messages.format(JarPackagerMessages.JarFileExportOperation_exporting, destinationPath.toString())); fJarWriter.write((IFile) resource, destinationPath); } catch (CoreException ex) { Throwable realEx = ex.getStatus().getException(); if (realEx instanceof ZipException && realEx.getMessage() != null && realEx.getMessage().startsWith("duplicate entry:")) //$NON-NLS-1$ addWarning(ex.getMessage(), realEx); else addToStatus(ex); } } }
From source file:org.eclipse.cft.server.standalone.ui.internal.application.JarArchivingUIHandler.java
License:Open Source License
protected void bootRepackage(final IPackageFragmentRoot[] roots, File packagedFile) throws CoreException { Repackager bootRepackager = new Repackager(packagedFile); try {/* w w w . j av a2s . c o m*/ bootRepackager.repackage(new Libraries() { public void doWithLibraries(LibraryCallback callBack) throws IOException { for (IPackageFragmentRoot root : roots) { if (root.isArchive()) { File rootFile = new File(root.getPath().toOSString()); if (rootFile.exists()) { callBack.library(new Library(rootFile, LibraryScope.COMPILE)); } } } } }); } catch (IOException e) { errorHandler.handleApplicationDeploymentFailure( NLS.bind(Messages.JavaCloudFoundryArchiver_ERROR_REPACKAGE_SPRING, e.getMessage())); } }
From source file:org.eclipse.che.jdt.internal.core.SourceMapper.java
License:Open Source License
private synchronized void computeAllRootPaths(IType type) { if (this.areRootPathsComputed) { return;/*from ww w. j ava 2s.c o m*/ } IPackageFragmentRoot root = (IPackageFragmentRoot) type.getPackageFragment().getParent(); IPath pkgFragmentRootPath = root.getPath(); final HashSet tempRoots = new HashSet(); long time = 0; if (VERBOSE) { System.out.println("compute all root paths for " + root.getElementName()); //$NON-NLS-1$ time = System.currentTimeMillis(); } final HashSet firstLevelPackageNames = new HashSet(); boolean containsADefaultPackage = false; boolean containsJavaSource = !pkgFragmentRootPath.equals(this.sourcePath); // used to optimize zip file reading only if source path and root path are equals, otherwise // assume that attachment contains Java source String sourceLevel = null; String complianceLevel = null; if (root.isArchive()) { // org.eclipse.jdt.internal.core.JavaModelManager manager = org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager(); ZipFile zip = null; try { zip = manager.getZipFile(pkgFragmentRootPath); for (Enumeration entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String entryName = entry.getName(); if (!entry.isDirectory()) { if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) { int index = entryName.indexOf('/'); if (index != -1) { String firstLevelPackageName = entryName.substring(0, index); if (!firstLevelPackageNames.contains(firstLevelPackageName)) { if (sourceLevel == null) { IJavaProject project = root.getJavaProject(); sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); } IStatus status = Status.OK_STATUS;// JavaConventions // .validatePackageName(firstLevelPackageName, sourceLevel, complianceLevel); if (status.isOK() || status.getSeverity() == IStatus.WARNING) { firstLevelPackageNames.add(firstLevelPackageName); } } } else { containsADefaultPackage = true; } } else if (!containsJavaSource && org.eclipse.che.jdt.internal.core.util.Util.isJavaLikeFileName(entryName)) { containsJavaSource = true; } } } } catch (CoreException e) { // ignore } finally { manager.closeZipFile(zip); // handle null case } } /*else { Object target = JavaModel.getTarget(root.getPath(), true); if (target instanceof IResource) { IResource resource = (IResource) target; if (resource instanceof IContainer) { try { IResource[] members = ((IContainer) resource).members(); for (int i = 0, max = members.length; i < max; i++) { IResource member = members[i]; String resourceName = member.getName(); if (member.getType() == IResource.FOLDER) { if (sourceLevel == null) { IJavaProject project = root.getJavaProject(); sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); } IStatus status = JavaConventions.validatePackageName(resourceName, sourceLevel, complianceLevel); if (status.isOK() || status.getSeverity() == IStatus.WARNING) { firstLevelPackageNames.add(resourceName); } } else if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(resourceName)) { containsADefaultPackage = true; } else if (!containsJavaSource && Util.isJavaLikeFileName(resourceName)) { containsJavaSource = true; } } } catch (CoreException e) { // ignore } } } }*/ if (containsJavaSource) { // no need to read source attachment if it contains no Java source (see https://bugs.eclipse // .org/bugs/show_bug.cgi?id=190840 ) // Object target = JavaModel.getTarget(this.sourcePath, true); // if (target instanceof IContainer) { // IContainer folder = (IContainer)target; // computeRootPath(folder, firstLevelPackageNames, containsADefaultPackage, tempRoots, folder.getFullPath().segmentCount() // /*if external folder, this is the linked folder path*/); // } else { // JavaModelManager // manager = JavaModelManager.getJavaModelManager(); ZipFile zip = null; try { zip = manager.getZipFile(this.sourcePath); for (Enumeration entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String entryName; if (!entry.isDirectory() && Util.isJavaLikeFileName(entryName = entry.getName())) { IPath path = new Path(entryName); int segmentCount = path.segmentCount(); if (segmentCount > 1) { for (int i = 0, max = path.segmentCount() - 1; i < max; i++) { if (firstLevelPackageNames.contains(path.segment(i))) { tempRoots.add(path.uptoSegment(i)); // don't break here as this path could contain other first level package names (see https://bugs // .eclipse.org/bugs/show_bug.cgi?id=74014) } if (i == max - 1 && containsADefaultPackage) { tempRoots.add(path.uptoSegment(max)); } } } else if (containsADefaultPackage) { tempRoots.add(new Path("")); //$NON-NLS-1$ } } } } catch (CoreException e) { // ignore } finally { manager.closeZipFile(zip); // handle null case } // } } int size = tempRoots.size(); if (this.rootPaths != null) { for (Iterator iterator = this.rootPaths.iterator(); iterator.hasNext();) { tempRoots.add(new Path((String) iterator.next())); } this.rootPaths.clear(); } else { this.rootPaths = new ArrayList(size); } size = tempRoots.size(); if (size > 0) { ArrayList sortedRoots = new ArrayList(tempRoots); if (size > 1) { Collections.sort(sortedRoots, new Comparator() { public int compare(Object o1, Object o2) { IPath path1 = (IPath) o1; IPath path2 = (IPath) o2; return path1.segmentCount() - path2.segmentCount(); } }); } for (Iterator iter = sortedRoots.iterator(); iter.hasNext();) { IPath path = (IPath) iter.next(); this.rootPaths.add(path.toString()); } } this.areRootPathsComputed = true; if (VERBOSE) { System.out.println("Spent " + (System.currentTimeMillis() - time) + "ms"); //$NON-NLS-1$ //$NON-NLS-2$ System.out.println("Found " + size + " root paths"); //$NON-NLS-1$ //$NON-NLS-2$ int i = 0; for (Iterator iterator = this.rootPaths.iterator(); iterator.hasNext();) { System.out.println("root[" + i + "]=" + ((String) iterator.next()));//$NON-NLS-1$ //$NON-NLS-2$ i++; } } }
From source file:org.eclipse.che.jdt.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 ww . j a v a 2 s. c om*/ * * @param packageFragment the package which is requesting for the document * @param urlPrefix * @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, String urlPrefix) 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, urlPrefix).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, urlPrefix); 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:org.eclipse.che.plugin.java.testing.JavaTestFinder.java
License:Open Source License
private IRegion getRegion(IJavaElement element) { IRegion result = JavaCore.newRegion(); if (element.getElementType() == IJavaElement.JAVA_PROJECT) { //for projects only add the contained source folders try {/*from w w w . ja v a2s . c om*/ IPackageFragmentRoot[] packageFragmentRoots = ((IJavaProject) element).getPackageFragmentRoots(); for (IPackageFragmentRoot packageFragmentRoot : packageFragmentRoots) { if (!packageFragmentRoot.isArchive()) { result.add(packageFragmentRoot); } } } catch (JavaModelException e) { LOG.info("Can't read source folders.", e); } } else { result.add(element); } return result; }