text:
package org.jboss.maven.extension.dependency.modelmodifier.versionoverride;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.maven.MavenExecutionException;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingException;
import org.jboss.maven.extension.dependency.resolver.EffectiveModelBuilder;
import org.jboss.maven.extension.dependency.util.Log;
import org.jboss.maven.extension.dependency.util.MavenUtil;
import org.jboss.maven.extension.dependency.util.VersionPropertyReader;
import org.sonatype.aether.resolution.ArtifactDescriptorException;
import org.sonatype.aether.resolution.ArtifactResolutionException;

/**
 * Overrides dependency versions in a model
 */
public class DepVersionOverrider
    extends AbstractVersionOverrider
{
    /**
     * The String that needs to be prepended a system property to make it a version override.

     * ex: -Dversion:junit:junit=4.10
     */
    private static final String DEPENDENCY_VERSION_OVERRIDE_PREFIX = "version:";

    /**
     * The name of the property that specifies whether or not to override transitive dependencies in the build. This
     * causes non-matching dependencies to be added to the dependency management section of the pom. Default is true.

     * ex: -overrideTransitive=true
     */
    private static final String OVERRIDE_TRANSITIVE = "overrideTransitive";

    /**
     * The name of the property which contains the GAV of the remote pom from which to retrieve dependency management
     * information.

     * ex: -DdependencyManagement:org.foo:bar-dep-mgmt:1.0
     */
    private static final String DEPENDENCY_MANAGEMENT_POM_PROPERTY = "dependencyManagement";

    /**
     * A short description of the thing being overridden
     */
    private static final String OVERRIDE_NAME = "dependency";

    /**
     * Cache for override properties. Null until getVersionOverrides() is called.
     */
    private Map<String, String> dependencyVersionOverrides;

    /**
     * The set of projects currently in the reactor. The versions of these projects should not be overridden.
     */
    private Set<String> reactorProjects;

    /**
     * Modify model's dependency management and direct dependencies.
     */
    @Override
    public boolean updateModel( Model model ) throws MavenExecutionException
    {
        Map<String, String> versionOverrides = getVersionOverrides();
        if ( versionOverrides.size() == 0 )
        {
            return false;
        }

        versionOverrides = removeReactorGAs( versionOverrides );

        String projectGA = model.getGroupId() + ":" + model.getArtifactId();

        versionOverrides = applyModuleVersionOverrides( projectGA, versionOverrides );

        // Add/override a property to the build for each override
        addVersionOverrideProperties( versionOverrides, model.getProperties() );

        // If the model doesn't have any Dependency Management set by default, create one for it
        DependencyManagement dependencyManagement = model.getDependencyManagement();
        if ( dependencyManagement == null )
        {
            dependencyManagement = new DependencyManagement();
            model.setDependencyManagement( dependencyManagement );
            Log.getLog().debug( "Added <DependencyManagement/> for current project" );
        }

        // Apply overrides to project dependency management
        List<Dependency> dependencies = dependencyManagement.getDependencies();
        Map<String, String> nonMatchingVersionOverrides = applyOverrides( dependencies, versionOverrides );
        if ( overrideTransitive() )
        {
            // Add dependencies to Dependency Management which did not match any existing dependency
            for ( String groupIdArtifactId : nonMatchingVersionOverrides.keySet() )
            {
                String[] groupIdArtifactIdParts = groupIdArtifactId.split( ":" );

                Dependency newDependency = new Dependency();
                newDependency.setGroupId( groupIdArtifactIdParts[0] );
                newDependency.setArtifactId( groupIdArtifactIdParts[1] );

                String artifactVersion = nonMatchingVersionOverrides.get( groupIdArtifactId );
                newDependency.setVersion( artifactVersion );

                dependencyManagement.getDependencies().add( newDependency );
                Log.getLog().debug( "New entry added to <DependencyManagement/> - " + groupIdArtifactId + ":" +
                                        artifactVersion );
            }
        }
        else
        {
            Log.getLog().debug( "Non-matching dependencies ignored." );
        }

        // Apply overrides to project direct dependencies
        List<Dependency> projectDependencies = model.getDependencies();
        applyOverrides( projectDependencies, versionOverrides );

        // Include the overrides in the built files for repeatability
        writeOverrideMap( model, getName(), versionOverrides );

        // Assuming the Model changed since overrides were given
        return true;
    }

    private Set<String> getReactorProjects()
    {
        if ( reactorProjects == null || reactorProjects.size() == 0 )
        {
            String[] reactorProjectGAs = System.getProperty( "reactorProjectGAs" ).split( "," );
            reactorProjects = new HashSet<String>( Arrays.asList( reactorProjectGAs ) );
        }
        return reactorProjects;
    }

    @Override
    public String getName()
    {
        return OVERRIDE_NAME;
    }

    /**
     * Whether to override unmanaged transitive dependencies in the build. Has the effect of adding (or not) new entries
     * to dependency management when no matching dependency is found in the pom. Defaults to true.
     *
     * @return
     */
    private boolean overrideTransitive()
    {
        String overrideTransitive = System.getProperties().getProperty( OVERRIDE_TRANSITIVE, "true" );
        return overrideTransitive.equals( "true" );
    }

    /**
     * Get the set of versions which will be used to override local dependency versions. This is the full set of version
     * overrides from system properties and remote poms.
     *
     * The format of the key is "groupId:artifactId[@moduleGroupId:moduleArtifactId]"
     * The value is the version string
     */
    private Map<String, String> getVersionOverrides() throws MavenExecutionException
    {
        if ( dependencyVersionOverrides == null )
        {
            dependencyVersionOverrides = new HashMap<String, String>();

            Map<String, String> remoteDepOverrides = loadRemoteDepVersionOverrides();
            dependencyVersionOverrides.putAll( remoteDepOverrides );

            Map<String, String> propDepOverrides =
                VersionPropertyReader.getPropertiesByPrefix( DEPENDENCY_VERSION_OVERRIDE_PREFIX );
            dependencyVersionOverrides.putAll( propDepOverrides );

        }
        return dependencyVersionOverrides;
    }

    /**
     * Remove version overrides which refer to projects in the current reactor.
     * Projects in the reactor include things like inter-module dependencies
     * which should never be overridden.
     *
     * @param versionOverrides
     * @return A new Map with the reactor GAs removed.
     */
    private Map<String, String> removeReactorGAs( Map<String, String> versionOverrides )
    {
        Map<String, String> reducedVersionOverrides = new HashMap<String, String>( versionOverrides );
        Set<String> reactorProjects = getReactorProjects();
        for ( String reactorGA : reactorProjects )
        {
            reducedVersionOverrides.remove( reactorGA );
        }
        return reducedVersionOverrides;
    }

    /**
     * Remove module overrides which do not apply to the current module. Searches the full list of version overrides
     * for any keys which contain the '@' symbol.  Removes these from the version overrides list, and add them back
     * without the '@' symbol only if they apply to the current module.
     *
     * @param versionOverides The full list of version overrides, both global and module specific
     * @return The map of global and module specific overrides which apply to the given module
     */
    private Map<String, String> applyModuleVersionOverrides( String projectGA, Map<String, String> versionOverrides )
    {
        Map<String, String> moduleVersionOverrides = new HashMap<String, String>( versionOverrides );
        for ( String currentKey : versionOverrides.keySet() )
        {
            if ( currentKey.contains( "@" ) )
            {
                moduleVersionOverrides.remove( currentKey );
                String[] artifactAndModule = currentKey.split( "@" );
                String artifactGA = artifactAndModule[0];
                String moduleGA = artifactAndModule[1];
                if ( moduleGA.equals( projectGA ) )
                {
                    if ( versionOverrides.get( currentKey) != null && versionOverrides.get( currentKey).length() > 0)
                    {
                        moduleVersionOverrides.put( artifactGA, versionOverrides.get( currentKey ) );
                    }
                    else
                    {
                        moduleVersionOverrides.remove(artifactGA);
                        Log.getLog().debug("Ignoring module dependency override for " + moduleGA);
                    }
                }
            }
        }
        return moduleVersionOverrides;
    }

    /***
     * Add properties to the build which match the version overrides.
     * The property names are in the format
     */
    private void addVersionOverrideProperties( Map<String, String> overrides, Properties props )
    {
        String propPrefix = getVersionPropertyPrefix();
        String gaSeparator = getGASeparator();
        String propSuffix = getVersionPropertySuffix();

        for (String currentGA : overrides.keySet() )
        {
            String versionPropName = propPrefix + currentGA.replace( ":", gaSeparator ) + propSuffix;
            props.setProperty( versionPropName, overrides.get( currentGA ) );
        }
    }

    /**
     * Get the prefix that should be used for version property names
     * @return The prefix set in the system properties or the defult DEPENDENCY_VERSION_OVERRIDE_PREFIX
     */
    private String getVersionPropertyPrefix()
    {
        return System.getProperty( "versionPropertyPrefix", DEPENDENCY_VERSION_OVERRIDE_PREFIX);
    }

    /**
     * Get the groupId/artifactId separator
     * @return The separator set in the system properties, or ":" by default
     */
    private String getGASeparator()
    {
        return System.getProperty( "versionPropertyGASeparator", ":" );
    }

    /**
     * Get the suffix that should be used for version property names
     * @return The suffix set in the system properties or the default empty string
     */
    private String getVersionPropertySuffix()
    {
        return System.getProperty( "versionPropertySuffix", "");
    }

    /**
     * Apply a set of version overrides to a list of dependencies. Return a set of the overrides which were not applied.
     *
     * @param dependencies The list of dependencies
     * @param overrides The map of dependency version overrides
     * @return The map of overrides that were not matched in the dependencies
     */
    private static Map<String, String> applyOverrides( List<Dependency> dependencies, Map<String, String> overrides )
    {
        Set<String> excludes = new HashSet<String>();
        return applyOverrides( dependencies, overrides, excludes );
    }

    /**
     * Apply a set of version overrides to a list of dependencies. Return a set of the overrides which were not applied.
     *
     * @param dependencies The list of dependencies
     * @param overrides The map of dependency version overrides
     * @param excludes A set of GAs to ignore when overridding dep versions
     * @return The map of overrides that were not matched in the dependencies
     */
    private static Map<String, String> applyOverrides( List<Dependency> dependencies, Map<String, String> overrides,
                                                       Set<String> excludes )
    {
        // Duplicate the override map so unused overrides can be easily recorded
        Map<String, String> unmatchedVersionOverrides = new HashMap<String, String>();
        unmatchedVersionOverrides.putAll( overrides );

        // Apply matching overrides to dependencies
        for ( Dependency dependency : dependencies )
        {
            String groupIdArtifactId = dependency.getGroupId() + GAV_SEPERATOR + dependency.getArtifactId();
            if ( overrides.containsKey( groupIdArtifactId ) && !excludes.contains( groupIdArtifactId ) )
            {
                String oldVersion = dependency.getVersion();
                String overrideVersion = overrides.get( groupIdArtifactId );

                if (overrideVersion == null || overrideVersion.length() == 0)
                {
                    Log.getLog().warn("Unable to align to an empty version for " + groupIdArtifactId + "; ignoring");
                }
                else
                {
                    dependency.setVersion( overrideVersion );
                    Log.getLog().debug( "Altered dependency " + groupIdArtifactId + " " + oldVersion + "->" +
                                        overrideVersion );
                    unmatchedVersionOverrides.remove( groupIdArtifactId );
                }
            }
        }

        return unmatchedVersionOverrides;
    }

    /**
     * Get dependency management version properties from a remote POM
     *
     * @return Map between the GA of the dependency and the version of the dependency. If the property is not set,
     *         returns an empty map
     */
    private static Map<String, String> loadRemoteDepVersionOverrides() throws MavenExecutionException
    {
        Properties systemProperties = System.getProperties();
        String depMgmtPomCSV = systemProperties.getProperty( DEPENDENCY_MANAGEMENT_POM_PROPERTY );

        Map<String, String> versionOverrides = new HashMap<String, String>( 0 );

        if ( depMgmtPomCSV == null )
        {
            return versionOverrides;
        }

        String[] depMgmtPomGAVs = depMgmtPomCSV.split( "," );

        // Iterate in reverse order so that the first GAV in the list overwrites the last
        for ( int i = ( depMgmtPomGAVs.length - 1 ); i > -1; --i )
        {
            String nextGAV = depMgmtPomGAVs[i];
            if ( !MavenUtil.validGav( nextGAV ) )
            {
                Log.getLog().warn( "Skipping invalid dependency management GAV: " + nextGAV );
                continue;
            }
            try
            {
                EffectiveModelBuilder resolver = EffectiveModelBuilder.getInstance();
                versionOverrides.putAll( resolver.getRemoteDependencyVersionOverrides( nextGAV ) );
            }
            catch ( ArtifactResolutionException e )
            {
                Log.getLog().error( "Unable to resolve remote pom: " + e );
                throw new MavenExecutionException("Unable to resolve remote pom", e);
            }
            catch ( ArtifactDescriptorException e )
            {
                Log.getLog().error( "Unable to resolve remote pom: " + e );
                throw new MavenExecutionException("Unable to resolve remote pom", e);
            }
            catch ( ModelBuildingException e )
            {
                Log.getLog().error( "Unable to resolve remote pom: " + e );
                throw new MavenExecutionException("Unable to resolve remote pom", e);
            }
        }

        return versionOverrides;
    }
}


