<commit_before>/*
 * Copyright (C) 2009, 2010 Jayway AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jayway.maven.plugins.android;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.plugin.MojoExecutionException;

/**
 * Represents an Android SDK.
 * 
 * @author <EMAIL>
 * @author <NAME> <<EMAIL>>
 */
public class AndroidSdk
{

    /**
     * property file in each platform folder with details about platform.
     */
    private static final String SOURCE_PROPERTIES_FILENAME = "source.properties";
    /**
     * property name for platform version in sdk source.properties file.
     */
    private static final String PLATFORM_VERSION_PROPERTY = "Platform.Version";
    /**
     * property name for api level version in sdk source.properties file.
     */
    private static final String API_LEVEL_PROPERTY = "AndroidVersion.ApiLevel";
    /**
     * property name for the sdk tools revision in sdk/tools/lib source.properties
     */
    private static final String SDK_TOOLS_REVISION_PROPERTY = "Pkg.Revision";

    /**
     * folder name for the sdk sub folder that contains the different platform versions.
     */
    private static final String PLATFORMS_FOLDER_NAME = "platforms";

    /**
     * folder name for the sdk sub folder that contains the platform tools.
     */
    private static final String PLATFORM_TOOLS_FOLDER_NAME = "platform-tools";

    private static final String PARAMETER_MESSAGE = "Please provide a proper Android SDK directory path as "
            + "configuration parameter <sdk><path>...</path></sdk> in the plugin <configuration/>. As an alternative,"
            + " you may add the parameter to commandline: -Dandroid.sdk.path=... or set environment variable "
            + AbstractAndroidMojo.ENV_ANDROID_HOME + ".";

    private static final class Platform
    {
        final String name;
        final String apiLevel;
        final String path;

        public Platform( String name, String apiLevel, String path )
        {
            super();
            this.name = name;
            this.apiLevel = apiLevel;
            this.path = path;
        }
    }

    private final File sdkPath;
    private final Platform platform;
    private int sdkMajorVersion;

    private Set< Platform > availablePlatforms;

    public AndroidSdk( File sdkPath, String platformOrApiLevel )
    {
        this.sdkPath = sdkPath;
        findAvailablePlatforms();
        loadSDKToolsMajorVersion();

        if ( platformOrApiLevel == null )
        {
            platform = null;
            // letting this through to preserve compatibility for now
        }
        else
        {
            platform = findPlatformByNameOrApiLevel( platformOrApiLevel );
            if ( platform == null )
            {
                throw new InvalidSdkException( "Invalid SDK: Platform/API level " + platformOrApiLevel
                        + " not available. This command should give you all you need:\n" + sdkPath.getAbsolutePath()
                        + File.separator + "tools" + File.separator + "android update sdk --no-ui --obsolete --force" );
            }
        }
    }

    private Platform findPlatformByNameOrApiLevel( String platformOrApiLevel )
    {
        for ( Platform p : availablePlatforms )
        {
            if ( p.name.equals( platformOrApiLevel ) || p.apiLevel.equals( platformOrApiLevel ) )
            {
                return p;
            }
        }
        return null;
    }

    /**
     * The file system layout of the SDK. Should probably be removed since the 15 layout is very old and probably wont
     * work completely. No urgency though..
     */
    public enum Layout
    {
        LAYOUT_1_5, LAYOUT_2_3
    }

    public Layout getLayout()
    {

        assertPathIsDirectory( sdkPath );

        final File platformTools = new File( sdkPath, PLATFORM_TOOLS_FOLDER_NAME );
        if ( platformTools.exists() && platformTools.isDirectory() )
        {
            return Layout.LAYOUT_2_3;
        }

        final File platforms = new File( sdkPath, PLATFORMS_FOLDER_NAME );
        if ( platforms.exists() && platforms.isDirectory() )
        {
            return Layout.LAYOUT_1_5;
        }

        throw new InvalidSdkException( "Android SDK could not be identified from path \"" + sdkPath + "\". "
                + PARAMETER_MESSAGE );
    }

