Tracking Eclipse .classpath changes (#146)

In order to improve the integration of **Ant** and `javac` now changes
to the Eclipse `.classpath` file will produce an update to the
`build.properties` file. This file has a new property
`uno.java.classpath` that allows to provide `javac` with all the
necessary dependencies when compiling.

If in Eclipse a sibling project is declared as a dependency then either
its jar archive (if it was produced by a `Build.jardesc` file) or its
`bin` folder (the folder where Eclipse puts the `.class` files) will be
available in the `uno.java.classpath` property of the `build.properties`
file.

This allows us to have compilations with `javac` (ie: **Ant**) that work
just as well as compilation by Eclipse's internal compiler.

Concerning the Java jar archives to be put in the `MANIFEST.MF`
`Class-Path` of a UNO Java project, there are two ways to do it:
- Either create a `lib` or `libs` folder at the root of the UNO project.
In this case all archives put in this folder are put in the `Class-Path`
of the `MANIFEST.MF`.
- In the absence of a `lib` or `libs` folder, all archives local to the
project and declared in the Eclipse build path will be put in the
`Class-Path` of the `MANIFEST.MF`.
This commit is contained in:
prrvchr
2025-02-13 15:33:24 +01:00
committed by GitHub
parent 16e4dea2a6
commit a3ce0b61d9
18 changed files with 608 additions and 299 deletions

View File

@ -51,6 +51,7 @@ import java.util.stream.Stream;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
@ -82,7 +83,9 @@ import org.libreoffice.plugin.core.model.UnoPackage;
*/
public class JavaBuilder implements ILanguageBuilder {
// subdirectory in Project, holds external jars
// XXX: directory in the project that contains the external
// XXX: jar files and in sub-directories the source jar files
private static final String LIB_DIR_NAME = "lib";
private static final String LIBS_DIR_NAME = "libs";
private Language mLanguage;
@ -99,22 +102,22 @@ public class JavaBuilder implements ILanguageBuilder {
* {@inheritDoc}
*/
@Override
public IFile createLibrary(IUnoidlProject pUnoProject) throws Exception {
IFile jarFile = ((JavaProjectHandler) mLanguage.getProjectHandler()).getJarFile(pUnoProject);
public IFile createLibrary(IUnoidlProject unoProject) throws Exception {
IFile jarFile = ((JavaProjectHandler) mLanguage.getProjectHandler()).getJarFile(unoProject);
// Add all the jar dependencies
IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(pUnoProject.getName());
IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(unoProject.getName());
IJavaProject javaPrj = JavaCore.create(prj);
List<IFile> externalJars = getLibs(javaPrj);
List<IResource> externalJars = getProjectLibs(unoProject, javaPrj);
JarPackageData description = new JarPackageData();
description.setGenerateManifest(true);
description.setJarLocation(jarFile.getLocation());
String regClassname = ((JavaProjectHandler) mLanguage.getProjectHandler())
.getRegistrationClassName(pUnoProject);
description.setManifestProvider(new UnoManifestProvider(regClassname, pUnoProject, externalJars));
description.setManifestLocation(pUnoProject.getFile("MANIFEST.MF").getFullPath()); //$NON-NLS-1$
.getRegistrationClassName(unoProject);
description.setManifestProvider(new UnoManifestProvider(regClassname, unoProject, externalJars));
description.setManifestLocation(unoProject.getFile("MANIFEST.MF").getFullPath()); //$NON-NLS-1$
description.setSaveManifest(false);
description.setReuseManifest(false);
description.setExportOutputFolders(true);
@ -124,16 +127,19 @@ public class JavaBuilder implements ILanguageBuilder {
// Get the files to export: javamaker output + project classes
FilesVisitor visitor = new FilesVisitor();
visitor.addException(pUnoProject.getFolder(pUnoProject.getUrdPath()));
visitor.addException(unoProject.getFolder(unoProject.getUrdPath()));
// TODO: retrieve value from configuration
visitor.addException(unoProject.getFolder(unoProject.getBuildPath().append("idl")));
visitor.addException(unoProject.getFolder(unoProject.getBuildPath().append("classes")));
IFolder buildDir = pUnoProject.getFolder(pUnoProject.getBuildPath());
IFolder buildDir = unoProject.getFolder(unoProject.getBuildPath());
buildDir.accept(visitor);
// Adding the source directory is not strictly necessary
// (and it has practically no impact on the generated jar).
// But if the build path is empty, the build fails.
// So the contract seems to be that setElements must be called with a non-empty list of files.
IFolder sourceDir = pUnoProject.getFolder(pUnoProject.getSourcePath());
IFolder sourceDir = unoProject.getFolder(unoProject.getSourcePath());
sourceDir.accept(visitor);
description.setElements(visitor.getFiles());
@ -148,8 +154,9 @@ public class JavaBuilder implements ILanguageBuilder {
* {@inheritDoc}
*/
@Override
public void generateFromTypes(ISdk sdk, IOOo ooo, IProject prj, File typesFile,
File buildFolder, String rootModule, IProgressMonitor monitor) {
public void generateFromTypes(ISdk sdk, IOOo ooo, IProject prj,
File typesFile, File buildFolder,
String rootModule, IProgressMonitor monitor) {
if (typesFile.exists()) {
@ -171,8 +178,8 @@ public class JavaBuilder implements ILanguageBuilder {
}
private void runJavamaker(String firstModule, String typesArgs,
ISdk sdk, IProject prj, File typesFile,
File buildFolder, IProgressMonitor monitor) {
ISdk sdk, IProject prj, File typesFile,
File buildFolder, IProgressMonitor monitor) {
StringBuffer errBuf = new StringBuffer();
try {
@ -214,9 +221,9 @@ public class JavaBuilder implements ILanguageBuilder {
* {@inheritDoc}
*/
@Override
public String[] getBuildEnv(IUnoidlProject pUnoProject) {
public String[] getBuildEnv(IUnoidlProject unoProject) {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(pUnoProject.getName());
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(unoProject.getName());
String[] env = new String[2];
@ -273,109 +280,76 @@ public class JavaBuilder implements ILanguageBuilder {
* {@inheritDoc}
*/
@Override
public void fillUnoPackage(UnoPackage pUnoPackage, IUnoidlProject pUnoPrj) {
public void fillUnoPackage(UnoPackage unoPackage, IUnoidlProject unoPrj) {
// Add the component Jar file
JavaProjectHandler handler = (JavaProjectHandler) mLanguage.getProjectHandler();
File libFile = SystemHelper.getFile(handler.getJarFile(pUnoPrj));
File prjFile = SystemHelper.getFile(pUnoPrj);
File libFile = SystemHelper.getFile(handler.getJarFile(unoPrj));
File prjFile = SystemHelper.getFile(unoPrj);
pUnoPackage.addComponentFile(
unoPackage.addComponentFile(
UnoPackage.getPathRelativeToBase(libFile, prjFile),
libFile, "Java"); //$NON-NLS-1$
// Add all the jar dependencies
IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(pUnoPrj.getName());
// Add all the external jar dependencies
IProject prj = ResourcesPlugin.getWorkspace().getRoot().getProject(unoPrj.getName());
IJavaProject javaPrj = JavaCore.create(prj);
List<IFile> libs = getLibs(javaPrj);
for (IFile lib : libs) {
List<IResource> libs = getProjectLibs(unoPrj, javaPrj);
for (IResource lib : libs) {
File jarFile = SystemHelper.getFile(lib);
pUnoPackage.addOtherFile(UnoPackage.getPathRelativeToBase(jarFile, prjFile), jarFile);
unoPackage.addOtherFile(UnoPackage.getPathRelativeToBase(jarFile, prjFile), jarFile);
}
}
/**
* either get list from libs dir when exist, or from the classpath.
* either get list from lib or libs dir when exist, or from the classpath.
*
* @param javaPrj the project from which to extract the libraries
* @param unoPrj the UNO project from which to extract the libraries
*
* @param javaPrj the Java project from which to extract the libraries
*
* @return list of IFile
*/
private List<IFile> getLibs(IJavaProject javaPrj) {
List<IFile> libs = new ArrayList<>();
if (javaPrj.getProject().getFolder(LIBS_DIR_NAME).exists()) {
libs = getLibsFromLibsDir(javaPrj);
private List<IResource> getProjectLibs(IUnoidlProject unoPrj, IJavaProject javaPrj) {
List<IResource> libs = new ArrayList<>();
if (javaPrj.getProject().getFolder(LIB_DIR_NAME).exists()) {
libs = getLibsFromDir(javaPrj, LIB_DIR_NAME);
} else if (javaPrj.getProject().getFolder(LIBS_DIR_NAME).exists()) {
libs = getLibsFromDir(javaPrj, LIBS_DIR_NAME);
} else {
libs = getLibsFromClasspath(javaPrj);
libs = JavaClassPathProvider.getProjectLibs(unoPrj, javaPrj);
}
PluginLogger.debug("Found " + libs.size() + " Jars");
return libs;
}
/**
* Get the libraries in the classpath that are located in the project
* directory or one of its subfolder.
*
* @param javaPrj the project from which to extract the libraries
*
* @return a list of all the File pointing to the libraries.
*/
private List<IFile> getLibsFromClasspath(IJavaProject javaPrj) {
PluginLogger.debug("Collecting Jars from .classpath");
ArrayList<IFile> libs = new ArrayList<>();
try {
IClasspathEntry[] entries = javaPrj.getResolvedClasspath(true);
for (IClasspathEntry entry : entries) {
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
/*
* At first, add only the libraries located in the project
* or one of its children. All others libraries have to be
* managed by the user.
*/
IPath path = entry.getPath();
if (!new File(path.toOSString()).exists() && path.isAbsolute() &&
path.toString().startsWith("/" + javaPrj.getProject().getName())) { //$NON-NLS-1$
// Relative to the project
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
if (file != null && file.exists()) {
libs.add(file);
}
}
}
}
} catch (JavaModelException e) {
// Enable to add some missing library
}
return libs;
}
/**
* when an libs dir exist in the project then return a list of jars.
* Return all jar files contained in the folder without taking into account sub folders.
*
* @param javaProject the java project
*
* @param folder the folder in which we must search
*
* @return list of jar files
*/
private List<IFile> getLibsFromLibsDir(IJavaProject javaProject) {
PluginLogger.debug("Collecting Jars from " + LIBS_DIR_NAME);
private List<IResource> getLibsFromDir(IJavaProject javaProject, String folder) {
PluginLogger.debug("Collecting Jars from: /" + folder);
List<IFile> libs = new ArrayList<>();
IFolder libFolder = javaProject.getProject().getFolder(LIBS_DIR_NAME);
List<IResource> libs = new ArrayList<>();
IFolder libFolder = javaProject.getProject().getFolder(folder);
if (libFolder.exists()) {
java.nio.file.Path pathLibs = Paths.get(libFolder.getRawLocation().toOSString());
try (Stream<java.nio.file.Path> walk = Files.walk(pathLibs)) {
libs = walk.map(jarFile -> {
java.nio.file.Path pathRelative = pathLibs.relativize(jarFile);
return libFolder.getFile(pathRelative.toString());
}).filter(f -> "jar".equalsIgnoreCase(f.getFileExtension()))
.collect(Collectors.toList());
}).filter(f -> f.getType() == IResource.FILE)
.filter(f -> "jar".equals(f.getFileExtension()))
.collect(Collectors.toList());
} catch (IOException e) {
PluginLogger.error(
Messages.getString("JavaBuilder.GetExternalLibsFailed"), e);
PluginLogger.error(Messages.getString("JavaBuilder.GetLibsFromDirFailed"), e);
}
}
PluginLogger.debug("Found " + libs.size() + " Jars");
return libs;
}
}

View File

@ -0,0 +1,230 @@
/*************************************************************************
* The Contents of this file are made available subject to the terms of
* the GNU Lesser General Public License Version 2.1
*
* Sun Microsystems Inc., October, 2000
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2000 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc..
*
* Copyright: 2002 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Contributor(s): Cedric Bosdonnat
*
*
************************************************************************/
package org.libreoffice.ide.eclipse.java;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.libreoffice.ide.eclipse.core.PluginLogger;
import org.libreoffice.ide.eclipse.core.model.IUnoidlProject;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
/**
* An helper for getting the java project class path.
*/
public class JavaClassPathProvider {
/**
* Get the libraries in the classpath that are located in the workspace.
*
* @param unoPrj the project from which to extract the libraries
*
* @param javaPrj the Java project from which to extract the libraries
*
* @return a list of all the File pointing to the libraries.
*/
public static final List<IResource> getProjectLibs(IUnoidlProject unoPrj, IJavaProject javaPrj) {
return getWorkspaceLibs(unoPrj, javaPrj, false);
}
/**
* Get the libraries in the classpath that are located in the workspace.
*
* @param unoPrj the project from which to extract the libraries
*
* @return a list of all the File pointing to the libraries.
*/
public static final List<IResource> getWorkspaceLibs(IUnoidlProject unoPrj) {
return getWorkspaceLibs(unoPrj, JavaCore.create(unoPrj.getProject()), true);
}
/**
* Get the libraries in the classpath that are located in the workspace.
*
* @param unoPrj the UNO project from which to extract the libraries
*
* @param javaPrj the Java project from which to extract the libraries
*
* @param all get the libraries for workspace or project
*
* @return a list of all the File pointing to the libraries.
*/
private static final List<IResource> getWorkspaceLibs(IUnoidlProject unoPrj,
IJavaProject javaPrj,
boolean all) {
PluginLogger.debug("Collecting Jars from: .classpath");
List<IResource> libs = new ArrayList<>();
try {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IClasspathEntry[] entries = javaPrj.getResolvedClasspath(true);
for (IClasspathEntry entry : entries) {
IPath path = entry.getPath();
IResource res = root.findMember(path);
if (res == null || !res.exists()) {
continue;
}
PluginLogger.debug("JavaClassPathProvider.getWorkspaceLibs() lib: " + path.toOSString());
setWorkspaceLibs(root, unoPrj, libs, entry, res, path , all);
}
} catch (JavaModelException e) {
PluginLogger.error(Messages.getString("JavaClassPathProvider.GetWorkspaceLibsFailed"), e);
}
return libs;
}
private static final void setWorkspaceLibs(IWorkspaceRoot root,
IUnoidlProject unoPrj,
List<IResource> libs,
IClasspathEntry entry,
IResource res,
IPath path,
boolean all) {
// We will retrieve all libraries located in the workspace
if (all && entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
// A project has been added to the build path, we are looking for a Build.jardesc file
IFile file = root.getFile(path.append("Build.jardesc"));
if (file.exists()) {
setLibsFromProjectJarDesc(root, libs, file);
} else if (!setLibsFromProjectClass(root, libs, path)) {
String msg = "Collecting Jars from project: %s can't find Build.jardesc file!!!";
PluginLogger.error(String.format(msg, res.getName()));
}
// We will retrieve all libraries located in the project except the build directory
} else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
if (!res.getProjectRelativePath().equals(unoPrj.getBuildPath()) &&
(all || res.getProject() == unoPrj.getProject()) &&
res.getType() == IResource.FILE) {
libs.add(res);
}
}
}
private static final void setLibsFromProjectJarDesc(IWorkspaceRoot root,
List<IResource> libs,
IFile file) {
try {
FileInputStream byteStream = new FileInputStream(file.getLocation().toFile());
InputSource source = new InputSource(byteStream);
// XPath expression evaluation
XPathFactory factory = XPathFactory.newInstance();
javax.xml.xpath.XPath xpath = factory.newXPath();
final String xPathExpr = "//jardesc/jar";
XPathExpression exp = xpath.compile(xPathExpr);
Element node = (Element) exp.evaluate(source, XPathConstants.NODE);
if (node != null) {
String value = node.getAttribute("path");
if (value != null && !value.isBlank()) {
IResource res = root.findMember(value);
if (res != null && res.exists() && res.getType() == IResource.FILE) {
libs.add(res);
}
}
}
byteStream.close();
} catch (IOException | XPathExpressionException e) {
String msg = "Collecting Jars can't parse file: %s!!!";
PluginLogger.debug(String.format(msg, file.getLocation().toOSString()));
}
}
private static final boolean setLibsFromProjectClass(IWorkspaceRoot root,
List<IResource> libs,
IPath path) {
boolean retrieved = false;
IFile file = root.getFile(path.append(".classpath"));
if (file.exists()) {
IResource res = getLibsFromProjectClass(root, libs, path, file);
if (res != null) {
libs.add(res);
retrieved = true;
}
}
return retrieved;
}
private static final IResource getLibsFromProjectClass(IWorkspaceRoot root,
List<IResource> libs,
IPath path,
IFile file) {
IResource res = null;
try {
FileInputStream byteStream = new FileInputStream(file.getLocation().toFile());
InputSource source = new InputSource(byteStream);
// XPath expression evaluation
XPathFactory factory = XPathFactory.newInstance();
javax.xml.xpath.XPath xpath = factory.newXPath();
final String xPathExpr = "//classpath/classpathentry[@kind='output']";
XPathExpression exp = xpath.compile(xPathExpr);
Element node = (Element) exp.evaluate(source, XPathConstants.NODE);
if (node != null) {
String value = node.getAttribute("path");
if (value != null && !value.isBlank()) {
IResource folder = root.getFolder(path.append(value));
if (folder != null && folder.exists() && folder.getType() == IResource.FOLDER) {
res = folder;
}
}
}
byteStream.close();
} catch (IOException | XPathExpressionException e) {
String msg = "Collecting Jars can't parse file: %s!!!";
PluginLogger.debug(String.format(msg, file.getLocation().toOSString()));
}
return res;
}
}

View File

@ -90,7 +90,9 @@ public class JavaProjectHandler implements IProjectHandler {
"juh.jar", //$NON-NLS-1$
"jurt.jar", //$NON-NLS-1$
"unoloader.jar", //$NON-NLS-1$
"officebean.jar" //$NON-NLS-1$
"officebean.jar", //$NON-NLS-1$
"java_websocket.jar", //$NON-NLS-1$
"unoagent.jar" //$NON-NLS-1$
};
/**
@ -108,19 +110,19 @@ public class JavaProjectHandler implements IProjectHandler {
* {@inheritDoc}
*/
@Override
public void addProjectNature(IProject pProject) {
public void addProjectNature(IProject project) {
try {
if (!pProject.exists()) {
pProject.create(null);
if (!project.exists()) {
project.create(null);
PluginLogger.debug("Project created during language specific operation"); //$NON-NLS-1$
}
if (!pProject.isOpen()) {
pProject.open(null);
if (!project.isOpen()) {
project.open(null);
PluginLogger.debug("Project opened"); //$NON-NLS-1$
}
IProjectDescription description = pProject.getDescription();
IProjectDescription description = project.getDescription();
String[] natureIds = description.getNatureIds();
String[] newNatureIds = new String[natureIds.length + 1];
System.arraycopy(natureIds, 0, newNatureIds, 0, natureIds.length);
@ -129,7 +131,7 @@ public class JavaProjectHandler implements IProjectHandler {
newNatureIds[natureIds.length] = JavaCore.NATURE_ID;
description.setNatureIds(newNatureIds);
pProject.setDescription(description, null);
project.setDescription(description, null);
PluginLogger.debug(Messages.getString("Language.JavaNatureSet")); //$NON-NLS-1$
} catch (CoreException e) {
@ -189,17 +191,17 @@ public class JavaProjectHandler implements IProjectHandler {
* {@inheritDoc}
*/
@Override
public String getImplementationName(IUnoidlProject pPrj, String pService) throws Exception {
String prefix = pPrj.getCompanyPrefix();
String comp = pPrj.getOutputExtension();
public String getImplementationName(IUnoidlProject prj, String service) throws Exception {
String prefix = prj.getCompanyPrefix();
String comp = prj.getOutputExtension();
String implementationName = null;
if (pService.startsWith(prefix)) {
String localName = pService.substring(prefix.length());
if (service.startsWith(prefix)) {
String localName = service.substring(prefix.length());
implementationName = prefix + "." + comp + localName + "Impl"; //$NON-NLS-1$ //$NON-NLS-2$
} else {
throw new Exception("Cannot find implementation name for service: " + pService); //$NON-NLS-1$
throw new Exception("Cannot find implementation name for service: " + service); //$NON-NLS-1$
}
return implementationName;
@ -240,44 +242,44 @@ public class JavaProjectHandler implements IProjectHandler {
* {@inheritDoc}
*/
@Override
public String getLibraryPath(IUnoidlProject pProject) {
return getJarFile(pProject).getLocation().toOSString();
public String getLibraryPath(IUnoidlProject project) {
return getJarFile(project).getLocation().toOSString();
}
/**
* Returns a handle to the project jar file. Beware that this handle may refer to a non-existing file. Users have to
* create it if necessary.
*
* @param pProject
* @param project
* the concerned UNO project
* @return a handle to the jar file of the project
*/
public IFile getJarFile(IUnoidlProject pProject) {
String filename = pProject.getName().replace(" ", "") + ".jar"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return pProject.getFile(filename);
public IFile getJarFile(IUnoidlProject project) {
String filename = project.getName().replace(" ", "") + ".jar"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return project.getFile(filename);
}
/**
* Returns a handle to the project jar file. Beware that this handle may refer to a non-existing file. Users have to
* create it if necessary.
*
* @param pProjectDir
* @param projectDir
* the concerned UNO project directory
* @return a handle to the jar file of the project
*/
public File getJarFile(File pProjectDir) throws IOException, XPathException {
String filename = getName(pProjectDir).replace(" ", "") + ".jar"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return new File(pProjectDir, filename);
public File getJarFile(File projectDir) throws IOException, XPathException {
String filename = getName(projectDir).replace(" ", "") + ".jar"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return new File(projectDir, filename);
}
private String getName(File pProjectDir) throws IOException, XPathException {
private String getName(File projectDir) throws IOException, XPathException {
String name = null;
File projectFile = new File(pProjectDir, ".project");
File projectFile = new File(projectDir, ".project");
if (projectFile.exists()) {
final FileInputStream byteStream = new FileInputStream(projectFile);
InputSource source = new InputSource(byteStream);
// evaluation de l'expression XPath
// XPath expression evaluation
XPathFactory factory = XPathFactory.newInstance();
javax.xml.xpath.XPath xpath = factory.newXPath();
final String xPathExpr = "//projectDescription/name";
@ -293,24 +295,24 @@ public class JavaProjectHandler implements IProjectHandler {
/**
* Get the UNO registration class name of the project.
*
* @param pProject
* @param project
* the project for witch to get the registration class.
*
* @return the registration class name
*/
public String getRegistrationClassName(IUnoidlProject pProject) {
return pProject.getProperty(P_REGISTRATION_CLASSNAME);
public String getRegistrationClassName(IUnoidlProject project) {
return project.getProperty(P_REGISTRATION_CLASSNAME);
}
/**
* {@inheritDoc}
*/
@Override
public IFolder[] getBinFolders(IUnoidlProject pUnoidlProject) {
public IFolder[] getBinFolders(IUnoidlProject unoProject) {
ArrayList<IFolder> folders = new ArrayList<IFolder>();
IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
IProject prj = workspace.getProject(pUnoidlProject.getName());
IProject prj = workspace.getProject(unoProject.getName());
IJavaProject javaPrj = JavaCore.create(prj);
try {
folders.add(workspace.getFolder(javaPrj.getOutputLocation()));

View File

@ -40,6 +40,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -48,8 +49,8 @@ import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.CoreException;
import org.libreoffice.ide.eclipse.core.PluginLogger;
import org.libreoffice.ide.eclipse.core.model.IUnoidlProject;
import org.libreoffice.ide.eclipse.core.model.ProjectsManager;
import org.libreoffice.ide.eclipse.java.registration.RegistrationHelper;
@ -68,20 +69,21 @@ public class JavaResourceDeltaVisitor implements IResourceDeltaVisitor {
boolean visitChildren = true;
if (!(delta.getResource() instanceof IWorkspaceRoot)) {
IResource res = delta.getResource();
if (res != null && res.getType() == IResource.FILE) {
IProject project = delta.getResource().getProject();
IUnoidlProject unoprj = ProjectsManager.getProject(project.getName());
if (unoprj != null) {
IUnoidlProject prj = ProjectsManager.getProject(project.getName());
if (prj != null) {
// The resource is a UNO project or is contained in a UNO project
visitChildren = true;
// Check if the resource is a service implementation
if (delta.getKind() == IResourceDelta.ADDED) {
addImplementation(delta, unoprj);
addImplementation(prj, res);
} else if (delta.getKind() == IResourceDelta.REMOVED) {
removeImplementation(delta, unoprj);
removeImplementation(prj, delta, res);
} else if (delta.getKind() == IResourceDelta.CHANGED) {
changeJavaBuildPorperties(prj, res);
}
}
}
@ -92,107 +94,121 @@ public class JavaResourceDeltaVisitor implements IResourceDeltaVisitor {
/**
* Remove the delta resource from the implementations.
*
* @param prj the concerned UNO project
* @param delta the delta to remove
* @param pUnoprj the concerned UNO project
* @param res the concerned delta resource
*/
private void removeImplementation(IResourceDelta delta,
IUnoidlProject pUnoprj) {
IResource res = delta.getResource();
private void removeImplementation(IUnoidlProject prj, IResourceDelta delta, IResource res) {
if (res.getName().endsWith(".java")) { //$NON-NLS-1$
String prjPath = delta.getProjectRelativePath().toString();
prjPath = prjPath.replace(".java", ""); //$NON-NLS-1$ //$NON-NLS-2$
prjPath = prjPath.replace("/", "."); //$NON-NLS-1$ //$NON-NLS-2$
String path = delta.getProjectRelativePath().toString();
path = path.replace(".java", ""); //$NON-NLS-1$ //$NON-NLS-2$
path = path.replace("/", "."); //$NON-NLS-1$ //$NON-NLS-2$
Vector<String> classes = RegistrationHelper.readClassesList(pUnoprj);
Vector<String> classes = RegistrationHelper.readClassesList(prj);
for (String implName : classes) {
if (prjPath.endsWith(implName)) {
RegistrationHelper.removeImplementation(pUnoprj, implName);
if (path.endsWith(implName)) {
RegistrationHelper.removeImplementation(prj, implName);
}
}
} else if (res.getName().endsWith(".jar")) {
RegistrationHelper.checkClassesListFile(pUnoprj);
RegistrationHelper.checkClassesListFile(prj);
}
}
/**
* Add the delta resource to the implementations.
*
* @param delta the delta resource to add.
* @param unoProject the concerned UNO project
* @param prj the concerned UNO project
* @param res the concerned delta resource
*/
private void addImplementation(IResourceDelta delta, IUnoidlProject unoProject) {
String className = isJavaServiceImpl(delta.getResource());
if (className != null) {
RegistrationHelper.addImplementation(unoProject, className);
private void addImplementation( IUnoidlProject prj, IResource res) {
if (res.getName().endsWith(".java")) { //$NON-NLS-1$
String className = getJavaServiceImpl(res);
if (className != null) {
RegistrationHelper.addImplementation(prj, className);
}
}
}
/**
* Check whether a resource is a UNO implementation.
* Change the delta resource to the java project.
*
* @param prj the concerned UNO project
* @param res the concerned delta resource
*/
private void changeJavaBuildPorperties(IUnoidlProject prj, IResource res) {
if (res.getName().equals(".classpath") && //$NON-NLS-1$
prj.hasBuildFile()) {
List <IResource> libs = JavaClassPathProvider.getWorkspaceLibs(prj);
PluginLogger.debug("Found " + libs.size() + " Jars");
prj.saveJavaBuildProperties(libs);
}
}
/**
* Get the java service implementation class name.
*
* @param res the resource to check.
* @return <code>true</code> if it contains the necessary static methods for Java
* UNO service implementation registration.
* @return <code>class name</code> if it contains the necessary static methods for Java
* UNO service implementation registration or <code>null</code> if not.
*/
private String isJavaServiceImpl(IResource res) {
private String getJavaServiceImpl(IResource res) {
String className = null;
if (res.getType() == IResource.FILE && res.getName().endsWith(".java")) { //$NON-NLS-1$
/*
* For sure the resource is a Java class file.
* Now the file has to be read to find out if it contains the two
* following methods:
*
* + public static XSingleComponentFactory __getComponentFactory
* + public static boolean __writeRegistryServiceInfo(XRegistryKey xRegistryKey )
*/
FileInputStream in = null;
BufferedReader reader = null;
/*
* For sure the resource is a Java class file.
* Now the file has to be read to find out if it contains the two
* following methods:
*
* + public static XSingleComponentFactory __getComponentFactory
* + public static boolean __writeRegistryServiceInfo(XRegistryKey xRegistryKey )
*/
FileInputStream in = null;
BufferedReader reader = null;
try {
File file = res.getLocation().toFile();
in = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(in));
// Read the file into a string without line delimiters
String line = reader.readLine();
String fileContent = ""; //$NON-NLS-1$
while (line != null) {
fileContent = fileContent + line;
line = reader.readLine();
}
String getFactoryRegex = "public\\s+static\\s+XSingleComponentFactory" + //$NON-NLS-1$
"\\s+__getComponentFactory"; //$NON-NLS-1$
boolean containsGetFactory = fileContent.split(getFactoryRegex).length > 1;
String writeServiceRegex = "public\\s+static\\s+boolean\\s+__writeRegistryServiceInfo"; //$NON-NLS-1$
boolean containsWriteService = fileContent.split(writeServiceRegex).length > 1;
// Do not consider the RegistrationHandler class as a service implementation
if (containsGetFactory && containsWriteService &&
!res.getName().equals("RegistrationHandler.java")) { //$NON-NLS-1$
/*
* Computes the class name
*/
Matcher m3 = Pattern.compile("[^;]*package\\s+([^;]+);.*").matcher(fileContent); //$NON-NLS-1$
if (m3.matches()) {
String packageName = m3.group(1);
String fileName = res.getName();
className = fileName.substring(0, fileName.length() - ".java".length()); //$NON-NLS-1$
className = packageName + "." + className; //$NON-NLS-1$
}
}
} catch (Exception e) {
// nothing to log
} finally {
try {
File file = res.getLocation().toFile();
in = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(in));
// Read the file into a string without line delimiters
String line = reader.readLine();
String fileContent = ""; //$NON-NLS-1$
while (line != null) {
fileContent = fileContent + line;
line = reader.readLine();
}
String getFactoryRegex = "public\\s+static\\s+XSingleComponentFactory" + //$NON-NLS-1$
"\\s+__getComponentFactory"; //$NON-NLS-1$
boolean containsGetFactory = fileContent.split(getFactoryRegex).length > 1;
String writeServiceRegex = "public\\s+static\\s+boolean\\s+__writeRegistryServiceInfo"; //$NON-NLS-1$
boolean containsWriteService = fileContent.split(writeServiceRegex).length > 1;
// Do not consider the RegistrationHandler class as a service implementation
if (containsGetFactory && containsWriteService &&
!res.getName().equals("RegistrationHandler.java")) { //$NON-NLS-1$
/*
* Computes the class name
*/
Matcher m3 = Pattern.compile("[^;]*package\\s+([^;]+);.*").matcher(fileContent); //$NON-NLS-1$
if (m3.matches()) {
String packageName = m3.group(1);
String fileName = res.getName();
className = fileName.substring(0, fileName.length() - ".java".length()); //$NON-NLS-1$
className = packageName + "." + className; //$NON-NLS-1$
}
}
reader.close();
in.close();
} catch (Exception e) {
// nothing to log
} finally {
try {
reader.close();
in.close();
} catch (Exception e) {
}
}
}

View File

@ -48,10 +48,10 @@ public class FilesVisitor implements IResourceVisitor {
/**
* Adds a resource to skip during the visit.
*
* @param pRes the resource to skip
* @param res the resource to skip
*/
public void addException(IResource pRes) {
mExceptions.add(pRes);
public void addException(IResource res) {
mExceptions.add(res);
}
/**

View File

@ -36,7 +36,7 @@ import java.util.jar.Attributes;
import java.util.jar.Attributes.Name;
import java.util.jar.Manifest;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.internal.ui.jarpackager.ManifestProvider;
import org.eclipse.jdt.ui.jarpackager.JarPackageData;
import org.libreoffice.ide.eclipse.core.model.IUnoidlProject;
@ -52,7 +52,7 @@ public class UnoManifestProvider extends ManifestProvider {
private String mRegClass;
private IUnoidlProject mUnoProject;
private List<IFile> mExternalJars;
private List<IResource> mExternalJars;
/**
* Constructor.
@ -63,7 +63,7 @@ public class UnoManifestProvider extends ManifestProvider {
*
* @param externalJars the external Jars
*/
public UnoManifestProvider(String regClassname, IUnoidlProject unoProject, List<IFile> externalJars) {
public UnoManifestProvider(String regClassname, IUnoidlProject unoProject, List<IResource> externalJars) {
mRegClass = regClassname;
mUnoProject = unoProject;
mExternalJars = externalJars;
@ -76,11 +76,11 @@ public class UnoManifestProvider extends ManifestProvider {
protected void putAdditionalEntries(Manifest manifest, JarPackageData jarPackage) {
Name regClassName = new Attributes.Name("RegistrationClassName"); //$NON-NLS-1$
manifest.getMainAttributes().put(regClassName, mRegClass);
Name classPath = new Attributes.Name("Class-Path");
List<String> classPathList = new ArrayList<>();
for (IFile file:mExternalJars) {
String relativePath = UnoPackage.getPathRelativeToBase(SystemHelper.getFile(file),
for (IResource res : mExternalJars) {
String relativePath = UnoPackage.getPathRelativeToBase(SystemHelper.getFile(res),
SystemHelper.getFile(mUnoProject));
relativePath = FilenameUtils.separatorsToUnix(relativePath);
classPathList.add(relativePath);

View File

@ -7,4 +7,4 @@ OOoContainerPage.DialogImage=/icons/library_wiz.gif
OOoContainerPage.DialogTitle=LibreOffice Libraries
OOoContainerPage.ClasspathSetFailed=Error while setting the project classpath
JavaBuilder.GetExternalLibsFailed=Error while Collecting External Jars
JavaBuilder.GetLibsFromDirFailed=Error while Collecting External Jars

View File

@ -7,3 +7,5 @@ Language.GetClasspathError=Failed to get the project classpath
JavaWizardPage.IncludeTestClasses=Include base classes for tests
JavaWizardPage.PageTitle=Java implementation configuration
JavaWizardPage.PageDescription=This page helps you to configure the java implementation skeleton creation.
JavaClassPathProvider.GetWorkspaceLibsFailed=Error while Collecting .classpath Jars