text:
<!doctype html>

Overflow, transform 2D

redpajama_set_name: RedPajamaGithub


text:
package org.sagebionetworks.repo.web.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.Collections;
import java.util.UUID;

import javax.servlet.ServletException;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.sagebionetworks.ids.IdGenerator;
import org.sagebionetworks.ids.IdType;
import org.sagebionetworks.repo.model.AuthorizationConstants.BOOTSTRAP_PRINCIPAL;
import org.sagebionetworks.repo.model.Entity;
import org.sagebionetworks.repo.model.Project;
import org.sagebionetworks.repo.model.asynch.AsynchronousJobStatus;
import org.sagebionetworks.repo.model.dbo.dao.TestUtils;
import org.sagebionetworks.repo.model.dbo.file.FileHandleDao;
import org.sagebionetworks.repo.model.file.S3FileHandle;
import org.sagebionetworks.repo.model.table.TableEntity;
import org.sagebionetworks.repo.model.table.TableUpdateTransactionRequest;
import org.sagebionetworks.repo.model.table.UploadToTableRequest;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Autowired test for AsynchronousJobController
 * @author John
 *
 */
public class AsynchronousJobControllerTest extends AbstractAutowiredControllerTestBase {

	private Entity parent;
	private TableEntity table;
	private Long adminUserId;

	@Autowired
	private FileHandleDao fileMetadataDao;
	@Autowired
	private IdGenerator idGenerator;
	private S3FileHandle fileHandle;

	@BeforeEach
	public void before() throws Exception {
		adminUserId = BOOTSTRAP_PRINCIPAL.THE_ADMIN_USER.getPrincipalId();
		parent = new Project();
		parent.setName(UUID.randomUUID().toString());
		parent = servletTestHelper.createEntity(dispatchServlet, parent, adminUserId);
		assertNotNull(parent);
		// Create a table
		table = new TableEntity();
		table.setName("TableEntity");
		table.setParentId(parent.getId());
		table = servletTestHelper.createEntity(dispatchServlet, table, adminUserId);
		// Create a file handle
		fileHandle = TestUtils.createS3FileHandle(adminUserId.toString(), idGenerator.generateNewId(IdType.FILE_IDS).toString());
		fileHandle = (S3FileHandle) fileMetadataDao.createFile(fileHandle);
	}

	@AfterEach
	public void after(){
		if(parent != null){
			try {
				servletTestHelper.deleteEntity(dispatchServlet, Project.class, parent.getId(), adminUserId);
			} catch (Exception e) {}
		}
		if(fileHandle != null){
			fileMetadataDao.delete(fileHandle.getId());
		}
	}

	@Test
	public void testStartUploadJob() throws ServletException, Exception{
		TableUpdateTransactionRequest body = new TableUpdateTransactionRequest();
		UploadToTableRequest uploadToTableRequest = new UploadToTableRequest();
		uploadToTableRequest.setTableId(table.getId());
		uploadToTableRequest.setUploadFileHandleId(fileHandle.getId());
		body.setChanges(Collections.singletonList(uploadToTableRequest));
		// Start the job
		AsynchronousJobStatus status = servletTestHelper.startAsynchJob(dispatchServlet, adminUserId, body);
		assertNotNull(status);
		assertNotNull(status.getJobId());
		assertEquals(body, status.getRequestBody());
		// Now get the status again using the ID
		AsynchronousJobStatus clone = servletTestHelper.getAsynchJobStatus(dispatchServlet, adminUserId,
				status.getJobId());
		assertEquals(status, clone);
	}

}


text:
{{ if .Site.Params.favicon }}
<link rel="shortcut icon" href="/{{ .Site.Params.favicon }}" type="image/x-icon" />
{{ end }}


text:
<?php

namespace CursedScript\Exception;

use \CursedScript\GUI;
use \CursedScript\Shell\Cursor;

/**
 * The default exception screen
 *
 * @author Soufian Salim <soufi@nsal.im>
 */
class Screen extends GUI\Screen implements ScreenInterface
{
	/**
	 * Constructor
	 *
	 * @param \Exception $exception
	 */
	public function __construct(\Exception $exception)
	{
		parent::__construct();

		$cursor = new Cursor($this);
		$cursor->write($error->getMessage());
	}
}


text:
/*
 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
 * 2006.
 */