    private void assertPathIsDirectory( final File path )
    {
        if ( path == null )
        {
            throw new InvalidSdkException( PARAMETER_MESSAGE );
        }
        if ( !path.isDirectory() )
        {
            throw new InvalidSdkException( "Path \"" + path + "\" is not a directory. " + PARAMETER_MESSAGE );
        }
    }

    /**
     * Returns the complete path for a tool, based on this SDK.
     * 
     * @param tool
     *            which tool, for example <code>adb</code> or <code>dx.jar</code>.
     * @return the complete path as a <code>String</code>, including the tool's filename.
     */
    public String getPathForTool( String tool )
    {

        String[] possiblePaths =
        { sdkPath + "/" + PLATFORM_TOOLS_FOLDER_NAME + "/" + tool,
                sdkPath + "/" + PLATFORM_TOOLS_FOLDER_NAME + "/" + tool + ".exe",
                sdkPath + "/" + PLATFORM_TOOLS_FOLDER_NAME + "/" + tool + ".bat",
                sdkPath + "/" + PLATFORM_TOOLS_FOLDER_NAME + "/lib/" + tool, getPlatform() + "/tools/" + tool,
                getPlatform() + "/tools/" + tool + ".exe", getPlatform() + "/tools/" + tool + ".bat",
                getPlatform() + "/tools/lib/" + tool, sdkPath + "/tools/" + tool, sdkPath + "/tools/" + tool + ".exe",
                sdkPath + "/tools/" + tool + ".bat", sdkPath + "/tools/lib/" + tool };

        for ( String possiblePath : possiblePaths )
        {
            File file = new File( possiblePath );
            if ( file.exists() && !file.isDirectory() )
            {
                return file.getAbsolutePath();
            }
        }

        throw new InvalidSdkException( "Could not find tool '" + tool + "'. " + PARAMETER_MESSAGE );
    }<commit_msg>Fix for the new SDK structure<commit_after>/*
 * Copyright (C) 2009, 2010 Jayway AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jayway.maven.plugins.android;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.plugin.MojoExecutionException;

/**
 * Represents an Android SDK.
 * 
 * @author <EMAIL>
 * @author <NAME> <<EMAIL>>
 */
public class AndroidSdk
{

    /**
     * property file in each platform folder with details about platform.
     */
    private static final String SOURCE_PROPERTIES_FILENAME = "source.properties";
    /**
     * property name for platform version in sdk source.properties file.
     */
    private static final String PLATFORM_VERSION_PROPERTY = "Platform.Version";
    /**
     * property name for api level version in sdk source.properties file.
     */
    private static final String API_LEVEL_PROPERTY = "AndroidVersion.ApiLevel";
    /**
     * property name for the sdk tools revision in sdk/tools/lib source.properties
     */
    private static final String SDK_TOOLS_REVISION_PROPERTY = "Pkg.Revision";

    /**
     * folder name for the sdk sub folder that contains the different platform versions.
     */
    private static final String PLATFORMS_FOLDER_NAME = "platforms";

    /**
     * folder name for the sdk sub folder that contains the platform tools.
     */
    private static final String PLATFORM_TOOLS_FOLDER_NAME = "platform-tools";

    private static final String PARAMETER_MESSAGE = "Please provide a proper Android SDK directory path as "
            + "configuration parameter <sdk><path>...</path></sdk> in the plugin <configuration/>. As an alternative,"
            + " you may add the parameter to commandline: -Dandroid.sdk.path=... or set environment variable "
            + AbstractAndroidMojo.ENV_ANDROID_HOME + ".";

    private static final class Platform
    {
        final String name;
        final String apiLevel;
        final String path;

        public Platform( String name, String apiLevel, String path )
        {
            super();
            this.name = name;
            this.apiLevel = apiLevel;
            this.path = path;
        }
    }

    private final File sdkPath;
    private final Platform platform;
    private int sdkMajorVersion;

    private Set< Platform > availablePlatforms;

