text:
import { wrap } from './queryEngine'
import stackHierarchy from './stackHierarchy'
const { JSDOM } = require('jsdom')

const createElementHeirarchy = (dom = '', selector) =>
  new JSDOM(`<body>${dom}</body>`).window.document.querySelector(selector)

describe('stackHierarchy', () => {
  test(`takes a wrapped element and a depth and returns the element heirarchy up tp that depth`, function () {
    const child = createElementHeirarchy(
      `

      `,
      `#leaf`
    )
    const stackedHeirarchy = stackHierarchy(wrap(child), 3)

    expect(stackedHeirarchy[0].el).toBe(child)
    expect(stackedHeirarchy[1].el).toBe(child.parentNode)
    expect(stackedHeirarchy[2].el).toBe(child.parentNode.parentNode)
    expect(stackedHeirarchy.length).toBe(3)
  })

  test(`throws an error if the depth is invalid`, function () {
    const child = createElementHeirarchy(
      `

      `,
      `#leaf`
    )

    expect(() => stackHierarchy(wrap(child), 0)).toThrowError(
      /An invalid depth of/
    )

    expect(() => stackHierarchy(wrap(child), -5)).toThrowError(
      /An invalid depth of/
    )
  })

  test(`returns a partial heirarchy if the depth is higher than what is available on the DOM`, function () {
    const child = createElementHeirarchy(
      `

      `,
      `#leaf`
    )

    const stackedHeirarchy = stackHierarchy(wrap(child), 10)

    expect(stackedHeirarchy[0].el).toBe(child)
    expect(stackedHeirarchy[1].el).toBe(child.parentNode)
    // Body
    expect(stackedHeirarchy[2].el).toBe(child.parentNode.parentNode)
    /// HTML
    expect(stackedHeirarchy[3].el).toBe(child.parentNode.parentNode.parentNode)
    // Document
    expect(stackedHeirarchy[4].el).toBe(
      child.parentNode.parentNode.parentNode.parentNode
    )

    expect(stackedHeirarchy.length).toBe(5)
  })
})


text:
/* Setup - Keys- View */
var KeysView = Backbone.View.extend({
  initialize: function() {
		this.render();
  },
  render: function(){
    return this;
  },
  events: {
    "click #btn-setup-keys-settings": "processKeys",
  },
  show: function() {
    $('#setup').html($('#template-setup-keys').html());
  },
  processKeys: function(e) {
    e.preventDefault();
    var keys_data = $('#form-setup-keys').serializeObject();
    Mailpile.API.setup_crypto_post(keys_data, function(result) {
      console.log(result);
    });
  }
});

redpajama_set_name: RedPajamaGithub


text:
namespace WebmMuxLib
{

Stream::Frame::Frame()
{
}

Stream::Frame::~Frame()
{
}

void Stream::Frame::Release()
{
   delete this;
}

Stream::Stream(Context& c) :
    m_context(c),
    m_trackNumber(0)
{
}

Stream::~Stream()
{
}

void Stream::Final()
{
}

void Stream::SetTrackNumber(int tn)
{
   m_trackNumber = tn;
}

int Stream::GetTrackNumber() const
{
   return m_trackNumber;
}

void Stream::WriteTrackEntry(int tn)
{
    WebmUtil::EbmlScratchBuf& entry_buf = m_context.m_buf;

    // we need the starting length of |entry_buf| to properly calculate
    // |num_bytes_to_ignore|
    const uint64 buf_start_len = entry_buf.GetBufferLength();

    entry_buf.WriteID1(WebmUtil::kEbmlTrackEntryID);

    // store entry offset for patching later
    const uint64 entry_len_offset = entry_buf.GetBufferLength();

    // We must exclude |num_bytes_to_ignore| from the size we obtain from
    // |entry_buf|.  The value from |entry_buf| includes the bytes storing
    // all preceding data in the track entry -- using it as-is would result
    // in an invalid tracks element.
    const uint64 num_bytes_to_ignore = buf_start_len + 1 + sizeof(uint16);

    // reserve 2 bytes for patching in the size...
    entry_buf.Serialize2UInt(0);

    WriteTrackNumber(tn);
    WriteTrackUID();
    WriteTrackType();
    WriteTrackName();
    WriteTrackCodecID();
    WriteTrackCodecPrivate();
    WriteTrackCodecName();
    WriteTrackSettings();

    const uint64 entry_len = entry_buf.GetBufferLength() - num_bytes_to_ignore;
    entry_buf.RewriteUInt(entry_len_offset, entry_len, sizeof(uint16));
}

void Stream::WriteTrackNumber(int tn_)
{
    WebmUtil::EbmlScratchBuf& buf = m_context.m_buf;

    assert(tn_ > 0);
    assert(tn_ < 128);

    m_trackNumber = tn_;
    const uint8 track_num = static_cast<uint8>(tn_);

    buf.WriteID1(WebmUtil::kEbmlTrackNumberID);
    buf.Write1UInt(1);
    buf.Serialize1UInt(track_num);
}

void Stream::WriteTrackUID()
{
    WebmUtil::EbmlScratchBuf& buf = m_context.m_buf;

    const TrackUID_t uid = CreateTrackUID();

    buf.WriteID2(WebmUtil::kEbmlTrackUIDID);
    buf.Write1UInt(8);
    buf.Serialize8UInt(uid);
}

void Stream::WriteTrackName()
{
}

void Stream::WriteTrackCodecPrivate()
{
}

void Stream::WriteTrackSettings()
{
}

Stream::TrackUID_t Stream::CreateTrackUID()
{
    TrackUID_t result;

    //TODO: Do we need to do this?
    //NOTE: The TrackUID is serialized in the normal way (the same
    //as for any other integer that is the payload of an EBML tag),
    //but the Matroska spec does say that this is an unsigned
    //integer.  In order to allow this integer value to be used
    //as an EBML varying-size integer, we restrict its value so
    //that it satifies the constraints for a varying size integer
    //that is streamed out using 8 bytes.  That means the upper
    //byte (the first in the stream) is 0 (the upper byte in the
    //stream is reserved for indicating that this integer
    //occupies 8 bytes total in the stream), and the low order
    //byte (the last in the stream) is even, which prevents
    //the integer from ever having a value with all bits set
    //(because then it would look like a signed integer).

    BYTE* p = reinterpret_cast<BYTE*>(&result);
    BYTE* const q = p + 7;

    {
        const int n = rand();

        BYTE& b0 = *p++;

        b0 = static_cast<BYTE>(n >> 4); //throw away low-order bits

        b0 &= 0xFE;  //ensure low order bit is not set
    }

    while (p != q)
    {
        const int n = rand();
        *p++ = static_cast<BYTE>(n >> 4); //throw away low-order bits
    }

    *p = 0;

    return result;
}

int Stream::Frame::GetLacing() const
{
    return 0;
}

ULONG Stream::Frame::GetBlockSize() const
{
    const ULONG result = 1 + 2 + 1 + GetSize();  //tn, tc, flg, f
    return result;
}

void Stream::Frame::WriteSimpleBlock(
    const Stream& s,
    ULONG cluster_tc) const
{
    WriteBlock(s, cluster_tc, true, GetBlockSize());  //SimpleBlock
}

void Stream::Frame::WriteBlockGroup(
    const Stream& s,
    ULONG cluster_tc,
    LONG prev_tc,
    ULONG duration) const
{
    EbmlIO::File& file = s.m_context.m_file;

    const ULONG block_size = GetBlockSize();
    ULONG block_group_size = 5 + block_size;

    const bool bKey = IsKey();

    if (!bKey)
        block_group_size += 1 + 1 + 2;

    if (duration > 0)
        block_group_size += 1 + 1 + 4;

    //begin block group

    file.WriteID1(WebmUtil::kEbmlBlockGroupID);
    file.WriteUInt(block_group_size);

#ifdef _DEBUG
    const __int64 pos = file.GetPosition();
#endif

    WriteBlock(s, cluster_tc, false, block_size);

    if (!bKey)
    {
        assert(prev_tc >= 0);

        const ULONG curr_tc = GetTimecode();
        assert(curr_tc <= LONG_MAX);

        const LONG tc = prev_tc - LONG(curr_tc);
        assert(tc < 0);
        assert(tc >= SHRT_MIN);

        const SHORT val = static_cast<SHORT>(tc);

        file.WriteID1(WebmUtil::kEbmlReferenceBlockID);
        file.Write1UInt(2);
        file.Serialize2SInt(val);
    }

    if (duration > 0)
    {
        file.WriteID1(WebmUtil::kEbmlBlockDurationID);
        file.Write1UInt(4);  //TODO: use min size
        file.Serialize4UInt(duration);
    }

    //end block group

#ifdef _DEBUG
    const __int64 newpos = file.GetPosition();
    assert((newpos - pos) == block_group_size);
#endif
}

void Stream::Frame::WriteBlock(
    const Stream& s,
    ULONG cluster_timecode,
    bool simple_block,
    ULONG block_size) const
{
    EbmlIO::File& file = s.m_context.m_file;

    //begin block

    const BYTE id = simple_block ? 0xA3 : 0xA1;  //SimpleBlock vs. Block

    file.WriteID1(id);
    file.WriteUInt(block_size);

#ifdef _DEBUG
    const __int64 pos = file.GetPosition();
#endif

    const int tn_ = s.GetTrackNumber();
    assert(tn_ > 0);
    assert(tn_ <= 255);

    const BYTE tn = static_cast<BYTE>(tn_);

    file.Write1UInt(tn);   //track number

    {
        const ULONG ft = GetTimecode();
        assert(ft <= LONG_MAX);

        const LONG tc_ = LONG(ft) - LONG(cluster_timecode);
        assert(tc_ >= SHRT_MIN);
        assert(tc_ <= SHRT_MAX);

        const SHORT tc = static_cast<SHORT>(tc_);

        file.Serialize2SInt(tc);       //relative timecode
    }

    BYTE flags = 0;

    if (simple_block & IsKey())
        flags |= BYTE(1 << 7);

    const int lacing = GetLacing();
    assert(lacing >= 0);
    assert(lacing <= 3);

    const BYTE fLacing = static_cast<BYTE>(lacing << 1);
    flags |= fLacing;

    file.Write(&flags, 1);   //written as binary, not uint

    file.Write(GetData(), GetSize());  //frame

    //end block

#ifdef _DEBUG
    const __int64 newpos = file.GetPosition();
    assert((newpos - pos) == block_size);
#endif
}

}  //end namespace WebmMuxLib


text:
package com.blp.stick.core;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import com.blp.stick.gfx.Textures;

/**
 * Project: StickJumper

 *
 * Class: GameObject
 *
 * @author  BossLetsPlays
 *
 */
public abstract class GameObject {
    protected float    x, y;
    protected float    velX, velY;
    protected int      width, height;
    protected Textures tex;