/* ====================================================================
 * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include "apps.h"
#include <string.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/evp.h>

#define KEY_PRIVKEY     1
#define KEY_PUBKEY      2
#define KEY_CERT        3

static void usage(void);

#undef PROG

#define PROG pkeyutl_main

static EVP_PKEY_CTX *init_ctx(int *pkeysize,
                              const char *keyfile, int keyform, int key_type,
                              char *passargin, int pkey_op, ENGINE *e,
                              int   impl);

static int setup_peer(BIO *err, EVP_PKEY_CTX *ctx, int peerform,
                      const char *file, ENGINE* e);

static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
                    unsigned char *out, size_t *poutlen,
                    unsigned char *in, size_t inlen);

int MAIN(int argc, char **);

int MAIN(int argc, char **argv)
{
    BIO *in = NULL, *out = NULL;
    char *infile = NULL, *outfile = NULL, *sigfile = NULL;
    ENGINE *e = NULL;
    int pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
    int keyform = FORMAT_PEM, peerform = FORMAT_PEM;
    char badarg = 0, rev = 0;
    char hexdump = 0, asn1parse = 0;
    EVP_PKEY_CTX *ctx = NULL;
    char *passargin = NULL;
    int keysize = -1;
    int engine_impl = 0;
    unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
    size_t buf_outlen = 0;
    int buf_inlen = 0, siglen = -1;
    const char *inkey = NULL;
    const char *peerkey = NULL;
    STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;

    int ret = 1, rv = -1;

    argc--;
    argv++;

    if (!bio_err)
        bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);

    if (!load_config(bio_err, NULL))
        goto end;
    ERR_load_crypto_strings();
    OpenSSL_add_all_algorithms();

    while (argc >= 1) {
        if (!strcmp(*argv, "-in")) {
            if (--argc < 1)
                badarg = 1;
            else
                infile = *(++argv);
        } else if (!strcmp(*argv, "-out")) {
            if (--argc < 1)
                badarg = 1;
            else
                outfile = *(++argv);
        } else if (!strcmp(*argv, "-sigfile")) {
            if (--argc < 1)
                badarg = 1;
            else
                sigfile = *(++argv);
        } else if (!strcmp(*argv, "-inkey")) {
            if (--argc < 1)
                badarg = 1;
            else
                inkey = *++argv;
        } else if (!strcmp(*argv, "-peerkey")) {
            if (--argc < 1)
                badarg = 1;
            else
                peerkey = *++argv;
        } else if (!strcmp(*argv, "-passin")) {
            if (--argc < 1)
                badarg = 1;
            else
                passargin = *(++argv);
        } else if (strcmp(*argv, "-peerform") == 0) {
            if (--argc < 1)
                badarg = 1;
            else
                peerform = str2fmt(*(++argv));
        } else if (strcmp(*argv, "-keyform") == 0) {
            if (--argc < 1)
                badarg = 1;
            else
                keyform = str2fmt(*(++argv));
        }
#ifndef OPENSSL_NO_ENGINE
        else if (!strcmp(*argv, "-engine")) {
            if (--argc < 1)
                badarg = 1;
            else
                e = setup_engine(bio_err, *(++argv), 0);
        } else if (!strcmp(*argv, "-engine_impl")) {
                engine_impl = 1;
        }
#endif
        else if (!strcmp(*argv, "-pubin"))
            key_type = KEY_PUBKEY;
        else if (!strcmp(*argv, "-certin"))
            key_type = KEY_CERT;
        else if (!strcmp(*argv, "-asn1parse"))
            asn1parse = 1;
        else if (!strcmp(*argv, "-hexdump"))
            hexdump = 1;
        else if (!strcmp(*argv, "-sign"))
            pkey_op = EVP_PKEY_OP_SIGN;
        else if (!strcmp(*argv, "-verify"))
            pkey_op = EVP_PKEY_OP_VERIFY;
        else if (!strcmp(*argv, "-verifyrecover"))
            pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
        else if (!strcmp(*argv, "-encrypt"))
            pkey_op = EVP_PKEY_OP_ENCRYPT;
        else if (!strcmp(*argv, "-decrypt"))
            pkey_op = EVP_PKEY_OP_DECRYPT;
        else if (!strcmp(*argv, "-derive"))
            pkey_op = EVP_PKEY_OP_DERIVE;
        else if (!strcmp(*argv, "-rev"))
            rev = 1;
        else if (strcmp(*argv, "-pkeyopt") == 0) {
            if (--argc < 1)
                badarg = 1;
            else if ((pkeyopts == NULL &&
                     (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
                    sk_OPENSSL_STRING_push(pkeyopts, *++argv) == 0) {
                BIO_puts(bio_err, "out of memory\n");
                goto end;
            }
        } else
            badarg = 1;
        if (badarg) {
            usage();
            goto end;
        }
        argc--;
        argv++;
    }

    if (inkey == NULL ||
        (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE)) {
        usage();
        goto end;
    }
    ctx = init_ctx(&keysize, inkey, keyform, key_type,
                   passargin, pkey_op, e, engine_impl);
    if (!ctx) {
        BIO_puts(bio_err, "Error initializing context\n");
        ERR_print_errors(bio_err);
        goto end;
    }
    if (peerkey != NULL && !setup_peer(bio_err, ctx, peerform, peerkey, e)) {
        BIO_puts(bio_err, "Error setting up peer key\n");
        ERR_print_errors(bio_err);
        goto end;
    }
    if (pkeyopts != NULL) {
        int num = sk_OPENSSL_STRING_num(pkeyopts);
        int i;

        for (i = 0; i < num; ++i) {
            const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);

            if (pkey_ctrl_string(ctx, opt) <= 0) {
                BIO_puts(bio_err, "parameter setting error\n");
                ERR_print_errors(bio_err);
                goto end;
            }
        }
    }

    if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) {
        BIO_puts(bio_err, "Signature file specified for non verify\n");
        goto end;
    }

    if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) {
        BIO_puts(bio_err, "No signature file specified for verify\n");
        goto end;
    }

/* FIXME: seed PRNG only if needed */
    app_RAND_load_file(NULL, bio_err, 0);

    if (pkey_op != EVP_PKEY_OP_DERIVE) {
        if (infile) {
            if (!(in = BIO_new_file(infile, "rb"))) {
                BIO_puts(bio_err, "Error Opening Input File\n");
                ERR_print_errors(bio_err);
                goto end;
            }
        } else
            in = BIO_new_fp(stdin, BIO_NOCLOSE);
    }

    if (outfile) {
        if (!(out = BIO_new_file(outfile, "wb"))) {
            BIO_printf(bio_err, "Error Creating Output File\n");
            ERR_print_errors(bio_err);
            goto end;
        }
    } else {
        out = BIO_new_fp(stdout, BIO_NOCLOSE);
#ifdef OPENSSL_SYS_VMS
        {
            BIO *tmpbio = BIO_new(BIO_f_linebuffer());
            out = BIO_push(tmpbio, out);
        }
#endif
    }

    if (sigfile) {
        BIO *sigbio = BIO_new_file(sigfile, "rb");
        if (!sigbio) {
            BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
            goto end;
        }
        siglen = bio_to_mem(&sig, keysize * 10, sigbio);
        BIO_free(sigbio);
        if (siglen < 0) {
            BIO_printf(bio_err, "Error reading signature data\n");
            goto end;
        }
    }

    if (in) {
        /* Read the input data */
        buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
        if (buf_inlen < 0) {
            BIO_printf(bio_err, "Error reading input Data\n");
            exit(1);
        }
        if (rev) {
            size_t i;
            unsigned char ctmp;
            size_t l = (size_t)buf_inlen;
            for (i = 0; i < l / 2; i++) {
                ctmp = buf_in[i];
                buf_in[i] = buf_in[l - 1 - i];
                buf_in[l - 1 - i] = ctmp;
            }
        }
    }

    if (pkey_op == EVP_PKEY_OP_VERIFY) {
        rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
                             buf_in, (size_t)buf_inlen);
        if (rv == 0)
            BIO_puts(out, "Signature Verification Failure\n");
        else if (rv == 1)
            BIO_puts(out, "Signature Verified Successfully\n");
        if (rv >= 0)
            goto end;
    } else {
        rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
                      buf_in, (size_t)buf_inlen);
        if (rv > 0 && buf_outlen != 0) {
            buf_out = OPENSSL_malloc(buf_outlen);
            if (!buf_out)
                rv = -1;
            else
                rv = do_keyop(ctx, pkey_op,
                              buf_out, (size_t *)&buf_outlen,
                              buf_in, (size_t)buf_inlen);
        }
    }

    if (rv <= 0) {
        BIO_printf(bio_err, "Public Key operation error\n");
        ERR_print_errors(bio_err);
        goto end;
    }
    ret = 0;
    if (asn1parse) {
        if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
            ERR_print_errors(bio_err);
    } else if (hexdump)
        BIO_dump(out, (char *)buf_out, buf_outlen);
    else
        BIO_write(out, buf_out, buf_outlen);

 end:
    if (ctx)
        EVP_PKEY_CTX_free(ctx);
    release_engine(e);
    BIO_free(in);
    BIO_free_all(out);
    if (buf_in != NULL)
        OPENSSL_free(buf_in);
    if (buf_out != NULL)
        OPENSSL_free(buf_out);
    if (sig != NULL)
        OPENSSL_free(sig);
    if (pkeyopts != NULL)
        sk_OPENSSL_STRING_free(pkeyopts);
    return ret;
}

static void usage()
{
    BIO_printf(bio_err, "Usage: pkeyutl [options]\n");
    BIO_printf(bio_err, "-in file        input file\n");
    BIO_printf(bio_err, "-out file       output file\n");
    BIO_printf(bio_err,
               "-sigfile file signature file (verify operation only)\n");
    BIO_printf(bio_err, "-inkey file     input key\n");
    BIO_printf(bio_err, "-keyform arg    private key format - default PEM\n");
    BIO_printf(bio_err, "-pubin          input is a public key\n");
    BIO_printf(bio_err,
               "-certin         input is a certificate carrying a public key\n");
    BIO_printf(bio_err, "-pkeyopt X:Y    public key options\n");
    BIO_printf(bio_err, "-sign           sign with private key\n");
    BIO_printf(bio_err, "-verify         verify with public key\n");
    BIO_printf(bio_err,
               "-verifyrecover  verify with public key, recover original data\n");
    BIO_printf(bio_err, "-encrypt        encrypt with public key\n");
    BIO_printf(bio_err, "-decrypt        decrypt with private key\n");
    BIO_printf(bio_err, "-derive         derive shared secret\n");
    BIO_printf(bio_err, "-hexdump        hex dump output\n");
#ifndef OPENSSL_NO_ENGINE
    BIO_printf(bio_err,
               "-engine e       use engine e, maybe a hardware device, for loading keys.\n");
    BIO_printf(bio_err, "-engine_impl    also use engine given by -engine for crypto operations\n");
#endif
    BIO_printf(bio_err, "-passin arg     pass phrase source\n");

}

static EVP_PKEY_CTX *init_ctx(int *pkeysize,
                              const char *keyfile, int keyform, int key_type,
                              char *passargin, int pkey_op, ENGINE *e,
                              int   engine_impl)
{
    EVP_PKEY *pkey = NULL;
    EVP_PKEY_CTX *ctx = NULL;
    ENGINE *impl = NULL;
    char *passin = NULL;
    int rv = -1;
    X509 *x;
    if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
         || (pkey_op == EVP_PKEY_OP_DERIVE))
        && (key_type != KEY_PRIVKEY)) {
        BIO_printf(bio_err, "A private key is needed for this operation\n");
        goto end;
    }
    if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
        BIO_printf(bio_err, "Error getting password\n");
        goto end;
    }
    switch (key_type) {
    case KEY_PRIVKEY:
        pkey = load_key(bio_err, keyfile, keyform, 0,
                        passin, e, "Private Key");
        break;

    case KEY_PUBKEY:
        pkey = load_pubkey(bio_err, keyfile, keyform, 0,
                           NULL, e, "Public Key");
        break;

    case KEY_CERT:
        x = load_cert(bio_err, keyfile, keyform, NULL, e, "Certificate");
        if (x) {
            pkey = X509_get_pubkey(x);
            X509_free(x);
        }
        break;

    }

    *pkeysize = EVP_PKEY_size(pkey);

    if (!pkey)
        goto end;

#ifndef OPENSSL_NO_ENGINE
    if (engine_impl)
	impl = e;
#endif

    ctx = EVP_PKEY_CTX_new(pkey, impl);

    EVP_PKEY_free(pkey);

    if (!ctx)
        goto end;

    switch (pkey_op) {
    case EVP_PKEY_OP_SIGN:
        rv = EVP_PKEY_sign_init(ctx);
        break;

    case EVP_PKEY_OP_VERIFY:
        rv = EVP_PKEY_verify_init(ctx);
        break;

    case EVP_PKEY_OP_VERIFYRECOVER:
        rv = EVP_PKEY_verify_recover_init(ctx);
        break;

    case EVP_PKEY_OP_ENCRYPT:
        rv = EVP_PKEY_encrypt_init(ctx);
        break;

    case EVP_PKEY_OP_DECRYPT:
        rv = EVP_PKEY_decrypt_init(ctx);
        break;

    case EVP_PKEY_OP_DERIVE:
        rv = EVP_PKEY_derive_init(ctx);
        break;
    }

    if (rv <= 0) {
        EVP_PKEY_CTX_free(ctx);
        ctx = NULL;
    }

 end:

    if (passin)
        OPENSSL_free(passin);

    return ctx;

}

