text:
package org.owasp.dependencycheck.analyzer;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.owasp.dependencycheck.Engine;
import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
import org.owasp.dependencycheck.dependency.Confidence;
import org.owasp.dependencycheck.dependency.Dependency;
import org.owasp.dependencycheck.dependency.EvidenceCollection;
import org.owasp.dependencycheck.exception.InitializationException;
import org.owasp.dependencycheck.utils.FileFilterBuilder;
import org.owasp.dependencycheck.utils.FileUtils;
import org.owasp.dependencycheck.utils.Settings;
import org.owasp.dependencycheck.xml.pom.License;
import org.owasp.dependencycheck.xml.pom.Model;
import org.owasp.dependencycheck.xml.pom.PomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Used to load a JAR file and collect information that can be used to determine
 * the associated CPE.
 *
 * @author Jeremy Long
 */
public class JarAnalyzer extends AbstractFileTypeAnalyzer {

    //<editor-fold defaultstate="collapsed" desc="Constants and Member Variables">
    /**
     * The logger.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(JarAnalyzer.class);
    /**
     * The count of directories created during analysis. This is used for
     * creating temporary directories.
     */
    private static final AtomicInteger DIR_COUNT = new AtomicInteger(0);
    /**
     * The system independent newline character.
     */
    private static final String NEWLINE = System.getProperty("line.separator");
    /**
     * A list of values in the manifest to ignore as they only result in false
     * positives.
     */
    private static final Set<String> IGNORE_VALUES = newHashSet(
            "Sun Java System Application Server");
    /**
     * A list of elements in the manifest to ignore.
     */
    private static final Set<String> IGNORE_KEYS = newHashSet(
            "built-by",
            "created-by",
            "builtby",
            "createdby",
            "build-jdk",
            "buildjdk",
            "ant-version",
            "antversion",
            "dynamicimportpackage",
            "dynamicimport-package",
            "dynamic-importpackage",
            "dynamic-import-package",
            "import-package",
            "ignore-package",
            "export-package",
            "importpackage",
            "ignorepackage",
            "exportpackage",
            "sealed",
            "manifest-version",
            "archiver-version",
            "manifestversion",
            "archiverversion",
            "classpath",
            "class-path",
            "tool",
            "bundle-manifestversion",
            "bundlemanifestversion",
            "bundle-vendor",
            "include-resource",
            "embed-dependency",
            "ipojo-components",
            "ipojo-extension",
            "eclipse-sourcereferences");
    /**
     * Deprecated Jar manifest attribute, that is, nonetheless, useful for
     * analysis.
     */
    @SuppressWarnings("deprecation")
    private static final String IMPLEMENTATION_VENDOR_ID = Attributes.Name.IMPLEMENTATION_VENDOR_ID
            .toString();
    /**
     * item in some manifest, should be considered medium confidence.
     */
    private static final String BUNDLE_VERSION = "Bundle-Version"; //: 2.1.2
    /**
     * item in some manifest, should be considered medium confidence.
     */
    private static final String BUNDLE_DESCRIPTION = "Bundle-Description"; //: Apache Struts 2
    /**
     * item in some manifest, should be considered medium confidence.
     */
    private static final String BUNDLE_NAME = "Bundle-Name"; //: Struts 2 Core
    /**
     * A pattern to detect HTML within text.
     */
    private static final Pattern HTML_DETECTION_PATTERN = Pattern.compile("\\<[a-z]+.*/?\\>", Pattern.CASE_INSENSITIVE);
    /**
     * The name of the analyzer.
     */
    private static final String ANALYZER_NAME = "Jar Analyzer";
    /**
     * The phase that this analyzer is intended to run in.
     */
    private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
    /**
     * The set of file extensions supported by this analyzer.
     */
    private static final String[] EXTENSIONS = {"jar", "war"};

    /**
     * The file filter used to determine which files this analyzer supports.
     */
    private static final FileFilter FILTER = FileFilterBuilder.newInstance().addExtensions(EXTENSIONS).build();

    /**
     * The expected first bytes when reading a zip file.
     */
    private static final byte[] ZIP_FIRST_BYTES = new byte[]{0x50, 0x4B, 0x03, 0x04};

    /**
     * The expected first bytes when reading an empty zip file.
     */
    private static final byte[] ZIP_EMPTY_FIRST_BYTES = new byte[]{0x50, 0x4B, 0x05, 0x06};

    /**
     * The expected first bytes when reading a spanned zip file.
     */
    private static final byte[] ZIP_SPANNED_FIRST_BYTES = new byte[]{0x50, 0x4B, 0x07, 0x08};

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc="All standard implmentation details of Analyzer">
    /**
     * Returns the FileFilter.
     *
     * @return the FileFilter
     */
    @Override
    protected FileFilter getFileFilter() {
        return FILTER;
    }

    /**
     * Returns the name of the analyzer.
     *
     * @return the name of the analyzer.
     */
    @Override
    public String getName() {
        return ANALYZER_NAME;
    }

    /**
     * Returns the phase that the analyzer is intended to run in.
     *
     * @return the phase that the analyzer is intended to run in.
     */
    @Override
    public AnalysisPhase getAnalysisPhase() {
        return ANALYSIS_PHASE;
    }
    //</editor-fold>

    /**
     * Returns the key used in the properties file to reference the analyzer's
     * enabled property.
     *
     * @return the analyzer's enabled property setting key
     */
    @Override
    protected String getAnalyzerEnabledSettingKey() {
        return Settings.KEYS.ANALYZER_JAR_ENABLED;
    }

    /**
     * Loads a specified JAR file and collects information from the manifest and
     * checksums to identify the correct CPE information.
     *
     * @param dependency the dependency to analyze.
     * @param engine the engine that is scanning the dependencies
     * @throws AnalysisException is thrown if there is an error reading the JAR
     * file.
     */
    @Override
    public void analyzeDependency(Dependency dependency, Engine engine) throws AnalysisException {
        try {
            final List<ClassNameInformation> classNames = collectClassNames(dependency);
            final String fileName = dependency.getFileName().toLowerCase();
            if (classNames.isEmpty()
                    && (fileName.endsWith("-sources.jar")
                    || fileName.endsWith("-javadoc.jar")
                    || fileName.endsWith("-src.jar")
                    || fileName.endsWith("-doc.jar")
                    || isMacOSMetaDataFile(dependency, engine))
                    || !isZipFile(dependency)) {
                engine.getDependencies().remove(dependency);
                return;
            }
            final boolean hasManifest = parseManifest(dependency, classNames);
            final boolean hasPOM = analyzePOM(dependency, classNames, engine);
            final boolean addPackagesAsEvidence = !(hasManifest && hasPOM);
            analyzePackageNames(classNames, dependency, addPackagesAsEvidence);
        } catch (IOException ex) {
            throw new AnalysisException("Exception occurred reading the JAR file (" + dependency.getFileName() + ").", ex);
        }
    }

    /**
     * Checks if the given dependency appears to be a macOS metadata file, returning true if its filename starts with a
     * ._ prefix and if there is another dependency with the same filename minus the ._ prefix, otherwise it returns
     * false.
     *
     * @param dependency the dependency to check if it's a macOS metadata file
     * @param engine     the engine that is scanning the dependencies
     * @return whether or not the given dependency appears to be a macOS metadata file
     */
    private boolean isMacOSMetaDataFile(final Dependency dependency, final Engine engine) {
        final String fileName = Paths.get(dependency.getActualFilePath()).getFileName().toString();
        return fileName.startsWith("._") && hasDependencyWithFilename(engine.getDependencies(), fileName.substring(2));
    }