    public GameObject(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public GameObject(float x, float y, Textures tex) {
        this(x, y);
        this.tex = tex;
    }

    public void tick() { }

    public void render(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g.setColor(Color.RED);
        g2d.draw(getTopBounds());
        g2d.draw(getBottomBounds());
        g2d.draw(getRightBounds());
        g2d.draw(getLeftBounds());
        g.setColor(Color.BLUE);
        g2d.draw(getBounds());
    }

    public Rectangle getTopBounds() {
        return new Rectangle((int) x + 6, (int) y, width - 12, 12);
    }

    public Rectangle getBottomBounds() {
        return new Rectangle((int) x + 6, (int) y + (height - 6), width - 12, 6);
    }

    public Rectangle getRightBounds() {
        return new Rectangle((int) x + (width - 6), (int) y + 15, 6, height - 20);
    }

    public Rectangle getLeftBounds() {
        return new Rectangle((int) x, (int) y + 15, 6, height - 20);
    }

    public Rectangle getBounds() {
        return new Rectangle((int) x, (int) y, width, height);
    }

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setVelX(float velX) {
        this.velX = velX;
    }

    public void setVelY(float velY) {
        this.velY = velY;
    }

}


text:
package com.sebastian_daschner.jaxrs_analyzer.analysis.classes.testclasses.resource.response;

import com.sebastian_daschner.jaxrs_analyzer.model.Types;
import com.sebastian_daschner.jaxrs_analyzer.model.elements.HttpResponse;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Variant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

public class TestClass21 {

    // continue testing all available static Response methods
    @javax.ws.rs.GET
    public Response method() {
        Response.ResponseBuilder responseBuilder = Response.notModified("");
        responseBuilder = Response.ok(1d, MediaType.APPLICATION_JSON_TYPE);
        responseBuilder = Response.ok(1L, new Variant(MediaType.TEXT_PLAIN_TYPE, Locale.ENGLISH, "UTF-8"));

        return responseBuilder.build();
    }

    public static Set<HttpResponse> getResult() {
        final HttpResponse firstResult = new HttpResponse();
        final HttpResponse secondResult = new HttpResponse();
        final HttpResponse thirdResult = new HttpResponse();

        firstResult.getStatuses().add(304);
        firstResult.getHeaders().add("ETag");

        secondResult.getStatuses().add(200);
        secondResult.getContentTypes().add("application/json");
        secondResult.getEntityTypes().add("Ljava/lang/Double;");

        thirdResult.getStatuses().add(200);
        thirdResult.getHeaders().addAll(Arrays.asList("Content-Language", "Content-Encoding"));
        thirdResult.getEntityTypes().add(Types.LONG);

        return new HashSet<>(Arrays.asList(firstResult, secondResult, thirdResult));
    }

}


text:
package com.peterjosling.scroball.transforms;

import com.peterjosling.scroball.Track;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static com.google.common.truth.Truth.assertThat;

@RunWith(JUnit4.class)
public class TitleExtractorTest {

  private TitleExtractor titleExtractor = new TitleExtractor();

  @Test
  public void transform_doesNotModifyWhenNoSeparator() {
    Track input = Track.builder().track("Some Title").artist("").build();

    Track output = titleExtractor.transform(input);

    assertThat(output.track()).isEqualTo("Some Title");
  }
}


text:
import Base64Utils from '../utils/base64-utils';
import Connection from '../connection/connection-controller';
import SpeechRecording from './speech-recording';
import when from 'when';

/**
 * Controller class for the SpeechRecording model.
 * @private
 */
export default class SpeechRecordingController {
  /**
   * @param {Connection} connection - Object to use for making a connection to the REST API and Websocket server.
   */
  constructor(connection) {
    this._connection = connection;
  }

  /**
   * Initialise the speech recording challenge through RPCs.
   *
   * @param {SpeechChallenge} challenge - SpeechChallenge.
   * @private
   */
  speechRecordingInitChallenge(challenge) {
    return this._connection.call('recording.init_challenge',
      [this._connection._recordingId, challenge.id]).then(
      // RPC success callback
      recordingId => {
        console.log('Challenge initialised for recordingId: ' + this._connection._recordingId);
        return recordingId;
      });
  }

  /**
   * Initialise the speech recording audio specs through RPCs.
   *
   * @param {AudioRecorder} recorder - AudioRecorder.
   * @param {Function} dataavailableCb - Callback.
   * @private
   */
  speechRecordingInitAudio(recorder, dataavailableCb) {
    // Indicate to the socket server that we're about to start recording a
    // challenge. This allows the socket server some time to fetch the metadata
    // and reference audio to start the recording when audio is actually submitted.
    const specs = recorder.getAudioSpecs();
    return this._connection.call('recording.init_audio',
      [this._connection._recordingId, specs.audioFormat], specs.audioParameters)
      .then(recordingId => {
        console.log('Accepted audio parameters for recordingId after init_audio: ' + this._connection._recordingId);
        // Start listening for streaming data.
        recorder.addEventListener('dataavailable', dataavailableCb);
        return recordingId;
      });
  }

  /**
   * Start a speech recording from streaming audio.
   *
   * @param {SpeechChallenge} challenge - The speech challenge to perform.
   * @param {AudioRecorder} recorder - The audio recorder to extract audio from.
   * @returns {Promise.<SpeechRecording>} A {@link https://github.com/cujojs/when} Promise containing a {@link SpeechRecording}.
   * @emits {string} 'ReadyToReceive' when the call is made to receive audio. The recorder can now send audio.
   * @throws {Promise.<Error>} If challenge is not an object or not defined.
   * @throws {Promise.<Error>} If challenge has no id.
   * @throws {Promise.<Error>} If the connection is not open.
   * @throws {Promise.<Error>} If the recorder is already recording.
   * @throws {Promise.<Error>} If a session is already in progress.
   * @throws {Promise.<Error>} If something went wrong during recording.
   */
  startStreamingSpeechRecording(challenge, recorder) {
    // Validate required domain model.
    // Validate environment prerequisites.
    if (typeof challenge !== 'object' || !challenge) {
      return Promise.reject(new Error('"challenge" parameter is required or invalid'));
    }
    if (!challenge.id) {
      return Promise.reject(new Error('challenge.id field is required'));
    }
    if (!this._connection._session) {
      return Promise.reject(new Error('WebSocket connection was not open.'));
    }
    if (recorder.isRecording()) {
      return Promise.reject(new Error('Recorder should not yet be recording.'));
    }
    if (this._connection._recordingId !== null) {
      return Promise.reject(new Error('Session with recordingId ' + this._connection._recordingId +
        ' still in progress.'));
    }
    const self = this;
    return new when.Promise((resolve, reject, notify) => {
      self._connection._recordingId = null;

      function _cb(data) {
        const recording = new SpeechRecording(
          challenge.id, data.userId, data.id, new Date(data.created), new Date(data.updated),
          self._connection.addAccessToken(data.audioUrl));
        resolve({recordingId: self._connection._recordingId, recording});
      }

      function recordedCb(activeRecordingId, audioBlob, forcedStop) {
        self._connection.call('recording.close',
          [self._connection._recordingId]).then(
          // RPC success callback
          res => {
            // Pass along details to the success callback
            _cb(res, forcedStop);
          },
          // RPC error callback
          res => {
            Connection.logRPCError(res);
            reject(res);
          });
        recorder.removeEventListener('recorded', recordedCb);
        recorder.removeEventListener('dataavailable', startStreaming);
      }

      // Start streaming the binary audio when the user instructs
      // the audio recorder to start recording.
      function startStreaming(chunk) {
        const encoded = Base64Utils._arrayBufferToBase64(chunk);
        console.log('Sending audio chunk to websocket for recordingId: ' +
          self._connection._recordingId);
        self._connection.call('recording.write',
          [self._connection._recordingId, encoded, 'base64']).then(
          // RPC success callback
          res => {
            // Wrote data.
            console.log('Wrote data');
            return res;
          },
          // RPC error callback
          res => {
            Connection.logRPCError(res);
            reject(res);
          }
        );
      }

      function startRecording(recordingId) {
        self._connection._recordingId = recordingId;
        console.log('Got recordingId after initialisation: ' + self._connection._recordingId);
      }

      recorder.addEventListener('recorded', recordedCb);
      self._connection.call('recording.init_recording', [])
        .then(startRecording)
        .then(() =>
          self.speechRecordingInitChallenge(challenge)
            .then(() => {
              const p = new Promise(resolve_ => {
                if (recorder.hasUserMediaApproval()) {
                  resolve_();
                } else {
                  recorder.addEventListener('ready', resolve_);
                }
              });
              p.then(() => {
                self.speechRecordingInitAudio(recorder, startStreaming)
                  .catch(reject);
              });
            })
            .then(() => notify('ReadyToReceive'))
        )
        .catch(reject);
    })
      .then(res => {
        self._connection._recordingId = null;
        return Promise.resolve(res);
      })
      .catch(error => {
        self._connection._recordingId = null;
        Connection.logRPCError(error);
        return Promise.reject(error);
      });
  }

  /**
   * Get a speech recording in a speech challenge from the current active {@link Organisation} derived from the OAuth2
   * scope.
   *
   * @param {string} challengeId - Specify a speech challenge identifier.
   * @param {string} recordingId - Specify a speech recording identifier.
   * @returns {Promise.<SpeechRecording>} Promise containing a SpeechRecording.
   * @throws {Promise.<Error>} {@link SpeechChallenge#id} field is required.
   * @throws {Promise.<Error>} {@link SpeechRecording#id} field is required.
   * @throws {Promise.<Error>} If no result could not be found.
   */
  getSpeechRecording(challengeId, recordingId) {
    if (!challengeId) {
      return Promise.reject(new Error('challengeId field is required'));
    }
    if (!recordingId) {
      return Promise.reject(new Error('recordingId field is required'));
    }
    const url = this._connection._settings.apiUrl + '/challenges/speech/' + challengeId + '/recordings/' + recordingId;
    return this._connection._secureAjaxGet(url)
      .then(data => new SpeechRecording(challengeId, data.userId, data.id, new Date(data.created),
          new Date(data.updated), this._connection.addAccessToken(data.audioUrl)));
  }

  /**
   * Get and return all speech recordings in a specific speech challenge from the current active {@link Organisation}
   * derived from the OAuth2 scope.
   *
   * @param {string} challengeId - Specify a speech challenge identifier to list speech recordings for.
   * @returns {Promise.<SpeechRecording[]>} Promise containing an array of SpeechRecording.
   * @throws {Promise.<Error>} {@link SpeechChallenge#id} is required.
   * @throws {Promise.<Error>} If no result could not be found.
   */
  getSpeechRecordings(challengeId) {
    if (!challengeId) {
      return Promise.reject(new Error('challengeId field is required'));
    }
    const url = this._connection._settings.apiUrl + '/challenges/speech/' + challengeId + '/recordings';
    return this._connection._secureAjaxGet(url)
      .then(data => data.map(datum => new SpeechRecording(challengeId, datum.userId, datum.id, new Date(datum.created),
          new Date(datum.updated), this._connection.addAccessToken(datum.audioUrl))));
  }
}


text:
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0

// Note: This code is no longer used but is kept here as an example and in case we
//       wish to switch back to a internal file based version of a whitelist

package com.google.appinventor.server;

import com.google.appinventor.server.flags.Flag;
import com.google.appinventor.server.util.CsvParser;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Implementation of a whitelist.  Note that it uses email addresses
 * (which can change), and not user ids (which are unique), because we
 * do not expect that the people creating the whitelist have access to
 * the user ids of users.
 *
 * @author kerr@google.com (Debby Wallach)
 */
public class Whitelist {
  /**
   * Class representing a case-insensitive email address.
   */
  private static class EmailAddress {
    private final String email;

    EmailAddress(String email) {
      Preconditions.checkNotNull(email);
      this.email = email;
    }

    @Override
    public String toString() {
      return email;
    }

    @Override
    public boolean equals(Object other) {
      boolean result = (other instanceof EmailAddress) &&
          ((EmailAddress) other).email.equalsIgnoreCase(email);
      return result;
    }