static int setup_peer(BIO *err, EVP_PKEY_CTX *ctx, int peerform,
                      const char *file, ENGINE* e)
{
    EVP_PKEY *peer = NULL;
    ENGINE* engine = NULL;
    int ret;

    if (peerform == FORMAT_ENGINE)
        engine = e;
    peer = load_pubkey(bio_err, file, peerform, 0, NULL, engine, "Peer Key");

    if (!peer) {
        BIO_printf(bio_err, "Error reading peer key %s\n", file);
        ERR_print_errors(err);
        return 0;
    }

    ret = EVP_PKEY_derive_set_peer(ctx, peer);

    EVP_PKEY_free(peer);
    if (ret <= 0)
        ERR_print_errors(err);
    return ret;
}

static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
                    unsigned char *out, size_t *poutlen,
                    unsigned char *in, size_t inlen)
{
    int rv = 0;
    switch (pkey_op) {
    case EVP_PKEY_OP_VERIFYRECOVER:
        rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
        break;

    case EVP_PKEY_OP_SIGN:
        rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
        break;

    case EVP_PKEY_OP_ENCRYPT:
        rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
        break;

    case EVP_PKEY_OP_DECRYPT:
        rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
        break;

    case EVP_PKEY_OP_DERIVE:
        rv = EVP_PKEY_derive(ctx, out, poutlen);
        break;

    }
    return rv;
}


text:
'use strict';

var React = require('react');

module.exports = React.createClass({
    getInitialState: function (){
        return {
            first_run: true,

            route: this.props.route,
            route_error: this._getRouteError(this.props.route),

            target: this.props.target,
            target_error: this._getTargetError(this.props.target),

            enabled: this.props.enabled,
        };
    },
    getDefaultProps: function(){
        return {
            route: '',
            target: '',
            enabled: false,
            addNew: function(){},
            locked: false
        };
    },
    _getRouteError(value){
        if (! value) {
            return 'Please set a value';
        }
        if (! value.startsWith('/')) {
            return "Route must start with '/'";
        }
        return "";
    },
    handleRouteChange: function(e){
        var value = e.target.value;
        var error = this._getRouteError(value);
        this.setState({
            route_error: error,
            route: value,
            first_run: false
        });
    },
    _getTargetError(value){
        if (! value) {
            return 'Please set a value';
        }

        if (value.startsWith('http://')){
            if (value.length <= 'http://'.length){
                return "Please provide domain/IP address";
            }
        } else if (value.startsWith('https://')){
            if (value.length <= 'http://'.length){
                return "Please provide domain/IP address";
            }
        } else {
            return "Target must start with 'http://' or 'https://'";
        }
        return "";
    },
    handleTargetChange: function(e){
        var value = e.target.value;
        var error = this._getTargetError(value);
        this.setState({
            target_error: error,
            target: value,
            first_run: false
        });
    },
    handleEnabledChange: function(e){
        this.setState({
            enabled: !!e.target.checked
        });
    },
    handleCreateClick: function(){
        var redirect = {
            route: this.state.route,
            target: this.state.target,
            enabled: this.state.enabled
        };
        this.props.addNew(redirect);
    },
    render: function(){
        var routeError;
        var targetError;
        if (! this.state.first_run){
            if (this.state.route_error){
                routeError = {this.state.route_error};
            }

            if (this.state.target_error){
                targetError = {this.state.target_error};
            }
        }

        var creation_disabled = !!(this.state.first_run || this.state.route_error || this.state.target_error);

        return (

                <input disabled={this.props.locked} className='route-input' type='text' value={this.state.route} placeholder='/some-route' onChange={this.handleRouteChange}/> {routeError}

                <input disabled={this.props.locked} className='target-input' type='url' value={this.state.target} placeholder='http(s)://somehost' onChange={this.handleTargetChange}/> {targetError}

                <input disabled={this.props.locked} className='toggle' type='checkbox' checked={!!this.state.enabled} onChange={this.handleEnabledChange}/>  Enabled
                <button className='submit-new-route' disabled={this.props.locked || creation_disabled} onClick={this.handleCreateClick}>Create</button>

        );
    }
});


text:
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"

	>
<channel>
	<title>Comments on: 2015’s Top Movies in Legos</title>
	<atom:link href="/2015s-top-movies-in-legos/feed/" rel="self" type="application/rss+xml" />
	<link>/2015s-top-movies-in-legos/</link>
	<description>Unity by sharing</description>
	<lastBuildDate>Sat, 03 Jun 2017 22:16:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.7.4</generator>
</channel>
</rss>


text:
package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"strings"
	"time"

	"github.com/apache/beam/sdks/v2/go/pkg/beam"
	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/fhirio"
	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/pubsubio"
	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/textio"
	"github.com/apache/beam/sdks/v2/go/pkg/beam/log"
	"github.com/apache/beam/sdks/v2/go/pkg/beam/options/gcpopts"
	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
)

var (
	// Required flag with the source directory for GCS files to read, including
	// wildcards. Directory should contain the resources files in NDJSON format.
	sourceGcsLocation = flag.String("sourceGcsLocation", "", "The source directory for GCS files to read, including wildcards.")

	// Required flag with target FHIR store to write data to, must be of the full format:
	// "projects/project_id/locations/location/datasets/DATASET_ID/fhirStores/FHIR_STORE_ID"
	fhirStore = flag.String("fhirStore", "", "The target FHIR Store to write data to, must be of the full format.")

	// Optional flag with the pubsub topic of your FHIR store to read and log upon store updates.
	pubsubTopic = flag.String("pubsubTopic", "", "The PubSub topic to listen to.")
)

func init() {
	register.Function1x1[string, string](WrapInBundle)
	register.DoFn2x0[context.Context, string](&LoggerFn{})
}

// WrapInBundle takes a FHIR resource string and wraps it as a Bundle resource.
// Useful so we can publish the given resource through ExecuteBundles.
func WrapInBundle(resource string) string {
	var r struct {
		ResourceType string `json:"resourceType"`
	}
	json.NewDecoder(strings.NewReader(resource)).Decode(&r)
	return fmt.Sprintf(`{
        "resourceType": "Bundle",
        "type": "batch",
        "entry": [
        	{
        		"request": {
        			"method": "POST",
        			"url": "%s"
        		},
        		"resource": %s
        	}
		]
	}`, r.ResourceType, resource)
}

// LoggerFn is a helper DoFn to log elements received.
type LoggerFn struct {
	LogPrefix string
}

// ProcessElement logs each element it receives.
func (fn *LoggerFn) ProcessElement(ctx context.Context, elm string) {
	log.Infof(ctx, "%s: %v", fn.LogPrefix, elm)
}

// FinishBundle waits a bit so the job server finishes receiving logs.
func (fn *LoggerFn) FinishBundle() {
	time.Sleep(2 * time.Second)
}

func main() {
	flag.Parse()
	beam.Init()

	p, s := beam.NewPipelineWithRoot()

	// Read resources from GCS.
	resourcesInGcs := textio.Read(s, *sourceGcsLocation)
	resourceBundles := beam.ParDo(s, WrapInBundle, resourcesInGcs)

	// Write resources to store.
	_, failedWritesErrorMessage := fhirio.ExecuteBundles(s, *fhirStore, resourceBundles)
	beam.ParDo0(s, &LoggerFn{"Failed Write"}, failedWritesErrorMessage)

	if *pubsubTopic != "" {
		// PubSub notifications will be emitted containing the path of the resource once
		// it is written to the store. Simultaneously read notifications and resources
		// from PubSub and store, respectively.
		resourceNotifications := pubsubio.Read(s, *gcpopts.Project, *pubsubTopic, nil)
		resourcesInFhirStore, deadLetters := fhirio.Read(s, resourceNotifications)

		// Log the read resources or read errors to the server.
		beam.ParDo0(s, &LoggerFn{"Read Resource"}, resourcesInFhirStore)
		beam.ParDo0(s, &LoggerFn{"Got Dead Letter"}, deadLetters)
	}

	ctx := context.Background()
	if err := beamx.Run(ctx, p); err != nil {
		log.Fatalf(ctx, "Failed to execute job: %v", err)
	}
}


text:
package com.google.doclava;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Proofread {
  static Writer out = null;
  static final Pattern WHITESPACE = Pattern.compile("\\r?\\n");
  static final String INDENT = "        ";
  static final String NEWLINE = "\n" + INDENT;

  public static void initProofread(String filename) {
    try {
      out = new BufferedWriter(new FileWriter(filename));
      out.write("javadoc proofread file: " + filename + "\n");
    } catch (IOException e) {
      if (out != null) {
        try {
          out.close();
        } catch (IOException ex) {}
        out = null;
      }
      System.err.println("error opening file: " + filename);
    }
  }

  public static void finishProofread(String filename) {
    if (out == null) {
      return;
    }

    try {
      out.close();
    } catch (IOException e) {}
  }

  public static void write(String s) {
    if (out == null) {
      return;
    }
    try {
      out.write(s);
    } catch (IOException e) {}
  }

  public static void writeIndented(String s) {
    s = s.trim();
    Matcher m = WHITESPACE.matcher(s);
    s = m.replaceAll(NEWLINE);
    write(INDENT);
    write(s);
    write("\n");
  }

  public static void writeFileHeader(String filename) {
    write("\n\n=== ");
    write(filename);
    write(" ===\n");
  }

  public static void writeTagList(TagInfo[] tags) {
    if (out == null) {
      return;
    }

    for (TagInfo t : tags) {
      String k = t.kind();
      if ("Text".equals(t.name())) {
        writeIndented(t.text());
      } else if ("@more".equals(k)) {
        writeIndented("");
      } else if ("@see".equals(k)) {
        SeeTagInfo see = (SeeTagInfo) t;
        String label = see.label();
        if (label == null) {
          label = "";
        }
        writeIndented("{" + see.name() + " ... " + label + "}");
      } else if ("@code".equals(k)) {
        writeIndented(t.text());
      } else if ("@samplecode".equals(k)) {
        writeIndented(t.text());
      } else {
        writeIndented("{" + (t.name() != null ? t.name() : "") + "/" + t.text() + "}");
      }
    }
  }

  public static void writePackages(String filename, TagInfo[] tags) {
    if (out == null) {
      return;
    }

    writeFileHeader(filename);
    writeTagList(tags);
  }

  public static void writePackage(String filename, TagInfo[] tags) {
    if (out == null) {
      return;
    }

    writeFileHeader(filename);
    writeTagList(tags);
  }

  public static void writeClass(String filename, ClassInfo cl) {
    if (out == null) {
      return;
    }

    writeFileHeader(filename);
    writeTagList(cl.inlineTags());

    // enum constants
    for (FieldInfo f : cl.enumConstants()) {
      write("ENUM: " + f.name() + "\n");
      writeTagList(f.inlineTags());
    }

    // fields
    for (FieldInfo f : cl.selfFields()) {
      write("FIELD: " + f.name() + "\n");
      writeTagList(f.inlineTags());
    }

    // constructors
    for (MethodInfo m : cl.constructors()) {
      write("CONSTRUCTOR: " + m.name() + "\n");
      writeTagList(m.inlineTags().tags());
    }

    // methods
    for (MethodInfo m : cl.selfMethods()) {
      write("METHOD: " + m.name() + "\n");
      writeTagList(m.inlineTags().tags());
    }
  }
}