    /**
     * Iterates through the given list of dependencies and returns true when it finds a dependency with a filename
     * matching the given filename, otherwise returns false.
     *
     * @param dependencies the dependencies to search within
     * @param fileName     the filename to search for
     * @return whether or not the given dependencies contain a dependency with the given filename
     */
    private boolean hasDependencyWithFilename(final List<Dependency> dependencies, final String fileName) {
        for (final Dependency dependency : dependencies) {
            if (Paths.get(dependency.getActualFilePath()).getFileName().toString().toLowerCase()
                .equals(fileName.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    /**
     * Attempts to read the first bytes of the given dependency (using its actual file path) and returns true if they
     * match the expected first bytes of a zip file, which may be empty or spanned. If they don't match, or if the file
     * could not be read, then it returns false.
     *
     * @param dependency the dependency to check if it's a zip file
     * @return whether or not the given dependency appears to be a zip file from its first bytes
     */
    private boolean isZipFile(final Dependency dependency) {
        final byte[] buffer = new byte[4];
        try (final FileInputStream fileInputStream = new FileInputStream(dependency.getActualFilePath())) {
            fileInputStream.read(buffer);
            if (Arrays.equals(buffer, ZIP_FIRST_BYTES) || Arrays.equals(buffer, ZIP_EMPTY_FIRST_BYTES) ||
                Arrays.equals(buffer, ZIP_SPANNED_FIRST_BYTES)) {
                return true;
            }
        }
        catch (Exception e) {
            LOGGER.warn("Unable to check if '{}' is a zip file", dependency.getActualFilePath());
            LOGGER.trace("", e);
        }
        return false;
    }

    /**
     * Attempts to find a pom.xml within the JAR file. If found it extracts
     * information and adds it to the evidence. This will attempt to interpolate
     * the strings contained within the pom.properties if one exists.
     *
     * @param dependency the dependency being analyzed
     * @param classes a collection of class name information
     * @param engine the analysis engine, used to add additional dependencies
     * @throws AnalysisException is thrown if there is an exception parsing the
     * pom
     * @return whether or not evidence was added to the dependency
     */
    protected boolean analyzePOM(Dependency dependency, List<ClassNameInformation> classes, Engine engine) throws AnalysisException {
        try (JarFile jar = new JarFile(dependency.getActualFilePath())) {
            final List<String> pomEntries = retrievePomListing(jar);
            if (pomEntries != null && pomEntries.size() <= 1) {
                String path;
                File pomFile;
                Properties pomProperties = null;
                if (pomEntries.size() == 1) {
                    path = pomEntries.get(0);
                    pomFile = extractPom(path, jar);
                    pomProperties = retrievePomProperties(path, jar);
                } else {
                    path = FilenameUtils.removeExtension(dependency.getActualFilePath()) + ".pom";
                    pomFile = new File(path);
                }
                if (pomFile.isFile()) {
                    final Model pom = PomUtils.readPom(pomFile);
                    if (pom != null && pomProperties != null) {
                        pom.processProperties(pomProperties);
                    }
                    return pom != null && setPomEvidence(dependency, pom, classes);
                } else {
                    return false;
                }
            }

            //reported possible null dereference on pomEntries is on a non-feasible path
            for (String path : pomEntries) {
                //TODO - one of these is likely the pom for the main JAR we are analyzing
                LOGGER.debug("Reading pom entry: {}", path);
                try {
                    //extract POM to its own directory and add it as its own dependency
                    final Properties pomProperties = retrievePomProperties(path, jar);
                    final File pomFile = extractPom(path, jar);
                    final Model pom = PomUtils.readPom(pomFile);
                    pom.processProperties(pomProperties);

                    final String displayPath = String.format("%s%s%s",
                            dependency.getFilePath(),
                            File.separator,
                            path);
                    final String displayName = String.format("%s%s%s",
                            dependency.getFileName(),
                            File.separator,
                            path);
                    final Dependency newDependency = new Dependency();
                    newDependency.setActualFilePath(pomFile.getAbsolutePath());
                    newDependency.setFileName(displayName);
                    newDependency.setFilePath(displayPath);
                    setPomEvidence(newDependency, pom, null);
                    engine.getDependencies().add(newDependency);
                } catch (AnalysisException ex) {
                    LOGGER.warn("An error occurred while analyzing '{}'.", dependency.getActualFilePath());
                    LOGGER.trace("", ex);
                }
            }
        } catch (IOException ex) {
            LOGGER.warn("Unable to read JarFile '{}'.", dependency.getActualFilePath());
            LOGGER.trace("", ex);
        }
        return false;
    }

    /**
     * Given a path to a pom.xml within a JarFile, this method attempts to load
     * a sibling pom.properties if one exists.
     *
     * @param path the path to the pom.xml within the JarFile
     * @param jar the JarFile to load the pom.properties from
     * @return a Properties object or null if no pom.properties was found
     */
    private Properties retrievePomProperties(String path, final JarFile jar) {
        Properties pomProperties = null;
        final String propPath = path.substring(0, path.length() - 7) + "pom.properies";
        final ZipEntry propEntry = jar.getEntry(propPath);
        if (propEntry != null) {
            try (Reader reader = new InputStreamReader(jar.getInputStream(propEntry), "UTF-8")) {
                pomProperties = new Properties();
                pomProperties.load(reader);
                LOGGER.debug("Read pom.properties: {}", propPath);
            } catch (UnsupportedEncodingException ex) {
                LOGGER.trace("UTF-8 is not supported", ex);
            } catch (IOException ex) {
                LOGGER.trace("Unable to read the POM properties", ex);
            }
        }
        return pomProperties;
    }

    /**
     * Searches a JarFile for pom.xml entries and returns a listing of these
     * entries.
     *
     * @param jar the JarFile to search
     * @return a list of pom.xml entries
     * @throws IOException thrown if there is an exception reading a JarEntry
     */
    private List<String> retrievePomListing(final JarFile jar) throws IOException {
        final List<String> pomEntries = new ArrayList<>();
        final Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            final String entryName = (new File(entry.getName())).getName().toLowerCase();
            if (!entry.isDirectory() && "pom.xml".equals(entryName)) {
                LOGGER.trace("POM Entry found: {}", entry.getName());
                pomEntries.add(entry.getName());
            }
        }
        return pomEntries;
    }

    /**
     * Retrieves the specified POM from a jar.
     *
     * @param path the path to the pom.xml file within the jar file
     * @param jar the jar file to extract the pom from
     * @return returns the POM file
     * @throws AnalysisException is thrown if there is an exception extracting
     * the file
     */
    private File extractPom(String path, JarFile jar) throws AnalysisException {
        final File tmpDir = getNextTempDirectory();
        final File file = new File(tmpDir, "pom.xml");
        final ZipEntry entry = jar.getEntry(path);
        if (entry == null) {
            throw new AnalysisException(String.format("Pom (%s) does not exist in %s", path, jar.getName()));
        }
        try (InputStream input = jar.getInputStream(entry);
                FileOutputStream fos = new FileOutputStream(file)) {
            IOUtils.copy(input, fos);
        } catch (IOException ex) {
            LOGGER.warn("An error occurred reading '{}' from '{}'.", path, jar.getName());
            LOGGER.error("", ex);
        }
        return file;
    }

    /**
     * Sets evidence from the pom on the supplied dependency.
     *
     * @param dependency the dependency to set data on
     * @param pom the information from the pom
     * @param classes a collection of ClassNameInformation - containing data
     * about the fully qualified class names within the JAR file being analyzed
     * @return true if there was evidence within the pom that we could use;
     * otherwise false
     */
    public static boolean setPomEvidence(Dependency dependency, Model pom, List<ClassNameInformation> classes) {
        if (pom == null) {
            return false;
        }
        boolean foundSomething = false;
        boolean addAsIdentifier = true;
        String groupid = pom.getGroupId();
        String parentGroupId = pom.getParentGroupId();
        String artifactid = pom.getArtifactId();
        String parentArtifactId = pom.getParentArtifactId();
        String version = pom.getVersion();
        String parentVersion = pom.getParentVersion();

        if ("org.sonatype.oss".equals(parentGroupId) && "oss-parent".equals(parentArtifactId)) {
            parentGroupId = null;
            parentArtifactId = null;
            parentVersion = null;
        }

        if ((groupid == null || groupid.isEmpty()) && parentGroupId != null && !parentGroupId.isEmpty()) {
            groupid = parentGroupId;
        }

        final String originalGroupID = groupid;
        if (groupid != null && (groupid.startsWith("org.") || groupid.startsWith("com."))) {
            groupid = groupid.substring(4);
        }

        if ((artifactid == null || artifactid.isEmpty()) && parentArtifactId != null && !parentArtifactId.isEmpty()) {
            artifactid = parentArtifactId;
        }

        final String originalArtifactID = artifactid;
        if (artifactid != null && (artifactid.startsWith("org.") || artifactid.startsWith("com."))) {
            artifactid = artifactid.substring(4);
        }

        if ((version == null || version.isEmpty()) && parentVersion != null && !parentVersion.isEmpty()) {
            version = parentVersion;
        }

        if (groupid != null && !groupid.isEmpty()) {
            foundSomething = true;
            dependency.getVendorEvidence().addEvidence("pom", "groupid", groupid, Confidence.HIGHEST);
            dependency.getProductEvidence().addEvidence("pom", "groupid", groupid, Confidence.LOW);
            addMatchingValues(classes, groupid, dependency.getVendorEvidence());
            addMatchingValues(classes, groupid, dependency.getProductEvidence());
            if (parentGroupId != null && !parentGroupId.isEmpty() && !parentGroupId.equals(groupid)) {
                dependency.getVendorEvidence().addEvidence("pom", "parent-groupid", parentGroupId, Confidence.MEDIUM);
                dependency.getProductEvidence().addEvidence("pom", "parent-groupid", parentGroupId, Confidence.LOW);
                addMatchingValues(classes, parentGroupId, dependency.getVendorEvidence());
                addMatchingValues(classes, parentGroupId, dependency.getProductEvidence());
            }
        } else {
            addAsIdentifier = false;
        }

        if (artifactid != null && !artifactid.isEmpty()) {
            foundSomething = true;
            dependency.getProductEvidence().addEvidence("pom", "artifactid", artifactid, Confidence.HIGHEST);
            dependency.getVendorEvidence().addEvidence("pom", "artifactid", artifactid, Confidence.LOW);
            addMatchingValues(classes, artifactid, dependency.getVendorEvidence());
            addMatchingValues(classes, artifactid, dependency.getProductEvidence());
            if (parentArtifactId != null && !parentArtifactId.isEmpty() && !parentArtifactId.equals(artifactid)) {
                dependency.getProductEvidence().addEvidence("pom", "parent-artifactid", parentArtifactId, Confidence.MEDIUM);
                dependency.getVendorEvidence().addEvidence("pom", "parent-artifactid", parentArtifactId, Confidence.LOW);
                addMatchingValues(classes, parentArtifactId, dependency.getVendorEvidence());
                addMatchingValues(classes, parentArtifactId, dependency.getProductEvidence());
            }
        } else {
            addAsIdentifier = false;
        }

        if (version != null && !version.isEmpty()) {
            foundSomething = true;
            dependency.getVersionEvidence().addEvidence("pom", "version", version, Confidence.HIGHEST);
            if (parentVersion != null && !parentVersion.isEmpty() && !parentVersion.equals(version)) {
                dependency.getVersionEvidence().addEvidence("pom", "parent-version", version, Confidence.LOW);
            }
        } else {
            addAsIdentifier = false;
        }

        if (addAsIdentifier) {
            dependency.addIdentifier("maven", String.format("%s:%s:%s", originalGroupID, originalArtifactID, version), null, Confidence.HIGH);
        }

        // org name
        final String org = pom.getOrganization();
        if (org != null && !org.isEmpty()) {
            dependency.getVendorEvidence().addEvidence("pom", "organization name", org, Confidence.HIGH);
            dependency.getProductEvidence().addEvidence("pom", "organization name", org, Confidence.LOW);
            addMatchingValues(classes, org, dependency.getVendorEvidence());
            addMatchingValues(classes, org, dependency.getProductEvidence());
        }
        // org name
        final String orgUrl = pom.getOrganizationUrl();
        if (orgUrl != null && !orgUrl.isEmpty()) {
            dependency.getVendorEvidence().addEvidence("pom", "organization url", orgUrl, Confidence.MEDIUM);
            dependency.getProductEvidence().addEvidence("pom", "organization url", orgUrl, Confidence.LOW);
        }
        //pom name
        final String pomName = pom.getName();
        if (pomName
                != null && !pomName.isEmpty()) {
            foundSomething = true;
            dependency.getProductEvidence().addEvidence("pom", "name", pomName, Confidence.HIGH);
            dependency.getVendorEvidence().addEvidence("pom", "name", pomName, Confidence.HIGH);
            addMatchingValues(classes, pomName, dependency.getVendorEvidence());
            addMatchingValues(classes, pomName, dependency.getProductEvidence());
        }

        //Description
        final String description = pom.getDescription();
        if (description != null && !description.isEmpty() && !description.startsWith("POM was created by")) {
            foundSomething = true;
            final String trimmedDescription = addDescription(dependency, description, "pom", "description");
            addMatchingValues(classes, trimmedDescription, dependency.getVendorEvidence());
            addMatchingValues(classes, trimmedDescription, dependency.getProductEvidence());
        }

        final String projectURL = pom.getProjectURL();
        if (projectURL != null && !projectURL.trim().isEmpty()) {
            dependency.getVendorEvidence().addEvidence("pom", "url", projectURL, Confidence.HIGHEST);
        }

        extractLicense(pom, dependency);
        return foundSomething;
    }

    /**
     * Analyzes the path information of the classes contained within the
     * JarAnalyzer to try and determine possible vendor or product names. If any
     * are found they are stored in the packageVendor and packageProduct
     * hashSets.
     *
     * @param classNames a list of class names
     * @param dependency a dependency to analyze
     * @param addPackagesAsEvidence a flag indicating whether or not package
     * names should be added as evidence.
     */
    protected void analyzePackageNames(List<ClassNameInformation> classNames,
            Dependency dependency, boolean addPackagesAsEvidence) {
        final Map<String, Integer> vendorIdentifiers = new HashMap<>();
        final Map<String, Integer> productIdentifiers = new HashMap<>();
        analyzeFullyQualifiedClassNames(classNames, vendorIdentifiers, productIdentifiers);

        final int classCount = classNames.size();
        final EvidenceCollection vendor = dependency.getVendorEvidence();
        final EvidenceCollection product = dependency.getProductEvidence();

        for (Map.Entry<String, Integer> entry : vendorIdentifiers.entrySet()) {
            final float ratio = entry.getValue() / (float) classCount;
            if (ratio > 0.5) {
                //TODO remove weighting
                vendor.addWeighting(entry.getKey());
                if (addPackagesAsEvidence && entry.getKey().length() > 1) {
                    vendor.addEvidence("jar", "package name", entry.getKey(), Confidence.LOW);
                }
            }
        }
        for (Map.Entry<String, Integer> entry : productIdentifiers.entrySet()) {
            final float ratio = entry.getValue() / (float) classCount;
            if (ratio > 0.5) {
                product.addWeighting(entry.getKey());
                if (addPackagesAsEvidence && entry.getKey().length() > 1) {
                    product.addEvidence("jar", "package name", entry.getKey(), Confidence.LOW);
                }
            }
        }
    }

    /**
     * <p>
     * Reads the manifest from the JAR file and collects the entries. Some
     * vendorKey entries are:</p>
     * <ul><li>Implementation Title</li>
     * <li>Implementation Version</li> <li>Implementation Vendor</li>
     * <li>Implementation VendorId</li> <li>Bundle Name</li> <li>Bundle
     * Version</li> <li>Bundle Vendor</li> <li>Bundle Description</li> <li>Main
     * Class</li> </ul>
     * However, all but a handful of specific entries are read in.
     *
     * @param dependency A reference to the dependency
     * @param classInformation a collection of class information
     * @return whether evidence was identified parsing the manifest
     * @throws IOException if there is an issue reading the JAR file
     */
    protected boolean parseManifest(Dependency dependency, List<ClassNameInformation> classInformation)
            throws IOException {
        boolean foundSomething = false;
        try (JarFile jar = new JarFile(dependency.getActualFilePath())) {
            final Manifest manifest = jar.getManifest();
            if (manifest == null) {
                if (!dependency.getFileName().toLowerCase().endsWith("-sources.jar")
                        && !dependency.getFileName().toLowerCase().endsWith("-javadoc.jar")
                        && !dependency.getFileName().toLowerCase().endsWith("-src.jar")
                        && !dependency.getFileName().toLowerCase().endsWith("-doc.jar")) {
                    LOGGER.debug("Jar file '{}' does not contain a manifest.",
                            dependency.getFileName());
                }
                return false;
            }
            final EvidenceCollection vendorEvidence = dependency.getVendorEvidence();
            final EvidenceCollection productEvidence = dependency.getProductEvidence();
            final EvidenceCollection versionEvidence = dependency.getVersionEvidence();
            String source = "Manifest";
            String specificationVersion = null;
            boolean hasImplementationVersion = false;
            Attributes atts = manifest.getMainAttributes();
            for (Entry<Object, Object> entry : atts.entrySet()) {
                String key = entry.getKey().toString();
                String value = atts.getValue(key);
                if (HTML_DETECTION_PATTERN.matcher(value).find()) {
                    value = Jsoup.parse(value).text();
                }
                if (IGNORE_VALUES.contains(value)) {
                    continue;
                } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_TITLE.toString())) {
                    foundSomething = true;
                    productEvidence.addEvidence(source, key, value, Confidence.HIGH);
                    addMatchingValues(classInformation, value, productEvidence);
                } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VERSION.toString())) {
                    hasImplementationVersion = true;
                    foundSomething = true;
                    versionEvidence.addEvidence(source, key, value, Confidence.HIGH);
                } else if ("specification-version".equalsIgnoreCase(key)) {
                    specificationVersion = value;
                } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR.toString())) {
                    foundSomething = true;
                    vendorEvidence.addEvidence(source, key, value, Confidence.HIGH);
                    addMatchingValues(classInformation, value, vendorEvidence);
                } else if (key.equalsIgnoreCase(IMPLEMENTATION_VENDOR_ID)) {
                    foundSomething = true;
                    vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                    addMatchingValues(classInformation, value, vendorEvidence);
                } else if (key.equalsIgnoreCase(BUNDLE_DESCRIPTION)) {
                    foundSomething = true;
                    addDescription(dependency, value, "manifest", key);
                    addMatchingValues(classInformation, value, productEvidence);
                } else if (key.equalsIgnoreCase(BUNDLE_NAME)) {
                    foundSomething = true;
                    productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                    addMatchingValues(classInformation, value, productEvidence);
//                //the following caused false positives.
//                } else if (key.equalsIgnoreCase(BUNDLE_VENDOR)) {
                } else if (key.equalsIgnoreCase(BUNDLE_VERSION)) {
                    foundSomething = true;
                    versionEvidence.addEvidence(source, key, value, Confidence.HIGH);
                } else if (key.equalsIgnoreCase(Attributes.Name.MAIN_CLASS.toString())) {
                    continue;
                    //skipping main class as if this has important information to add it will be added during class name analysis...
                } else {
                    key = key.toLowerCase();
                    if (!IGNORE_KEYS.contains(key)
                            && !key.endsWith("jdk")
                            && !key.contains("lastmodified")
                            && !key.endsWith("package")
                            && !key.endsWith("classpath")
                            && !key.endsWith("class-path")
                            && !key.endsWith("-scm") //todo change this to a regex?
                            && !key.startsWith("scm-")
                            && !value.trim().startsWith("scm:")
                            && !isImportPackage(key, value)
                            && !isPackage(key, value)) {
                        foundSomething = true;
                        if (key.contains("version")) {
                            if (!key.contains("specification")) {
                                versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                            }
                        } else if ("build-id".equals(key)) {
                            int pos = value.indexOf('(');
                            if (pos > 0) {
                                value = value.substring(0, pos - 1);
                            }
                            pos = value.indexOf('[');
                            if (pos > 0) {
                                value = value.substring(0, pos - 1);
                            }
                            versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                        } else if (key.contains("title")) {
                            productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                            addMatchingValues(classInformation, value, productEvidence);
                        } else if (key.contains("vendor")) {
                            if (key.contains("specification")) {
                                vendorEvidence.addEvidence(source, key, value, Confidence.LOW);
                            } else {
                                vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                                addMatchingValues(classInformation, value, vendorEvidence);
                            }
                        } else if (key.contains("name")) {
                            productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                            vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                            addMatchingValues(classInformation, value, vendorEvidence);
                            addMatchingValues(classInformation, value, productEvidence);
                        } else if (key.contains("license")) {
                            addLicense(dependency, value);
                        } else if (key.contains("description")) {
                            addDescription(dependency, value, "manifest", key);
                        } else {
                            productEvidence.addEvidence(source, key, value, Confidence.LOW);
                            vendorEvidence.addEvidence(source, key, value, Confidence.LOW);
                            addMatchingValues(classInformation, value, vendorEvidence);
                            addMatchingValues(classInformation, value, productEvidence);
                            if (value.matches(".*\\d.*")) {
                                final StringTokenizer tokenizer = new StringTokenizer(value, " ");
                                while (tokenizer.hasMoreElements()) {
                                    final String s = tokenizer.nextToken();
                                    if (s.matches("^[0-9.]+$")) {
                                        versionEvidence.addEvidence(source, key, s, Confidence.LOW);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            for (Map.Entry<String, Attributes> item : manifest.getEntries().entrySet()) {
                final String name = item.getKey();
                source = "manifest: " + name;
                atts = item.getValue();
                for (Entry<Object, Object> entry : atts.entrySet()) {
                    final String key = entry.getKey().toString();
                    final String value = atts.getValue(key);
                    if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_TITLE.toString())) {
                        foundSomething = true;
                        productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                        addMatchingValues(classInformation, value, productEvidence);
                    } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VERSION.toString())) {
                        foundSomething = true;
                        versionEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                    } else if (key.equalsIgnoreCase(Attributes.Name.IMPLEMENTATION_VENDOR.toString())) {
                        foundSomething = true;
                        vendorEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                        addMatchingValues(classInformation, value, vendorEvidence);
                    } else if (key.equalsIgnoreCase(Attributes.Name.SPECIFICATION_TITLE.toString())) {
                        foundSomething = true;
                        productEvidence.addEvidence(source, key, value, Confidence.MEDIUM);
                        addMatchingValues(classInformation, value, productEvidence);
                    }
                }
            }
            if (specificationVersion != null && !hasImplementationVersion) {
                foundSomething = true;
                versionEvidence.addEvidence(source, "specification-version", specificationVersion, Confidence.HIGH);
            }
        }
        return foundSomething;
    }