    @Override
    public int hashCode() {
      return email.toLowerCase().hashCode();
    }
  }

  private static final Logger LOG = Logger.getLogger(Whitelist.class.getName());
  private static final boolean DEBUG = false;

  // When running on appengine, the application is running in a way that
  // the rootPath should not be set to anything.  This flag needs to be
  // set for testing.
  @VisibleForTesting
  public static final Flag<String> rootPath = Flag.createFlag("root.path", "");

  private String pathToWhitelist = rootPath.get() + "WEB-INF/whitelist";
  private boolean validWhitelist;

  private final Set<EmailAddress> addresses = new HashSet<EmailAddress>();

  Whitelist() {
    validWhitelist = false;
    try {
      parseToAddresses(new CsvParser(new FileInputStream(pathToWhitelist)));
      if (addresses.size() == 0) {
        LOG.severe("Whitelist file contained no entries.");
      } else {
        if (DEBUG) {
          logWhitelistContents();
        }
        validWhitelist = true;
      }
    } catch (FileNotFoundException e) {
      LOG.severe("No whitelist found.");
    } catch (SecurityException e) {
      LOG.severe("Whitelist found, but wrong permission.");
    } catch (IOException e) {
      LOG.log(Level.SEVERE, "Unexpected whitelist error", e);
    }
  }

  // should throw something if not valid whitelist?
  public boolean isInWhitelist(LocalUser user) {
    if (! validWhitelist) {
      // If we have not loaded a valid whitelist, reject everyone.
      return false;
    }

    boolean found = addresses.contains(new EmailAddress(user.getUserEmail()));
    if (!found) {
      LOG.info("User with email address " + user.getUserEmail() +
          " was not found in the whitelist");
    }
    return found;
  }

  private void logWhitelistContents() {
    LOG.info("Whitelist contains " + addresses.size() + " addresses.");
    StringBuilder sb = new StringBuilder();
    String delimiter = "";
    for (EmailAddress address : addresses) {
      sb.append(delimiter).append(address);
      if (sb.length() > 70) {
        LOG.info("On whitelist: " + sb);
        sb = new StringBuilder();
        delimiter = "";
      } else {
        delimiter = ",";
      }
    }
    if (sb.length() > 0) {
      LOG.info("On whitelist: " + sb);
    }
  }

  private void parseToAddresses(CsvParser parser) {
    /*
     * expected file format:
     * "emailaddress1"
     * "emailaddress2"
     *
     */
    Pattern patternPlus = Pattern.compile("([^+]*)(\\+[^@]*)(@.*)");

    while (parser.hasNext()) {
      List<String> line = parser.next();
      String address = line.get(0).trim();

      // Change foo+bar@gmail.com to foo@gmail.com
      Matcher matcher = patternPlus.matcher(address);
      if (matcher.matches()) {
        address = matcher.group(1) + matcher.group(3);
      }

      addresses.add(new EmailAddress(address));
    }
  }
}


text:
@implementation CacheManager
+ (id)sharedCacheManager
{
    static dispatch_once_t pred;
    static CacheDBDao *_cacheM;
    dispatch_once(&pred, ^{
        _cacheM = [[CacheDBDao alloc]init];
    });
    return _cacheM;
}
@end


text:
namespace {

// Holds the information about a known registry key.
struct KnownReservedKey {
  const wchar_t* name;
  HKEY key;
};

// Contains all the known registry key by name and by handle.
const KnownReservedKey kKnownKey[] = {
    { L"HKEY_CLASSES_ROOT", HKEY_CLASSES_ROOT },
    { L"HKEY_CURRENT_USER", HKEY_CURRENT_USER },
    { L"HKEY_LOCAL_MACHINE", HKEY_LOCAL_MACHINE},
    { L"HKEY_USERS", HKEY_USERS},
    { L"HKEY_PERFORMANCE_DATA", HKEY_PERFORMANCE_DATA},
    { L"HKEY_PERFORMANCE_TEXT", HKEY_PERFORMANCE_TEXT},
    { L"HKEY_PERFORMANCE_NLSTEXT", HKEY_PERFORMANCE_NLSTEXT},
    { L"HKEY_CURRENT_CONFIG", HKEY_CURRENT_CONFIG},
    { L"HKEY_DYN_DATA", HKEY_DYN_DATA}
};

// These functions perform case independent path comparisons.
bool EqualPath(const base::string16& first, const base::string16& second) {
  return _wcsicmp(first.c_str(), second.c_str()) == 0;
}

bool EqualPath(const base::string16& first, size_t first_offset,
               const base::string16& second, size_t second_offset) {
  return _wcsicmp(first.c_str() + first_offset,
                  second.c_str() + second_offset) == 0;
}

bool EqualPath(const base::string16& first,
               const wchar_t* second, size_t second_len) {
  return _wcsnicmp(first.c_str(), second, second_len) == 0;
}

bool EqualPath(const base::string16& first, size_t first_offset,
               const wchar_t* second, size_t second_len) {
  return _wcsnicmp(first.c_str() + first_offset, second, second_len) == 0;
}

// Returns true if |path| starts with "\??\" and returns a path without that
// component.
bool IsNTPath(const base::string16& path, base::string16* trimmed_path ) {
  if ((path.size() < sandbox::kNTPrefixLen) ||
      (0 != path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix))) {
    *trimmed_path = path;
    return false;
  }

  *trimmed_path = path.substr(sandbox::kNTPrefixLen);
  return true;
}

// Returns true if |path| starts with "\Device\" and returns a path without that
// component.
bool IsDevicePath(const base::string16& path, base::string16* trimmed_path ) {
  if ((path.size() < sandbox::kNTDevicePrefixLen) ||
      (!EqualPath(path, sandbox::kNTDevicePrefix,
                  sandbox::kNTDevicePrefixLen))) {
    *trimmed_path = path;
    return false;
  }

  *trimmed_path = path.substr(sandbox::kNTDevicePrefixLen);
  return true;
}

bool StartsWithDriveLetter(const base::string16& path) {
  if (path.size() < 3)
    return false;

  if (path[1] != L':' || path[2] != L'\\')
    return false;

  return (path[0] >= 'a' && path[0] <= 'z') ||
         (path[0] >= 'A' && path[0] <= 'Z');
}

const wchar_t kNTDotPrefix[] = L"\\\\.\\";
const size_t kNTDotPrefixLen = arraysize(kNTDotPrefix) - 1;

// Removes "\\\\.\\" from the path.
void RemoveImpliedDevice(base::string16* path) {
  if (0 == path->compare(0, kNTDotPrefixLen, kNTDotPrefix))
    *path = path->substr(kNTDotPrefixLen);
}

}  // namespace