text:
package org.mockitousage.misuse;

import org.junit.After;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
import org.mockito.exceptions.misusing.UnfinishedVerificationException;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;

import java.util.Observer;

import static junit.framework.TestCase.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Mockito.*;

public class DetectingMisusedMatchersTest extends TestBase {

    class WithFinal {
        final Object finalMethod(Object object) {
            return null;
        }
    }

    @Mock private WithFinal withFinal;

    @After
    public void resetState() {
        super.resetState();
    }

    private void misplaced_anyObject_argument_matcher() {
        anyObject();
    }

    private void misplaced_anyInt_argument_matcher() {
        anyInt();
    }

    private void misplaced_anyBoolean_argument_matcher() {
        anyBoolean();
    }

    @Test
    public void should_fail_fast_when_argument_matchers_are_abused() {
        misplaced_anyObject_argument_matcher();
        try {
            mock(IMethods.class);
            fail();
        } catch (InvalidUseOfMatchersException e) {
            assertThat(e).hasMessageContaining("Misplaced argument matcher");
        }
    }

    @Test
    public void should_report_argument_locations_when_argument_matchers_misused() {
        try {
            Observer observer = mock(Observer.class);

            misplaced_anyInt_argument_matcher();
            misplaced_anyObject_argument_matcher();
            misplaced_anyBoolean_argument_matcher();

            observer.update(null, null);

            validateMockitoUsage();
            fail();
        } catch (InvalidUseOfMatchersException e) {
            assertThat(e)
                .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyInt_argument_matcher")
                .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyObject_argument_matcher")
                .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyBoolean_argument_matcher");
        }
    }

    @Test
    public void shouldSayUnfinishedVerificationButNotInvalidUseOfMatchers() {
        assumeTrue("Does not apply for inline mocks", withFinal.getClass() != WithFinal.class);
        verify(withFinal).finalMethod(anyObject());
        try {
            verify(withFinal);
            fail();
        } catch (UnfinishedVerificationException e) {}
    }
}


text:
package org.jkiss.dbeaver.model.impl.jdbc;

/**
 * JDBCUnknownType
 *
 * @author Serge Rider
 */
public class JDBCUnknownType {

    private int type;
    private Object value;

    public JDBCUnknownType(int type, Object value)
    {
        this.type = type;
        this.value = value;
    }

    public int getType()
    {
        return type;
    }

    public Object getValue()
    {
        return value;
    }

    public String toString()
    {
        return "Unsupported JDBC type: " + type;
    }

}


text:
'use strict';

exports.up = async function(knex, Promise) {
  await knex.schema.createTable('comments', function(table) {
    table.charset('utf8');
    table.collate('utf8_unicode_ci');

    table.increments('commentIdAttr').unsigned().primary();
    table.text('text').notNullable();
    table.integer('postId').unsigned().notNullable().references('posts.postIdAttr').onUpdate('CASCADE').onDelete('CASCADE');
    table.integer('createdById').unsigned().nullable().references('users.userIdAttr').onUpdate('CASCADE').onDelete('RESTRICT');

    // Soft delete.
    table.dateTime('deletedAt').nullable().index();

    // Timestamps.
    table.dateTime('createdAt').notNullable().defaultTo(knex.fn.now()).index();
    table.dateTime('updatedAt').notNullable().defaultTo(knex.fn.now());
  });
};

exports.down = async function(knex, Promise) {
  await knex.schema.dropTable('comments');
};


text:
package org.javacs;

import com.sun.source.doctree.DocCommentTree;
import com.sun.source.tree.*;
import com.sun.source.util.*;
import java.io.IOException;
import java.nio.file.*;
import java.time.Instant;
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.lang.model.element.*;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import org.javacs.lsp.*;

class Parser {
    private static final JavaCompiler COMPILER = ServiceLoader.load(JavaCompiler.class).iterator().next();
    private static final SourceFileManager FILE_MANAGER = new SourceFileManager();

    /** Create a task that compiles a single file */
    private static JavacTask singleFileTask(JavaFileObject file) {
        return (JavacTask)
                COMPILER.getTask(null, FILE_MANAGER, Parser::ignoreError, List.of(), List.of(), List.of(file));
    }

    final JavaFileObject file;
    final String contents;
    final JavacTask task;
    final CompilationUnitTree root;
    final Trees trees;