    /**
     * Adds a description to the given dependency. If the description contains
     * one of the following strings beyond 100 characters, then the description
     * used will be trimmed to that position:
     * <ul><li>"such as"</li><li>"like "</li><li>"will use "</li><li>"* uses
     * "</li></ul>
     *
     * @param dependency a dependency
     * @param description the description
     * @param source the source of the evidence
     * @param key the "name" of the evidence
     * @return if the description is trimmed, the trimmed version is returned;
     * otherwise the original description is returned
     */
    public static String addDescription(Dependency dependency, String description, String source, String key) {
        if (dependency.getDescription() == null) {
            dependency.setDescription(description);
        }
        String desc;
        if (HTML_DETECTION_PATTERN.matcher(description).find()) {
            desc = Jsoup.parse(description).text();
        } else {
            desc = description;
        }
        dependency.setDescription(desc);
        if (desc.length() > 100) {
            desc = desc.replaceAll("\\s\\s+", " ");
            final int posSuchAs = desc.toLowerCase().indexOf("such as ", 100);
            final int posLike = desc.toLowerCase().indexOf("like ", 100);
            final int posWillUse = desc.toLowerCase().indexOf("will use ", 100);
            final int posUses = desc.toLowerCase().indexOf(" uses ", 100);
            int pos = -1;
            pos = Math.max(pos, posSuchAs);
            if (pos >= 0 && posLike >= 0) {
                pos = Math.min(pos, posLike);
            } else {
                pos = Math.max(pos, posLike);
            }
            if (pos >= 0 && posWillUse >= 0) {
                pos = Math.min(pos, posWillUse);
            } else {
                pos = Math.max(pos, posWillUse);
            }
            if (pos >= 0 && posUses >= 0) {
                pos = Math.min(pos, posUses);
            } else {
                pos = Math.max(pos, posUses);
            }

            if (pos > 0) {
                desc = desc.substring(0, pos) + "...";
            }
            dependency.getProductEvidence().addEvidence(source, key, desc, Confidence.LOW);
            dependency.getVendorEvidence().addEvidence(source, key, desc, Confidence.LOW);
        } else {
            dependency.getProductEvidence().addEvidence(source, key, desc, Confidence.MEDIUM);
            dependency.getVendorEvidence().addEvidence(source, key, desc, Confidence.MEDIUM);
        }
        return desc;
    }