namespace sandbox {

// Returns true if the provided path points to a pipe.
bool IsPipe(const base::string16& path) {
  size_t start = 0;
  if (0 == path.compare(0, sandbox::kNTPrefixLen, sandbox::kNTPrefix))
    start = sandbox::kNTPrefixLen;

  const wchar_t kPipe[] = L"pipe\\";
  if (path.size() < start + arraysize(kPipe) - 1)
    return false;

  return EqualPath(path, start, kPipe, arraysize(kPipe) - 1);
}

HKEY GetReservedKeyFromName(const base::string16& name) {
  for (size_t i = 0; i < arraysize(kKnownKey); ++i) {
    if (name == kKnownKey[i].name)
      return kKnownKey[i].key;
  }

  return NULL;
}

bool ResolveRegistryName(base::string16 name, base::string16* resolved_name) {
  for (size_t i = 0; i < arraysize(kKnownKey); ++i) {
    if (name.find(kKnownKey[i].name) == 0) {
      HKEY key;
      DWORD disposition;
      if (ERROR_SUCCESS != ::RegCreateKeyEx(kKnownKey[i].key, L"", 0, NULL, 0,
                                            MAXIMUM_ALLOWED, NULL, &key,
                                            &disposition))
        return false;

      bool result = GetPathFromHandle(key, resolved_name);
      ::RegCloseKey(key);

      if (!result)
        return false;

      *resolved_name += name.substr(wcslen(kKnownKey[i].name));
      return true;
    }
  }

  return false;
}

// |full_path| can have any of the following forms:
//    \??\c:\some\foo\bar
//    \Device\HarddiskVolume0\some\foo\bar
//    \??\HarddiskVolume0\some\foo\bar
DWORD IsReparsePoint(const base::string16& full_path, bool* result) {
  // Check if it's a pipe. We can't query the attributes of a pipe.
  if (IsPipe(full_path)) {
    *result = FALSE;
    return ERROR_SUCCESS;
  }

  base::string16 path;
  bool nt_path = IsNTPath(full_path, &path);
  bool has_drive = StartsWithDriveLetter(path);
  bool is_device_path = IsDevicePath(path, &path);

  if (!has_drive && !is_device_path && !nt_path)
    return ERROR_INVALID_NAME;

  bool added_implied_device = false;
  if (!has_drive) {
      path = base::string16(kNTDotPrefix) + path;
      added_implied_device = true;
  }

  base::string16::size_type last_pos = base::string16::npos;
  bool passed_once = false;

  do {
    path = path.substr(0, last_pos);

    DWORD attributes = ::GetFileAttributes(path.c_str());
    if (INVALID_FILE_ATTRIBUTES == attributes) {
      DWORD error = ::GetLastError();
      if (error != ERROR_FILE_NOT_FOUND &&
          error != ERROR_PATH_NOT_FOUND &&
          error != ERROR_INVALID_NAME) {
        // Unexpected error.
        if (passed_once && added_implied_device &&
            (path.rfind(L'\\') == kNTDotPrefixLen - 1)) {
          break;
        }
        NOTREACHED_NT();
        return error;
      }
    } else if (FILE_ATTRIBUTE_REPARSE_POINT & attributes) {
      // This is a reparse point.
      *result = true;
      return ERROR_SUCCESS;
    }

    passed_once = true;
    last_pos = path.rfind(L'\\');
  } while (last_pos > 2);  // Skip root dir.

  *result = false;
  return ERROR_SUCCESS;
}

// We get a |full_path| of the forms accepted by IsReparsePoint(), and the name
// we'll get from |handle| will be \device\harddiskvolume1\some\foo\bar.
bool SameObject(HANDLE handle, const wchar_t* full_path) {
  // Check if it's a pipe.
  if (IsPipe(full_path))
    return true;

  base::string16 actual_path;
  if (!GetPathFromHandle(handle, &actual_path))
    return false;

  base::string16 path(full_path);
  DCHECK_NT(!path.empty());

  // This may end with a backslash.
  const wchar_t kBackslash = '\\';
  if (path[path.length() - 1] == kBackslash)
    path = path.substr(0, path.length() - 1);

  // Perfect match (case-insesitive check).
  if (EqualPath(actual_path, path))
    return true;

  bool nt_path = IsNTPath(path, &path);
  bool has_drive = StartsWithDriveLetter(path);

  if (!has_drive && nt_path) {
    base::string16 simple_actual_path;
    if (!IsDevicePath(actual_path, &simple_actual_path))
      return false;

    // Perfect match (case-insesitive check).
    return (EqualPath(simple_actual_path, path));
  }

  if (!has_drive)
    return false;

  // We only need 3 chars, but let's alloc a buffer for four.
  wchar_t drive[4] = {0};
  wchar_t vol_name[MAX_PATH];
  memcpy(drive, &path[0], 2 * sizeof(*drive));

  // We'll get a double null terminated string.
  DWORD vol_length = ::QueryDosDeviceW(drive, vol_name, MAX_PATH);
  if (vol_length < 2 || vol_length == MAX_PATH)
    return false;

  // Ignore the nulls at the end.
  vol_length = static_cast<DWORD>(wcslen(vol_name));

  // The two paths should be the same length.
  if (vol_length + path.size() - 2 != actual_path.size())
    return false;

  // Check up to the drive letter.
  if (!EqualPath(actual_path, vol_name, vol_length))
    return false;

  // Check the path after the drive letter.
  if (!EqualPath(actual_path, vol_length, path, 2))
    return false;

  return true;
}

// Paths like \Device\HarddiskVolume0\some\foo\bar are assumed to be already
// expanded.
bool ConvertToLongPath(const base::string16& short_path,
                       base::string16* long_path) {
  if (IsPipe(short_path)) {
    // TODO(rvargas): Change the signature to use a single argument.
    long_path->assign(short_path);
    return true;
  }

  base::string16 path;
  if (IsDevicePath(short_path, &path))
    return false;

  bool is_nt_path = IsNTPath(path, &path);
  bool added_implied_device = false;
  if (!StartsWithDriveLetter(path) && is_nt_path) {
    path = base::string16(kNTDotPrefix) + path;
    added_implied_device = true;
  }

  DWORD size = MAX_PATH;
  scoped_ptr<wchar_t[]> long_path_buf(new wchar_t[size]);

  DWORD return_value = ::GetLongPathName(path.c_str(), long_path_buf.get(),
                                         size);
  while (return_value >= size) {
    size *= 2;
    long_path_buf.reset(new wchar_t[size]);
    return_value = ::GetLongPathName(path.c_str(), long_path_buf.get(), size);
  }

  DWORD last_error = ::GetLastError();
  if (0 == return_value && (ERROR_FILE_NOT_FOUND == last_error ||
                            ERROR_PATH_NOT_FOUND == last_error ||
                            ERROR_INVALID_NAME == last_error)) {
    // The file does not exist, but maybe a sub path needs to be expanded.
    base::string16::size_type last_slash = path.rfind(L'\\');
    if (base::string16::npos == last_slash)
      return false;

    base::string16 begin = path.substr(0, last_slash);
    base::string16 end = path.substr(last_slash);
    if (!ConvertToLongPath(begin, &begin))
      return false;

    // Ok, it worked. Let's reset the return value.
    path = begin + end;
    return_value = 1;
  } else if (0 != return_value) {
    path = long_path_buf.get();
  }

  if (return_value != 0) {
    if (added_implied_device)
      RemoveImpliedDevice(&path);

    if (is_nt_path) {
      *long_path = kNTPrefix;
      *long_path += path;
    } else {
      *long_path = path;
    }

    return true;
  }

  return false;
}

bool GetPathFromHandle(HANDLE handle, base::string16* path) {
  NtQueryObjectFunction NtQueryObject = NULL;
  ResolveNTFunctionPtr("NtQueryObject", &NtQueryObject);

  OBJECT_NAME_INFORMATION initial_buffer;
  OBJECT_NAME_INFORMATION* name = &initial_buffer;
  ULONG size = sizeof(initial_buffer);
  // Query the name information a first time to get the size of the name.
  NTSTATUS status = NtQueryObject(handle, ObjectNameInformation, name, size,
                                  &size);

  scoped_ptr<OBJECT_NAME_INFORMATION> name_ptr;
  if (size) {
    name = reinterpret_cast<OBJECT_NAME_INFORMATION*>(new BYTE[size]);
    name_ptr.reset(name);

    // Query the name information a second time to get the name of the
    // object referenced by the handle.
    status = NtQueryObject(handle, ObjectNameInformation, name, size, &size);
  }

  if (STATUS_SUCCESS != status)
    return false;

  path->assign(name->ObjectName.Buffer, name->ObjectName.Length /
                                        sizeof(name->ObjectName.Buffer[0]));
  return true;
}

bool GetNtPathFromWin32Path(const base::string16& path,
                            base::string16* nt_path) {
  HANDLE file = ::CreateFileW(path.c_str(), 0,
    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
    OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  if (file == INVALID_HANDLE_VALUE)
    return false;
  bool rv = GetPathFromHandle(file, nt_path);
  ::CloseHandle(file);
  return rv;
}

bool WriteProtectedChildMemory(HANDLE child_process, void* address,
                               const void* buffer, size_t length) {
  // First, remove the protections.
  DWORD old_protection;
  if (!::VirtualProtectEx(child_process, address, length,
                          PAGE_WRITECOPY, &old_protection))
    return false;

  SIZE_T written;
  bool ok = ::WriteProcessMemory(child_process, address, buffer, length,
                                 &written) && (length == written);

  // Always attempt to restore the original protection.
  if (!::VirtualProtectEx(child_process, address, length,
                          old_protection, &old_protection))
    return false;

  return ok;
}

};  // namespace sandbox

void ResolveNTFunctionPtr(const char* name, void* ptr) {
  static volatile HMODULE ntdll = NULL;

  if (!ntdll) {
    HMODULE ntdll_local = ::GetModuleHandle(sandbox::kNtdllName);
    // Use PEImage to sanity-check that we have a valid ntdll handle.
    base::win::PEImage ntdll_peimage(ntdll_local);
    CHECK_NT(ntdll_peimage.VerifyMagic());
    // Race-safe way to set static ntdll.
    ::InterlockedCompareExchangePointer(
        reinterpret_cast<PVOID volatile*>(&ntdll), ntdll_local, NULL);

  }

  CHECK_NT(ntdll);
  FARPROC* function_ptr = reinterpret_cast<FARPROC*>(ptr);
  *function_ptr = ::GetProcAddress(ntdll, name);
  CHECK_NT(*function_ptr);
}


text:
module Gitlab
  module Email
    module Hook
      class EmailTemplateInterceptor
        ##
        # Remove HTML part if HTML emails are disabled.
        #
        def self.delivering_email(message)
          unless Gitlab::CurrentSettings.html_emails_enabled
            message.parts.delete_if do |part|
              part.content_type.start_with?('text/html')
            end
          end
        end
      end
    end
  end
end


text:
<phpunit
		bootstrap="./tests/phpunit/bootstrap.php"
		backupGlobals="false"
		colors="true"
		convertErrorsToExceptions="true"
		convertNoticesToExceptions="true"
		convertWarningsToExceptions="true"
>
	<testsuites>
		<testsuite name="installer-tests">
            <directory suffix=".php">./tests/phpunit/tests/</directory>
		</testsuite>
	</testsuites>
	<filter>
		<whitelist>
			<directory suffix=".php">./</directory>
			<exclude>
				<directory>./locale</directory>
				<directory>./tests</directory>
				<directory>./vendor</directory>
			</exclude>
		</whitelist>
	</filter>
</phpunit>


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:padding="4dp">

        <ImageView
            android:id="@+id/freehand_iv_paint"
            android:layout_width="0dp"
            android:layout_height="@dimen/freehand_image_view_height"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/freehand_image_view_margin"
            android:layout_marginRight="@dimen/freehand_image_view_margin"
            android:layout_weight="1"
            android:clickable="true"
            android:src="@drawable/ic_brush_activated_24dp" />

        <ImageView
            android:id="@+id/freehand_iv_eraser"
            android:layout_width="0dp"
            android:layout_height="@dimen/freehand_image_view_height"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/freehand_image_view_margin"
            android:layout_marginRight="@dimen/freehand_image_view_margin"
            android:layout_weight="1"
            android:clickable="true"
            android:src="@drawable/ic_eraser_unactivated_24dp" />

        <ImageView
            android:id="@+id/freehand_iv_revocation"
            android:layout_width="0dp"
            android:layout_height="@dimen/freehand_image_view_height"
            android:layout_marginLeft="@dimen/freehand_image_view_margin"
            android:layout_marginRight="@dimen/freehand_image_view_margin"
            android:layout_weight="1"
            android:src="@drawable/ic_revocation_unactivated_24dp" />

        <ImageView
            android:id="@+id/freehand_iv_revert"
            android:layout_width="0dp"
            android:layout_height="@dimen/freehand_image_view_height"
            android:layout_marginLeft="@dimen/freehand_image_view_margin"
            android:layout_marginRight="@dimen/freehand_image_view_margin"
            android:layout_weight="1"
            android:src="@drawable/ic_revert_unactivated_24dp" />

        <ImageView
            android:id="@+id/freehand_iv_clear"
            android:layout_width="0dp"
            android:layout_height="@dimen/freehand_image_view_height"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/freehand_image_view_margin"
            android:layout_marginRight="@dimen/freehand_image_view_margin"
            android:layout_weight="1"
            android:src="@drawable/ic_delete_forever_24dp" />
    </LinearLayout>

    <tech.huqi.quicknote.core.view.DrawingBoardView
        android:id="@+id/draw_board"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


text:
/*
 * GLSampler.cpp
 *
 *  Created on: Apr 1, 2015
 *      Author: Collin
 */

#include <glm/gtc/type_ptr.hpp>

#include "GLSampler.h"

struct AnisotropicExtensionChecker
{
	bool isAvailable;

