com.android.sdklib.internal.repository.sources
Class SdkSource

java.lang.Object
  extended by com.android.sdklib.internal.repository.sources.SdkSource
All Implemented Interfaces:
IDescription, java.lang.Comparable<SdkSource>
Direct Known Subclasses:
SdkAddonSource, SdkRepoSource, SdkSysImgSource

public abstract class SdkSource
extends java.lang.Object
implements IDescription, java.lang.Comparable<SdkSource>

An sdk-addon or sdk-repository source, i.e. a download site. It may be a full repository or an add-on only repository. A repository describes one or Packages available for download.


Constructor Summary
SdkSource(java.lang.String url, java.lang.String uiName)
          Constructs a new source for the given repository URL.
 
Method Summary
 void clearPackages()
          Clear the internal packages list.
 int compareTo(SdkSource rhs)
          Implementation of the Comparable interface.
 boolean equals(java.lang.Object obj)
          Two repo source are equal if they have the same URL.
protected abstract  org.w3c.dom.Document findAlternateToolsXml(java.io.InputStream xml)
          In case we fail to load an XML, examine the XML to see if it matches a future schema that as at least a tools node that we could load to update the SDK Manager.
protected abstract  java.lang.String[] getDefaultXmlFileUrls()
          Returns the basename of the default URLs to try to download the XML manifest.
protected  org.w3c.dom.Document getDocument(java.io.InputStream xml, ITaskMonitor monitor)
          Takes an XML document as a string as parameter and returns a DOM for it.
 java.lang.String getFetchError()
          Returns the last fetch error description.
 java.lang.String getLongDescription()
          Returns a description of the given element.
protected abstract  int getNsLatestVersion()
          Returns SdkRepoConstants.NS_LATEST_VERSION or SdkAddonConstants.NS_LATEST_VERSION.
protected abstract  java.lang.String getNsPattern()
          Returns SdkRepoConstants.NS_PATTERN or SdkAddonConstants.NS_PATTERN.
protected abstract  java.lang.String getNsUri()
          Returns SdkRepoConstants.NS_URI or SdkAddonConstants.NS_URI.
 Package[] getPackages()
          Returns the list of known packages found by the last call to load().
protected abstract  java.lang.String getRootElementName()
           
protected abstract  java.lang.String getSchemaUri(int version)
          Returns SdkRepoConstants.getSchemaUri() or SdkAddonConstants.getSchemaUri().
 java.lang.String getShortDescription()
          Returns a description of the given element.
 java.lang.String getUiName()
          Returns the UI-visible name of the source.
 java.lang.String getUrl()
          Returns the URL of the XML file for this source.
protected  int getXmlSchemaVersion(java.io.InputStream xml)
          Manually parses the root element of the XML to extract the schema version at the end of the xmlns:sdk="http://schemas.android.com/sdk/android/repository/$N" declaration.
protected abstract  java.io.InputStream getXsdStream(int version)
          Returns SdkRepoConstants.getXsdStream() or SdkAddonConstants.getXsdStream().
 int hashCode()
           
abstract  boolean isAddonSource()
          Returns true if this is an addon source.
 boolean isEnabled()
          Indicates if the source is enabled.
abstract  boolean isSysImgSource()
          Returns true if this is a system-image source.
 void load(DownloadCache cache, ITaskMonitor monitor, boolean forceHttp)
          Tries to fetch the repository index for the given URL and updates the package list.
protected  boolean parsePackages(org.w3c.dom.Document doc, java.lang.String nsUri, ITaskMonitor monitor)
          Parse all packages defined in the SDK Repository XML and creates a new mPackages array with them.
 void setEnabled(boolean enabled)
          Changes whether the source is marked as enabled.
protected  void setPackages(Package[] packages)
           
 java.lang.String toString()
          Returns the short description of the source, if not null.
protected  java.lang.String validateXml(java.io.InputStream xml, java.lang.String url, int version, java.lang.String[] outError, java.lang.Boolean[] validatorFound)
          Validates this XML against one of the requested SDK Repository schemas.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SdkSource

public SdkSource(java.lang.String url,
                 java.lang.String uiName)
Constructs a new source for the given repository URL.

Parameters:
url - The source URL. Cannot be null. If the URL ends with a /, the default repository.xml filename will be appended automatically.
uiName - The UI-visible name of the source. Can be null.
Method Detail

isAddonSource

public abstract boolean isAddonSource()
Returns true if this is an addon source. We only load addons and extras from these sources.


isSysImgSource

public abstract boolean isSysImgSource()
Returns true if this is a system-image source. We only load system-images from these sources.


getDefaultXmlFileUrls

protected abstract java.lang.String[] getDefaultXmlFileUrls()
Returns the basename of the default URLs to try to download the XML manifest. E.g. this is typically SdkRepoConstants.URL_DEFAULT_XML_FILE or SdkAddonConstants.URL_DEFAULT_XML_FILE


getNsLatestVersion

protected abstract int getNsLatestVersion()
Returns SdkRepoConstants.NS_LATEST_VERSION or SdkAddonConstants.NS_LATEST_VERSION.


getNsUri