    public AndroidSdk( File sdkPath, String platformOrApiLevel )
    {
        this.sdkPath = sdkPath;
        findAvailablePlatforms();
        loadSDKToolsMajorVersion();

        if ( platformOrApiLevel == null )
        {
            platform = null;
            // letting this through to preserve compatibility for now
        }
        else
        {
            platform = findPlatformByNameOrApiLevel( platformOrApiLevel );
            if ( platform == null )
            {
                throw new InvalidSdkException( "Invalid SDK: Platform/API level " + platformOrApiLevel
                        + " not available. This command should give you all you need:\n" + sdkPath.getAbsolutePath()
                        + File.separator + "tools" + File.separator + "android update sdk --no-ui --obsolete --force" );
            }
        }
    }

    private Platform findPlatformByNameOrApiLevel( String platformOrApiLevel )
    {
        for ( Platform p : availablePlatforms )
        {
            if ( p.name.equals( platformOrApiLevel ) || p.apiLevel.equals( platformOrApiLevel ) )
            {
                return p;
            }
        }
        return null;
    }

    /**
     * The file system layout of the SDK. Should probably be removed since the 15 layout is very old and probably wont
     * work completely. No urgency though..
     */
    public enum Layout
    {
        LAYOUT_1_5, LAYOUT_2_3
    }

    public Layout getLayout()
    {

        assertPathIsDirectory( sdkPath );

        final File platformTools = new File( sdkPath, PLATFORM_TOOLS_FOLDER_NAME );
        if ( platformTools.exists() && platformTools.isDirectory() )
        {
            return Layout.LAYOUT_2_3;
        }

        final File platforms = new File( sdkPath, PLATFORMS_FOLDER_NAME );
        if ( platforms.exists() && platforms.isDirectory() )
        {
            return Layout.LAYOUT_1_5;
        }

        throw new InvalidSdkException( "Android SDK could not be identified from path \"" + sdkPath + "\". "
                + PARAMETER_MESSAGE );
    }

    private void assertPathIsDirectory( final File path )
    {
        if ( path == null )
        {
            throw new InvalidSdkException( PARAMETER_MESSAGE );
        }
        if ( !path.isDirectory() )
        {
            throw new InvalidSdkException( "Path \"" + path + "\" is not a directory. " + PARAMETER_MESSAGE );
        }
    }

    /**
     * Returns the complete path for a tool, based on this SDK.
     * 
     * @param tool
     *            which tool, for example <code>adb</code> or <code>dx.jar</code>.
     * @return the complete path as a <code>String</code>, including the tool's filename.
     */
    public String getPathForTool( String tool )
    {

        String[] possiblePaths =
        { sdkPath + "/" + PLATFORM_TOOLS_FOLDER_NAME + "/" + tool,
                sdkPath + "/" + PLATFORM_TOOLS_FOLDER_NAME + "/" + tool + ".exe",
                sdkPath + "/" + PLATFORM_TOOLS_FOLDER_NAME + "/" + tool + ".bat",
                sdkPath + "/" + PLATFORM_TOOLS_FOLDER_NAME + "/lib/" + tool, getPlatform() + "/tools/" + tool,
                getPlatform() + "/tools/" + tool + ".exe", getPlatform() + "/tools/" + tool + ".bat",
                getPlatform() + "/tools/lib/" + tool, sdkPath + "/tools/" + tool, sdkPath + "/tools/" + tool + ".exe",
                sdkPath + "/tools/" + tool + ".bat", sdkPath + "/tools/lib/" + tool,
                sdkPath + "/build-tools/17.0.0/" + tool, 
                sdkPath + "/build-tools/17.0.0/" + tool + ".bat",
                sdkPath + "/build-tools/17.0.0/" + tool + ".exe"};

        for ( String possiblePath : possiblePaths )
        {
            File file = new File( possiblePath );
            if ( file.exists() && !file.isDirectory() )
            {
                return file.getAbsolutePath();
            }
        }

        throw new InvalidSdkException( "Could not find tool '" + tool + "'. " + PARAMETER_MESSAGE );
    }
{'fraction_non_alphanumeric': 0.10039869517941283, 'fraction_numerical': 0.004059441826748822, 'mean_word_length': 2.154813629087583, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4043240', 'n_tokens_mistral': 3860, 'n_tokens_neox': 3713, 'n_words': 1168}