    private Parser(JavaFileObject file) {
        this.file = file;
        try {
            this.contents = file.getCharContent(false).toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        this.task = singleFileTask(file);
        try {
            this.root = task.parse().iterator().next();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        this.trees = Trees.instance(task);
    }

    static Parser parseFile(Path file) {
        return parseJavaFileObject(new SourceFileObject(file));
    }

    private static Parser cachedParse;
    private static long cachedModified = -1;

    private static boolean needsParse(JavaFileObject file) {
        if (cachedParse == null) return true;
        if (!cachedParse.file.equals(file)) return true;
        if (file.getLastModified() > cachedModified) return true;
        return false;
    }

    private static void loadParse(JavaFileObject file) {
        cachedParse = new Parser(file);
        cachedModified = file.getLastModified();
    }

    static Parser parseJavaFileObject(JavaFileObject file) {
        if (needsParse(file)) {
            loadParse(file);
        } else {
            LOG.info("...using cached parse");
        }
        return cachedParse;
    }

    Set<Name> packagePrivateClasses() {
        var result = new HashSet<Name>();
        for (var t : root.getTypeDecls()) {
            if (t instanceof ClassTree) {
                var c = (ClassTree) t;
                var isPublic = c.getModifiers().getFlags().contains(Modifier.PUBLIC);
                if (!isPublic) {
                    result.add(c.getSimpleName());
                }
            }
        }
        return result;
    }

    static Range range(JavacTask task, CharSequence contents, TreePath path) {
        // Find start position
        var trees = Trees.instance(task);
        var pos = trees.getSourcePositions();
        var root = path.getCompilationUnit();
        var lines = root.getLineMap();
        var start = (int) pos.getStartPosition(root, path.getLeaf());
        var end = (int) pos.getEndPosition(root, path.getLeaf());

        // If start is -1, give up
        if (start == -1) {
            LOG.warning(String.format("Couldn't locate `%s`", path.getLeaf()));
            return Range.NONE;
        }
        // If end is bad, guess based on start
        if (end == -1) {
            end = start + path.getLeaf().toString().length();
        }

        if (path.getLeaf() instanceof ClassTree) {
            var cls = (ClassTree) path.getLeaf();

            // If class has annotations, skip over them
            if (!cls.getModifiers().getAnnotations().isEmpty())
                start = (int) pos.getEndPosition(root, cls.getModifiers());

            // Find position of class name
            var name = cls.getSimpleName().toString();
            start = indexOf(contents, name, start);
            if (start == -1) {
                LOG.warning(String.format("Couldn't find identifier `%s` in `%s`", name, path.getLeaf()));
                return Range.NONE;
            }
            end = start + name.length();
        }
        if (path.getLeaf() instanceof MethodTree) {
            var method = (MethodTree) path.getLeaf();

            // If method has annotations, skip over them
            if (!method.getModifiers().getAnnotations().isEmpty())
                start = (int) pos.getEndPosition(root, method.getModifiers());

            // Find position of method name
            var name = method.getName().toString();
            if (name.equals("<init>")) {
                name = className(path);
            }
            start = indexOf(contents, name, start);
            if (start == -1) {
                LOG.warning(String.format("Couldn't find identifier `%s` in `%s`", name, path.getLeaf()));
                return Range.NONE;
            }
            end = start + name.length();
        }
        if (path.getLeaf() instanceof VariableTree) {
            var field = (VariableTree) path.getLeaf();

            // If field has annotations, skip over them
            if (!field.getModifiers().getAnnotations().isEmpty())
                start = (int) pos.getEndPosition(root, field.getModifiers());

            // Find position of method name
            var name = field.getName().toString();
            start = indexOf(contents, name, start);
            if (start == -1) {
                LOG.warning(String.format("Couldn't find identifier `%s` in `%s`", name, path.getLeaf()));
                return Range.NONE;
            }
            end = start + name.length();
        }
        if (path.getLeaf() instanceof MemberSelectTree) {
            var member = (MemberSelectTree) path.getLeaf();
            var name = member.getIdentifier().toString();
            start = indexOf(contents, name, start);
            if (start == -1) {
                LOG.warning(String.format("Couldn't find identifier `%s` in `%s`", name, path.getLeaf()));
                return Range.NONE;
            }
            end = start + name.length();
        }
        var startLine = (int) lines.getLineNumber(start);
        var startCol = (int) lines.getColumnNumber(start);
        var endLine = (int) lines.getLineNumber(end);
        var endCol = (int) lines.getColumnNumber(end);
        var range = new Range(new Position(startLine - 1, startCol - 1), new Position(endLine - 1, endCol - 1));

        return range;
    }

    private static int indexOf(CharSequence contents, String name, int start) {
        var matcher = Pattern.compile("\\b" + name + "\\b").matcher(contents);
        if (matcher.find(start)) {
            return matcher.start();
        }
        return -1;
    }

    private static final DocCommentTree EMPTY_DOC = makeEmptyDoc();

    private static DocCommentTree makeEmptyDoc() {
        var file = new SourceFileObject(Paths.get("/Foo.java"), "/** */ class Foo { }", Instant.now());
        var task =
                (JavacTask)
                        COMPILER.getTask(
                                null,
                                FILE_MANAGER,
                                Parser::ignoreError,
                                List.of(),
                                null,
                                Collections.singletonList(file));
        var docs = DocTrees.instance(task);
        CompilationUnitTree root;
        try {
            root = task.parse().iterator().next();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        class FindEmptyDoc extends TreePathScanner<Void, Void> {
            DocCommentTree found;

            @Override
            public Void visitClass(ClassTree t, Void __) {
                found = docs.getDocCommentTree(getCurrentPath());
                return null;
            }
        }
        var find = new FindEmptyDoc();
        find.scan(root, null);
        return Objects.requireNonNull(find.found);
    }

    private static void ignoreError(javax.tools.Diagnostic<? extends JavaFileObject> __) {
        // Too noisy, this only comes up in parse tasks which tend to be less important
        // LOG.warning(err.getMessage(Locale.getDefault()));
    }

    static String describeTree(Tree leaf) {
        if (leaf instanceof MethodTree) {
            var method = (MethodTree) leaf;
            var params = new StringJoiner(", ");
            for (var p : method.getParameters()) {
                params.add(p.getType() + " " + p.getName());
            }
            return method.getName() + "(" + params + ")";
        }
        if (leaf instanceof ClassTree) {
            var cls = (ClassTree) leaf;
            return "class " + cls.getSimpleName();
        }
        if (leaf instanceof BlockTree) {
            var block = (BlockTree) leaf;
            return String.format("{ ...%d lines... }", block.getStatements().size());
        }
        return leaf.toString();
    }

    List<String> accessibleClasses(String partialName, String fromPackage) {
        var toPackage = Objects.toString(root.getPackageName(), "");
        var samePackage = fromPackage.equals(toPackage) || toPackage.isEmpty();
        var result = new ArrayList<String>();
        for (var t : root.getTypeDecls()) {
            if (!(t instanceof ClassTree)) continue;
            var cls = (ClassTree) t;
            // If class is not accessible, skip it
            var isPublic = cls.getModifiers().getFlags().contains(Modifier.PUBLIC);
            if (!samePackage && !isPublic) continue;
            // If class doesn't match partialName, skip it
            var name = cls.getSimpleName().toString();
            if (!StringSearch.matchesPartialName(name, partialName)) continue;
            if (root.getPackageName() != null) {
                name = root.getPackageName() + "." + name;
            }
            result.add(name);
        }
        return result;
    }

    private static String prune(
            CompilationUnitTree root,
            SourcePositions pos,
            StringBuilder buffer,
            long[] offsets,
            boolean eraseAfterCursor) {
        class Scan extends TreeScanner<Void, Void> {
            boolean erasedAfterCursor = !eraseAfterCursor;

            boolean containsCursor(Tree node) {
                var start = pos.getStartPosition(root, node);
                var end = pos.getEndPosition(root, node);
                for (var cursor : offsets) {
                    if (start <= cursor && cursor <= end) {
                        return true;
                    }
                }
                return false;
            }

            boolean anyContainsCursor(Collection<? extends Tree> nodes) {
                for (var n : nodes) {
                    if (containsCursor(n)) return true;
                }
                return false;
            }

            long lastCursorIn(Tree node) {
                var start = pos.getStartPosition(root, node);
                var end = pos.getEndPosition(root, node);
                long last = -1;
                for (var cursor : offsets) {
                    if (start <= cursor && cursor <= end) {
                        last = cursor;
                    }
                }
                if (last == -1) {
                    throw new RuntimeException(
                            String.format("No cursor in %s is between %d and %d", offsets, start, end));
                }
                return last;
            }

            @Override
            public Void visitImport(ImportTree node, Void __) {
                // Erase 'static' keyword so autocomplete works better
                if (containsCursor(node) && node.isStatic()) {
                    var start = (int) pos.getStartPosition(root, node);
                    start = buffer.indexOf("static", start);
                    var end = start + "static".length();
                    erase(buffer, start, end);
                }
                return super.visitImport(node, null);
            }

            @Override
            public Void visitSwitch(SwitchTree node, Void __) {
                if (containsCursor(node)) {
                    // Prevent the enclosing block from erasing the closing } of the switch
                    erasedAfterCursor = true;
                }
                return super.visitSwitch(node, null);
            }

            @Override
            public Void visitBlock(BlockTree node, Void __) {
                if (containsCursor(node)) {
                    super.visitBlock(node, null);
                    // When we find the deepest block that includes the cursor
                    if (!erasedAfterCursor) {
                        var cursor = lastCursorIn(node);
                        var start = cursor;
                        var end = pos.getEndPosition(root, node);
                        if (end >= buffer.length()) end = buffer.length() - 1;
                        // Find the next line
                        while (start < end && buffer.charAt((int) start) != '\n') start++;
                        // Find the end of the block
                        while (end > start && buffer.charAt((int) end) != '}') end--;
                        // Erase from next line to end of block
                        erase(buffer, start, end - 1);
                        erasedAfterCursor = true;
                    }
                } else if (!node.getStatements().isEmpty()) {
                    var first = node.getStatements().get(0);
                    var last = node.getStatements().get(node.getStatements().size() - 1);
                    var start = pos.getStartPosition(root, first);
                    var end = pos.getEndPosition(root, last);
                    if (end >= buffer.length()) end = buffer.length() - 1;
                    erase(buffer, start, end);
                }
                return null;
            }

            @Override
            public Void visitErroneous(ErroneousTree node, Void nothing) {
                return super.scan(node.getErrorTrees(), nothing);
            }
        }

        new Scan().scan(root, null);

        var pruned = buffer.toString();
        // For debugging:
        if (false) {
            var file = Paths.get(root.getSourceFile().toUri());
            var out = file.resolveSibling(file.getFileName() + ".pruned");
            try {
                Files.writeString(out, pruned);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return pruned;
    }

    private static void erase(StringBuilder buffer, long start, long end) {
        for (int i = (int) start; i < end; i++) {
            switch (buffer.charAt(i)) {
                case '\r':
                case '\n':
                    break;
                default:
                    buffer.setCharAt(i, ' ');
            }
        }
    }

    String prune(long cursor) {
        var pos = Trees.instance(task).getSourcePositions();
        var buffer = new StringBuilder(contents);
        long[] cursors = {cursor};
        return prune(root, pos, buffer, cursors, true);
    }

    static Optional<Path> declaringFile(Element e) {
        // Find top-level type surrounding `to`
        LOG.info(String.format("...looking up declaring file of `%s`...", e));
        var top = topLevelDeclaration(e);
        if (!top.isPresent()) {
            LOG.warning("...no top-level type!");
            return Optional.empty();
        }
        // Find file by looking at package and class name
        LOG.info(String.format("...top-level type is %s", top.get()));
        var file = FileStore.findDeclaringFile(top.get());
        if (!file.isPresent()) {
            LOG.info(String.format("...couldn't find declaring file for type"));
            return Optional.empty();
        }
        return file;
    }

    private static Optional<TypeElement> topLevelDeclaration(Element e) {
        if (e == null) return Optional.empty();
        var parent = e;
        TypeElement result = null;
        while (parent.getEnclosingElement() != null) {
            if (parent instanceof TypeElement) result = (TypeElement) parent;
            parent = parent.getEnclosingElement();
        }
        return Optional.ofNullable(result);
    }

    static String className(TreePath t) {
        while (t != null) {
            if (t.getLeaf() instanceof ClassTree) {
                var cls = (ClassTree) t.getLeaf();
                return cls.getSimpleName().toString();
            }
            t = t.getParentPath();
        }
        return "";
    }

    private static final Logger LOG = Logger.getLogger("main");
}


text:
package stroom.dashboard.client.table;

import stroom.annotation.client.ChangeAssignedToPresenter;
import stroom.annotation.client.ChangeStatusPresenter;
import stroom.annotation.client.ShowAnnotationEvent;
import stroom.annotation.shared.Annotation;
import stroom.annotation.shared.EventId;
import stroom.dashboard.shared.IndexConstants;
import stroom.dashboard.shared.TableComponentSettings;
import stroom.query.api.v2.Field;
import stroom.svg.client.SvgPresets;
import stroom.widget.menu.client.presenter.IconMenuItem;
import stroom.widget.menu.client.presenter.Item;
import stroom.widget.menu.client.presenter.ShowMenuEvent;
import stroom.widget.popup.client.presenter.PopupPosition;
import stroom.widget.popup.client.presenter.PopupPosition.VerticalLocation;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.inject.Inject;

public class AnnotationManager {

    private final ChangeStatusPresenter changeStatusPresenter;
    private final ChangeAssignedToPresenter changeAssignedToPresenter;

    private TableComponentSettings tableComponentSettings;
    private List<TableRow> selectedItems;

    @Inject
    public AnnotationManager(final ChangeStatusPresenter changeStatusPresenter,
                             final ChangeAssignedToPresenter changeAssignedToPresenter) {
        this.changeStatusPresenter = changeStatusPresenter;
        this.changeAssignedToPresenter = changeAssignedToPresenter;
    }

    public void showAnnotationMenu(final NativeEvent event,
                                   final TableComponentSettings tableComponentSettings,
                                   final List<TableRow> selectedItems) {
        this.tableComponentSettings = tableComponentSettings;
        this.selectedItems = selectedItems;

        final Element target = event.getEventTarget().cast();
        final PopupPosition popupPosition = new PopupPosition(target.getAbsoluteLeft(),
                target.getAbsoluteRight(), target.getAbsoluteTop(), target.getAbsoluteBottom(), null,
                VerticalLocation.BELOW);

        final List<Item> menuItems = getMenuItems(tableComponentSettings, selectedItems);
        ShowMenuEvent
                .builder()
                .items(menuItems)
                .popupPosition(popupPosition)
                .fire(changeStatusPresenter);
    }

    private List<Item> getMenuItems(final TableComponentSettings tableComponentSettings,
                                    final List<TableRow> selectedItems) {
        final List<Item> menuItems = new ArrayList<>();

        final List<EventId> eventIdList = getEventIdList(tableComponentSettings, selectedItems);
        final List<Long> annotationIdList = getAnnotationIdList(tableComponentSettings, selectedItems);

        // Create menu item.
        menuItems.add(createCreateMenu(eventIdList));

        if (annotationIdList.size() > 0) {
            // Status menu item.
            menuItems.add(createStatusMenu(annotationIdList));
            // Assigned to menu item.
            menuItems.add(createAssignMenu(annotationIdList));
        }

        return menuItems;
    }

    public List<EventId> getEventIdList(final TableComponentSettings tableComponentSettings,
                                        final List<TableRow> selectedItems) {
        final List<EventId> idList = new ArrayList<>();

        final List<String> streamIds = getValues(
                tableComponentSettings,
                selectedItems,
                IndexConstants.generateObfuscatedColumnName(IndexConstants.STREAM_ID));
        final List<String> eventIds = getValues(
                tableComponentSettings,
                selectedItems,
                IndexConstants.generateObfuscatedColumnName(IndexConstants.EVENT_ID));
        final List<String> eventIdLists = getValues(tableComponentSettings, selectedItems, "EventIdList");

        for (int i = 0; i < streamIds.size() && i < eventIds.size(); i++) {
            final Long streamId = toLong(streamIds.get(i));
            final Long eventId = toLong(eventIds.get(i));
            if (streamId != null && eventId != null) {
                idList.add(new EventId(streamId, eventId));
            }
        }

        for (final String eventIdList : eventIdLists) {
            if (eventIdList != null) {
                final String[] events = eventIdList.split(" ");
                for (final String event : events) {
                    try {
                        final String[] parts = event.split(":");
                        if (parts.length == 2) {
                            final long streamId = Long.parseLong(parts[0]);
                            final long eventId = Long.parseLong(parts[1]);
                            idList.add(new EventId(streamId, eventId));
                        }
                    } catch (final NumberFormatException e) {
                        // Ignore.
                    }
                }
            }
        }

        return idList;
    }

    private String getFieldId(final TableComponentSettings tableComponentSettings, final String fieldName) {
        for (final Field field : tableComponentSettings.getFields()) {
            if (field.getName().equalsIgnoreCase(fieldName)) {
                return field.getId();
            }
        }
        return null;
    }

    public List<Long> getAnnotationIdList(final TableComponentSettings tableComponentSettings,
                                          final List<TableRow> selectedItems) {
        final List<String> values = getValues(tableComponentSettings, selectedItems, "annotation:Id");
        return values
                .stream()
                .map(this::toLong)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    public List<String> getValues(final TableComponentSettings tableComponentSettings,
                                  final List<TableRow> selectedItems,
                                  final String fieldName) {
        final List<String> values = new ArrayList<>();
        if (selectedItems != null && selectedItems.size() > 0) {
            final String fieldId = getFieldId(tableComponentSettings, fieldName);
            if (fieldId != null) {
                for (final TableRow row : selectedItems) {
                    final String value = row.getText(fieldId);
                    values.add(value);
                }
            }
        }
        return values;
    }

    public String getValue(final TableComponentSettings tableComponentSettings,
                           final List<TableRow> selectedItems,
                           final String fieldName) {
        if (selectedItems != null && selectedItems.size() > 0) {
            final String fieldId = getFieldId(tableComponentSettings, fieldName);
            if (fieldId != null) {
                for (final TableRow row : selectedItems) {
                    final String value = row.getText(fieldId);
                    if (value != null) {
                        return value;
                    }
                }
            }
        }
        return null;
    }

    private Long toLong(final String string) {
        if (string != null) {
            try {
                return Long.parseLong(string);
            } catch (final NumberFormatException e) {
                // Ignore.
            }
        }
        return null;
    }

    private Item createCreateMenu(final List<EventId> eventIdList) {
        return new IconMenuItem.Builder()
                .priority(0)
                .icon(SvgPresets.EDIT)
                .text("Create Annotation")
                .command(() -> createAnnotation(eventIdList))
                .build();
    }

    private Item createStatusMenu(final List<Long> annotationIdList) {
        return new IconMenuItem.Builder()
                .priority(1)
                .icon(SvgPresets.EDIT)
                .text("Change Status")
                .command(() -> changeStatus(annotationIdList))
                .build();
    }

    private Item createAssignMenu(final List<Long> annotationIdList) {
        return new IconMenuItem.Builder()
                .priority(2)
                .icon(SvgPresets.EDIT)
                .text("Change Assigned To")
                .command(() -> changeAssignedTo(annotationIdList))
                .build();
    }

    private void createAnnotation(final List<EventId> eventIdList) {
        final String title = getValue(tableComponentSettings, selectedItems, "title");
        final String subject = getValue(tableComponentSettings, selectedItems, "subject");
        final String status = getValue(tableComponentSettings, selectedItems, "status");
        final String assignedTo = getValue(tableComponentSettings, selectedItems, "assignedTo");
        final String comment = getValue(tableComponentSettings, selectedItems, "comment");

        final Annotation annotation = new Annotation();
        annotation.setTitle(title);
        annotation.setSubject(subject);
        annotation.setStatus(status);
        annotation.setAssignedTo(assignedTo);
        annotation.setComment(comment);

        ShowAnnotationEvent.fire(changeStatusPresenter, annotation, eventIdList);
    }

    private void changeStatus(final List<Long> annotationIdList) {
        changeStatusPresenter.show(annotationIdList);
    }

    private void changeAssignedTo(final List<Long> annotationIdList) {
        changeAssignedToPresenter.show(annotationIdList);
    }
}


text:
// ========================================================================
// Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// You may elect to redistribute this code under either of these licenses.
// ========================================================================

package org.eclipse.jetty.util;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
 * Scanner
 *
 * Utility for scanning a directory for added, removed and changed
 * files and reporting these events via registered Listeners.
 *
 */
public class Scanner extends AbstractLifeCycle
{
    private static final Logger LOG = Log.getLogger(Scanner.class);
    private static int __scannerId=0;
    private int _scanInterval;
    private int _scanCount = 0;
    private final List<Listener> _listeners = new ArrayList<Listener>();
    private final Map<String,TimeNSize> _prevScan = new HashMap<String,TimeNSize> ();
    private final Map<String,TimeNSize> _currentScan = new HashMap<String,TimeNSize> ();
    private FilenameFilter _filter;
    private final List<File> _scanDirs = new ArrayList<File>();
    private volatile boolean _running = false;
    private boolean _reportExisting = true;
    private boolean _reportDirs = true;
    private Timer _timer;
    private TimerTask _task;
    private int _scanDepth=0;

    public enum Notification { ADDED, CHANGED, REMOVED };
    private final Map<String,Notification> _notifications = new HashMap<String,Notification>();

    static class TimeNSize
    {
        final long _lastModified;
        final long _size;

        public TimeNSize(long lastModified, long size)
        {
            _lastModified = lastModified;
            _size = size;
        }

        @Override
        public int hashCode()
        {
            return (int)_lastModified^(int)_size;
        }

        @Override
        public boolean equals(Object o)
        {
            if (o instanceof TimeNSize)
            {
                TimeNSize tns = (TimeNSize)o;
                return tns._lastModified==_lastModified && tns._size==_size;
            }
            return false;
        }

        @Override
        public String toString()
        {
            return "[lm="+_lastModified+",s="+_size+"]";
        }
    }

    /**
     * Listener
     *
     * Marker for notifications re file changes.
     */
    public interface Listener
    {
    }

    public interface ScanListener extends Listener
    {
        public void scan();
    }

    public interface DiscreteListener extends Listener
    {
        public void fileChanged (String filename) throws Exception;
        public void fileAdded (String filename) throws Exception;
        public void fileRemoved (String filename) throws Exception;
    }

    public interface BulkListener extends Listener
    {
        public void filesChanged (List<String> filenames) throws Exception;
    }

    /**
     * Listener that notifies when a scan has started and when it has ended.
     */
    public interface ScanCycleListener extends Listener
    {
        public void scanStarted(int cycle) throws Exception;
        public void scanEnded(int cycle) throws Exception;
    }

    /**
     *
     */
    public Scanner ()
    {
    }

    /**
     * Get the scan interval
     * @return interval between scans in seconds
     */
    public int getScanInterval()
    {
        return _scanInterval;
    }

    /**
     * Set the scan interval
     * @param scanInterval pause between scans in seconds, or 0 for no scan after the initial scan.
     */
    public synchronized void setScanInterval(int scanInterval)
    {
        _scanInterval = scanInterval;
        schedule();
    }

    /**
     * Set the location of the directory to scan.
     * @param dir
     * @deprecated use setScanDirs(List dirs) instead
     */
    @Deprecated
    public void setScanDir (File dir)
    {
        _scanDirs.clear();
        _scanDirs.add(dir);
    }

    /**
     * Get the location of the directory to scan
     * @return the first directory (of {@link #getScanDirs()} being scanned)
     * @deprecated use getScanDirs() instead
     */
    @Deprecated
    public File getScanDir ()
    {
        return (_scanDirs==null?null:(File)_scanDirs.get(0));
    }

    public void setScanDirs (List<File> dirs)
    {
        _scanDirs.clear();
        _scanDirs.addAll(dirs);
    }

    public synchronized void addScanDir( File dir )
    {
        _scanDirs.add( dir );
    }

    public List<File> getScanDirs ()
    {
        return Collections.unmodifiableList(_scanDirs);
    }

    /* ------------------------------------------------------------ */
    /**
     * @param recursive True if scanning is recursive
     * @see  #setScanDepth(int)
     */
    public void setRecursive (boolean recursive)
    {
        _scanDepth=recursive?-1:0;
    }

    /* ------------------------------------------------------------ */
    /**
     * @return True if scanning is fully recursive (scandepth==-1)
     * @see #getScanDepth()
     */
    public boolean getRecursive ()
    {
        return _scanDepth==-1;
    }

    /* ------------------------------------------------------------ */
    /** Get the scanDepth.
     * @return the scanDepth
     */
    public int getScanDepth()
    {
        return _scanDepth;
    }

    /* ------------------------------------------------------------ */
    /** Set the scanDepth.
     * @param scanDepth the scanDepth to set
     */
    public void setScanDepth(int scanDepth)
    {
        _scanDepth = scanDepth;
    }

    /**
     * Apply a filter to files found in the scan directory.
     * Only files matching the filter will be reported as added/changed/removed.
     * @param filter
     */
    public void setFilenameFilter (FilenameFilter filter)
    {
        _filter = filter;
    }

    /**
     * Get any filter applied to files in the scan dir.
     * @return the filename filter
     */
    public FilenameFilter getFilenameFilter ()
    {
        return _filter;
    }

    /* ------------------------------------------------------------ */
    /**
     * Whether or not an initial scan will report all files as being
     * added.
     * @param reportExisting if true, all files found on initial scan will be
     * reported as being added, otherwise not
     */
    public void setReportExistingFilesOnStartup (boolean reportExisting)
    {
        _reportExisting = reportExisting;
    }

    /* ------------------------------------------------------------ */
    public boolean getReportExistingFilesOnStartup()
    {
        return _reportExisting;
    }

    /* ------------------------------------------------------------ */
    /** Set if found directories should be reported.
     * @param dirs
     */
    public void setReportDirs(boolean dirs)
    {
        _reportDirs=dirs;
    }

    /* ------------------------------------------------------------ */
    public boolean getReportDirs()
    {
        return _reportDirs;
    }

    /* ------------------------------------------------------------ */
    /**
     * Add an added/removed/changed listener
     * @param listener
     */
    public synchronized void addListener (Listener listener)
    {
        if (listener == null)
            return;
        _listeners.add(listener);
    }

    /**
     * Remove a registered listener
     * @param listener the Listener to be removed
     */
    public synchronized void removeListener (Listener listener)
    {
        if (listener == null)
            return;
        _listeners.remove(listener);
    }

    /**
     * Start the scanning action.
     */
    @Override
    public synchronized void doStart()
    {
        if (_running)
            return;

        _running = true;

        if (_reportExisting)
        {
            // if files exist at startup, report them
            scan();
            scan(); // scan twice so files reported as stable
        }
        else
        {
            //just register the list of existing files and only report changes
            scanFiles();
            _prevScan.putAll(_currentScan);
        }
        schedule();
    }

    public TimerTask newTimerTask ()
    {
        return new TimerTask()
        {
            @Override
            public void run() { scan(); }
        };
    }

    public Timer newTimer ()
    {
        return new Timer("Scanner-"+__scannerId++, true);
    }

    public void schedule ()
    {
        if (_running)
        {
            if (_timer!=null)
                _timer.cancel();
            if (_task!=null)
                _task.cancel();
            if (getScanInterval() > 0)
            {
                _timer = newTimer();
                _task = newTimerTask();
                _timer.schedule(_task, 1010L*getScanInterval(),1010L*getScanInterval());
            }
        }
    }
    /**
     * Stop the scanning.
     */
    @Override
    public synchronized void doStop()
    {
        if (_running)
        {
            _running = false;
            if (_timer!=null)
                _timer.cancel();
            if (_task!=null)
                _task.cancel();
            _task=null;
            _timer=null;
        }
    }

    /**
     * Perform a pass of the scanner and report changes
     */
    public synchronized void scan ()
    {
        reportScanStart(++_scanCount);
        scanFiles();
        reportDifferences(_currentScan, _prevScan);
        _prevScan.clear();
        _prevScan.putAll(_currentScan);
        reportScanEnd(_scanCount);

        for (Listener l : _listeners)
        {
            try
            {
                if (l instanceof ScanListener)
                    ((ScanListener)l).scan();
            }
            catch (Exception e)
            {
                LOG.warn(e);
            }
            catch (Error e)
            {
                LOG.warn(e);
            }
        }
    }

    /**
     * Recursively scan all files in the designated directories.
     */
    public synchronized void scanFiles ()
    {
        if (_scanDirs==null)
            return;

        _currentScan.clear();
        Iterator<File> itor = _scanDirs.iterator();
        while (itor.hasNext())
        {
            File dir = itor.next();

            if ((dir != null) && (dir.exists()))
                try
                {
                    scanFile(dir.getCanonicalFile(), _currentScan,0);
                }
                catch (IOException e)
                {
                    LOG.warn("Error scanning files.", e);
                }
        }
    }

    /**
     * Report the adds/changes/removes to the registered listeners
     *
     * @param currentScan the info from the most recent pass
     * @param oldScan info from the previous pass
     */
    public synchronized void reportDifferences (Map<String,TimeNSize> currentScan, Map<String,TimeNSize> oldScan)
    {
        // scan the differences and add what was found to the map of notifications:

        Set<String> oldScanKeys = new HashSet<String>(oldScan.keySet());

        // Look for new and changed files
        for (Map.Entry<String, TimeNSize> entry: currentScan.entrySet())
        {
            String file = entry.getKey();
            if (!oldScanKeys.contains(file))
            {
                Notification old=_notifications.put(file,Notification.ADDED);
                if (old!=null)
                {
                    switch(old)
                    {
                        case REMOVED:
                        case CHANGED:
                            _notifications.put(file,Notification.CHANGED);
                    }
                }
            }
            else if (!oldScan.get(file).equals(currentScan.get(file)))
            {
                Notification old=_notifications.put(file,Notification.CHANGED);
                if (old!=null)
                {
                    switch(old)
                    {
                        case ADDED:
                            _notifications.put(file,Notification.ADDED);
                    }
                }
            }
        }

        // Look for deleted files
        for (String file : oldScan.keySet())
        {
            if (!currentScan.containsKey(file))
            {
                Notification old=_notifications.put(file,Notification.REMOVED);
                if (old!=null)
                {
                    switch(old)
                    {
                        case ADDED:
                            _notifications.remove(file);
                    }
                }
            }
        }

        if (LOG.isDebugEnabled())
            LOG.debug("scanned "+_scanDirs+": "+_notifications);

        // Process notifications
        // Only process notifications that are for stable files (ie same in old and current scan).
        List<String> bulkChanges = new ArrayList<String>();
        for (Iterator<Entry<String,Notification>> iter = _notifications.entrySet().iterator();iter.hasNext();)
        {
            Entry<String,Notification> entry=iter.next();
            String file=entry.getKey();

            // Is the file stable?
            if (oldScan.containsKey(file))
            {
                if (!oldScan.get(file).equals(currentScan.get(file)))
                    continue;
            }
            else if (currentScan.containsKey(file))
                continue;

            // File is stable so notify
            Notification notification=entry.getValue();
            iter.remove();
            bulkChanges.add(file);
            switch(notification)
            {
                case ADDED:
                    reportAddition(file);
                    break;
                case CHANGED:
                    reportChange(file);
                    break;
                case REMOVED:
                    reportRemoval(file);
                    break;
            }
        }
        if (!bulkChanges.isEmpty())
            reportBulkChanges(bulkChanges);
    }

    /**
     * Get last modified time on a single file or recurse if
     * the file is a directory.
     * @param f file or directory
     * @param scanInfoMap map of filenames to last modified times
     */
    private void scanFile (File f, Map<String,TimeNSize> scanInfoMap, int depth)
    {
        try
        {
            if (!f.exists())
                return;

            if (f.isFile() || depth>0&& _reportDirs && f.isDirectory())
            {
                if ((_filter == null) || ((_filter != null) && _filter.accept(f.getParentFile(), f.getName())))
                {
                    String name = f.getCanonicalPath();
                    scanInfoMap.put(name, new TimeNSize(f.lastModified(),f.length()));
                }
            }

            // If it is a directory, scan if it is a known directory or the depth is OK.
            if (f.isDirectory() && (depth<_scanDepth || _scanDepth==-1 || _scanDirs.contains(f)))
            {
                File[] files = f.listFiles();
                for (int i=0;i<files.length;i++)
                    scanFile(files[i], scanInfoMap,depth+1);
            }
        }
        catch (IOException e)
        {
            LOG.warn("Error scanning watched files", e);
        }
    }

    private void warn(Object listener,String filename,Throwable th)
    {
        LOG.warn(listener+" failed on '"+filename, th);
    }

    /**
     * Report a file addition to the registered FileAddedListeners
     * @param filename
     */
    private void reportAddition (String filename)
    {
        Iterator<Listener> itor = _listeners.iterator();
        while (itor.hasNext())
        {
            Listener l = itor.next();
            try
            {
                if (l instanceof DiscreteListener)
                    ((DiscreteListener)l).fileAdded(filename);
            }
            catch (Exception e)
            {
                warn(l,filename,e);
            }
            catch (Error e)
            {
                warn(l,filename,e);
            }
        }
    }

    /**
     * Report a file removal to the FileRemovedListeners
     * @param filename
     */
    private void reportRemoval (String filename)
    {
        Iterator<Listener> itor = _listeners.iterator();
        while (itor.hasNext())
        {
            Object l = itor.next();
            try
            {
                if (l instanceof DiscreteListener)
                    ((DiscreteListener)l).fileRemoved(filename);
            }
            catch (Exception e)
            {
                warn(l,filename,e);
            }
            catch (Error e)
            {
                warn(l,filename,e);
            }
        }
    }

    /**
     * Report a file change to the FileChangedListeners
     * @param filename
     */
    private void reportChange (String filename)
    {
        Iterator<Listener> itor = _listeners.iterator();
        while (itor.hasNext())
        {
            Listener l = itor.next();
            try
            {
                if (l instanceof DiscreteListener)
                    ((DiscreteListener)l).fileChanged(filename);
            }
            catch (Exception e)
            {
                warn(l,filename,e);
            }
            catch (Error e)
            {
                warn(l,filename,e);
            }
        }
    }

    private void reportBulkChanges (List<String> filenames)
    {
        Iterator<Listener> itor = _listeners.iterator();
        while (itor.hasNext())
        {
            Listener l = itor.next();
            try
            {
                if (l instanceof BulkListener)
                    ((BulkListener)l).filesChanged(filenames);
            }
            catch (Exception e)
            {
                warn(l,filenames.toString(),e);
            }
            catch (Error e)
            {
                warn(l,filenames.toString(),e);
            }
        }
    }

    /**
     * signal any scan cycle listeners that a scan has started
     */
    private void reportScanStart(int cycle)
    {
        for (Listener listener : _listeners)
        {
            try
            {
                if (listener instanceof ScanCycleListener)
                {
                    ((ScanCycleListener)listener).scanStarted(cycle);
                }
            }
            catch (Exception e)
            {
                LOG.warn(listener + " failed on scan start for cycle " + cycle, e);
            }
        }
    }

    /**
     * sign
     */
    private void reportScanEnd(int cycle)
    {
        for (Listener listener : _listeners)
        {
            try
            {
                if (listener instanceof ScanCycleListener)
                {
                    ((ScanCycleListener)listener).scanEnded(cycle);
                }
            }
            catch (Exception e)
            {
                LOG.warn(listener + " failed on scan end for cycle " + cycle, e);
            }
        }
    }

}