2 * Copyright (C) 2009 The Android Open Source Project
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.android.sdklib.internal.repository;
\r
19 import com.android.annotations.VisibleForTesting;
\r
20 import com.android.annotations.VisibleForTesting.Visibility;
\r
21 import com.android.sdklib.NullSdkLog;
\r
22 import com.android.sdklib.SdkConstants;
\r
23 import com.android.sdklib.SdkManager;
\r
24 import com.android.sdklib.internal.repository.Archive.Arch;
\r
25 import com.android.sdklib.internal.repository.Archive.Os;
\r
26 import com.android.sdklib.repository.RepoConstants;
\r
28 import org.w3c.dom.Node;
\r
30 import java.io.File;
\r
31 import java.util.ArrayList;
\r
32 import java.util.Map;
\r
33 import java.util.Properties;
\r
34 import java.util.regex.Pattern;
\r
37 * Represents a extra XML node in an SDK repository.
\r
39 public class ExtraPackage extends MinToolsPackage
\r
40 implements IMinApiLevelDependency {
\r
42 static final String PROP_PATH = "Extra.Path"; //$NON-NLS-1$
\r
43 static final String PROP_VENDOR = "Extra.Vendor"; //$NON-NLS-1$
\r
44 static final String PROP_MIN_API_LEVEL = "Extra.MinApiLevel"; //$NON-NLS-1$
\r
45 static final String PROP_PROJECT_FILES = "Extra.ProjectFiles"; //$NON-NLS-1$
\r
48 * The vendor folder name. It must be a non-empty single-segment path.
\r
50 * The paths "add-ons", "platforms", "platform-tools", "tools" and "docs" are reserved and
\r
52 * This limitation cannot be written in the XML Schema and must be enforced here by using
\r
53 * the method {@link #isPathValid()} *before* installing the package.
\r
55 private final String mVendor;
\r
58 * The sub-folder name. It must be a non-empty single-segment path and has the same
\r
59 * rules as {@link #mVendor}.
\r
61 private final String mPath;
\r
64 * The minimal API level required by this extra package, if > 0,
\r
65 * or {@link #MIN_API_LEVEL_NOT_SPECIFIED} if there is no such requirement.
\r
67 private final int mMinApiLevel;
\r
70 * The project-files listed by this extra package.
\r
71 * The array can be empty but not null.
\r
73 private final String[] mProjectFiles;
\r
76 * Creates a new tool package from the attributes and elements of the given XML node.
\r
77 * This constructor should throw an exception if the package cannot be created.
\r
79 * @param source The {@link SdkSource} where this is loaded from.
\r
80 * @param packageNode The XML element being parsed.
\r
81 * @param nsUri The namespace URI of the originating XML document, to be able to deal with
\r
82 * parameters that vary according to the originating XML schema.
\r
83 * @param licenses The licenses loaded from the XML originating document.
\r
85 ExtraPackage(SdkSource source, Node packageNode, String nsUri, Map<String,String> licenses) {
\r
86 super(source, packageNode, nsUri, licenses);
\r
88 mPath = XmlParserUtils.getXmlString(packageNode, RepoConstants.NODE_PATH);
\r
89 mVendor = XmlParserUtils.getXmlString(packageNode, RepoConstants.NODE_VENDOR);
\r
91 mMinApiLevel = XmlParserUtils.getXmlInt(packageNode, RepoConstants.NODE_MIN_API_LEVEL,
\r
92 MIN_API_LEVEL_NOT_SPECIFIED);
\r
94 mProjectFiles = parseProjectFiles(
\r
95 XmlParserUtils.getFirstChild(packageNode, RepoConstants.NODE_PROJECT_FILES));
\r
98 private String[] parseProjectFiles(Node projectFilesNode) {
\r
99 ArrayList<String> paths = new ArrayList<String>();
\r
101 if (projectFilesNode != null) {
\r
102 String nsUri = projectFilesNode.getNamespaceURI();
\r
103 for(Node child = projectFilesNode.getFirstChild();
\r
105 child = child.getNextSibling()) {
\r
107 if (child.getNodeType() == Node.ELEMENT_NODE &&
\r
108 nsUri.equals(child.getNamespaceURI()) &&
\r
109 RepoConstants.NODE_PATH.equals(child.getLocalName())) {
\r
110 String path = child.getTextContent();
\r
111 if (path != null) {
\r
112 path = path.trim();
\r
113 if (path.length() > 0) {
\r
121 return paths.toArray(new String[paths.size()]);
\r
125 * Manually create a new package with one archive and the given attributes or properties.
\r
126 * This is used to create packages from local directories in which case there must be
\r
127 * one archive which URL is the actual target location.
\r
129 * By design, this creates a package with one and only one archive.
\r
131 static Package create(SdkSource source,
\r
137 String description,
\r
141 String archiveOsPath) {
\r
142 ExtraPackage ep = new ExtraPackage(source, props, vendor, path, revision, license,
\r
143 description, descUrl, archiveOs, archiveArch, archiveOsPath);
\r
145 if (ep.isPathValid()) {
\r
148 String shortDesc = ep.getShortDescription() + " [*]"; //$NON-NLS-1$
\r
150 String longDesc = String.format(
\r
151 "Broken Extra Package: %1$s\n" +
\r
152 "[*] Package cannot be used due to error: Invalid install path %2$s",
\r
156 BrokenPackage ba = new BrokenPackage(props, shortDesc, longDesc,
\r
157 ep.getMinApiLevel(),
\r
158 IExactApiLevelDependency.API_LEVEL_INVALID,
\r
164 @VisibleForTesting(visibility=Visibility.PRIVATE)
\r
165 protected ExtraPackage(SdkSource source,
\r
171 String description,
\r
175 String archiveOsPath) {
\r
186 // The vendor argument is not supposed to be empty. However this attribute did not
\r
187 // exist prior to schema repo-v3 and tools r8, which means we need to cope with a
\r
188 // lack of it when reading back old local repositories. In this case we allow an
\r
190 mVendor = vendor != null ? vendor : getProperty(props, PROP_VENDOR, "");
\r
192 // The path argument comes before whatever could be in the properties
\r
193 mPath = path != null ? path : getProperty(props, PROP_PATH, path);
\r
195 mMinApiLevel = Integer.parseInt(
\r
196 getProperty(props, PROP_MIN_API_LEVEL, Integer.toString(MIN_API_LEVEL_NOT_SPECIFIED)));
\r
198 String projectFiles = getProperty(props, PROP_PROJECT_FILES, null);
\r
199 ArrayList<String> filePaths = new ArrayList<String>();
\r
200 if (projectFiles != null && projectFiles.length() > 0) {
\r
201 for (String filePath : projectFiles.split(Pattern.quote(File.pathSeparator))) {
\r
202 filePath = filePath.trim();
\r
203 if (filePath.length() > 0) {
\r
204 filePaths.add(filePath);
\r
208 mProjectFiles = filePaths.toArray(new String[filePaths.size()]);
\r
212 * Save the properties of the current packages in the given {@link Properties} object.
\r
213 * These properties will later be give the constructor that takes a {@link Properties} object.
\r
216 void saveProperties(Properties props) {
\r
217 super.saveProperties(props);
\r
219 props.setProperty(PROP_PATH, mPath);
\r
220 if (mVendor != null) {
\r
221 props.setProperty(PROP_VENDOR, mVendor);
\r
224 if (getMinApiLevel() != MIN_API_LEVEL_NOT_SPECIFIED) {
\r
225 props.setProperty(PROP_MIN_API_LEVEL, Integer.toString(getMinApiLevel()));
\r
228 if (mProjectFiles.length > 0) {
\r
229 StringBuilder sb = new StringBuilder();
\r
230 for (int i = 0; i < mProjectFiles.length; i++) {
\r
232 sb.append(File.pathSeparatorChar);
\r
234 sb.append(mProjectFiles[i]);
\r
236 props.setProperty(PROP_PROJECT_FILES, sb.toString());
\r
241 * Returns the minimal API level required by this extra package, if > 0,
\r
242 * or {@link #MIN_API_LEVEL_NOT_SPECIFIED} if there is no such requirement.
\r
244 public int getMinApiLevel() {
\r
245 return mMinApiLevel;
\r
249 * The project-files listed by this extra package.
\r
250 * The array can be empty but not null.
\r
252 * IMPORTANT: directory separators are NOT translated and may not match
\r
253 * the {@link File#separatorChar} of the current platform. It's up to the
\r
254 * user to adequately interpret the paths.
\r
255 * Similarly, no guarantee is made on the validity of the paths.
\r
256 * Users are expected to apply all usual sanity checks such as removing
\r
257 * "./" and "../" and making sure these paths don't reference files outside
\r
258 * of the installed archive.
\r
260 * @since sdk-repository-4.xsd or sdk-addon-2.xsd
\r
262 public String[] getProjectFiles() {
\r
263 return mProjectFiles;
\r
267 * Static helper to check if a given vendor and path is acceptable for an "extra" package.
\r
269 public boolean isPathValid() {
\r
270 return isSegmentValid(mVendor) && isSegmentValid(mPath);
\r
273 private boolean isSegmentValid(String segment) {
\r
274 if (SdkConstants.FD_ADDONS.equals(segment) ||
\r
275 SdkConstants.FD_PLATFORMS.equals(segment) ||
\r
276 SdkConstants.FD_PLATFORM_TOOLS.equals(segment) ||
\r
277 SdkConstants.FD_TOOLS.equals(segment) ||
\r
278 SdkConstants.FD_DOCS.equals(segment) ||
\r
279 RepoConstants.FD_TEMP.equals(segment)) {
\r
282 return segment != null && segment.indexOf('/') == -1 && segment.indexOf('\\') == -1;
\r
286 * Returns the sanitized path folder name. It is a single-segment path.
\r
288 * The package is installed in SDK/extras/vendor_name/path_name.
\r
290 * The paths "add-ons", "platforms", "tools" and "docs" are reserved and cannot be used.
\r
291 * This limitation cannot be written in the XML Schema and must be enforced here by using
\r
292 * the method {@link #isPathValid()} *before* installing the package.
\r
294 public String getPath() {
\r
295 // The XSD specifies the XML vendor and path should only contain [a-zA-Z0-9]+
\r
296 // and cannot be empty. Let's be defensive and enforce that anyway since things
\r
297 // like "____" are still valid values that we don't want to allow.
\r
299 // Sanitize the path
\r
300 String path = mPath.replaceAll("[^a-zA-Z0-9-]+", "_"); //$NON-NLS-1$
\r
301 if (path.length() == 0 || path.equals("_")) { //$NON-NLS-1$
\r
302 int h = path.hashCode();
\r
303 path = String.format("extra%08x", h); //$NON-NLS-1$
\r
310 * Returns the sanitized vendor folder name. It is a single-segment path.
\r
312 * The package is installed in SDK/extras/vendor_name/path_name.
\r
314 * An empty string is returned in case of error.
\r
316 public String getVendor() {
\r
318 // The XSD specifies the XML vendor and path should only contain [a-zA-Z0-9]+
\r
319 // and cannot be empty. Let's be defensive and enforce that anyway since things
\r
320 // like "____" are still valid values that we don't want to allow.
\r
322 if (mVendor != null && mVendor.length() > 0) {
\r
323 String vendor = mVendor;
\r
324 // Sanitize the vendor
\r
325 vendor = vendor.replaceAll("[^a-zA-Z0-9-]+", "_"); //$NON-NLS-1$
\r
326 if (vendor.equals("_")) { //$NON-NLS-1$
\r
327 int h = vendor.hashCode();
\r
328 vendor = String.format("vendor%08x", h); //$NON-NLS-1$
\r
334 return ""; //$NON-NLS-1$
\r
337 private String getPrettyName() {
\r
338 String name = mPath;
\r
340 // In the past, we used to save the extras in a folder vendor-path,
\r
341 // and that "vendor" would end up in the path when we reload the extra from
\r
342 // disk. Detect this and compensate.
\r
343 if (mVendor != null && mVendor.length() > 0) {
\r
344 if (name.startsWith(mVendor + "-")) { //$NON-NLS-1$
\r
345 name = name.substring(mVendor.length() + 1);
\r
349 // Uniformize all spaces in the name
\r
350 if (name != null) {
\r
351 name = name.replaceAll("[ _\t\f-]+", " ").trim(); //$NON-NLS-1$ //$NON-NLS-2$
\r
353 if (name == null || name.length() == 0) { //$NON-NLS-1$
\r
354 name = "Unkown Extra";
\r
357 if (mVendor != null && mVendor.length() > 0) {
\r
358 name = mVendor + " " + name; //$NON-NLS-1$
\r
359 name = name.replaceAll("[ _\t\f-]+", " ").trim(); //$NON-NLS-1$ //$NON-NLS-2$
\r
362 // Look at all lower case characters in range [1..n-1] and replace them by an upper
\r
363 // case if they are preceded by a space. Also upper cases the first character of the
\r
365 boolean changed = false;
\r
366 char[] chars = name.toCharArray();
\r
367 for (int n = chars.length - 1, i = 0; i < n; i++) {
\r
368 if (Character.isLowerCase(chars[i]) && (i == 0 || chars[i - 1] == ' ')) {
\r
369 chars[i] = Character.toUpperCase(chars[i]);
\r
374 name = new String(chars);
\r
377 // Special case: reformat a few typical acronyms.
\r
378 name = name.replaceAll(" Usb ", " USB "); //$NON-NLS-1$
\r
379 name = name.replaceAll(" Api ", " API "); //$NON-NLS-1$
\r
385 * Returns a description of this package that is suitable for a list display.
\r
390 public String getListDescription() {
\r
391 String s = String.format("%1$s package%2$s",
\r
393 isObsolete() ? " (Obsolete)" : ""); //$NON-NLS-2$
\r
399 * Returns a short description for an {@link IDescription}.
\r
402 public String getShortDescription() {
\r
404 String s = String.format("%1$s package, revision %2$d%3$s",
\r
407 isObsolete() ? " (Obsolete)" : ""); //$NON-NLS-2$
\r
413 * Returns a long description for an {@link IDescription}.
\r
415 * The long description is whatever the XML contains for the <description> field,
\r
416 * or the short description if the former is empty.
\r
419 public String getLongDescription() {
\r
420 String s = getDescription();
\r
421 if (s == null || s.length() == 0) {
\r
422 s = String.format("Extra %1$s package by %2$s", getPath(), getVendor());
\r
425 if (s.indexOf("revision") == -1) {
\r
426 s += String.format("\nRevision %1$d%2$s",
\r
428 isObsolete() ? " (Obsolete)" : ""); //$NON-NLS-2$
\r
431 if (getMinToolsRevision() != MIN_TOOLS_REV_NOT_SPECIFIED) {
\r
432 s += String.format("\nRequires tools revision %1$d", getMinToolsRevision());
\r
435 if (getMinApiLevel() != MIN_API_LEVEL_NOT_SPECIFIED) {
\r
436 s += String.format("\nRequires SDK Platform Android API %1$s", getMinApiLevel());
\r
439 // For a local archive, also put the install path in the long description.
\r
440 // This should help users locate the extra on their drive.
\r
441 File localPath = getLocalArchivePath();
\r
442 if (localPath != null) {
\r
443 s += String.format("\nLocation: %1$s", localPath.getAbsolutePath());
\r
450 * Computes a potential installation folder if an archive of this package were
\r
451 * to be installed right away in the given SDK root.
\r
453 * A "tool" package should always be located in SDK/tools.
\r
455 * @param osSdkRoot The OS path of the SDK root folder.
\r
456 * @param sdkManager An existing SDK manager to list current platforms and addons.
\r
457 * Not used in this implementation.
\r
458 * @return A new {@link File} corresponding to the directory to use to install this package.
\r
461 public File getInstallFolder(String osSdkRoot, SdkManager sdkManager) {
\r
463 // First find if this extra is already installed. If so, reuse the same directory.
\r
464 LocalSdkParser localParser = new LocalSdkParser();
\r
465 Package[] pkgs = localParser.parseSdk(osSdkRoot, sdkManager, new NullSdkLog());
\r
467 for (Package pkg : pkgs) {
\r
468 if (sameItemAs(pkg) && pkg instanceof ExtraPackage) {
\r
469 File localPath = ((ExtraPackage) pkg).getLocalArchivePath();
\r
470 if (localPath != null) {
\r
476 // The /extras dir at the root of the SDK
\r
477 File path = new File(osSdkRoot, SdkConstants.FD_EXTRAS);
\r
479 String vendor = getVendor();
\r
480 if (vendor != null && vendor.length() > 0) {
\r
481 path = new File(path, vendor);
\r
484 String name = getPath();
\r
485 if (name != null && name.length() > 0) {
\r
486 path = new File(path, name);
\r
493 public boolean sameItemAs(Package pkg) {
\r
494 // Extra packages are similar if they have the same path and vendor
\r
495 if (pkg instanceof ExtraPackage) {
\r
496 ExtraPackage ep = (ExtraPackage) pkg;
\r
498 // To be backward compatible, we need to support the old vendor-path form
\r
499 // in either the current or the remote package.
\r
501 // The vendor test below needs to account for an old installed package
\r
502 // (e.g. with an install path of vendor-name) that has then beeen updated
\r
503 // in-place and thus when reloaded contains the vendor name in both the
\r
504 // path and the vendor attributes.
\r
505 if (ep.mPath != null && mPath != null && mVendor != null) {
\r
506 if (ep.mPath.equals(mVendor + "-" + mPath) && //$NON-NLS-1$
\r
507 (ep.mVendor == null || ep.mVendor.length() == 0
\r
508 || ep.mVendor.equals(mVendor))) {
\r
512 if (mPath != null && ep.mPath != null && ep.mVendor != null) {
\r
513 if (mPath.equals(ep.mVendor + "-" + ep.mPath) && //$NON-NLS-1$
\r
514 (mVendor == null || mVendor.length() == 0 || mVendor.equals(ep.mVendor))) {
\r
520 if (!mPath.equals(ep.mPath)) {
\r
523 if ((mVendor == null && ep.mVendor == null) ||
\r
524 (mVendor != null && mVendor.equals(ep.mVendor))) {
\r
533 * For extra packages, we want to add vendor|path to the sorting key
\r
534 * <em>before<em/> the revision number.
\r
539 protected String comparisonKey() {
\r
540 String s = super.comparisonKey();
\r
541 int pos = s.indexOf("|r:"); //$NON-NLS-1$
\r
543 s = s.substring(0, pos) +
\r
544 "|ve:" + getVendor() + //$NON-NLS-1$
\r
545 "|pa:" + getPath() + //$NON-NLS-1$
\r
553 * If this package is installed, returns the install path of the archive if valid.
\r
554 * Returns null if not installed or if the path does not exist.
\r
556 private File getLocalArchivePath() {
\r
557 Archive[] archives = getArchives();
\r
558 if (archives.length == 1 && archives[0].isLocal()) {
\r
559 File path = new File(archives[0].getLocalOsPath());
\r
560 if (path.isDirectory()) {
\r