    /**
     * Adds a license to the given dependency.
     *
     * @param d a dependency
     * @param license the license
     */
    private void addLicense(Dependency d, String license) {
        if (d.getLicense() == null) {
            d.setLicense(license);
        } else if (!d.getLicense().contains(license)) {
            d.setLicense(d.getLicense() + NEWLINE + license);
        }
    }

    /**
     * The parent directory for the individual directories per archive.
     */
    private File tempFileLocation = null;

    /**
     * Initializes the JarAnalyzer.
     *
     * @throws InitializationException is thrown if there is an exception
     * creating a temporary directory
     */
    @Override
    public void initializeFileTypeAnalyzer() throws InitializationException {
        try {
            final File baseDir = Settings.getTempDirectory();
            tempFileLocation = File.createTempFile("check", "tmp", baseDir);
            if (!tempFileLocation.delete()) {
                final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
                setEnabled(false);
                throw new InitializationException(msg);
            }
            if (!tempFileLocation.mkdirs()) {
                final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath());
                setEnabled(false);
                throw new InitializationException(msg);
            }
        } catch (IOException ex) {
            setEnabled(false);
            throw new InitializationException("Unable to create a temporary file", ex);
        }
    }

    /**
     * Deletes any files extracted from the JAR during analysis.
     */
    @Override
    public void closeAnalyzer() {
        if (tempFileLocation != null && tempFileLocation.exists()) {
            LOGGER.debug("Attempting to delete temporary files");
            final boolean success = FileUtils.delete(tempFileLocation);
            if (!success && tempFileLocation.exists()) {
                final String[] l = tempFileLocation.list();
                if (l != null && l.length > 0) {
                    LOGGER.warn("Failed to delete some temporary files, see the log for more details");
                }
            }
        }
    }

    /**
     * Determines if the key value pair from the manifest is for an "import"
     * type entry for package names.
     *
     * @param key the key from the manifest
     * @param value the value from the manifest
     * @return true or false depending on if it is believed the entry is an
     * "import" entry
     */
    private boolean isImportPackage(String key, String value) {
        final Pattern packageRx = Pattern.compile("^([a-zA-Z0-9_#\\$\\*\\.]+\\s*[,;]\\s*)+([a-zA-Z0-9_#\\$\\*\\.]+\\s*)?$");
        final boolean matches = packageRx.matcher(value).matches();
        return matches && (key.contains("import") || key.contains("include") || value.length() > 10);
    }

    /**
     * Cycles through an enumeration of JarEntries, contained within the
     * dependency, and returns a list of the class names. This does not include
     * core Java package names (i.e. java.* or javax.*).
     *
     * @param dependency the dependency being analyzed
     * @return an list of fully qualified class names
     */
    private List<ClassNameInformation> collectClassNames(Dependency dependency) {
        final List<ClassNameInformation> classNames = new ArrayList<>();
        try (JarFile jar = new JarFile(dependency.getActualFilePath())) {
            final Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                final JarEntry entry = entries.nextElement();
                final String name = entry.getName().toLowerCase();
                //no longer stripping "|com\\.sun" - there are some com.sun jar files with CVEs.
                if (name.endsWith(".class") && !name.matches("^javax?\\..*$")) {
                    final ClassNameInformation className = new ClassNameInformation(name.substring(0, name.length() - 6));
                    classNames.add(className);
                }
            }
        } catch (IOException ex) {
            LOGGER.warn("Unable to open jar file '{}'.", dependency.getFileName());
            LOGGER.debug("", ex);
        }
        return classNames;
    }

    /**
     * Cycles through the list of class names and places the package levels 0-3
     * into the provided maps for vendor and product. This is helpful when
     * analyzing vendor/product as many times this is included in the package
     * name.
     *
     * @param classNames a list of class names
     * @param vendor HashMap of possible vendor names from package names (e.g.
     * owasp)
     * @param product HashMap of possible product names from package names (e.g.
     * dependencycheck)
     */
    private void analyzeFullyQualifiedClassNames(List<ClassNameInformation> classNames,
            Map<String, Integer> vendor, Map<String, Integer> product) {
        for (ClassNameInformation entry : classNames) {
            final List<String> list = entry.getPackageStructure();
            addEntry(vendor, list.get(0));

            if (list.size() == 2) {
                addEntry(product, list.get(1));
            } else if (list.size() == 3) {
                addEntry(vendor, list.get(1));
                addEntry(product, list.get(1));
                addEntry(product, list.get(2));
            } else if (list.size() >= 4) {
                addEntry(vendor, list.get(1));
                addEntry(vendor, list.get(2));
                addEntry(product, list.get(1));
                addEntry(product, list.get(2));
                addEntry(product, list.get(3));
            }
        }
    }

    /**
     * Adds an entry to the specified collection and sets the Integer (e.g. the
     * count) to 1. If the entry already exists in the collection then the
     * Integer is incremented by 1.
     *
     * @param collection a collection of strings and their occurrence count
     * @param key the key to add to the collection
     */
    private void addEntry(Map<String, Integer> collection, String key) {
        if (collection.containsKey(key)) {
            collection.put(key, collection.get(key) + 1);
        } else {
            collection.put(key, 1);
        }
    }

    /**
     * Cycles through the collection of class name information to see if parts
     * of the package names are contained in the provided value. If found, it
     * will be added as the HIGHEST confidence evidence because we have more
     * then one source corroborating the value.
     *
     * @param classes a collection of class name information
     * @param value the value to check to see if it contains a package name
     * @param evidence the evidence collection to add new entries too
     */
    private static void addMatchingValues(List<ClassNameInformation> classes, String value, EvidenceCollection evidence) {
        if (value == null || value.isEmpty() || classes == null || classes.isEmpty()) {
            return;
        }
        final String text = value.toLowerCase();
        for (ClassNameInformation cni : classes) {
            for (String key : cni.getPackageStructure()) {
                final Pattern p = Pattern.compile("\b" + key + "\b");
                if (p.matcher(text).find()) {
                    //if (text.contains(key)) { //note, package structure elements are already lowercase.
                    evidence.addEvidence("jar", "package name", key, Confidence.HIGHEST);
                }
            }
        }
    }

    /**
     * Simple check to see if the attribute from a manifest is just a package
     * name.
     *
     * @param key the key of the value to check
     * @param value the value to check
     * @return true if the value looks like a java package name, otherwise false
     */
    private boolean isPackage(String key, String value) {

        return !key.matches(".*(version|title|vendor|name|license|description).*")
                && value.matches("^([a-zA-Z_][a-zA-Z0-9_\\$]*(\\.[a-zA-Z_][a-zA-Z0-9_\\$]*)*)?$");

    }

    /**
     * Extracts the license information from the pom and adds it to the
     * dependency.
     *
     * @param pom the pom object
     * @param dependency the dependency to add license information too
     */
    public static void extractLicense(Model pom, Dependency dependency) {
        //license
        if (pom.getLicenses() != null) {
            String license = null;
            for (License lic : pom.getLicenses()) {
                String tmp = null;
                if (lic.getName() != null) {
                    tmp = lic.getName();
                }
                if (lic.getUrl() != null) {
                    if (tmp == null) {
                        tmp = lic.getUrl();
                    } else {
                        tmp += ": " + lic.getUrl();
                    }
                }
                if (tmp == null) {
                    continue;
                }
                if (HTML_DETECTION_PATTERN.matcher(tmp).find()) {
                    tmp = Jsoup.parse(tmp).text();
                }
                if (license == null) {
                    license = tmp;
                } else {
                    license += "\n" + tmp;
                }
            }
            if (license != null) {
                dependency.setLicense(license);

            }
        }
    }

    /**
     * Stores information about a class name.
     */
    protected static class ClassNameInformation {

        /**
         * The fully qualified class name.
         */
        private String name;
        /**
         * Up to the first four levels of the package structure, excluding a
         * leading "org" or "com".
         */
        private final ArrayList<String> packageStructure = new ArrayList<>();

        /**
         * <p>
         * Stores information about a given class name. This class will keep the
         * fully qualified class name and a list of the important parts of the
         * package structure. Up to the first four levels of the package
         * structure are stored, excluding a leading "org" or "com".
         * Example:</p>
         * <code>ClassNameInformation obj = new ClassNameInformation("org/owasp/dependencycheck/analyzer/JarAnalyzer");
         * System.out.println(obj.getName());
         * for (String p : obj.getPackageStructure())
         *     System.out.println(p);
         * </code>
         * <p>
         * Would result in:</p>
         * <code>org.owasp.dependencycheck.analyzer.JarAnalyzer
         * owasp
         * dependencycheck
         * analyzer
         * jaranalyzer</code>
         *
         * @param className a fully qualified class name
         */
        ClassNameInformation(String className) {
            name = className;
            if (name.contains("/")) {
                final String[] tmp = StringUtils.split(className.toLowerCase(), '/');
                int start = 0;
                int end = 3;
                if ("com".equals(tmp[0]) || "org".equals(tmp[0])) {
                    start = 1;
                    end = 4;
                }
                if (tmp.length <= end) {
                    end = tmp.length - 1;
                }
                packageStructure.addAll(Arrays.asList(tmp).subList(start, end + 1));
            } else {
                packageStructure.add(name);
            }
        }

        /**
         * Get the value of name
         *
         * @return the value of name
         */
        public String getName() {
            return name;
        }

        /**
         * Set the value of name
         *
         * @param name new value of name
         */
        public void setName(String name) {
            this.name = name;
        }

        /**
         * Get the value of packageStructure
         *
         * @return the value of packageStructure
         */
        public ArrayList<String> getPackageStructure() {
            return packageStructure;
        }
    }

    /**
     * Retrieves the next temporary directory to extract an archive too.
     *
     * @return a directory
     * @throws AnalysisException thrown if unable to create temporary directory
     */
    private File getNextTempDirectory() throws AnalysisException {
        final int dirCount = DIR_COUNT.incrementAndGet();
        final File directory = new File(tempFileLocation, String.valueOf(dirCount));
        //getting an exception for some directories not being able to be created; might be because the directory already exists?
        if (directory.exists()) {
            return getNextTempDirectory();
        }
        if (!directory.mkdirs()) {
            final String msg = String.format("Unable to create temp directory '%s'.", directory.getAbsolutePath());
            throw new AnalysisException(msg);
        }
        return directory;
    }
}