protected abstract java.lang.String getNsUri()
Returns SdkRepoConstants.NS_URI or SdkAddonConstants.NS_URI.


getNsPattern

protected abstract java.lang.String getNsPattern()
Returns SdkRepoConstants.NS_PATTERN or SdkAddonConstants.NS_PATTERN.


getSchemaUri

protected abstract java.lang.String getSchemaUri(int version)
Returns SdkRepoConstants.getSchemaUri() or SdkAddonConstants.getSchemaUri().


getRootElementName

protected abstract java.lang.String getRootElementName()

getXsdStream

protected abstract java.io.InputStream getXsdStream(int version)
Returns SdkRepoConstants.getXsdStream() or SdkAddonConstants.getXsdStream().


findAlternateToolsXml

protected abstract org.w3c.dom.Document findAlternateToolsXml(@Nullable
                                                              java.io.InputStream xml)
                                                       throws java.io.IOException
In case we fail to load an XML, examine the XML to see if it matches a future schema that as at least a tools node that we could load to update the SDK Manager.

Parameters:
xml - The input XML stream. Can be null.
Returns:
Null on failure, otherwise returns an XML DOM with just the tools we need to update this SDK Manager.
Throws:
java.io.IOException

equals

public boolean equals(java.lang.Object obj)
Two repo source are equal if they have the same URL.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

compareTo

public int compareTo(SdkSource rhs)
Implementation of the Comparable interface. Simply compares the URL using the string's default ordering.

Specified by:
compareTo in interface java.lang.Comparable<SdkSource>

getUiName

public java.lang.String getUiName()
Returns the UI-visible name of the source. Can be null.


getUrl

public java.lang.String getUrl()
Returns the URL of the XML file for this source.


getPackages

public Package[] getPackages()
Returns the list of known packages found by the last call to load(). This is null when the source hasn't been loaded yet -- caller should then call load(com.android.sdklib.internal.repository.DownloadCache, com.android.sdklib.internal.repository.ITaskMonitor, boolean) to load the packages.


setPackages

protected void setPackages(Package[] packages)

clearPackages

public void clearPackages()
Clear the internal packages list. After this call, getPackages() will return null till load() is called.


isEnabled

public boolean isEnabled()
Indicates if the source is enabled.

A 3rd-party add-on source can be disabled by the user to prevent from loading it.

Returns:
True if the source is enabled (default is true).

setEnabled

public void setEnabled(boolean enabled)
Changes whether the source is marked as enabled.

When changing the enable state, the current package list is purged and the next load will either return an empty list (if disabled) or the actual package list (if enabled.)

Parameters:
enabled - True for the source to be enabled (can be loaded), false otherwise.

toString

public java.lang.String toString()
Returns the short description of the source, if not null. Otherwise returns the default Object toString result.

This is mostly helpful for debugging. For UI display, use the IDescription interface.

Overrides:
toString in class java.lang.Object

getShortDescription

public java.lang.String getShortDescription()
Description copied from interface: IDescription
Returns a description of the given element. Cannot be null.

A description is a multi-line of text, typically much more elaborate than what Object.toString() would provide.

Specified by:
getShortDescription in interface IDescription

getLongDescription

public java.lang.String getLongDescription()
Description copied from interface: IDescription
Returns a description of the given element. Cannot be null.

A description is a multi-line of text, typically much more elaborate than what Object.toString() would provide.

Specified by:
getLongDescription in interface IDescription

getFetchError

public java.lang.String getFetchError()
Returns the last fetch error description. If there was no error, returns null.


load

public void load(DownloadCache cache,
                 ITaskMonitor monitor,
                 boolean forceHttp)
Tries to fetch the repository index for the given URL and updates the package list. When a source is disabled, this create an empty non-null package list.

Callers can get the package list using getPackages() after this. It will be null in case of error, in which case getFetchError() can be used to an error message.


validateXml

protected java.lang.String validateXml(java.io.InputStream xml,
                                       java.lang.String url,
                                       int version,
                                       java.lang.String[] outError,
                                       java.lang.Boolean[] validatorFound)
Validates this XML against one of the requested SDK Repository schemas. If the XML was correctly validated, returns the schema that worked. If it doesn't validate, returns null and stores the error in outError[0]. If we can't find a validator, returns null and set validatorFound[0] to false.


getXmlSchemaVersion

protected int getXmlSchemaVersion(java.io.InputStream xml)
Manually parses the root element of the XML to extract the schema version at the end of the xmlns:sdk="http://schemas.android.com/sdk/android/repository/$N" declaration.

Returns:
1..SdkRepoConstants.NS_LATEST_VERSION for a valid schema version or 0 if no schema could be found.

parsePackages

protected boolean parsePackages(org.w3c.dom.Document doc,
                                java.lang.String nsUri,
                                ITaskMonitor monitor)
Parse all packages defined in the SDK Repository XML and creates a new mPackages array with them.


getDocument

protected org.w3c.dom.Document getDocument(java.io.InputStream xml,
                                           ITaskMonitor monitor)
Takes an XML document as a string as parameter and returns a DOM for it. On error, returns null and prints a (hopefully) useful message on the monitor.