📄 library.java
字号:
/**
* Copyright (c) 2003-2005 Craig Setera
* All Rights Reserved.
* Licensed under the Eclipse Public License - v 1.0
* For more information see http://www.eclipse.org/legal/epl-v10.html
*/
package eclipseme.core.model.impl;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IAccessRule;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.JavaCore;
import eclipseme.core.internal.EclipseMECorePlugin;
import eclipseme.core.model.API;
import eclipseme.core.model.APIType;
import eclipseme.core.model.ILibrary;
import eclipseme.core.persistence.IPersistenceProvider;
import eclipseme.core.persistence.PersistenceException;
/**
* A library is a wrapper around a classpath item that attempts
* to provide further metadata about that library.
* <p />
* Copyright (c) 2003-2005 Craig Setera<br>
* All Rights Reserved.<br>
* Licensed under the Eclipse Public License - v 1.0<p/>
* <br>
* $Revision: 1.4 $
* <br>
* $Date: 2006/12/01 23:59:39 $
* <br>
* @author Craig Setera
*/
public class Library implements ILibrary {
private static IAccessRule[] NO_ACCESS_RULES = new IAccessRule[0];
private API[] apis;
private URL javadocURL;
private File libraryFile;
private IPath sourceAttachmentPath;
private IPath sourceAttachmentRootPath;
private IAccessRule[] accessRules;
/**
* Construct a new library.
*/
public Library() {
super();
accessRules = NO_ACCESS_RULES;
}
/**
* Test the equality of this library as compared to the
* specified library.
*
* @param library
* @return
*/
public boolean equals(Library library) {
return libraryFile.equals(library.libraryFile);
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
boolean equals = false;
if (obj instanceof Library) {
equals = equals((Library) obj);
}
return equals;
}
/**
* @see eclipseme.core.model.ILibrary#getAPI(int)
*/
public API getAPI(APIType apiType) {
API api = null;
for (int i = 0; i < apis.length; i++) {
if (apis[i].getType() == apiType) {
api = apis[i];
break;
}
}
return api;
}
/**
* @see eclipseme.core.model.ILibrary#getAPIs()
*/
public API[] getAPIs() {
return apis;
}
/**
* @see eclipseme.core.model.ILibrary#getConfiguration()
*/
public API getConfiguration() {
return getAPI(APIType.CONFIGURATION);
}
/**
* @return Returns the javadocURL.
*/
public URL getJavadocURL() {
return javadocURL;
}
/**
* Return the file that makes up this library.
*
* @return Returns the libraryFile.
*/
public File getLibraryFile() {
return libraryFile;
}
/**
* @see eclipseme.core.model.ILibrary#getProfile()
*/
public API getProfile() {
return getAPI(APIType.PROFILE);
}
/**
* @return Returns the sourceAttachmentPath.
*/
public IPath getSourceAttachmentPath() {
return sourceAttachmentPath;
}
/**
* @return Returns the sourceAttachmentRootPath.
*/
public IPath getSourceAttachmentRootPath() {
return sourceAttachmentRootPath;
}
/**
* @see eclipseme.core.model.ILibrary#hasConfiguration()
*/
public boolean hasConfiguration() {
return (getConfiguration() != null);
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return libraryFile.hashCode();
}
/**
* @see eclipseme.core.model.ILibrary#hasProfile()
*/
public boolean hasProfile() {
return (getProfile() != null);
}
/**
* @see eclipseme.core.persistence.IPersistable#loadUsing(eclipseme.core.persistence.IPersistenceProvider)
*/
public void loadUsing(IPersistenceProvider persistenceProvider)
throws PersistenceException
{
libraryFile = new File(persistenceProvider.loadString("file"));
int apiLength = persistenceProvider.loadInteger("apiCount");
apis = new API[apiLength];
for (int i = 0; i < apis.length; i++) {
apis[i] = (API) persistenceProvider.loadPersistable("entry" + i);
}
String javadocURLString = persistenceProvider.loadString("javadocURL");
if (javadocURLString != null) {
try {
javadocURL = new URL(javadocURLString);
} catch (MalformedURLException e) {
EclipseMECorePlugin.log(
IStatus.WARNING,
"Error loading javadoc url " + javadocURLString,
e);
}
}
String sourceAttachString = persistenceProvider.loadString("sourceAttachmentPath");
if (sourceAttachString != null) {
sourceAttachmentPath = new Path(sourceAttachString);
}
String rootPathString = persistenceProvider.loadString("sourceAttachmentRootPath");
if (rootPathString != null) {
sourceAttachmentRootPath = new Path(rootPathString);
}
}
/**
* Set the access rules for this library.
*
* @param accessRules
*/
public void setAccessRules(IAccessRule[] accessRules) {
this.accessRules = accessRules;
}
/**
* @param api The apis to set.
*/
public void setApis(API[] apis) {
this.apis = apis;
}
/**
* @param javadocURL The javadocURL to set.
*/
public void setJavadocURL(URL javadocURL) {
this.javadocURL = javadocURL;
}
/**
* Sets the file that makes up this library.
*
* @param libraryFile The libraryFile to set.
*/
public void setLibraryFile(File libraryFile) {
this.libraryFile = libraryFile;
}
/**
* @param sourceAttachmentPath The sourceAttachmentPath to set.
*/
public void setSourceAttachmentPath(IPath sourceAttachmentPath) {
this.sourceAttachmentPath = sourceAttachmentPath;
}
/**
* @param sourceAttachmentRootPath The sourceAttachmentRootPath to set.
*/
public void setSourceAttachmentRootPath(IPath sourceAttachmentRootPath) {
this.sourceAttachmentRootPath = sourceAttachmentRootPath;
}
/**
* @see eclipseme.core.persistence.IPersistable#storeUsing(eclipseme.core.persistence.IPersistenceProvider)
*/
public void storeUsing(IPersistenceProvider persistenceProvider)
throws PersistenceException
{
persistenceProvider.storeString("file", libraryFile.toString());
persistenceProvider.storeInteger("apiCount", apis.length);
for (int i = 0; i < apis.length; i++) {
persistenceProvider.storePersistable("entry" + i, apis[i]);
}
if (javadocURL != null) {
persistenceProvider.storeString("javadocURL", javadocURL.toString());
}
if (sourceAttachmentPath != null) {
persistenceProvider.storeString(
"sourceAttachmentPath",
sourceAttachmentPath.toString());
}
if (sourceAttachmentRootPath != null) {
persistenceProvider.storeString(
"sourceAttachmentRootPath",
sourceAttachmentRootPath.toString());
}
}
/**
* @see eclipseme.core.model.ILibrary#toClasspathEntry()
*/
public IClasspathEntry toClasspathEntry() {
// System.out.println("toClasspathEntry: " + toString());
// System.out.println("\t" + accessRules.length + " rules");
// for (int i = 0; i < accessRules.length; i++) {
// IAccessRule rule = accessRules[i];
// System.out.println("\t" + rule);
// }
IPath entryPath = new Path(libraryFile.toString());
IClasspathAttribute[] attributes = getClasspathAttributes();
return JavaCore.newLibraryEntry(
entryPath,
sourceAttachmentPath,
sourceAttachmentRootPath,
(accessRules == null) ? NO_ACCESS_RULES : accessRules,
attributes,
false);
}
/**
* @see eclipseme.core.model.ILibrary#toFile()
*/
public File toFile() {
return libraryFile;
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
return libraryFile.toString();
}
/**
* @see eclipseme.core.model.ILibrary#toURL()
*/
public URL toURL() {
URL url = null;
try {
url = libraryFile.toURL();
} catch (MalformedURLException e) {
// This should not happen
EclipseMECorePlugin.log(IStatus.ERROR, e);
}
return url;
}
/**
* Return the classpath attributes to be used in constructing
* the IClasspathEntry.
*
* @return
*/
private IClasspathAttribute[] getClasspathAttributes() {
ArrayList attributes = new ArrayList();
IClasspathAttribute javadocAttribute = getJavadocAttribute();
if (javadocAttribute != null) attributes.add(javadocAttribute);
return (IClasspathAttribute[]) attributes.toArray(new IClasspathAttribute[attributes.size()]);
}
/**
* Return a classpath attribute specifying the javadoc attachment.
*
* @return
*/
private IClasspathAttribute getJavadocAttribute() {
IClasspathAttribute attribute = null;
if (javadocURL != null) {
attribute = JavaCore.newClasspathAttribute(
IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
javadocURL.toString());
}
return attribute;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -