PackageInfo.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.pkg |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.pkg;
import java.io.File;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;
import org.xnap.util.StringHelper;
import org.xnap.util.VersionParser;
/**
* This class serves as a plugin information record.
*/
public class PackageInfo implements Comparable
{
//--- Constant(s) ---
public static final String DELIMETER = ",";
public static final String ACTION_INSTALL = "install";
public static final String PACKAGE_STATUS_INSTALLED = "installed";
public static final String STATUS_INSTALLED = "install ok installed";
public static final String STATUS_NOT_INSTALLED = "purge ok uninstalled";
//--- Data field(s) ---
private Properties props;
//--- Constructor(s) ---
/**
* Constructs a new plugin information record from <code>p</code>.
* All keys with <code>prefix</code> are copied to this info object.
*
* @param prefix the key prefix
*/
public PackageInfo(Properties props)
{
this.props = (Properties)props.clone();
}
PackageInfo(String packageName)
{
this.props = new Properties();
this.props.setProperty("Package", packageName);
this.props.setProperty("Version", "");
}
PackageInfo()
{
this.props = new Properties();
}
//--- Method(s) ---
/**
* Lower versions come first.
*/
public int compareTo(Object o)
{
PackageInfo info = (PackageInfo)o;
int result = getPackage().compareTo(info.getPackage());
return (result == 0) ? compareToVersion(info) : result;
}
/**
* Returns a value > 0 if the version of this package is higher
* than the version of info.
*/
public int compareToVersion(PackageInfo info)
{
String compareString
= (info.getReleaseNr() != -1 && getReleaseNr() != -1)
? info.getReleaseNr() + ""
: info.getVersion();
return compareToVersion(compareString);
}
public int compareToVersion(String version)
{
if (getReleaseNr() != -1) {
try {
long nr = Long.parseLong(version);
return (new Long(getReleaseNr() - nr)).intValue();
}
catch (NumberFormatException e) {
// fall back to version compare
}
}
return VersionParser.compare(getVersion(), version);
}
public boolean containsProperties(Properties p)
{
for (Iterator i = p.keySet().iterator(); i.hasNext();) {
String key = (String)i.next();
if (!p.getProperty(key, "").equals(props.getProperty(key, ""))) {
return false;
}
}
return true;
}
public boolean equals(Object o)
{
if (o instanceof PackageInfo) {
PackageInfo info = (PackageInfo)o;
return getPackage().equals(info.getPackage())
&& getVersion().equals(info.getVersion());
}
return false;
}
/**
* See {@link #setAction(String)} for possible return values.
*/
public String getAction()
{
return getStatus(0);
}
/**
* Returns "ok".
*/
public String getActionStatus()
{
return getStatus(1);
}
public String getAuthors()
{
return props.getProperty("Authors");
}
/**
* Returns an empty array if no dependencies are defined.
*
* @return never returns null
*/
public String[] getClassPath()
{
return getPropertyList("Class-Path");
}
public String getControlPath()
{
return props.getProperty("Control-Path");
}
public String getDepends()
{
return props.getProperty("Depends");
}
public String getDescription()
{
return props.getProperty("Description");
}
public String getDownloadFilename()
{
return props.getProperty("Download-Filename");
}
public String[] getDownloadURLs()
{
return getPropertyList("Download-URLs");
}
/**
* Returns a file that is relative to this packages location.
*/
public File getFile(String filename)
{
return new File(getControlPath(), filename);
}
public String getFilename()
{
return props.getProperty
("Filename",
getPackage() + "-" + getVersion() + ".zip");
}
/**
* Returns a long description of the package's functionality that may
* contain html tags.
*/
public String getLongDescription()
{
return getProperty("Long-Description", "");
}
public String getName()
{
String name = props.getProperty("Name");
return (name != null) ? name : getPackage();
}
public String getPackage()
{
return props.getProperty("Package");
}
public String getPackageStatus()
{
return getStatus(2);
}
public Properties getProperties()
{
return props;
}
/**
* Returns a property.
*/
public String getProperty(String key, String defaultValue)
{
return props.getProperty(key, defaultValue);
}
/**
* Returns a property.
*/
public String getProperty(String key)
{
return props.getProperty(key);
}
/**
* Returns a property that is a list of strings.
*/
public String[] getPropertyList(String key)
{
return StringHelper.toArray(getProperty(key, ""), DELIMETER);
}
public String getProvides()
{
return props.getProperty("Provides");
}
public long getReleaseNr()
{
try {
return Long.parseLong(props.getProperty("Release-Nr"));
}
catch (NumberFormatException e) {
}
return -1;
}
public String getSection()
{
return props.getProperty("Section");
}
public long getSize()
{
try {
return Long.parseLong(props.getProperty("Size", "0"));
}
catch (NumberFormatException e) {
return 0;
}
}
public String getStatus()
{
return props.getProperty("Status", STATUS_NOT_INSTALLED);
}
public String getStatus(int pos)
{
String status = getStatus();
if (status != null) {
StringTokenizer t = new StringTokenizer(status);
// skip tokens
for(int i = 0; i < pos && t.hasMoreTokens(); i++) {
t.nextToken();
}
// return token at pos
if (t.hasMoreTokens()) {
return t.nextToken();
}
}
return null;
}
public String getVersion()
{
return props.getProperty("Version");
}
public boolean isAvailable()
{
return props.getProperty("Available", "").equals("True");
}
public boolean isBase()
{
String section = getSection();
return (section != null) && section.startsWith("base");
}
public boolean isCore()
{
String packageName = getPackage();
return isBase() && (packageName != null)
&& packageName.indexOf("core") != -1;
}
public boolean isPatch()
{
String packageName = getPackage();
return isBase() && (packageName != null)
&& packageName.indexOf("patch") != -1;
}
public boolean isInstalled()
{
return STATUS_INSTALLED.equals(getStatus());
}
public boolean isNew()
{
return props.getProperty("New", "").equals("True");
}
public boolean isPlugin()
{
String section = getSection();
return (section != null) ? section.startsWith("plugin") : false;
}
public boolean isUpdateAvailable()
{
return false;
}
public boolean isValid()
{
return getPackage() != null && getVersion() != null;
}
public void putAll(Properties p)
{
props.putAll(p);
}
/**
* Action can be "deinstall", "hold", "install", "purge"
*/
public void setAction(String action)
{
if (action.equals("deinstall") || action.equals("hold")
|| action.equals("install") || action.equals("purge")) {
setStatus(action, getActionStatus(), getPackageStatus());
}
}
public void setAvailable(boolean available)
{
props.setProperty("Available", available ? "True" : "False");
}
public void setClassPath(String[] classPath)
{
props.setProperty("Class-Path",
StringHelper.toString(classPath, DELIMETER));
}
public void setControlPath(String controlPath)
{
props.setProperty("Control-Path", controlPath);
}
public void setDescription(String description)
{
props.setProperty("Description", description);
}
public void setDownloadFilename(String filename)
{
props.setProperty("Download-Filename", filename);
}
public void setInstalled(boolean installed)
{
setStatus(STATUS_INSTALLED);
}
public void setNew(boolean isNew)
{
props.setProperty("New", (isNew) ? "True" : "False");
}
public void setPackage(String packageName)
{
props.setProperty("Package", packageName);
}
public void setSection(String section)
{
props.setProperty("Section", section);
}
public void setStatus(String status)
{
props.setProperty("Status", status);
}
public void setStatus(String action, String actionStatus,
String packageStatus)
{
setStatus(action + " " + actionStatus + " " + packageStatus);
}
public void setVersion(String version)
{
props.setProperty("Version", version);
}
/**
* Returns the value of {@link #getName() getName()}.
*/
public String toString()
{
return getName();
}
//--- Inner Class(es) ---
}
The table below shows all metrics for PackageInfo.java.