text:
survana-tools
=============

A set of tools for Survana.

export-qualtrics
----------------
Exports Survana forms to Qualtrics TXT format suitable for import.

* Prerequisites: Go 1.x
* Testing: Tested on OS X.

Installation

```
cd export-qualtrics
make
./bin/export-qualtrics -i dump.json -o results
```

Usage:

* `-i` - input database dump
* `-o` - output directory
* `-f` - comma-separated list of form IDs
* `-g` - output debug information
* `-v` - output more information
* `--help` - output the help page

To import the TXT file into Qualtrics, follow this guide: http://www.qualtrics.com/university/researchsuite/advanced-building/advanced-options-drop-down/import-and-export-surveys/

export-responses
----------------

Prerequisites: easy_install xlsxwriter

redpajama_set_name: RedPajamaGithub


text:
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using DocumentFormat.OpenXml.Framework;
using DocumentFormat.OpenXml.Framework.Metadata;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace DocumentFormat.OpenXml.Features
{
    internal class ReflectionBasedRootElementFactory : IRootElementFactory
    {
        private readonly Lazy<ElementFactoryCollection> _factoryCollection;

        public ReflectionBasedRootElementFactory()
            : this(typeof(ReflectionBasedRootElementFactory).GetTypeInfo().Assembly, ClassActivator<OpenXmlElement>.CreateActivator)
        {
        }

        public ReflectionBasedRootElementFactory(Assembly assembly, Func<Type, Func<OpenXmlElement>> activatorFactory)
        {
            _factoryCollection = new Lazy<ElementFactoryCollection>(() => CreatePartLookup(assembly, activatorFactory), isThreadSafe: true);
        }

        private static ElementFactoryCollection CreatePartLookup(Assembly assembly, Func<Type, Func<OpenXmlElement>> activatorFactory)
        {
            List<ElementFactory>? lookup = null;

            foreach (var child in GetAllRootElements(assembly))
            {
                if (lookup is null)
                {
                    lookup = new List<ElementFactory>();
                }

                var key = ElementFactory.Create(child, activatorFactory(child));

                lookup.Add(key);
            }

            if (lookup is null)
            {
                return ElementFactoryCollection.Empty;
            }

            return new ElementFactoryCollection(lookup);
        }

        private static IEnumerable<Type> GetAllRootElements(Assembly assembly)
        {
            var types = assembly.GetTypes();

            foreach (var elementType in types)
            {
                if (!elementType.GetTypeInfo().IsAbstract && typeof(OpenXmlPartRootElement).GetTypeInfo().IsAssignableFrom(elementType.GetTypeInfo()))
                {
                    yield return elementType;
                }
            }
        }

        public ElementFactoryCollection Collection => _factoryCollection.Value;

        public bool TryCreate(in OpenXmlQualifiedName qname, [NotNullWhen(true)] out OpenXmlElement? element)
        {
            element = _factoryCollection.Value.Create(in qname);
            return element is not null;
        }
    }
}


text:
package com.github.texxel.data;

/**
 *
Classes implementing this are able to be saved into a data stream. In order for classes to be
 * restored, the class must implement at least one of the following (first implementation listed will be used):

 *

 *
A constructor that accepts a single DataIn object

 *
A no arg constructor

 *

 *
DataSerializable's cannot be inner classes (unless static) or anonymous classes. The constructors can have any
 * access level To stop IDEs complaining that the constructor is unused, the {@link Constructor} annotation is provided

 */
public interface DataSerializable {

    /**
     * Stores the data for this object into a bundle so it can be reloaded latter. The passed bundle
     * is an empty bundle that this object should write all its data into
     * @param data the Data bundle to save data to
     */
    void bundleInto(DataOut data);

}


text:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Dynamic;
using System.Linq;

namespace Databind.Binding
{
    public static partial class DataBinder
    {
        #region Single instance binding

        /// <summary>
        /// Bind single DataRow to dynamic object
        /// </summary>
        /// <param name="dataRow">DataRow to be transformad to an object</param>
        /// <returns>Dynamic object instance</returns>
        public static dynamic BindDynamic(DataRow dataRow)
        {
            dynamic result = null;
            if (dataRow != null)
            {
                result = new ExpandoObject();
                var resultDictionary = (IDictionary<string, object>)result;
                foreach (DataColumn column in dataRow.Table.Columns)
                {
                    var dataValue = dataRow[column.ColumnName];
                    resultDictionary.Add(column.ColumnName, DBNull.Value.Equals(dataValue) ? null : dataValue);
                }
            }
            return result;
        }

        /// <summary>
        /// Bind first SqlDataReader record to dynamic object
        /// </summary>
        /// <param name="dataReader">SqlDataReader to acquire data from</param>
        /// <returns>Dynamic object instance</returns>
        public static dynamic BindDynamic(SqlDataReader dataReader)
        {
            var models = BindDynamics(dataReader, true);
            if (models != null && models.Any())
            {
                return models.FirstOrDefault();
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Bind first DataRow from DataTable to dynamic object
        /// </summary>
        /// <param name="dataTable">DataTable to acquire data from</param>
        /// <returns>Dynamic object instance</returns>
        public static dynamic BindDynamic(DataTable dataTable)
        {
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                return BindDynamic(dataTable.Rows[0]);
            }
            return null;
        }

        /// <summary>
        /// Bind first DataRow from DataTable with specific index from the DataSet to dynamic object
        /// </summary>
        /// <param name="dataSet">DataSet to bind data from</param>
        /// <param name="tableIndex">Index of the DataTable in DataSet to bind the data from</param>
        /// <returns>Dynamic object instance</returns>
        public static dynamic BindDynamic(DataSet dataSet, int tableIndex = 0)
        {
            if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables.Count - 1 >= tableIndex)
            {
                return BindDynamic(dataSet.Tables[tableIndex]);
            }
            return null;
        }

        /// <summary>
        /// Bind first DataRow from DataTable with specific table name from the DataSet to dynamic object
        /// </summary>
        /// <param name="dataSet">DataSet to bind data from</param>
        /// <param name="tableName">Name of the DataTable in DataSet to bind the data from</param>
        /// <returns>Dynamic object instance</returns>
        public static dynamic BindDynamic(DataSet dataSet, string tableName)
        {
            if (dataSet != null && dataSet.Tables.Count > 0)
            {
                return BindDynamic(dataSet.Tables[tableName]);
            }
            return null;
        }

        #endregion

        #region Multiple instance binding

        /// <summary>
        /// Returns multiple dynamics object instances created from a DataReader
        /// </summary>
        /// <param name="dataReader">Reader used to itereate the data and bind to dynamic object instance</param>
        /// <returns>IEnumerable of dynamic object instances</returns>
        public static IEnumerable<dynamic> BindDynamics(SqlDataReader dataReader, bool takeFirstOnly = false)
        {
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    dynamic item = new ExpandoObject();
                    for (int columnIndex = 0; columnIndex < dataReader.FieldCount; columnIndex++)
                    {
                        var resultDictionary = (IDictionary<string, object>)item;
                        var dataValue = dataReader.GetValue(columnIndex);
                        resultDictionary.Add(dataReader.GetName(columnIndex), DBNull.Value.Equals(dataValue) ? null : dataValue);
                    }
                    yield return item;

                    if (takeFirstOnly)
                    {
                        break;
                    }
                }
            }

            if (!dataReader.IsClosed)
            {
                dataReader.Close();
            }
        }

        /// <summary>
        /// Returns multiple dynamics object instances created from a DataTable
        /// </summary>
        /// <param name="dataTable">DataTable containing data to be transformed to dynamic objects</param>
        /// <returns>IEnumerable of dynamic object instances</returns>
        public static IEnumerable<dynamic> BindDynamics(DataTable dataTable)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                yield return BindDynamic(row);
            }
        }

        /// <summary>
        /// Returns multiple dynamics object instances created from a DataSet table with specific index
        /// </summary>
        /// <param name="dataSet">DataTable containing DataTable to be transformed to dynamic objects</param>
        /// <param name="tableIndex">Index of DataTable in DataSet to be used for binding (default is 0)</param>
        /// <returns>IEnumerable of dynamic object instances</returns>
        public static IEnumerable<dynamic> BindDynamics(DataSet dataSet, int tableIndex = 0)
        {
            return BindDynamics(dataSet.Tables[tableIndex]);
        }

        /// <summary>
        /// Returns multiple dynamics object instances created from a DataSet table with specific name
        /// </summary>
        /// <param name="dataSet">DataTable containing DataTable to be transformed to dynamic objects</param>
        /// <param name="tableName">Name of DataTable in DataSet to be used for binding</param>
        /// <returns>IEnumerable of dynamic object instances</returns>
        public static IEnumerable<dynamic> BindDynamics(DataSet dataSet, string tableName)
        {
            return BindDynamics(dataSet.Tables[tableName]);
        }
        #endregion
    }
}