	AnisotropicExtensionChecker()
	{
		if(GLEW_EXT_texture_filter_anisotropic)
			isAvailable = true;
		else
			isAvailable = false;
	}
};

GLSampler::~GLSampler()
{
	glDeleteSamplers(1, &handle);
}

void GLSampler::setMagFilter(GLenum setting)
{
	glSamplerParameteri(handle, GL_TEXTURE_MAG_FILTER, setting);
}
void GLSampler::setMinFilter(GLenum setting)
{
	glSamplerParameteri(handle, GL_TEXTURE_MIN_FILTER, setting);
}

void GLSampler::setAnisotropy(GLfloat anisotropy)
{
	// This is safe because static initialization happens
	// upon first entry to a method (so this will happen
	// after a context is created)
	static AnisotropicExtensionChecker checker;

	if(checker.isAvailable)
		glSamplerParameterf(handle, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
}

void GLSampler::setWrapModeS(GLenum setting)
{
	glSamplerParameteri(handle, GL_TEXTURE_WRAP_S, setting);
}
void GLSampler::setWrapModeT(GLenum setting)
{
	glSamplerParameteri(handle, GL_TEXTURE_WRAP_T, setting);
}
void GLSampler::setWrapModeR(GLenum setting)
{
	glSamplerParameteri(handle, GL_TEXTURE_WRAP_R, setting);
}
void GLSampler::setWrapMode(GLenum setting)
{
	setWrapModeS(setting);
	setWrapModeT(setting);
	setWrapModeR(setting);
}

void GLSampler::setBorderColor(glm::vec4 color)
{
	glSamplerParameterfv(handle, GL_TEXTURE_BORDER_COLOR, glm::value_ptr(color));
}


text:
require 'rails_helper'

describe Admin::PostTypesController, type: :controller do
  render_views

  before do
    create(:blog)
    user = create(:user, :as_admin)
    sign_in user
  end

  describe 'GET #index' do
    it 'responds successfully with an HTTP 200 status code' do
      get :index
      expect(response).to be_success
      expect(response).to have_http_status(200)
    end

    it 'renders the index template' do
      get :index
      expect(response).to render_template('index')
    end
  end

  describe 'create a new post_type' do
    it 'should create a post and redirect to #index' do
      expect do
        post :create, params: { post_type: { name: 'new post type' } }
        expect(response).to redirect_to(action: 'index')
        expect(PostType.count).to eq(1)
        expect(PostType.first.name).to eq('new post type')
      end.to change(PostType, :count)
    end
  end

  describe 'GET #edit' do
    before(:each) do
      get :edit, params: { id: FactoryGirl.create(:post_type).id }
    end

    it 'renders the edit template with an HTTP 200 status code' do
      expect(response).to be_success
      expect(response).to have_http_status(200)
      expect(response).to render_template('edit')
    end
  end

  describe '#update an existing post_type' do
    it 'should update a post_type and redirect to #index' do
      @test_id = FactoryGirl.create(:post_type).id
      post :update, params: { id: @test_id, post_type: { name: 'another name' } }
      assert_response :redirect, action: 'index'
      expect(PostType.count).to eq(1)
      expect(PostType.first.name).to eq('another name')
    end
  end

  describe 'destroy a post_type' do
    it 'should destroy the post_type and redirect to #index' do
      @test_id = FactoryGirl.create(:post_type).id
      post :destroy, params: { id: @test_id }
      expect(response).to redirect_to(action: 'index')
      expect(PostType.count).to eq(0)
    end
  end
end


text:
package OpenXPKI::Server::API2::Plugin::Crypto::generate_key;
use OpenXPKI::Server::API2::EasyPlugin;

=head1 NAME

OpenXPKI::Server::API2::Plugin::Crypto::generate_key

=cut

# Project modules
use OpenXPKI::Server::Context qw( CTX );
use OpenXPKI::Server::API2::Types;

=head1 COMMANDS

=head2 generate_key

Creates a new cryptographic key and returns it encrypted representation in PEM
format.

B<Parameters>

=over

=item * C<password> I<Str> - passwort for key encryption. Required.

=item * C<key_alg> I<Str> - key algorithm, e.g. "RSA", "DSA", "EC" etc. Default: "RSA"

=item * C<enc_alg> I<Str> - encryption algorithm, e.g. "AES256" etc. Default:
crypto backend's default

=item * C<key_length> I<Int> - only RSA/DSA: key length. Default: 2048

=item * C<curve> I<Str> - only EC: curve name. Required.

=item * C<pkeyopt> I<HashRef> - more options to directly pass to OpenSSL.
If specified, these option win over other parameters
(e.g. C<options-E<gt>{rsa_keygen_bits}> wins over C<key_length>)

=item * C<paramset> I<Str> - PEM encoded parameter set whose contents will be
passed to C<openssl genpkey -paramfile ...>

B<Changes compared to API v1:>

The previous parameter C<PARAMS> was removed. The hash keys used in it are now
"first class" parameters:

    # old
    PARAMS => {
        PKEYOPT    =>    { ... },
        KEY_LENGTH => $len,
        ECPARAM    => $pem_ec_param,
        DSAPARAM   => $pem_dsa_param,
        CURVE_NAME => $curve,
    }
    # new
    key_length  => $len,
    pkeyopt     => { ... },
    paramset    => $pem_ec_param, # or $pem_dsa_param
    curve       => $curve,

The previously unused parameter C<ISSUER> was removed.

=back

=cut
command "generate_key" => {
    password    => { isa => 'Str', required => 1, },
    key_alg     => { isa => 'Str', default => 'rsa' },
    key_length  => { isa => 'Int', default => 2048 },
    curve       => { isa => 'Str', },
    enc_alg     => { isa => 'Str', },
    pkeyopt     => { isa => 'HashRef', },
    paramset    => { isa => 'PEM', },
} => sub {
    my ($self, $params) = @_;
    my $key_alg = lc($params->key_alg);
    my $paramset = $params->paramset;

    my $token = CTX('api')->get_default_token();

    # prepare command definition
    my $command = {
         COMMAND => 'create_pkey',
         KEY_ALG => $key_alg,
         $params->has_enc_alg ? (ENC_ALG => $params->enc_alg) : (),
         PASSWD  => $params->password,
    };

    # RSA
    if ($key_alg eq "rsa") {
        $command->{PKEYOPT} = { rsa_keygen_bits => $params->key_length };
    }
    # EC
    elsif ($key_alg eq "ec") {
        # With openssl <=1.0.1 you need to create EC the same way as DSA
        # means params and key in two steps
        # see http://openssl.6102.n7.nabble.com/EC-private-key-generation-problem-td47261.html
        if (not $paramset) {
            OpenXPKI::Exception->throw(
                message => 'Parameter "curve" must be specified for EC key algorithm'
            ) unless $params->has_curve;

            $paramset = $token->command({
                COMMAND => 'create_params',
                TYPE    => 'EC',
                PKEYOPT => { ec_paramgen_curve => $params->curve }
            })
            or OpenXPKI::Exception->throw(message => 'Error generating EC parameter set');
        }
        $command->{PARAM} = $paramset;
        delete $command->{KEY_ALG};
    }
    # DSA
    elsif ($key_alg eq "dsa") {
        if (not $paramset) {
            $paramset = $token->command({
                COMMAND => 'create_params',
                TYPE    => 'DSA',
                PKEYOPT => { dsa_paramgen_bits => $params->key_length }
            })
            or OpenXPKI::Exception->throw(message => 'Error generating DSA parameter set');
        }
        $command->{PARAM} = $paramset;
        delete $command->{KEY_ALG};
    }

    # add additional options
    if ($params->has_pkeyopt) {
        my $opt = $params->pkeyopt;
        $command->{PKEYOPT}->{$_} = $opt->{$_} for keys %$opt;
    }

    CTX('log')->audit('key')->info("generating private key", { key_alg => $key_alg});

    ##! 16: 'command: ' . Dumper $command
    return $token->command($command);
};

__PACKAGE__->meta->make_immutable;


text:
package org.crazyit.transaction.service;

public class BusinessException extends RuntimeException {

	public BusinessException(String s) {
		super(s);
	}
}


text:
Imports System.IO
Imports System.Xml
Imports System.Collections.Specialized

Public Class EntityXml
    Private Shared Function GetRelation(ByVal node As XmlNode) As EntityRelation
        If node.Name <> "Relation" Then
            Throw New ArgumentException("No es Relation")
        End If

        Dim rel As New EntityRelation()

        rel.Name = GetNodeProperty(node, "Name")
        rel.Type = GetNodeProperty(node, "Type")
        rel.SourceEntityName = GetNodeProperty(node, "SourceEntity")
        rel.TargetEntityName = GetNodeProperty(node, "TargetEntity")
        rel.SourcePropertyName = GetNodeProperty(node, "SourceProperty")
        rel.TargetPropertyName = GetNodeProperty(node, "TargetProperty")
        rel.Description = GetNodeProperty(node, "Description")

        Return rel
    End Function

    Private Shared Sub GetRelations(ByVal entity As Entity, ByVal node As XmlNode)
        If node Is Nothing Then
            Return
        End If
        If node.Name <> "Relations" Then
            Throw New ArgumentException("No es Relations")
        End If

        Dim child As XmlNode
        Dim rel As EntityRelation

        For Each child In node.SelectNodes("Relation")
            rel = GetRelation(child)
            entity.Relations.Add(rel)
        Next
    End Sub

    Private Shared Function GetProperty(ByVal node As XmlNode) As EntityProperty
        If node.Name <> "Property" Then
            Throw New ArgumentException("No es Property")
        End If

        Dim prop As New EntityProperty()

        prop.Name = GetNodeProperty(node, "Name")
        prop.Description = GetNodeProperty(node, "Description")
        prop.Type = GetNodeProperty(node, "Type")
        prop.SqlType = GetNodeProperty(node, "SqlType")

        Return prop
    End Function

    Private Shared Sub GetProperties(ByVal entity As Entity, ByVal node As XmlNode)
        If node Is Nothing Then
            Throw New ArgumentNullException("No hay Properties")
        End If
        If node.Name <> "Properties" Then
            Throw New ArgumentException("No es Properties")
        End If

        Dim child As XmlNode
        Dim prop As EntityProperty

        For Each child In node.SelectNodes("Property")
            prop = GetProperty(child)
            entity.Properties.Add(prop)
        Next
    End Sub

    Public Shared Function GetEntity(ByVal node As XmlNode) As Entity
        If node.Name <> "Entity" Then
            Throw New ArgumentException("No es Entity")
        End If

        If Not node.Attributes("Source") Is Nothing Then
            Return GetEntity(node.Attributes("Source").Value)
        End If

        Dim entity As New Entity()

        entity.Name = GetNodeProperty(node, "Name")
        entity.SetName = GetNodeProperty(node, "SetName")
        entity.Description = GetNodeProperty(node, "Description")
        entity.SqlTable = GetNodeProperty(node, "SqlTable")
        entity.Descriptor = GetNodeProperty(node, "Descriptor")
        entity.SetDescriptor = GetNodeProperty(node, "SetDescriptor")
        entity.Genre = GetNodeProperty(node, "Genre")

        GetProperties(entity, node.SelectSingleNode("Properties"))
        GetRelations(entity, node.SelectSingleNode("Relations"))

        Return entity
    End Function

    Public Shared Function GetEntity(ByVal input As TextReader) As Entity
        Dim docxml As New XmlDocument()

        docxml.Load(input)

        Return GetEntity(docxml.DocumentElement)
    End Function

    Public Shared Function GetEntity(ByVal inputFile As String) As Entity
        Dim docxml As New XmlDocument()

        docxml.Load(Path.Combine(ModelXml.CurrentPath, inputFile))

        Return GetEntity(docxml.DocumentElement)
    End Function
End Class


text:
package io.perbone.udao.configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Provider setting entry.
 *
 * @author Paulo Perbone <pauloperbone@yahoo.com>
 * @since 0.1.0
 */
public class ProviderEntry
{
    private String id;
    private String backendName;
    private String type;
    private Boolean readOnly;
    private Boolean managedBeansOnly;
    private String cacheId;
    private Map<String, String> properties;

    public ProviderEntry()
    {
        id = null;
        type = null;
        readOnly = null;
        managedBeansOnly = null;
        cacheId = null;
        properties = new HashMap<>();
    }

    public String id()
    {
        return id;
    }

    public ProviderEntry id(String id)
    {
        this.id = id;
        return this;
    }

    public String backendName()
    {
        return backendName;
    }

    public ProviderEntry backendName(String backendName)
    {
        this.backendName = backendName;
        return this;
    }

    public String type()
    {
        return type;
    }

    public ProviderEntry type(String type)
    {
        this.type = type;
        return this;
    }

    public Boolean readOnly()
    {
        return readOnly;
    }

    public ProviderEntry readOnly(Boolean readOnly)
    {
        this.readOnly = readOnly;
        return this;
    }

    public Boolean managedBeansOnly()
    {
        return managedBeansOnly;
    }

    public ProviderEntry managedBeansOnly(Boolean managedBeansOnly)
    {
        this.managedBeansOnly = managedBeansOnly;
        return this;
    }

    public String cacheId()
    {
        return cacheId;
    }

    public ProviderEntry cacheId(String cacheId)
    {
        this.cacheId = cacheId;
        return this;
    }

    public Map<String, String> properties()
    {
        return properties;
    }

    public ProviderEntry properties(Map<String, String> properties)
    {
        this.properties = properties;
        return this;
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((cacheId == null) ? 0 : cacheId.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((managedBeansOnly == null) ? 0 : managedBeansOnly.hashCode());
        result = prime * result + ((properties == null) ? 0 : properties.hashCode());
        result = prime * result + ((readOnly == null) ? 0 : readOnly.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ProviderEntry other = (ProviderEntry) obj;
        if (cacheId == null)
        {
            if (other.cacheId != null)
                return false;
        }
        else if (!cacheId.equals(other.cacheId))
            return false;
        if (id == null)
        {
            if (other.id != null)
                return false;
        }
        else if (!id.equals(other.id))
            return false;
        if (managedBeansOnly == null)
        {
            if (other.managedBeansOnly != null)
                return false;
        }
        else if (!managedBeansOnly.equals(other.managedBeansOnly))
            return false;
        if (properties == null)
        {
            if (other.properties != null)
                return false;
        }
        else if (!properties.equals(other.properties))
            return false;
        if (readOnly == null)
        {
            if (other.readOnly != null)
                return false;
        }
        else if (!readOnly.equals(other.readOnly))
            return false;
        if (type == null)
        {
            if (other.type != null)
                return false;
        }
        else if (!type.equals(other.type))
            return false;
        return true;
    }

    @Override
    public String toString()
    {
        return "ProviderEntry [id=" + id + ", backendName=" + backendName + ", type=" + type + ", readOnly=" + readOnly
                + ", managedBeansOnly=" + managedBeansOnly + ", cacheId=" + cacheId + ", properties=" + properties
                + "]";
    }
}


text:
import { TestBed } from '@angular/core/testing';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { IonicModule } from 'ionic-angular';
import { MobileClient } from './app.component';

describe('MobileApp Component', () => {
  let fixture;
  let component;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [MobileClient],
      imports: [
        IonicModule.forRoot(MobileClient)
      ],
      providers: [
        StatusBar,
        SplashScreen
      ]
    });
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(MobileClient);
    component = fixture.componentInstance;
  });

  it ('should be created', () => {
    expect(component instanceof MobileClient).toBe(true);
  });

});


text:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.scottlogic.dmiley.battleship.activities.oneplayerversus.OnePlayerVersusGameActivity">

    <com.scottlogic.dmiley.battleship.gridview.GameGridView
        android:id="@+id/onePlayerVersusRadarGrid"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        custom:gridForegroundColorOne = "#81b5ed"
        custom:gridForegroundColorTwo = "#81b5ed"
        custom:gridBorderColor = "#c0c0c0"
        custom:coordinateLabelForegroundColor = "#ffffff"
        custom:coordinateLabelBorderColor = "#808080"
        custom:coordinateLabelTextColor = "#000000"
        custom:gridForegroundAlternateColorOne = "#000000"
        custom:gridForegroundAlternateColorTwo = "#000000"
        custom:gridBorderAlternateColor = "#00ff00"
        custom:coordinateLabelForegroundAlternateColor = "#000000"
        custom:coordinateLabelBorderAlternateColor = "#00ff00"
        custom:coordinateLabelTextAlternateColor = "#ffffff"
        custom:textSizeModifier = "5"
        custom:defaultBorderPixelSize = "1"
        custom:validCellSelectedBorderColor = "#00ff00"
        custom:validCellSelectedBorderAlternateColor = "#ffffff"
        custom:invalidCellSelectedBorderColor = "#ff0000"
        custom:validCellSelectedForegroundColor = "#00ff00"
        custom:validCellSelectedForegroundAlternateColor = "#008080"
        custom:invalidCellSelectedForegroundColor = "#ff0000"
        custom:sunkCellFormatColor="#ffd700"
        custom:hitCellFormatColor = "#ff0000"
        custom:missCellFormatColor = "#ffffff"
        custom:missCellFormatAlternateColor = "#00ff00"
        custom:destroyerForegroundColor = "#aa65a8"
        custom:cruiserForegroundColor = "#ea937d"
        custom:submarineForegroundColor = "#8066ff"
        custom:battleshipForegroundColor = "#888888"
        custom:aircraftCarrierForegroundColor = "#347d4e"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">

    <Button
        android:id="@+id/one_player_versus_fire_button"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="@string/fire_button"
        android:enabled="false"
        android:onClick="onFireButtonClicked"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/one_player_versus_toggle_view_button"
        android:onClick="onToggleViewButtonClicked"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/one_player_versus_finish_game_button"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="@string/finish_game_button"
        android:visibility="invisible"
        android:enabled="false"
        android:onClick="onFinishGameButtonClicked"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    </RelativeLayout>

</LinearLayout>


text:
<!DOCTYPE html>
<html>
  <head>
    <title>Sugar Full Development Package</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../stylesheets/custom.css" type="text/css" />
    <link rel="stylesheet" href="../stylesheets/tooltip.css" type="text/css" />
    <!-- jquery -->
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <!-- test suite -->
    <script type="text/javascript" src="../javascripts/suite.js"></script>
    <script type="text/javascript" src="../javascripts/suite-ui.js"></script>
    <script type="text/javascript" src="../javascripts/helpers.js"></script>
    <script type="text/javascript" src="../javascripts/date-helpers.js"></script>
    <script type="text/javascript" src="../javascripts/environment.js"></script>
    <script type="text/javascript" src="../javascripts/tooltip.js"></script>
    <!-- sugar -->
    <script type="text/javascript" src="../../release/sugar-full.dev.js"></script>
    <script type="text/javascript">
      registerEnvironment('core', [
        {
          name: 'core',
          path: '../environments/sugar/',
          tests: [
            'object.js',
            'equals.js',
            'array.js',
            'number.js',
            'string.js',
            'inflections.js',
            'regexp.js',
            'es5.js',
            'date.js',
            'date_range.js',
            'date_zh_cn.js',
            'date_zh_tw.js',
            'date_ko.js',
            'date_ru.js',
            'date_es.js',
            'date_pt.js',
            'date_fr.js',
            'date_it.js',
            'date_de.js',
            'date_ja.js',
            'function.js'
          ]
        }
      ]);

    </script>
  </head>
  <body>
  </body>
</html>


text:
pub struct Person {
    greeting: &'static str,
}

impl Person {
    pub fn new() -> Person {
        Person { greeting: "Hello!" }
    }

    pub fn greet(&self) -> &'static str {
        self.greeting
    }
}


text:
namespace ui {

void TreeModel::SetTitle(TreeModelNode* node,
                         const string16& title) {
  NOTREACHED();
}

int TreeModel::GetIconIndex(TreeModelNode* node) {
  return -1;
}

}  // namespace ui


text:
.. _data_formats:

############
Data Formats
############

``latools`` can be set up to work with pretty much any conceivable text-based data format.
To get your data into ``latools``, you need to think about two things:

1. File Structure
=================
At present, ``latools`` is designed for data that is collected so that each text file contains ablations of a single sample or (a set of) standards, with a name corresponding to the identity of the sample.
An ideal data structure would look something like this:

.. code-block :: bash

    data/
      STD-1.csv
      Sample-1.csv
      Sample-2.csv
      Sample-3.csv
      STD-2.csv

Where each of the .csv files within the 'data/' contains one or more ablations of a single sample, or numerous standards (i.e. STD-1 could contain ablations of three different standards).
The names of the .csv files are used to label the data throughout analysis, so should be unique, and meaningful.
Standards are recognised by :meth:`~latools.latools.analyse` by the presence of identifying characters that are present in all standard names, in this case ``'STD'``.

When importing the data, you give :meth:`~latools.latools.analyse` the ``data/`` folder, and some information about the SRM identifier (``srm_identifier='STD'``) and the file extension (``extension='.csv'``), and it imports all data files in the folder.

.. important:: If you data are not in this format (e.g. all your data are stored in one long file), you'll need to convert them into this format to use ``latools``. You can find Information on how to do this in the :ref:`preprocessing` pages.

.. _data_format_description:
2. Data Format
==============
We tried to make the data import mechanism as simple as possible, but because of the diversity and complexity of formats from different instruments, it can still be a bit tricky to understand. The following will hopefully give you everything you need to write your data format description.

Data Format Description : General Principles
--------------------------------------------

The data format description is stored in a plain-text file, in the `JSON <https://en.wikipedia.org/wiki/JSON>`_ format.
In practice, the format description consists of a number of names entries with corresponding values, which are read and interpreted by ``latools``.
A generic JSON file might look something like this:

.. code-block:: JSON

    {
        'entry_1': 'value',
        'entry_2': ['this', 'is', 'a', 'list'],
        'entry_3': (['a', 'set', 'of'], 'three', 'values')
    }

