use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class RenamePackageTest method helperProjectsPrgTest.
/**
* 2 Projects with a root each: Project RenamePack2 (root: srcTest) requires project RenamePack1
* (root: srcPrg).
*
* @param packageNames package names per root
* @param newPackageName the new package name for packageNames[0][0]
* @param cuNames cu names per package
* @throws Exception if one of the resources cannot be created
*/
private void helperProjectsPrgTest(String[][] packageNames, String newPackageName, String[][][] cuNames) throws Exception {
IJavaProject projectPrg = null;
IJavaProject projectTest = null;
try {
projectPrg = JavaProjectHelper.createJavaProject("RenamePack1", "bin");
assertNotNull(JavaProjectHelper.addRTJar(projectPrg));
IPackageFragmentRoot srcPrg = JavaProjectHelper.addSourceContainer(projectPrg, "srcPrg");
Map optionsPrg = projectPrg.getOptions(false);
JavaProjectHelper.set15CompilerOptions(optionsPrg);
projectPrg.setOptions(optionsPrg);
projectTest = JavaProjectHelper.createJavaProject("RenamePack2", "bin");
assertNotNull(JavaProjectHelper.addRTJar(projectTest));
IPackageFragmentRoot srcTest = JavaProjectHelper.addSourceContainer(projectTest, "srcTest");
Map optionsTest = projectTest.getOptions(false);
JavaProjectHelper.set15CompilerOptions(optionsTest);
projectTest.setOptions(optionsTest);
JavaProjectHelper.addRequiredProject(projectTest, projectPrg);
helperMultiProjects(new IPackageFragmentRoot[] { srcPrg, srcTest }, packageNames, newPackageName, cuNames);
} finally {
JavaProjectHelper.delete(projectPrg);
JavaProjectHelper.delete(projectTest);
}
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class JavadocContentAccess2 method getHTMLContent.
/**
* Returns the Javadoc for a package which could be present in package.html, package-info.java
* or from an attached Javadoc.
*
* @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 {
// parent is the IPackageFragment
element = packageInfo.getParent();
}
return new JavadocContentAccess2(element, packageJavadocNode, cuSource, urlPrefix).toHTML();
}
}
} else // 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
{
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;
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class JavaDocLocations method getJavadocBaseLocation.
public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
return getProjectJavadocLocation((IJavaProject) element);
}
IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);
if (root == null) {
return null;
}
if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
IClasspathEntry entry = root.getResolvedClasspathEntry();
URL javadocLocation = getLibraryJavadocLocation(entry);
if (javadocLocation != null) {
return getLibraryJavadocLocation(entry);
}
entry = root.getRawClasspathEntry();
switch(entry.getEntryKind()) {
case IClasspathEntry.CPE_LIBRARY:
case IClasspathEntry.CPE_VARIABLE:
return getLibraryJavadocLocation(entry);
default:
return null;
}
} else {
return getProjectJavadocLocation(root.getJavaProject());
}
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class JavaNavigation method getContent.
public ClassContent getContent(IJavaProject project, int rootId, String path) throws CoreException {
IPackageFragmentRoot root = getPackageFragmentRoot(project, rootId);
if (root == null) {
return null;
}
if (path.startsWith("/")) {
//non java file
if (root instanceof JarPackageFragmentRoot) {
JarPackageFragmentRoot jarPackageFragmentRoot = (JarPackageFragmentRoot) root;
ZipFile jar = null;
try {
jar = jarPackageFragmentRoot.getJar();
ZipEntry entry = jar.getEntry(path.substring(1));
if (entry != null) {
try (InputStream stream = jar.getInputStream(entry)) {
return createContent(IoUtil.readStream(stream), false);
} catch (IOException e) {
LOG.error("Can't read file content: " + entry.getName(), e);
}
}
} finally {
if (jar != null) {
JavaModelManager.getJavaModelManager().closeZipFile(jar);
}
}
}
Object[] resources = root.getNonJavaResources();
for (Object resource : resources) {
if (resource instanceof JarEntryFile) {
JarEntryFile file = (JarEntryFile) resource;
if (file.getFullPath().toOSString().equals(path)) {
return readFileContent(file);
}
}
if (resource instanceof JarEntryDirectory) {
JarEntryDirectory directory = (JarEntryDirectory) resource;
JarEntryFile file = findJarFile(directory, path);
if (file != null) {
return readFileContent(file);
}
}
}
} else {
return getContent(project, path);
}
return null;
}
use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.
the class JavaNavigation method getProjectDependecyJars.
public List<Jar> getProjectDependecyJars(IJavaProject project) throws JavaModelException {
List<Jar> jars = new ArrayList<>();
for (IPackageFragmentRoot fragmentRoot : project.getAllPackageFragmentRoots()) {
if (fragmentRoot instanceof JarPackageFragmentRoot) {
Jar jar = DtoFactory.getInstance().createDto(Jar.class);
jar.setId(fragmentRoot.hashCode());
jar.setName(fragmentRoot.getElementName());
jars.add(jar);
}
}
return jars;
}
Aggregations