text:
package jp.co.opentone.bsol.linkbinder.dao.handler;

import jp.co.opentone.bsol.framework.core.extension.ibatis.EnumValue;
import jp.co.opentone.bsol.framework.core.extension.ibatis.EnumValueHandler;
import jp.co.opentone.bsol.linkbinder.dto.code.RSSCategory;

/**
 * RSSCategoryの値をマッピングするクラス.
 * @author opentone
 *
 */
public class RSSCategoryValueHandler extends EnumValueHandler {

    /*
     * (non-Javadoc)
     * @see jp.co.opentone.bsol.framework.extension.ibatis.EnumValueHandler#getValues()
     */
    @Override
    public EnumValue[] getValues() {
        return RSSCategory.values();
    }
}


text:
use core::ty::Unsafe;
use core::option::Option::{self, Some, None};
use core::ops::Drop;
use core::kinds::Share;

/// A lock. Note that this disables interrupts. Consequently, a task
/// dying (e.g. by running out of stack space) while holding a lock
/// may cause a deadlock.
pub struct Lock {
  locked: Unsafe<bool>
}

#[must_use]
pub struct Guard<'a>(&'a Lock);

pub static STATIC_LOCK: Lock = Lock { locked: Unsafe { value: false, marker1: InvariantType } };

impl Lock {
  pub fn new() -> Lock {
    Lock { locked: Unsafe::new(false) }
  }

  pub fn try_lock<'a>(&'a self) -> Option<Guard<'a>> {
    unsafe {
      let crit = NoInterrupts::new();
      let locked = self.locked.get();
      match *locked {
        true  => return None,
        false => {
          *locked = true;
          return Some(Guard(self));
        }
      }
    }
  }

  fn unlock<'a>(&'a self) {
    unsafe {
      let crit = NoInterrupts::new();
      *self.locked.get() = false;
    }
  }
}

#[unsafe_destructor]
impl<'a> Drop for Guard<'a> {
  fn drop(&mut self) {
    let &Guard(ref lock) = self;
    lock.unlock();
  }
}

impl Share for Lock { }


text:
var searchData=
[
  ['task',['Task',['../classca_1_1axoninteractive_1_1_utilities_1_1_task.html',1,'ca::axoninteractive::Utilities']]]
];