.. tip:: There are a number of characters that are special in the JSON format (e.g. ``/ \ "``). If you want to include these characters in the file, you have to 'escape' them (i.e. mark them as special) by preceding them with a ``\``. If this sounds too confusing, you can just use an `online formatter <https://www.freeformatter.com/json-escape.html>`_ to make sure all your entries are JSON-safe.

Required Sections
^^^^^^^^^^^^^^^^^
  - ``meta_regex`` contains information on how to read the 'metadata' in the file header. Each entry has the form:

    .. code-block:: JSON

        {
            "meta_regex": {
                "line": [["metadata_name"], "Regular Expression with a capture group."],
            }
        }

    Don't worry at this point if 'Regular Expression' and 'capture group' mean nothing to you. :ref:`We'll get to that later <regex>`.

    Replace ``line`` with an identifier that selects the line in the data file that the regex is applied to. There are two ways to do this.

    **What should** ``"line"`` **be?**:
        - A number in quotations to pick out a line in the file, e.g. ``"3"`` to extract the fourth line of the file (remember here that python starts counting at zero). This works well if the file header is *always* the same.
        - A word or string of characters that is *always* in the line (i.e. won't change from file to file). For example you could use ``"Date:"``, and ``latools`` will find the first line in the file that contains ``Date:`` and apply your regular expression to it. This is useful for formats where the header size can vary depending on the analysis.

    .. tip:: The ``meta_regex`` component of the dataformat description should contain an entry that finds the 'date' of the analysis. This is used to define the time scale of the whole session which background and drift correction depend upon. This should be specified as``{"line": {["date"], "regex_string"}}`` where ``regex_string`` isolates the analysis date of the file in a capture group, as demonstrated `here <https://regex101.com/r/jfPV3Z/1>`_. If you don't identify a date in the metadata, ``latools`` will assume all your analyses were done consecutively with no time gaps between them, and in the order of their sample names. This can cause some unexpected behaviour in the analysis...

  - ``column_id`` contains information on where the column names of the data are, and how to interpret them. This requires 4 specific entries, and should look something like:

  .. code-block:: JSON

    {
        "column_id": {
            "delimiter": "Character that separates column headings, e.g. \t (tab) or , (comma)",
            "timecolumn": "Numeric index of time column. Usually zero (the first column). Must be an integer, without quotations.",
            "name_row": "The line number that contains the column headings. Must be an integer, without quotations",
            "pattern": "A Regular Expression that identifies valid analyte names in a capture group."
        }
    }
  - ``genfromtext_args`` contains information on how to read the actual data table. ``latools`` uses Numpy's :func:`~numpy.genfromtxt` function to read the raw data, so this section can contain any valid arguments for the :func:`~numpy.genfromtxt` function. For example, you might include:

  .. code-block:: JSON

    {
        "genfromtext_args": {
            "delimiter": "Character that separates data values in rows, e.g. \t (tab) or , (comma)",
            "skip_header": "Integer, without quotations, that specifies the number of lines at the start of the file that *don't* contain data values.",
        }
    }

Optional Sections
^^^^^^^^^^^^^^^^^
  - ``preformat_replace``. Particularly awkward data formats may require some 'cleaning' before they're readable by :func:`~numpy.genfromtxt` (e.g. the removal of non-numeric characters). You can do this by optionally including a ``preformat_replace`` section in your dataformat description. This consists of ``{"regex_expression": "replacement_text"}`` pairs, which are applied to the data before import. For example:

  .. code-block:: JSON

    {
        "preformat_replace": {
            "[^0-9, .]+": ""
        }
    }
  will replace all non-numeric characters that are not ``.``, ``,`` or a space with ``""`` (i.e. no text - remove them). The use of ``preformat_replace`` should not be necessary for most dataformats.
  - ``time_format``. ``latools`` attempts to automatically read the ``date`` information identified by ``meta_regex`` (using ``dateutil``'s :func:`~dateutil.parser.parse`), but in rare cases this will fail. If it fails, you'll need to manually specify the date format. Specify the date format using `standard notation for formatting and reading times <https://docs.python.org/3.6/library/datetime.html#strftime-and-strptime-behavior>`_. For example:

  .. code-block:: JSON

    {
        "time_format": "%d-%b-%Y %H:%M:%S"
    }
  will correctly read a time format of "01-Mar-2016 15:23:03".

.. _regex:

Regular Expressions (RegEx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Data import in ``latools`` makes use of `Regular Expressions <https://en.wikipedia.org/wiki/Regular_expression>`_ to identify different parts of your data.
Regular expressions are a way of defining *patterns* that allow the computer to extract information from text that isn't exactly the same in every instance.
A very basic example, if you apply the pattern:
::

    "He's not the Mesiah, (.*)"
to ``"He's not the Mesiah, he's a very naughty boy!"``, the expression will *match* the text, and you'll get ``"he's a very naughty boy!"`` in a *capture group*. To break the expression down a bit:

  - ``He's not the Mesiah, `` tells the computer that you're looking for text containing this phrase.
  - ``.`` signifies 'any character'
  - ``*`` signifies 'anywhere between zero and infinity occurrences of ``.``
  - ``()`` identifies the 'capture group'. The expression would still match without this, but you wouldn't be able to isolate the text within the capture group afterwards.
What would the capture group get if you applied the expression to ``He's not the Mesiah, he just thinks he is...``?

Applying this to metadata extraction, imagine you have a line in your file header like:
::

    Acquired      : Oct 29 2015  03:11:05 pm using AcqMethod OB102915.m
And you need to extract the date (``Oct 29 2015  03:11:05 pm``).
You know that the line always starts with ``Acquired [varying number of spaces] :``, and ends with ``using AcqMethod [some text]``.
The expression:
::

    Acquired +: (.*) using.*
will get the date in its capture group! For a full explanation of how this works, have a look at `this breakdown by Regex101 <https://regex101.com/r/C2Qs5z/1>`_ (Note 'Explanation' section in upper right).

Writing your own Regular Expressions can be tricky to get your head around at first.
We suggest using the superb `Regex101 <https://regex101.com/r/HKNavd/1>`_ site to help you design the Regular Expressions in your data format description. Just copy and paste the text you're working with (e.g. line from file header containing the date), play around with the expression until it works as required, and then copy it across to your dataformat file.

.. note:: If you're stuck on data formats, `submit a question to the mailing list <https://groups.google.com/forum/#!forum/latools>`_ and we'll try to help. If you think you've found a serious problem in the software that will prevent you importing your data, `file an issue on the GitHub project page <https://github.com/oscarbranson/latools/issues/new>`_, and we'll look into updating the software to fix the problem.

Writing a new Data Format Description : Step-By-Step
----------------------------------------------------
Data produced by the UC Davis Agilent 8800 looks like this:

.. code-block:: python
    :linenos:

    C:\Path\To\Data.D
    Intensity Vs Time,CPS
    Acquired      : Oct 29 2015  03:11:05 pm using AcqMethod OB102915.m
    Time [Sec],Mg24,Mg25,Al27,Ca43,Ca44,Mn55,Sr88,Ba137,Ba138
    0.367,666.68,25.00,3100.27,300.00,14205.75,7901.80,166.67,37.50,25.00
    ...

This step-by-step guide will go through the process of writing a dataformat description from scratch for the file.

.. tip:: We're working from scratch here for illustrative purposes. When doing this in reality, you might find the :func:`~latools.helpers.config.get_dataformat_template` (accessible via ``latools.config.get_dataformat_template()``), which creates an annotated data format file for you to adapt.

1. Create an empty file, name it, and give it a ``.json`` extension. Open the file in your favourite text editor. Data in ``.json`` files can be stored in lists (comma separated values inside square brackets, e.g. [1,2,3]) or as {'key': 'value'} pairs inside curly brackets.

2. The data format description contains three named sections - ``meta_regex``, ``column_id`` and ``genfromtext_args``, which we'll store as {'key': 'value'} pairs. Create empty entries for these in your new ``.json`` file. Your file should now look like this:

  .. code-block:: JSON

    {
        "meta_regex": {},
        "column_id": {},
        "genfromtext_args": {}
    }

3. Define the start time of the analysis. In this case, it's ``Oct 29 2015  03:11:05 pm``, but it will be different in other files. We therefore use a regular expression' to define a *pattern* that describes the date. To do this, we'll isolate the line containing the date (line 2 - numbers start at ero in Python!), and head on over to `Regex101 to write our expression <https://regex101.com/r/P1chhB/1>`_. Add this expression to the meta_regex ession, with the line number as its key:

.. code-block:: JSON

    {
        "meta_regex": {
            "2": [["date"],
                   "([A-Z][a-z]+ [0-9]+ [0-9]{4}[ ]+[0-9:]+ [amp]+)"]
        },
        "column_id": {},
        "genfromtext_args": {}
    }

.. tip:: Having trouble with Regular Expressions? We really recommend `Regex101 <http://regex101.com>`_!

4. Set some parameters that define where the column names are. ``name_row`` defines which row the column names are in (`3`), ``delimeter`` describes hat character separates the column names (`,`), ``timecolumn`` is the numberical index of the column containing the 'time' data (in this case, `0`). his will grab everything in row 3 that's separated by a comma, and tell ``latools`` that the first column contains the time info. Now we need to tell t which columns contain the analyte names. We'll do this with a regular expression again, copying the entire column over to `Regex101 to help us write he expression <https://regex101.com/r/cOG8dN/1>`_. Put all this information into the "column_id" section:

.. code-block:: JSON

    {
        "meta_regex": {
            "2": [["date"],
                   "([A-Z][a-z]+ [0-9]+ [0-9]{4}[ ]+[0-9:]+ [amp]+)"]
        },
        "column_id": {
            "name_row": 3,
            "delimiter": ",",
            "timecolumn": 0,
            "pattern": "([A-z]{1,2}[0-9]{1,3})"
        },
        "genfromtext_args": {}
    }

5. Finally, we need to add some parameters that tell ``latools`` how to read the actual data table. In this case, we want to skip the first 4 lines, nd then tell it that the values are separated by commas. Add this information to the ``genfromtext_args`` section:

.. code-block:: JSON

    {
        "meta_regex": {
            "2": [["date"],
                   "([A-Z][a-z]+ [0-9]+ [0-9]{4}[ ]+[0-9:]+ [amp]+)"]
        },
        "column_id": {
            "name_row": 3,
            "delimiter": ",",
            "timecolumn": 0,
            "pattern": "([A-z]{1,2}[0-9]{1,3})"
        },
        "genfromtext_args": {
            "delimiter": ",",
            "skip_header": 4
        }
    }

6. Test the format description, using the :func:`~latools.helpers.config.test_dataformat` function. In Python:

.. code-block:: python

    import latools as la

    my_dataformat = 'path/to/my/dataformat.json'
    my_datafile = 'path/to/my/datafile.csv

    la.config.test_dataformat(my_datafile, my_dataformat)

This will go through the data import process for you file, printing out the results of each stage, so if it fails you can see *where* if failed, and ix the problem.

7. Fix any errors, and you're done! You have a working data description.

I've written my dataformat, now what?
-------------------------------------

Once you're happy with your data format description, put it in a text file, and save it as 'my_dataformat.json' (obviously replace my_dataformat with something meaningful...).
When you want to import data using your newly defined format, you can point ``latools`` towards it by specifying ``dataformat='my_dataformat.dict'`` when starting a data analysis.
Alternatively, you can define a new :ref:`manage-configurations`, to make this the default data format for your setup.


text:
require 'perscribo/cucumber/version'

require 'perscribo/cucumber/formatter'


text:
<!DOCTYPE HTML>

		<meta http-equiv="Content-Type" content="html/html; charset=utf-8" />
		<title>Sample_ObjectiveC_Tutorial Document</title>
		<meta id="xcode-display" name="xcode-display" content="render"/>
		<link rel="stylesheet" type="text/css" href="../../css/styles.css" media="all" />
		<link rel="stylesheet" type="text/css" media="print" href="../../css/stylesPrint.css" />
		<meta name="generator" content="appledoc 2.1 (build 858)" />

Leap Motion API

				Leap Motion

Sample_ObjectiveC_Tutorial Document

Understanding the Objective-C Sample Application

This article discusses the Objective-C sample application included with

the Leap SDK. After reading this article, you should be ready to access

Leap hand tracking data from your own Objective-C applications.

In the Leap SDK folder, you can find the following files used for this

article:

LeapSDK/samples/SampleObjectiveC.xcodeproj — Xcode project file.

LeapSDK/samples/ObjectiveC/Sample.h — Header file for the sample

code which interacts with the Leap library.

LeapSDK/samples/ObjectiveC/Sample.m — Sample code that interacts

with the Leap library to print out tracking data.

LeapSDK/samples/ObjectiveC/* — Miscellaneous standard OS X

application files.

LeapSDK/include/LeapObjectiveC.h — Header file for the Leap

Objective C++ wrapper.

LeapSDK/include/LeapObjectiveC.mm — Objective C++ wrapper for the

Leap library.

LeapSDK/include/Leap.h — Leap C++ API class and struct

definitions.

LeapSDK/include/LeapMath.h — Leap C++ API Vector and Matrix class

and struct definitions.

LeapSDK/lib/libLeap.dylib — Leap library for Mac.

All the files needed for the sample application are already referenced

in the SampleObjectiveC Xcode project file.

Overview

In a nutshell, the Leap motion tracking device detects and tracks hands

and fingers placed within its field of view. The Leap captures this data

one frame at a time. Your applications can use the Leap API to access

this data.

The sample application demonstrates how to use the Leap API to listen

for frame events dispatched by the Leap and how to access the hand and

finger data in each frame. The application is a small program that

prints information about detected hands and fingers to standard output.

The sample application uses most of the key classes in the Leap API,

including:

LeapController — the interface between the Leap and your

application

LeapFrame — contains a set of hand and finger tracking data

LeapHand — contains tracking data for a detected hand

LeapFinger — contains tracking data for a detected finger

LeapVector — represents a 3D position or directional vector

LeapGesture — represents a recognized gesture.

For more detailed information about these classes, pleases refer to the

Leap API reference documentation.

Using the Leap Objective-C Wrapper

The Objective-C wrapper translates the classes in the standard Leap C++

API into classes that can be used directly from Objective-C.

The main difference between the Objective-C API and the Leap C++ API is

the Listener callback mechanism. In Objective-C, you can use either an

NSNotification-based listener or the LeapDelegate protocol to observe

Leap events dispatched by a LeapController object. Another difference in

the two APIs is that the class names of the Objective-C wrapper use the

prefix Leap. Thus, the C++ Controller class is named

LeapController in Objective-C.

Creating a LeapController object

The LeapController class provides the main interface between the Leap

and your application. When you create a LeapController object, it

connects to the Leap software running on the computer and makes hand

tracking data available through <LeapFrame> objects. You can access

these Frame objects by instantiating a LeapController object and calling

the [LeapController frame:] method.

If your application has a natural update loop or frame rate, then you

can call the LeapController frame: method as part of this

update. Otherwise, you can either receive NSNotifications when a new

Frame of data is ready or add a delegate to the controller object.

The sample application creates a LeapController object in the run

method of the Sample class. The Sample class adopts the LeapListener

protocol and adds itself to the LeapController object so that it can

receive the Leap events:

- (void)run
{
    controller = [[LeapController alloc] init];
    [controller addListener:self];
    NSLog(@"running");
}

The [LeapController addListener:] method automatically

subscribes the listener object to receive NSNotifications for the

members of the LeapListener protocol which it implements.

Note that the Leap library is multi-threaded. Using the

NSNotification-based listener avoids most threading issues since all of

the Leap callback functions are invoked on the main thread. If you use a

delegate instead, be aware that the delegate functions are invoked from

threads created by the Leap library, not the main, UI thread.

Adopting the LeapListener protocol

To add a listener object to a controller, create an object adopting the

LeapListener protocol. All the protocol methods are optional. The Sample

class implements the methods in the LeapListener protocol to handle

notifications dispatched by the Leap. The methods include:

[LeapListener onInit:] — dispatched once, when the controller to

which the delegate is registered is initialized.

[LeapListener onConnect:] — dispatched when the controller connects

to the Leap and is ready to begin sending frames of motion tracking

data.

[LeapListener onDisconnect:] — dispatched if the controller

disconnects from the Leap (for example, if you unplug the Leap

device or shut down the Leap software).

[LeapListener onExit:] — Not implemented for notifications; will

never be dispatched.

[LeapListener onFrame:] — dispatched when a new frame of motion

tracking data is available.

In the lifecycle notification handlers, onInit, onConnect, and

onDisconnect, the sample application simply prints a message to the

NSLog output. For the onFrame notification, the handler implementation

does a bit more work. When the controller dispatches

onFrame:, the method gets the latest frame of motion

tracking data and prints information about the detected objects to the

NSLog output.

You must add a listener to the LeapController to use NSNotifications.

While any object can subscribe to the Leap notifications, the controller

does not dispatch notifications unless there is at least one Listener

explicitly assigned.

Adopting the LeapDelgate Protocol

If you prefer to use a delegate rather than NSNotifications, you can

create an object adopting the LeapDelegate protocol. The methods of the

LeapDelegate protocol are the same as the LeapListener protocol except

that they take a LeapController object as a parameter instead of an

NSNotification object. The controller object invokes the methods defined

in your LeapDelegate implementation whenever a new frame of tracking

data is available (and also for a few other Leap events).

You can assign either a LeapListener object or a LeapDelegate object to

a controller, but not both.

Getting a Frame of data

The LeapController object dispatches an NSNotification object when the

Leap generates a new frame of motion tracking data. The notification

object includes a reference to the controller. You can access the new

frame of data by calling the controller object’s frame: method.

The LeapFrame object returned by frame: is the top level of the

Leap model heirarchy and provides such information as a frame ID, a

timestamp, and lists containing the hands, fingers, and tools in view.

The following code defined in the Sample class’s implementation of the

onFrame: method gets the most recent LeapFrame object from

the controller, retrieves the list of LeapHand objects from the LeapFrame and

then logs the frame ID, timestamp, and the number of hands, fingers, and

tools detected in the frame:

- (void)onFrame:(NSNotification *)notification;
{
    LeapController *aController = (LeapController *)[notification object];

    // Get the most recent frame and report some basic information
    LeapFrame *frame = [aController frame:0];
    NSLog(@"Frame id: %lld, timestamp: %lld, hands: %ld, fingers: %ld, tools: %ld",
          [frame id], [frame timestamp], [[frame hands] count],
          [[frame fingers] count], [[frame tools] count]);

The method goes on to examine the first hand in the list of hands:

if ([[frame hands] count] != 0) {
    // Get the first hand
    LeapHand *hand = [[frame hands] objectAtIndex:0];

A LeapHand object contains an ID, properties representing the hand's

physical characteristics, and a list of LeapFinger objects. Each

LeapFinger object contains an ID and properties representing the

characteristic of the finger.

Once it has retrieved a hand, the method checks it for fingers and then

averages the finger tip positions, printing the result with the number

of fingers:

// Check if the hand has any fingers
NSArray *fingers = [hand fingers];
if ([fingers count] != 0) {
    // Calculate the hand's average finger tip position
    LeapVector *avgPos = [[LeapVector alloc] init];
    for (int i = 0; i < [fingers count]; i++) {
        LeapFinger *finger = [fingers objectAtIndex:i];
        avgPos = [avgPos plus:[finger tipPosition]];
    }
    avgPos = [avgPos divide:[fingers count]];
    NSLog(@"Hand has %ld fingers, average finger tip position %@",
          [fingers count], avgPos);
}

Next, the method prints the radius of a sphere fit to the hand's

curvature, along with the hand’s palm position:

// Get the hand's sphere radius and palm position
NSLog(@"Hand sphere radius: %f mm, palm position: %@",
      [hand sphereRadius], [hand palmPosition]);

Finally, the onFrame: method uses LeapVector class methods

to get the hand’s pitch, roll, and yaw angles from the hand’s normal

vector and the direction vectors. The angles are converted from radians

to degrees:

// Get the hand's normal vector and direction
const LeapVector *normal = [hand palmNormal];
const LeapVector *direction = [hand direction];

// Calculate the hand's pitch, roll, and yaw angles
NSLog(@"Hand pitch: %f degrees, roll: %f degrees, yaw: %f degrees\n",
      [direction pitch] * LEAP_RAD_TO_DEG,
      [normal roll] * LEAP_RAD_TO_DEG,
      [direction yaw] * LEAP_RAD_TO_DEG);

Getting Gestures

To receive gestures from the Leap, you first have to enable recognition

for each type of gesture you are interested in. You can enable gesture

recognition any time after the controller connects to the Leap. In the

sample program, all gestures are enabled in the onConnect:

handler using the [LeapController enableGesture:enable:] method:

- (void)onConnect:(NSNotification *)notification;
{
    NSLog(@"Connected");
    LeapController *aController = (LeapController *)[notification object];
    [aController enableGesture:LEAP_GESTURE_TYPE_CIRCLE enable:YES];
    [aController enableGesture:LEAP_GESTURE_TYPE_KEY_TAP enable:YES];
    [aController enableGesture:LEAP_GESTURE_TYPE_SCREEN_TAP enable:YES];
    [aController enableGesture:LEAP_GESTURE_TYPE_SWIPE enable:YES];
}

The Leap adds LeapGesture objects representing each recognized movement

pattern to the gestures list in the Frame object. In the

onFrame: handler, the sample application loops through the

gesture list and prints information about each one to the standard

output. This operation is performed with a standard for-loop and switch

statement.

It is sometimes useful to compare the properties of a gesture in the

current frame to those from an earlier frame. For example, the circle

gesture has a progress attribute that describes how many times the

finger has traversed the circle. This is the total progress, however; if

you want the progress between frames, you must subtract the progress

value of the gesture in the previous frame. You can do this by looking

up the gesture in the previous frame using the gesture ID. The following

code calculates the progress since the previous frame to derive the

angle in radians:

// Calculate the angle swept since the last frame
float sweptAngle = 0;
if(gesture.state != LEAP_GESTURE_STATE_START) {
    LeapGesture *previousUpdate = [[aController frame:1] gesture:gesture.id];
    sweptAngle = (gesture.progress - previousUpdate.progress) * 2 * LEAP_PI;
}

The full code for the gesture loop is:

NSArray *gestures = [frame gestures:nil];
for (int g = 0; g < [gestures count]; g++) {
    LeapGesture *gesture = [gestures objectAtIndex:g];
    switch (gesture.type) {
        case LEAP_GESTURE_TYPE_CIRCLE: {
            LeapCircleGesture *circleGesture = (LeapCircleGesture *)gesture;
            // Calculate the angle swept since the last frame
            float sweptAngle = 0;
            if(circleGesture.state != LEAP_GESTURE_STATE_START) {
                LeapCircleGesture *previousUpdate =
                    (LeapCircleGesture *)[[aController frame:1] gesture:gesture.id];
                sweptAngle = (circleGesture.progress - previousUpdate.progress) * 2 * LEAP_PI;
            }

            NSLog(@"Circle id: %d, %@, progress: %f, radius %f, angle: %f degrees",
                  circleGesture.id, [Sample stringForState:gesture.state],
                  circleGesture.progress, circleGesture.radius, sweptAngle * LEAP_RAD_TO_DEG);
            break;
        }
        case LEAP_GESTURE_TYPE_SWIPE: {
            LeapSwipeGesture *swipeGesture = (LeapSwipeGesture *)gesture;
            NSLog(@"Swipe id: %d, %@, position: %@, direction: %@, speed: %f",
                  swipeGesture.id, [Sample stringForState:swipeGesture.state],
                  swipeGesture.position, swipeGesture.direction, swipeGesture.speed);
            break;
        }
        case LEAP_GESTURE_TYPE_KEY_TAP: {
            LeapKeyTapGesture *keyTapGesture = (LeapKeyTapGesture *)gesture;
            NSLog(@"Key Tap id: %d, %@, position: %@, direction: %@",
                  keyTapGesture.id, [Sample stringForState:keyTapGesture.state],
                  keyTapGesture.position, keyTapGesture.direction);
            break;
        }
        case LEAP_GESTURE_TYPE_SCREEN_TAP: {
            LeapScreenTapGesture *screenTapGesture = (LeapScreenTapGesture *)gesture;
            NSLog(@"Screen Tap id: %d, %@, position: %@, direction: %@",
                  screenTapGesture.id, [Sample stringForState:screenTapGesture.state],
                  screenTapGesture.position, screenTapGesture.direction);
            break;
        }
        default:
            NSLog(@"Unknown gesture type");
            break;
    }
}

Running the sample

To run the sample application:

Plug the Leap device into a USB port and place it in front of you.

If you haven’t already, install the Leap software.

Start the Leap software. If prompted, enter your registered email

address and password. The Leap icon appears on the finder bar and

turns green when ready.

Open the SampleObjectiveC project file in Xcode.

Press the Xcode Run button.

You should see the messages “Initialized” and “Connected” printed to

Debug Output area in Xcode when the application initializes and connects

to the Leap. You should then see frame information printed to the Debug

Output area each time the Leap dispatches the onFrame event. When you

place a hand above the Leap, you should also see finger and palm

position information printed. Note that the application window is

intentionally left blank. All output appears in the Xcode Debug Output

area.

Now that you have seen how to access motion tracking data from the Leap,

you can begin developing your own Objective-C applications that

integrate the Leap.

© 2013 Leap Motion. All rights reserved. (Last updated: 2013-07-19)

						Generated by appledoc 2.1 (build 858).