text:
"""Tests for GRU layer."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.python.keras._impl import keras
from tensorflow.python.keras._impl.keras import testing_utils
from tensorflow.python.platform import test

class GRULayerTest(test.TestCase):

  def test_return_sequences_GRU(self):
    num_samples = 2
    timesteps = 3
    embedding_dim = 4
    units = 2
    with self.test_session():
      testing_utils.layer_test(
          keras.layers.GRU,
          kwargs={'units': units,
                  'return_sequences': True},
          input_shape=(num_samples, timesteps, embedding_dim))

  def test_dynamic_behavior_GRU(self):
    num_samples = 2
    timesteps = 3
    embedding_dim = 4
    units = 2
    with self.test_session():
      layer = keras.layers.GRU(units, input_shape=(None, embedding_dim))
      model = keras.models.Sequential()
      model.add(layer)
      model.compile('sgd', 'mse')
      x = np.random.random((num_samples, timesteps, embedding_dim))
      y = np.random.random((num_samples, units))
      model.train_on_batch(x, y)

  def test_dropout_GRU(self):
    num_samples = 2
    timesteps = 3
    embedding_dim = 4
    units = 2
    with self.test_session():
      testing_utils.layer_test(
          keras.layers.GRU,
          kwargs={'units': units,
                  'dropout': 0.1,
                  'recurrent_dropout': 0.1},
          input_shape=(num_samples, timesteps, embedding_dim))

  def test_implementation_mode_GRU(self):
    num_samples = 2
    timesteps = 3
    embedding_dim = 4
    units = 2
    with self.test_session():
      for mode in [0, 1, 2]:
        testing_utils.layer_test(
            keras.layers.GRU,
            kwargs={'units': units,
                    'implementation': mode},
            input_shape=(num_samples, timesteps, embedding_dim))

  def test_statefulness_GRU(self):
    num_samples = 2
    timesteps = 3
    embedding_dim = 4
    units = 2
    layer_class = keras.layers.GRU
    with self.test_session():
      model = keras.models.Sequential()
      model.add(
          keras.layers.Embedding(
              4,
              embedding_dim,
              mask_zero=True,
              input_length=timesteps,
              batch_input_shape=(num_samples, timesteps)))
      layer = layer_class(
          units, return_sequences=False, stateful=True, weights=None)
      model.add(layer)
      model.compile(optimizer='sgd', loss='mse')
      out1 = model.predict(np.ones((num_samples, timesteps)))
      self.assertEqual(out1.shape, (num_samples, units))

      # train once so that the states change
      model.train_on_batch(
          np.ones((num_samples, timesteps)), np.ones((num_samples, units)))
      out2 = model.predict(np.ones((num_samples, timesteps)))

      # if the state is not reset, output should be different
      self.assertNotEqual(out1.max(), out2.max())

      # check that output changes after states are reset
      # (even though the model itself didn't change)
      layer.reset_states()
      out3 = model.predict(np.ones((num_samples, timesteps)))
      self.assertNotEqual(out2.max(), out3.max())

      # check that container-level reset_states() works
      model.reset_states()
      out4 = model.predict(np.ones((num_samples, timesteps)))
      np.testing.assert_allclose(out3, out4, atol=1e-5)

      # check that the call to `predict` updated the states
      out5 = model.predict(np.ones((num_samples, timesteps)))
      self.assertNotEqual(out4.max(), out5.max())

      # Check masking
      layer.reset_states()

      left_padded_input = np.ones((num_samples, timesteps))
      left_padded_input[0, :1] = 0
      left_padded_input[1, :2] = 0
      out6 = model.predict(left_padded_input)

      layer.reset_states()

      right_padded_input = np.ones((num_samples, timesteps))
      right_padded_input[0, -1:] = 0
      right_padded_input[1, -2:] = 0
      out7 = model.predict(right_padded_input)

      np.testing.assert_allclose(out7, out6, atol=1e-5)

  def test_regularizers_GRU(self):
    embedding_dim = 4
    layer_class = keras.layers.GRU
    with self.test_session():
      layer = layer_class(
          5,
          return_sequences=False,
          weights=None,
          input_shape=(None, embedding_dim),
          kernel_regularizer=keras.regularizers.l1(0.01),
          recurrent_regularizer=keras.regularizers.l1(0.01),
          bias_regularizer='l2',
          activity_regularizer='l1')
      layer.build((None, None, 2))
      self.assertEqual(len(layer.losses), 3)

      x = keras.backend.variable(np.ones((2, 3, 2)))
      layer(x)
      self.assertEqual(len(layer.get_losses_for(x)), 1)

  def test_constraints_GRU(self):
    embedding_dim = 4
    layer_class = keras.layers.GRU
    with self.test_session():
      k_constraint = keras.constraints.max_norm(0.01)
      r_constraint = keras.constraints.max_norm(0.01)
      b_constraint = keras.constraints.max_norm(0.01)
      layer = layer_class(
          5,
          return_sequences=False,
          weights=None,
          input_shape=(None, embedding_dim),
          kernel_constraint=k_constraint,
          recurrent_constraint=r_constraint,
          bias_constraint=b_constraint)
      layer.build((None, None, embedding_dim))
      self.assertEqual(layer.cell.kernel.constraint, k_constraint)
      self.assertEqual(layer.cell.recurrent_kernel.constraint, r_constraint)
      self.assertEqual(layer.cell.bias.constraint, b_constraint)

  def test_with_masking_layer_GRU(self):
    layer_class = keras.layers.GRU
    with self.test_session():
      inputs = np.random.random((2, 3, 4))
      targets = np.abs(np.random.random((2, 3, 5)))
      targets /= targets.sum(axis=-1, keepdims=True)
      model = keras.models.Sequential()
      model.add(keras.layers.Masking(input_shape=(3, 4)))
      model.add(layer_class(units=5, return_sequences=True, unroll=False))
      model.compile(loss='categorical_crossentropy', optimizer='adam')
      model.fit(inputs, targets, epochs=1, batch_size=2, verbose=1)

  def test_from_config_GRU(self):
    layer_class = keras.layers.GRU
    for stateful in (False, True):
      l1 = layer_class(units=1, stateful=stateful)
      l2 = layer_class.from_config(l1.get_config())
      assert l1.get_config() == l2.get_config()

if __name__ == '__main__':
  test.main()


text:
/*                                                                      *\
** Squants                                                              **
**                                                                      **
** Scala Quantities and Units of Measure Library and DSL                **
** (c) 2013-2015, Gary Keorkunian                                       **
**                                                                      **
\*                                                                      */

package squants.energy

import org.scalatest.{ Matchers, FlatSpec }
import scala.language.postfixOps
import squants.mass.Kilograms
import squants.QuantityParseException

/**
 * @author  garyKeorkunian
 * @since   0.1
 *
 */
class SpecificEnergySpec extends FlatSpec with Matchers {

  behavior of "SpecificEnergy and its Units of Measure"

  it should "create values using UOM factories" in {

    Grays(1).toGrays should be(1)
  }

  it should "create values from properly formatted Strings" in {
    SpecificEnergy("10.22 Gy").get should be(Grays(10.22))
    SpecificEnergy("10.22 zz").failed.get should be(QuantityParseException("Unable to parse SpecificEnergy", "10.22 zz"))
    SpecificEnergy("ZZ Gy").failed.get should be(QuantityParseException("Unable to parse SpecificEnergy", "ZZ Gy"))
  }

  it should "properly convert to all supported Units of Measure" in {
    val x = Grays(1)

    x.toGrays should be(1)
  }

  it should "return properly formatted strings for all supported Units of Measure" in {
    Grays(1).toString(Grays) should be("1.0 Gy")
  }

  it should "return Energy when multiplied by Mass" in {
    Grays(1) * Kilograms(10) should be(Joules(10))
  }

  behavior of "Conversions"

  it should "provide aliases for single unit values" in {
    import SpecificEnergyConversions._

    gray should be(Grays(1))
  }

  it should "provide implicit conversion from Double" in {
    import SpecificEnergyConversions._

    val d = 10d
    d.grays should be(Grays(d))
  }

  it should "provide Numeric support" in {
    import SpecificEnergyConversions.SpecificEnergyNumeric

    val ses = List(Grays(100), Grays(10))
    ses.sum should be(Grays(110))
  }
}


text:
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>biz.aQute.bnd</groupId>
		<artifactId>bnd-plugin-parent</artifactId>
		<version>${revision}</version>
		<relativePath>..</relativePath>
	</parent>

	<artifactId>bnd-indexer-maven-plugin</artifactId>
	<description>This maven plugin is used to make OSGi indexes from lists of maven dependencies.</description>
	<name>${project.groupId}:${project.artifactId}</name>
	<packaging>maven-plugin</packaging>
	<url>https://bnd.bndtools.org/</url>
	<scm>
		<url>https://github.com/bndtools/bnd</url>
		<connection>scm:git:https://github.com/bndtools/bnd.git</connection>
		<developerConnection>scm:git:git@github.com:bndtools/bnd.git</developerConnection>
	</scm>

	<dependencies>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.plugin-tools</groupId>
			<artifactId>maven-plugin-annotations</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.eclipse.aether</groupId>
			<artifactId>aether-api</artifactId>
		</dependency>
		<dependency>
			<groupId>biz.aQute.bnd</groupId>
			<artifactId>biz.aQute.bnd.maven</artifactId>
		</dependency>
		<dependency>
			<groupId>biz.aQute.bnd</groupId>
			<artifactId>biz.aQute.bndlib</artifactId>
		</dependency>
		<dependency>
			<groupId>org.osgi</groupId>
			<artifactId>osgi.core</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-invoker-plugin</artifactId>
				<dependencies>
					<dependency>
						<groupId>biz.aQute.bnd</groupId>
						<artifactId>biz.aQute.bndlib</artifactId>
						<version>${project.version}</version>
						<scope>runtime</scope>
					</dependency>
					<dependency>
						<groupId>org.osgi</groupId>
						<artifactId>osgi.core</artifactId>
						<version>6.0.0</version>
						<scope>runtime</scope>
					</dependency>
				</dependencies>
				<configuration>
					<preBuildHookScript>setup.groovy</preBuildHookScript>
					<scriptVariables>
						<pluginBasedir>${project.basedir}</pluginBasedir>
						<pluginBuildDirectory>${project.build.directory}</pluginBuildDirectory>
					</scriptVariables>
					<properties>
						<jsse.enableSNIExtension>false</jsse.enableSNIExtension>
						<!-- Used in the "no-dist-mgmt" deploy test -->
						<altSnapshotDeploymentRepository>LocalSnapshotDeploy::default::file://${project.build.directory}/integration-test/projects/test/localsnapshotdeployrepo</altSnapshotDeploymentRepository>
					</properties>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


text:
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectModuleManager">
    <modules>
      <module fileurl="file://$PROJECT_DIR$/Flippy.iml" filepath="$PROJECT_DIR$/Flippy.iml" />
      <module fileurl="file://$PROJECT_DIR$/Parse-1.5.1/Parse-1.5.1.iml" filepath="$PROJECT_DIR$/Parse-1.5.1/Parse-1.5.1.iml" />
      <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
    </modules>
  </component>
</project>


text:
package com.blazebit.apt.validation.constraint.validator;

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

import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

import com.blazebit.apt.validation.AnnotationProcessingUtils;
import com.blazebit.apt.validation.constraint.ConstraintScope;

/**
 *
 * @author Christian Beikov
 * @since 0.1.2
 */
public class UniqueValueConstraintValidator extends
		AbstractValueConstraintValidator {

	@SuppressWarnings("unchecked")
	@Override
	public void validate(ProcessingEnvironment procEnv,
			RoundEnvironment roundEnv, TypeElement annotationType,
			AnnotationMirror annotation, ExecutableElement annotationMember,
			Element e, Object value) {
		AnnotationValue scopeValue = AnnotationProcessingUtils
				.getAnnotationElementValue(procEnv, annotation, "scope");

		ConstraintScope scope = ConstraintScope.valueOf(scopeValue.getValue()
				.toString());
		Map<Element, Set<Element>> elements = new HashMap<Element, Set<Element>>();

		switch (scope) {
		case GLOBAL:
			elements.put(e, (Set<Element>) roundEnv
					.getElementsAnnotatedWith(annotationType));
			break;
		case CLASS:
			switch (e.getKind()) {
			case CONSTRUCTOR:
			case FIELD:
			case METHOD:
				elements.put(e, new HashSet<Element>());

				for (Element classMemberElement : procEnv.getElementUtils()
						.getAllMembers((TypeElement) e.getEnclosingElement())) {
					if (AnnotationProcessingUtils.findAnnotationMirror(
							classMemberElement, annotationType) != null) {
						elements.get(e).add(classMemberElement);
					}
				}

				break;
			case ENUM:
			case INTERFACE:
			case CLASS:
				elements.put(e, new HashSet<Element>());

				for (Element classMemberElement : procEnv.getElementUtils()
						.getAllMembers((TypeElement) e)) {
					if (AnnotationProcessingUtils.findAnnotationMirror(
							classMemberElement, annotationType) != null) {
						elements.get(e).add(classMemberElement);
					}
				}

				break;

			case ANNOTATION_TYPE:
				AnnotationMirror stereotypeAnnotationMirror = AnnotationProcessingUtils
						.findAnnotationMirror(procEnv, e,
								"javax.enterprise.inject.Stereotype");

				if (stereotypeAnnotationMirror == null) {
					procEnv.getMessager()
							.printMessage(
									Diagnostic.Kind.ERROR,
									"Annotation targets other than stereotyped annotation, enum, interface, class, constructor, field and methods are not supported",
									e, annotation);
					break;
				}

				Set<Element> stereotypeAnnotatedElements = (Set<Element>) roundEnv
						.getElementsAnnotatedWith((TypeElement) e);

				for (Element stereotypeAnnotatedElement : stereotypeAnnotatedElements) {

					// Always add the element to the map, otherwise it won't be
					// validated
					elements.put(stereotypeAnnotatedElement,
							new HashSet<Element>());

					switch (stereotypeAnnotatedElement.getKind()) {
					case CONSTRUCTOR:
					case FIELD:
					case METHOD:
						for (Element classMemberElement : procEnv
								.getElementUtils()
								.getAllMembers(
										(TypeElement) stereotypeAnnotatedElement
												.getEnclosingElement())) {
							if (AnnotationProcessingUtils
									.findAnnotationMirror(procEnv,
											classMemberElement, annotationType) != null) {
								elements.get(stereotypeAnnotatedElement).add(
										classMemberElement);
							}
						}

						break;
					case INTERFACE:
					case CLASS:
					case ENUM:
						for (Element classMemberElement : procEnv
								.getElementUtils()
								.getAllMembers(
										(TypeElement) stereotypeAnnotatedElement)) {
							if (AnnotationProcessingUtils
									.findAnnotationMirror(procEnv,
											classMemberElement, annotationType) != null) {
								elements.get(stereotypeAnnotatedElement).add(
										classMemberElement);
							}
						}

						break;
					default:
						procEnv.getMessager().printMessage(
								Diagnostic.Kind.ERROR,
								"Feature not yet implemented",
								stereotypeAnnotatedElement, annotation);
						break;
					}
				}

				break;
			default:
				procEnv.getMessager()
						.printMessage(
								Diagnostic.Kind.ERROR,
								"Annotation targets other than stereotyped annotation, enum, interface, class, constructor, field and methods are not supported",
								e, annotation);
				break;
			}
			break;
		case ELEMENT:
			elements.put(e, new HashSet<Element>(Arrays.asList(e)));
			break;
		}

		boolean unique;

		for (Map.Entry<Element, Set<Element>> elementEntry : elements
				.entrySet()) {
			unique = true;

			for (Element lookupElement : elementEntry.getValue()) {
				AnnotationMirror referencedAnnotationMirror = AnnotationProcessingUtils
						.findAnnotationMirror(lookupElement, annotationType);

				if (!e.equals(lookupElement)) {
					if (value
							.equals(AnnotationProcessingUtils
									.getAnnotationElementValue(
											procEnv,
											referencedAnnotationMirror,
											annotationMember.getSimpleName()
													.toString()).getValue())) {
						unique = false;
						break;
					}
				}
			}

			if (!unique) {
				procEnv.getMessager().printMessage(
						Diagnostic.Kind.ERROR,
						(String) AnnotationProcessingUtils
								.getAnnotationElementValue(procEnv, annotation,
										"errorMessage").getValue(), e,
						annotation);
			}
		}
	}
}


text:
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MefExportConventionsBuilder.cs" company="Quartz Software SRL">
//   Copyright (c) Quartz Software SRL. All rights reserved.
// </copyright>
// <summary>
//   Export conventions builder for MEF.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Kephas.Composition.Mef.Conventions
{
    using System;
    using System.Composition.Convention;

    using Kephas.Composition.Conventions;

    /// <summary>
    /// Export conventions builder for MEF.
    /// </summary>
    public class MefExportConventionsBuilder : IExportConventionsBuilder
    {
        /// <summary>
        /// The inner export convention builder.
        /// </summary>
        private readonly ExportConventionBuilder innerExportConventionBuilder;

        /// <summary>
        /// Initializes a new instance of the <see cref="MefExportConventionsBuilder"/> class.
        /// </summary>
        /// <param name="innerExportConventionBuilder">The inner export convention builder.</param>
        internal MefExportConventionsBuilder(ExportConventionBuilder innerExportConventionBuilder)
        {
            this.innerExportConventionBuilder = innerExportConventionBuilder;
        }

        /// <summary>
        /// Specify the contract type for the export.
        /// </summary>
        /// <param name="contractType">The contract type.</param>
        /// <returns>
        /// An export builder allowing further configuration.
        /// </returns>
        public IExportConventionsBuilder AsContractType(Type contractType)
        {
            this.innerExportConventionBuilder.AsContractType(contractType);

            return this;
        }

        /// <summary>
        /// Add export metadata to the export.
        /// </summary>
        /// <param name="name">The name of the metadata item.</param>
        /// <param name="value">The metadata value.</param>
        /// <returns>
        /// An export builder allowing further configuration.
        /// </returns>
        public IExportConventionsBuilder AddMetadata(string name, object value)
        {
            this.innerExportConventionBuilder.AddMetadata(name, value);

            return this;
        }

        /// <summary>
        /// Add export metadata to the export.
        /// </summary>
        /// <param name="name">The name of the metadata item.</param>
        /// <param name="getValueFromPartType">A function that calculates the metadata value based on the type.</param>
        /// <returns>
        /// An export builder allowing further configuration.
        /// </returns>
        public IExportConventionsBuilder AddMetadata(string name, Func<Type, object> getValueFromPartType)
        {
            this.innerExportConventionBuilder.AddMetadata(name, getValueFromPartType);

            return this;
        }
    }
}


text:
import re
import logging

#from cgi import parse_qs
from urlparse import parse_qs
from saml2.httputil import Unauthorized, NotFound, BadRequest
from saml2.httputil import ServiceError
from saml2.httputil import Response
from saml2.pack import http_form_post_message
from saml2.saml import AUTHN_PASSWORD
from saml2 import server
from saml2 import BINDING_HTTP_REDIRECT, BINDING_HTTP_POST
from saml2 import time_util
from Cookie import SimpleCookie

logger = logging.getLogger("saml2.IDP")

AUTHN = (AUTHN_PASSWORD, "http://www.example.com/login")

def _expiration(timeout, format=None):
    if timeout == "now":
        return time_util.instant(format)
    else:
        # validity time should match lifetime of assertions
        return time_util.in_a_while(minutes=timeout, format=format)

# -----------------------------------------------------------------------------
def dict_to_table(ava, lev=0, width=1):
    txt = ['
\n' % width]
    for prop, valarr in ava.items():
        txt.append("
\n")
        if isinstance(valarr, basestring):
            txt.append("%s	\n" % str(prop))
            try:
                txt.append("%s	\n" % valarr.encode("utf8"))
            except AttributeError:
                txt.append("%s	\n" % valarr)
        elif isinstance(valarr, list):
            index = 0
            num = len(valarr)
            for val in valarr:
                if not index:
                    txt.append("%s	\n" % (len(valarr), prop))
                else:
                    txt.append("
\n")
                if isinstance(val, dict):
                    txt.append("\n")
                    txt.extend(dict_to_table(val, lev+1, width-1))
                    txt.append("	\n")
                else:
                    try:
                        txt.append("%s	\n" % val.encode("utf8"))
                    except AttributeError:
                        txt.append("%s	\n" % val)
                if num > 1:
                    txt.append("
\n")
                num -= 1
                index += 1
        elif isinstance(valarr, dict):
            txt.append("%s	\n" % prop)
            txt.append("\n")
            txt.extend(dict_to_table(valarr, lev+1, width-1))
            txt.append("	\n")
        txt.append("
\n")
    txt.append('
\n')
    return txt

REPOZE_ID_EQUIVALENT = "uid"
FORM_SPEC = """<form name="myform" method="post" action="%s">
   <input type="hidden" name="SAMLResponse" value="%s" />
   <input type="hidden" name="RelayState" value="%s" />
</form>"""

def sso(environ, start_response, user):
    """ Supposed to return a self issuing Form POST """
    #edict = dict_to_table(environ)
    #if logger: logger.info("Environ keys: %s" % environ.keys())
    logger.info("--- In SSO ---")
    query = None
    if "QUERY_STRING" in environ:
        if logger:
            logger.info("Query string: %s" % environ["QUERY_STRING"])
        query = parse_qs(environ["QUERY_STRING"])
    elif "s2repoze.qinfo" in environ:
        query = environ["s2repoze.qinfo"]

    if not query:
        resp = Unauthorized('Unknown user')
        return resp(environ, start_response)

    # base 64 encoded request
    # Assume default binding, that is HTTP-redirect
    req = IDP.parse_authn_request(query["SAMLRequest"][0])

    if req is None:
        resp = ServiceError("Failed to parse the SAML request")
        return resp(environ, start_response)

    logger.info("parsed OK")
    logger.info("%s" % req)

    identity = dict(environ["repoze.who.identity"]["user"])
    logger.info("Identity: %s" % (identity,))
    userid = environ["repoze.who.identity"]['repoze.who.userid']
    if REPOZE_ID_EQUIVALENT:
        identity[REPOZE_ID_EQUIVALENT] = userid

    # What's the binding ? ProtocolBinding
    if req.message.protocol_binding == BINDING_HTTP_REDIRECT:
        _binding = BINDING_HTTP_POST
    else:
        _binding = req.message.protocol_binding

    try:
        resp_args = IDP.response_args(req.message, [_binding])
    except Exception:
        raise

    if req.message.assertion_consumer_service_url:
        if req.message.assertion_consumer_service_url != resp_args["destination"]:
            # serious error on someones behalf
            logger.error("%s != %s" % (req.message.assertion_consumer_service_url,
                                       resp_args["destination"]))
            resp = BadRequest("ConsumerURL and return destination mismatch")
            raise resp(environ, start_response)

    try:
        authn_resp = IDP.create_authn_response(identity, userid=userid,
                                               authn=AUTHN, **resp_args)
    except Exception, excp:
        logger.error("Exception: %s" % (excp,))
        raise

    logger.info("AuthNResponse: %s" % authn_resp)

    http_args = http_form_post_message(authn_resp, resp_args["destination"],
                                       relay_state=query["RelayState"][0],
                                       typ="SAMLResponse")

    resp = Response(http_args["data"], headers=http_args["headers"])
    return resp(environ, start_response)

def whoami(environ, start_response, user):
    identity = environ["repoze.who.identity"].copy()
    for prop in ["login", "password"]:
        try:
            del identity[prop]
        except KeyError:
            continue
    response = Response(dict_to_table(identity))
    return response(environ, start_response)

def not_found(environ, start_response):
    """Called if no URL matches."""
    resp = NotFound('Not Found')
    return resp(environ, start_response)

def not_authn(environ, start_response):
    if "QUERY_STRING" in environ:
        query = parse_qs(environ["QUERY_STRING"])
        logger.info("query: %s" % query)
    resp = Unauthorized('Unknown user')
    return resp(environ, start_response)

def slo(environ, start_response, user):
    """ Expects a HTTP-redirect logout request """

    query = None
    if "QUERY_STRING" in environ:
        logger.info("Query string: %s" % environ["QUERY_STRING"])
        query = parse_qs(environ["QUERY_STRING"])

    if not query:
        resp = Unauthorized('Unknown user')
        return resp(environ, start_response)

    try:
        req_info = IDP.parse_logout_request(query["SAMLRequest"][0],
                                            BINDING_HTTP_REDIRECT)
        logger.info("LOGOUT request parsed OK")
        logger.info("REQ_INFO: %s" % req_info.message)
    except KeyError, exc:
        logger.info("logout request error: %s" % (exc,))
        resp = BadRequest('Request parse error')
        return resp(environ, start_response)

    # look for the subject
    subject = req_info.subject_id()
    subject = subject.text.strip()
    logger.info("Logout subject: %s" % (subject,))

    status = None

    # Either HTTP-Post or HTTP-redirect is possible, prefer HTTP-Post.
    # Order matters
    bindings = [BINDING_HTTP_POST, BINDING_HTTP_REDIRECT]
    try:
        response = IDP.create_logout_response(req_info.message,
                                                        bindings)
        binding, destination = IDP.pick_binding("single_logout_service",
                                                bindings, "spsso", response)

        http_args = IDP.apply_binding(binding, "%s" % response, destination,
                                      query["RelayState"], response=True)

    except Exception, exc:
        resp = BadRequest('%s' % exc)
        return resp(environ, start_response)

    delco = delete_cookie(environ, "pysaml2idp")
    if delco:
        http_args["headers"].append(delco)

    if binding == BINDING_HTTP_POST:
        resp = Response(http_args["data"], headers=http_args["headers"])
    else:
        resp = NotFound(http_args["data"], headers=http_args["headers"])
    return resp(environ, start_response)

def delete_cookie(environ, name):
    kaka = environ.get("HTTP_COOKIE", '')
    if kaka:
        cookie_obj = SimpleCookie(kaka)
        morsel = cookie_obj.get(name, None)
        cookie = SimpleCookie()
        cookie[name] = morsel
        cookie[name]["expires"] = \
            _expiration("now", "%a, %d-%b-%Y %H:%M:%S CET")
        return tuple(cookie.output().split(": ", 1))
    return None

# ----------------------------------------------------------------------------

# map urls to functions
URLS = [
    (r'whoami$', whoami),
    (r'whoami/(.*)$', whoami),
    (r'sso$', sso),
    (r'sso/(.*)$', sso),
    (r'logout$', slo),
    (r'logout/(.*)$', slo),
]

# ----------------------------------------------------------------------------

def application(environ, start_response):
    """
    The main WSGI application. Dispatch the current request to
    the functions from above and store the regular expression
    captures in the WSGI environment as  `myapp.url_args` so that
    the functions from above can access the url placeholders.

    If nothing matches call the `not_found` function.

    :param environ: The HTTP application environment
    :param start_response: The application to run when the handling of the
        request is done
    :return: The response as a list of lines
    """
    user = environ.get("REMOTE_USER", "")
    kaka = environ.get("HTTP_COOKIE", '')
    if not user:
        user = environ.get("repoze.who.identity", "")

    path = environ.get('PATH_INFO', '').lstrip('/')
    logger.info("<application> PATH: %s" % path)
    logger.info("Cookie: %s" % (kaka,))
    for regex, callback in URLS:
        if user:
            match = re.search(regex, path)
            if match is not None:
                try:
                    environ['myapp.url_args'] = match.groups()[0]
                except IndexError:
                    environ['myapp.url_args'] = path
                logger.info("callback: %s" % (callback,))
                return callback(environ, start_response, user)
        else:
            logger.info("-- No USER --")
            return not_authn(environ, start_response)
    return not_found(environ, start_response)

# ----------------------------------------------------------------------------

from repoze.who.config import make_middleware_with_config

APP_WITH_AUTH = make_middleware_with_config(application, {"here":"."},
                        './who.ini', log_file="repoze_who.log")

# ----------------------------------------------------------------------------

if __name__ == '__main__':
    import sys
    from wsgiref.simple_server import make_server

    PORT = 8088

    IDP = server.Server(sys.argv[1])
    SRV = make_server('localhost', PORT, APP_WITH_AUTH)
    print "IdP listening on port: %s" % PORT
    SRV.serve_forever()