text:
package com.orientechnologies.orient.core.sql;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.util.OPair;
import com.orientechnologies.common.util.OTriple;
import com.orientechnologies.orient.core.command.OCommandDistributedReplicateRequest;
import com.orientechnologies.orient.core.command.OCommandRequest;
import com.orientechnologies.orient.core.command.OCommandRequestText;
import com.orientechnologies.orient.core.command.OCommandResultListener;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.OTrackedMap;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.exception.OConcurrentModificationException;
import com.orientechnologies.orient.core.exception.ORecordNotFoundException;
import com.orientechnologies.orient.core.metadata.OMetadataInternal;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.OSecurity;
import com.orientechnologies.orient.core.query.OQuery;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ODocumentInternal;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.core.sql.filter.OSQLFilter;
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItem;
import com.orientechnologies.orient.core.sql.query.OSQLAsynchQuery;
import com.orientechnologies.orient.core.storage.ORecordDuplicatedException;
import com.orientechnologies.orient.core.storage.OStorage;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * SQL UPDATE command.
 *
 * @author Luca Garulli
 *
 */
public class OCommandExecutorSQLUpdate extends OCommandExecutorSQLRetryAbstract implements OCommandDistributedReplicateRequest,
    OCommandResultListener {
  public static final String                    KEYWORD_UPDATE    = "UPDATE";
  private static final String                   KEYWORD_ADD       = "ADD";
  private static final String                   KEYWORD_PUT       = "PUT";
  private static final String                   KEYWORD_REMOVE    = "REMOVE";
  private static final String                   KEYWORD_INCREMENT = "INCREMENT";
  private static final String                   KEYWORD_MERGE     = "MERGE";
  private static final String                   KEYWORD_UPSERT    = "UPSERT";
  private static final Object                   EMPTY_VALUE       = new Object();
  private List<OPair<String, Object>>           setEntries        = new ArrayList<OPair<String, Object>>();
  private List<OPair<String, Object>>           addEntries        = new ArrayList<OPair<String, Object>>();
  private List<OTriple<String, String, Object>> putEntries        = new ArrayList<OTriple<String, String, Object>>();
  private List<OPair<String, Object>>           removeEntries     = new ArrayList<OPair<String, Object>>();
  private List<OPair<String, Number>>           incrementEntries  = new ArrayList<OPair<String, Number>>();
  private ODocument                             merge             = null;
  private String                                lockStrategy      = "NONE";
  private OReturnHandler                        returnHandler     = new ORecordCountHandler();
  private OQuery<?>                             query;
  private OSQLFilter                            compiledFilter;
  private String                                subjectName;
  private OCommandParameters                    parameters;
  private boolean                               upsertMode        = false;
  private boolean                               isUpsertAllowed   = false;
  private boolean                               updated           = false;
  private OClass                                clazz             = null;
  private DISTRIBUTED_EXECUTION_MODE            distributedMode;

  @SuppressWarnings("unchecked")
  public OCommandExecutorSQLUpdate parse(final OCommandRequest iRequest) {
    final OCommandRequestText textRequest = (OCommandRequestText) iRequest;

    String queryText = textRequest.getText();
    String originalQuery = queryText;
    try {
      queryText = preParse(queryText, iRequest);
      textRequest.setText(queryText);

      final ODatabaseDocument database = getDatabase();

      init((OCommandRequestText) iRequest);

      setEntries.clear();
      addEntries.clear();
      putEntries.clear();
      removeEntries.clear();
      incrementEntries.clear();
      content = null;
      merge = null;

      query = null;

      parserRequiredKeyword(KEYWORD_UPDATE);

      subjectName = parserRequiredWord(false, "Invalid target", " =><,\r\n");
      if (subjectName == null)
        throwSyntaxErrorException("Invalid subject name. Expected cluster, class, index or sub-query");

      clazz = extractClassFromTarget(subjectName);

      String word = parserNextWord(true);

      if (parserIsEnded()
          || (!word.equals(KEYWORD_SET) && !word.equals(KEYWORD_ADD) && !word.equals(KEYWORD_PUT) && !word.equals(KEYWORD_REMOVE)
              && !word.equals(KEYWORD_INCREMENT) && !word.equals(KEYWORD_CONTENT) && !word.equals(KEYWORD_MERGE)
              && !word.equals(KEYWORD_LOCK) && !word.equals(KEYWORD_RETURN) && !word.equals(KEYWORD_UPSERT)))
        throwSyntaxErrorException("Expected keyword " + KEYWORD_SET + "," + KEYWORD_ADD + "," + KEYWORD_CONTENT + ","
            + KEYWORD_MERGE + "," + KEYWORD_PUT + "," + KEYWORD_REMOVE + "," + KEYWORD_INCREMENT + "," + KEYWORD_LOCK + " or "
            + KEYWORD_RETURN + " or " + KEYWORD_UPSERT);

      while ((!parserIsEnded() && !parserGetLastWord().equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE))
          || parserGetLastWord().equals(KEYWORD_UPSERT)) {
        word = parserGetLastWord();

        if (word.equals(KEYWORD_CONTENT))
          parseContent();
        else if (word.equals(KEYWORD_MERGE))
          parseMerge();
        else if (word.equals(KEYWORD_SET))
          parseSetFields(clazz, setEntries);
        else if (word.equals(KEYWORD_ADD))
          parseAddFields();
        else if (word.equals(KEYWORD_PUT))
          parsePutFields();
        else if (word.equals(KEYWORD_REMOVE))
          parseRemoveFields();
        else if (word.equals(KEYWORD_INCREMENT))
          parseIncrementFields();
        else if (word.equals(KEYWORD_LOCK))
          lockStrategy = parseLock();
        else if (word.equals(KEYWORD_UPSERT))
          upsertMode = true;
        else if (word.equals(KEYWORD_RETURN))
          parseReturn();
        else if (word.equals(KEYWORD_RETRY))
          parseRetry();
        else
          break;

        parserNextWord(true);
      }

      final String additionalStatement = parserGetLastWord();

      if (subjectName.startsWith("(")) {
        subjectName = subjectName.trim();
        query = database.command(new OSQLAsynchQuery<ODocument>(subjectName.substring(1, subjectName.length() - 1), this)
            .setContext(context));

        if (additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE)
            || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LIMIT))
          compiledFilter = OSQLEngine.getInstance().parseCondition(parserText.substring(parserGetCurrentPosition()), getContext(),
              KEYWORD_WHERE);

      } else if (additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE)
          || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LIMIT)
          || additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_LET) || additionalStatement.equals(KEYWORD_LOCK)) {
        query = new OSQLAsynchQuery<ODocument>("select from " + subjectName + " " + additionalStatement + " "
            + parserText.substring(parserGetCurrentPosition()), this);

        isUpsertAllowed = (((OMetadataInternal) getDatabase().getMetadata()).getImmutableSchemaSnapshot().getClass(subjectName) != null);
      } else if (!additionalStatement.isEmpty())
        throwSyntaxErrorException("Invalid keyword " + additionalStatement);
      else
        query = new OSQLAsynchQuery<ODocument>("select from " + subjectName, this);

      if (upsertMode && !isUpsertAllowed)
        throwSyntaxErrorException("Upsert only works with class names ");

      if (upsertMode && !additionalStatement.equals(OCommandExecutorSQLAbstract.KEYWORD_WHERE))
        throwSyntaxErrorException("Upsert only works with WHERE keyword");
    } finally {
      textRequest.setText(originalQuery);
    }

    return this;
  }

  public Object execute(final Map<Object, Object> iArgs) {
    if (subjectName == null)
      throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");

    parameters = new OCommandParameters(iArgs);
    Map<Object, Object> queryArgs;
    if (parameters.size() > 0 && parameters.getByName(0) != null) {
      queryArgs = new HashMap<Object, Object>();
      for (int i = parameterCounter; i < parameters.size(); i++) {
        if (parameters.getByName(i) != null)
          queryArgs.put(i - parameterCounter, parameters.getByName(i));
      }
    } else {
      queryArgs = iArgs;
    }

    query.setUseCache(false);
    query.setContext(context);

    returnHandler.reset();

    if (lockStrategy.equals("RECORD"))
      query.getContext().setVariable("$locking", OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK);

    for (int r = 0; r < retry; ++r) {
      try {

        getDatabase().query(query, queryArgs);

        break;

      } catch (OConcurrentModificationException e) {
        if (r + 1 >= retry)
          // NO RETRY; PROPAGATE THE EXCEPTION
          throw e;

        // RETRY?
        if (wait > 0)
          try {
            Thread.sleep(wait);
          } catch (InterruptedException ignored) {
          }
      }
    }

    if (upsertMode && !updated) {
      // IF UPDATE DOES NOT PRODUCE RESULTS AND UPSERT MODE IS ENABLED, CREATE DOCUMENT AND APPLY SET/ADD/PUT/MERGE and so on
      final ODocument doc = subjectName != null ? new ODocument(subjectName) : new ODocument();
      final String suspendedLockStrategy = lockStrategy;
      lockStrategy = "NONE";// New record hasn't been created under exclusive lock - just to avoid releasing locks by result(doc)
      try {
        result(doc);
      } catch (ORecordDuplicatedException e) {
        if (upsertMode)
          // UPDATE THE NEW RECORD
          getDatabase().query(query, queryArgs);
        else
          throw e;
      } catch (ORecordNotFoundException e) {
        if (upsertMode)
          // UPDATE THE NEW RECORD
          getDatabase().query(query, queryArgs);
        else
          throw e;
      } catch (OConcurrentModificationException e) {
        if (upsertMode)
          // UPDATE THE NEW RECORD
          getDatabase().query(query, queryArgs);
        else
          throw e;
      }

      lockStrategy = suspendedLockStrategy;
    }

    return returnHandler.ret();
  }

  /**
   * Update current record.
   */
  @SuppressWarnings("unchecked")
  public boolean result(final Object iRecord) {
    final ODocument record = ((OIdentifiable) iRecord).getRecord();

    if (compiledFilter != null) {
      // ADDITIONAL FILTERING
      if (!(Boolean) compiledFilter.evaluate(record, null, context))
        return false;
    }

    parameters.reset();

    returnHandler.beforeUpdate(record);

    boolean updated = handleContent(record);
    updated |= handleMerge(record);
    updated |= handleSetEntries(record);
    updated |= handleIncrementEnries(record);
    updated |= handleAddEntries(record);
    updated |= handlePutEntries(record);
    updated |= handleRemoveEntries(record);

    if (updated) {
      record.setDirty();
      record.save();
      returnHandler.afterUpdate(record);
      this.updated = true;
    }

    return true;
  }

  @Override
  public String getSyntax() {
    return "UPDATE <class>|cluster:<cluster>> [SET|ADD|PUT|REMOVE|INCREMENT|CONTENT {<JSON>}|MERGE {<JSON>}] [[,] <field-name> = <expression>|<sub-command>]* [LOCK <NONE|RECORD>] [UPSERT] [RETURN <COUNT|BEFORE|AFTER>] [WHERE <conditions>]";
  }

  @Override
  public OCommandDistributedReplicateRequest.DISTRIBUTED_EXECUTION_MODE getDistributedExecutionMode() {
    if (distributedMode == null)
      // REPLICATE MODE COULD BE MORE EFFICIENT ON MASSIVE UPDATES
      distributedMode = upsertMode || query == null || getDatabase().getTransaction().isActive() ? DISTRIBUTED_EXECUTION_MODE.LOCAL
          : DISTRIBUTED_EXECUTION_MODE.REPLICATE;
    return distributedMode;
  }

  @Override
  public DISTRIBUTED_RESULT_MGMT getDistributedResultManagement() {
    return distributedMode == DISTRIBUTED_EXECUTION_MODE.LOCAL ? DISTRIBUTED_RESULT_MGMT.CHECK_FOR_EQUALS
        : DISTRIBUTED_RESULT_MGMT.MERGE;
  }

  @Override
  public void end() {
  }

  @Override
  public int getSecurityOperationType() {
    return ORole.PERMISSION_UPDATE;
  }

  protected void parseMerge() {
    if (!parserIsEnded() && !parserGetLastWord().equals(KEYWORD_WHERE)) {
      final String contentAsString = parserRequiredWord(false, "document to merge expected").trim();
      merge = new ODocument().fromJSON(contentAsString);
      parserSkipWhiteSpaces();
    }

    if (merge == null)
      throwSyntaxErrorException("Document to merge not provided. Example: MERGE { \"name\": \"Jay\" }");
  }

  protected String getBlock(String fieldValue) {
    final int startPos = parserGetCurrentPosition();

    if (fieldValue.startsWith("{") || fieldValue.startsWith("[")) {
      if (startPos > 0)
        parserSetCurrentPosition(startPos - fieldValue.length());
      else
        parserSetCurrentPosition(parserText.length() - fieldValue.length());

      parserSkipWhiteSpaces();
      final StringBuilder buffer = new StringBuilder();
      parserSetCurrentPosition(OStringSerializerHelper.parse(parserText, buffer, parserGetCurrentPosition(), -1,
          OStringSerializerHelper.DEFAULT_FIELD_SEPARATOR, true, true, false, -1, false,
          OStringSerializerHelper.DEFAULT_IGNORE_CHARS));
      fieldValue = buffer.toString();
    }
    return fieldValue;
  }

  /**
   * Parses the returning keyword if found.
   */
  protected void parseReturn() throws OCommandSQLParsingException {
    parserNextWord(false, " ");
    String mode = parserGetLastWord().trim();

    if (mode.equalsIgnoreCase("COUNT")) {
      returnHandler = new ORecordCountHandler();
    } else if (mode.equalsIgnoreCase("BEFORE") || mode.equalsIgnoreCase("AFTER")) {

      parserNextWord(false, " ");
      String returning = parserGetLastWord().trim();
      Object returnExpression = null;
      if (returning.equalsIgnoreCase(KEYWORD_WHERE) || returning.equalsIgnoreCase(KEYWORD_TIMEOUT)
          || returning.equalsIgnoreCase(KEYWORD_LIMIT) || returning.equalsIgnoreCase(KEYWORD_UPSERT)
          || returning.equalsIgnoreCase(KEYWORD_LOCK) || returning.length() == 0) {
        parserGoBack();
      } else {
        if (returning.startsWith("$") || returning.startsWith("@"))
          returnExpression = (returning.length() > 0) ? OSQLHelper.parseValue(this, returning, this.getContext()) : null;
        else
          throwSyntaxErrorException("record attribute (@attributes) or functions with $current variable expected");
      }

      if (mode.equalsIgnoreCase("BEFORE"))
        returnHandler = new OOriginalRecordsReturnHandler(returnExpression, getContext());
      else
        returnHandler = new OUpdatedRecordsReturnHandler(returnExpression, getContext());

    } else
      throwSyntaxErrorException(" COUNT | BEFORE | AFTER keywords expected");
  }

  private boolean handleContent(ODocument record) {
    boolean updated = false;
    if (content != null) {
      // REPLACE ALL THE CONTENT

      final OClass restricted = getDatabase().getMetadata().getSchema().getClass(OSecurity.RESTRICTED_CLASSNAME);

      final ODocument restrictedFields = new ODocument();
      if (restricted != null) {
        for (OProperty prop : restricted.properties()) {
          restrictedFields.field(prop.getName(), record.field(prop.getName()));
        }

        OClass recordClass = ODocumentInternal.getImmutableSchemaClass(record);
        if (recordClass != null && recordClass.isSubClassOf("V")) {
          for (String fieldName : record.fieldNames()) {
            if (fieldName.startsWith("in_") || fieldName.startsWith("out_")) {
              restrictedFields.field(fieldName, record.field(fieldName));
            }
          }
        } else if (recordClass != null && recordClass.isSubClassOf("E")) {
          for (String fieldName : record.fieldNames()) {
            if (fieldName.equals("in") || fieldName.equals("out")) {
              restrictedFields.field(fieldName, record.field(fieldName));
            }
          }
        }
      }

      record.merge(restrictedFields, false, false);
      record.merge(content, true, false);
      updated = true;
    }
    return updated;
  }

  private boolean handleMerge(ODocument record) {
    boolean updated = false;
    if (merge != null) {
      // MERGE THE CONTENT
      record.merge(merge, true, false);
      updated = true;
    }
    return updated;
  }

  private boolean handleSetEntries(final ODocument record) {
    boolean updated = false;
    // BIND VALUES TO UPDATE
    if (!setEntries.isEmpty()) {
      OSQLHelper.bindParameters(record, setEntries, parameters, context);
      updated = true;
    }
    return updated;
  }

  private boolean handleIncrementEnries(ODocument record) {
    boolean updated = false;
    // BIND VALUES TO INCREMENT
    if (!incrementEntries.isEmpty()) {
      for (OPair<String, Number> entry : incrementEntries) {
        final Number prevValue = record.field(entry.getKey());

        if (prevValue == null)
          // NO PREVIOUS VALUE: CONSIDER AS 0
          record.field(entry.getKey(), entry.getValue());
        else
          // COMPUTING INCREMENT
          record.field(entry.getKey(), OType.increment(prevValue, entry.getValue()));
      }
      updated = true;
    }
    return updated;
  }

  private boolean handleAddEntries(ODocument record) {
    boolean updated = false;
    // BIND VALUES TO ADD
    Object fieldValue;
    for (OPair<String, Object> entry : addEntries) {
      Collection<Object> coll = null;
      ORidBag bag = null;
      if (!record.containsField(entry.getKey())) {
        // GET THE TYPE IF ANY
        if (ODocumentInternal.getImmutableSchemaClass(record) != null) {
          OProperty prop = ODocumentInternal.getImmutableSchemaClass(record).getProperty(entry.getKey());
          if (prop != null && prop.getType() == OType.LINKSET)
            // SET TYPE
            coll = new HashSet<Object>();
          if (prop != null && prop.getType() == OType.LINKBAG) {
            // there is no ridbag value already but property type is defined as LINKBAG
            bag = new ORidBag();
            bag.setOwner(record);
            record.field(entry.getKey(), bag);
          }
        }
        if (coll == null && bag == null)
          // IN ALL OTHER CASES USE A LIST
          coll = new ArrayList<Object>();
        if (coll != null) {
          // containField's condition above does NOT check subdocument's fields so
          Collection<Object> currColl = record.field(entry.getKey());
          if (currColl == null) {
            record.field(entry.getKey(), coll);
            coll = record.field(entry.getKey());
          } else
            coll = currColl;
        }

      } else {
        fieldValue = record.field(entry.getKey());

        if (fieldValue instanceof Collection<?>)
          coll = (Collection<Object>) fieldValue;
        else if (fieldValue instanceof ORidBag)
          bag = (ORidBag) fieldValue;
        else
          continue;
      }

      final Object value = extractValue(record, entry);

      if (coll != null) {
        if (value instanceof OIdentifiable)
          coll.add(value);
        else
          OMultiValue.add(coll, value);
      } else {
        if (!(value instanceof OIdentifiable))
          throw new OCommandExecutionException("Only links or records can be added to LINKBAG");

        bag.add((OIdentifiable) value);
      }
      updated = true;
    }
    return updated;
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  private boolean handlePutEntries(ODocument record) {
    boolean updated = false;
    if (!putEntries.isEmpty()) {
      // BIND VALUES TO PUT (AS MAP)
      for (OTriple<String, String, Object> entry : putEntries) {
        Object fieldValue = record.field(entry.getKey());

        if (fieldValue == null) {
          if (ODocumentInternal.getImmutableSchemaClass(record) != null) {
            final OProperty property = ODocumentInternal.getImmutableSchemaClass(record).getProperty(entry.getKey());
            if (property != null
                && (property.getType() != null && (!property.getType().equals(OType.EMBEDDEDMAP) && !property.getType().equals(
                    OType.LINKMAP)))) {
              throw new OCommandExecutionException("field " + entry.getKey() + " is not defined as a map");
            }
          }
          fieldValue = new HashMap<String, Object>();
          record.field(entry.getKey(), fieldValue);
        }

        if (fieldValue instanceof Map<?, ?>) {
          Map<String, Object> map = (Map<String, Object>) fieldValue;

          OPair<String, Object> pair = entry.getValue();

          Object value = extractValue(record, pair);

          if (record.getSchemaClass() != null) {
            final OProperty property = record.getSchemaClass().getProperty(entry.getKey());
            if (property != null && property.getType().equals(OType.LINKMAP) && !(value instanceof OIdentifiable)) {
              throw new OCommandExecutionException("field " + entry.getKey() + " defined of type LINKMAP accept only link values");
            }
          }
          if (OType.LINKMAP.equals(OType.getTypeByValue(fieldValue)) && !(value instanceof OIdentifiable)) {
            map = new OTrackedMap(record, map, Object.class);
            record.field(entry.getKey(), map, OType.EMBEDDEDMAP);
          }
          map.put(pair.getKey(), value);
          updated = true;
        }
      }
    }
    return updated;
  }

  private boolean handleRemoveEntries(ODocument record) {
    boolean updated = false;
    if (!removeEntries.isEmpty()) {
      // REMOVE FIELD IF ANY
      for (OPair<String, Object> entry : removeEntries) {
        Object value = extractValue(record, entry);

        if (value == EMPTY_VALUE) {
          record.removeField(entry.getKey());
          updated = true;
        } else {
          final Object fieldValue = record.field(entry.getKey());

          if (fieldValue instanceof Collection<?>) {
            updated = removeFromCollection(updated, value, (Collection<?>) fieldValue);
          } else if (fieldValue instanceof Map<?, ?>) {
            updated = removeFromMap(updated, value, (Map<?, ?>) fieldValue);
          } else if (fieldValue instanceof ORidBag) {
            updated = removeFromBag(record, updated, value, (ORidBag) fieldValue);
          }
        }
      }
    }
    return updated;
  }

  private boolean removeFromCollection(boolean updated, Object value, Collection<?> collection) {
    if (value instanceof Collection<?>)
      updated |= collection.removeAll(((Collection) value));
    else
      updated |= collection.remove(value);
    return updated;
  }

  private boolean removeFromMap(boolean updated, Object value, Map<?, ?> map) {
    if (value instanceof Collection) {
      for (Object o : ((Collection) value)) {
        updated |= map.remove(o) != null;
      }
    } else
      updated |= map.remove(value) != null;
    return updated;
  }

  private boolean removeFromBag(ODocument record, boolean updated, Object value, ORidBag bag) {
    if (value instanceof Collection) {
      for (Object o : ((Collection) value)) {
        updated |= removeSingleValueFromBag(bag, o, record);
      }
    } else
      updated |= removeSingleValueFromBag(bag, value, record);
    return updated;
  }

  private boolean removeSingleValueFromBag(ORidBag bag, Object value, ODocument record) {
    if (!(value instanceof OIdentifiable))
      throw new OCommandExecutionException("Only links or records can be removed from LINKBAG");

    bag.remove((OIdentifiable) value);
    return record.isDirty();
  }

  private Object extractValue(ODocument record, OPair<String, Object> entry) {
    Object value = entry.getValue();

    if (value instanceof OSQLFilterItem)
      value = ((OSQLFilterItem) value).getValue(record, null, context);
    else if (value instanceof OCommandRequest)
      value = ((OCommandRequest) value).execute(record, null, context);

    if (value instanceof OIdentifiable)
      // USE ONLY THE RID TO AVOID CONCURRENCY PROBLEM WITH OLD VERSIONS
      value = ((OIdentifiable) value).getIdentity();
    return value;
  }

  private void parseAddFields() {
    String fieldName;
    String fieldValue;

    boolean firstLap = true;
    while (!parserIsEnded() && (firstLap || parserGetLastSeparator() == ',' || parserGetCurrentChar() == ',')
        && !parserGetLastWord().equals(KEYWORD_WHERE)) {

      fieldName = parserRequiredWord(false, "Field name expected");
      parserRequiredKeyword("=");
      fieldValue = parserRequiredWord(false, "Value expected", " =><,\r\n");

      final Object v = convertValue(clazz, fieldName, getFieldValueCountingParameters(fieldValue));

      // INSERT TRANSFORMED FIELD VALUE
      addEntries.add(new OPair<String, Object>(fieldName, v));
      parserSkipWhiteSpaces();

      firstLap = false;
    }

    if (addEntries.size() == 0)
      throwSyntaxErrorException("Entries to add <field> = <value> are missed. Example: name = 'Bill', salary = 300.2.");
  }

  private void parsePutFields() {
    String fieldName;
    String fieldKey;
    String fieldValue;

    boolean firstLap = true;
    while (!parserIsEnded() && (firstLap || parserGetLastSeparator() == ',' || parserGetCurrentChar() == ',')
        && !parserGetLastWord().equals(KEYWORD_WHERE)) {

      fieldName = parserRequiredWord(false, "Field name expected");
      parserRequiredKeyword("=");
      fieldKey = parserRequiredWord(false, "Key expected");
      fieldValue = getBlock(parserRequiredWord(false, "Value expected", " =><,\r\n"));

      // INSERT TRANSFORMED FIELD VALUE
      putEntries.add(new OTriple(fieldName, (String) getFieldValueCountingParameters(fieldKey),
          getFieldValueCountingParameters(fieldValue)));
      parserSkipWhiteSpaces();

      firstLap = false;
    }

    if (putEntries.size() == 0)
      throwSyntaxErrorException("Entries to put <field> = <key>, <value> are missed. Example: name = 'Bill', 30");
  }

  private void parseRemoveFields() {
    String fieldName;
    String fieldValue;
    Object value;

    boolean firstLap = true;
    while (!parserIsEnded() && (firstLap || parserGetLastSeparator() == ',' || parserGetCurrentChar() == ',')
        && !parserGetLastWord().equals(KEYWORD_WHERE)) {

      fieldName = parserRequiredWord(false, "Field name expected");
      final boolean found = parserOptionalKeyword("=", "WHERE", "RETURN", "LOCK", "LIMIT");
      if (found)
        if (parserGetLastWord().equals("=")) {
          fieldValue = getBlock(parserRequiredWord(false, "Value expected", " =><,\r\n"));
          value = getFieldValueCountingParameters(fieldValue);
        } else {
          parserGoBack();
          value = EMPTY_VALUE;
        }
      else
        value = EMPTY_VALUE;

      // INSERT FIELD NAME TO BE REMOVED
      removeEntries.add(new OPair<String, Object>(fieldName, value));
      parserSkipWhiteSpaces();

      firstLap = false;
    }

    if (removeEntries.size() == 0)
      throwSyntaxErrorException("Field(s) to remove are missed. Example: name, salary");
  }

  private void parseIncrementFields() {
    String fieldName;
    String fieldValue;

    boolean firstLap = true;
    while (!parserIsEnded() && (firstLap || parserGetLastSeparator() == ',') && !parserGetLastWord().equals(KEYWORD_WHERE)) {

      fieldName = parserRequiredWord(false, "Field name expected");
      parserRequiredKeyword("=");
      fieldValue = getBlock(parserRequiredWord(false, "Value expected"));

      // INSERT TRANSFORMED FIELD VALUE
      incrementEntries.add(new OPair(fieldName, (Number) getFieldValueCountingParameters(fieldValue)));
      parserSkipWhiteSpaces();

      firstLap = false;
    }

    if (incrementEntries.size() == 0)
      throwSyntaxErrorException("Entries to increment <field> = <value> are missed. Example: salary = -100");
  }

  @Override
  public QUORUM_TYPE getQuorumType() {
    return QUORUM_TYPE.WRITE;
  }
}


text:
https://www.veritas.com/support/en_US/article.TECH126257
https://tweakhound.com/2012/11/13/how-to-fix-the-windows-bootloader/
https://www.eightforums.com/bsod-crashes-debugging/61529-bsod-error-0xc0000225-only-have-access-bios-cmd.html
http://superuser.com/questions/949219/how-to-fix-windows-10-boot-loader-from-windows

## Grub Intro

https://wiki.archlinux.org/index.php/GRUB#Microsoft_Windows_installed_in_UEFI-GPT_mode
https://wiki.archlinux.org/index.php/GRUB#Using_the_rescue_console

redpajama_set_name: RedPajamaGithub


text:
package org.zaproxy.zap.extension.pscanrulesAlpha;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import net.htmlparser.jericho.StartTagType;

import org.parosproxy.paros.Constant;
import org.parosproxy.paros.core.scanner.Alert;
import org.parosproxy.paros.network.HtmlParameter;
import org.parosproxy.paros.network.HttpHeader;
import org.parosproxy.paros.network.HttpMessage;
import org.parosproxy.paros.network.HttpStatusCode;
import org.zaproxy.zap.extension.pscan.PassiveScanThread;
import org.zaproxy.zap.extension.pscan.PluginPassiveScanner;

/**
 * Port for the Watcher passive scanner (http://websecuritytool.codeplex.com/)
 * rule {@code CasabaSecurity.Web.Watcher.Checks.CheckPasvUserControlledCharset}
 */
public class UserControlledCharsetScanner extends PluginPassiveScanner {

	private PassiveScanThread parent = null;

	/**
	 * Prefix for internationalized messages used by this rule
	 */
	private static final String MESSAGE_PREFIX = "pscanalpha.usercontrolledcharset.";

	@Override
	public String getName() {
		return Constant.messages.getString(MESSAGE_PREFIX + "name");
	}

	@Override
	public void scanHttpRequestSend(HttpMessage msg, int id) {
		// do nothing
	}

	@Override
	public void scanHttpResponseReceive(HttpMessage msg, int id, Source source) {
		if (msg.getResponseHeader().getStatusCode() != HttpStatusCode.OK) {
			return;
		}

    	String responseBody = msg.getRequestBody().toString();
    	if (responseBody == null) {
    		return;
    	}

    	Set<HtmlParameter> params = new TreeSet<>(msg.getFormParams());
    	params.addAll(msg.getUrlParams());
    	if (params.size() == 0) {
    		return;
    	}

    	if (!isResponseHTML(msg, source) && !isResponseXML(source)) {
    		return;
    	}

        if (isResponseHTML(msg, source)) {
        	checkMetaContentCharset(msg, id, source, params);
        } else if (isResponseXML(source)) {
        	checkXmlEncodingCharset(msg, id, source, params);
        }

        checkContentTypeCharset(msg, id, params);
	}

	private void checkMetaContentCharset(HttpMessage msg, int id, Source source,
			Set<HtmlParameter> params) {
		List<Element> metaElements = source.getAllElements(HTMLElementName.META);
		if (metaElements == null || metaElements.size() == 0) {
			return;
		}

		for (Element metaElement : metaElements) {
			String httpEquiv = metaElement.getAttributeValue("http-equiv");
			String bodyContentType = metaElement.getAttributeValue("content");

			// If META element defines HTTP-EQUIV and CONTENT attributes,
			// compare charset values
			if (httpEquiv == null || bodyContentType == null ||
					!httpEquiv.equalsIgnoreCase("content-type")) {
				continue;
			}

			String bodyContentCharset = getBodyContentCharset(bodyContentType);
	        for (HtmlParameter param: params) {
	            if (bodyContentCharset.equalsIgnoreCase(param.getValue())) {
	            	raiseAlert(msg, id, "META", "Content-Type", param, bodyContentCharset);
	            }
	        }
		}
	}

	// TODO: taken from CharsetMismatchScanner. Extract into helper method
	private String getBodyContentCharset(String bodyContentType) {
		// preconditions
		assert bodyContentType != null;

		String charset = null;

		bodyContentType = bodyContentType.trim();

		int charsetIndex;
		if ((charsetIndex = bodyContentType.indexOf("charset=")) != -1) {
			// 8 is a length of "charset="
			charset = bodyContentType.substring(charsetIndex + 8);
		}

		return charset;
	}

	private void checkXmlEncodingCharset(HttpMessage msg, int id, Source source,
			Set<HtmlParameter> params) {
		List<StartTag> xmlDeclarationTags = source.getAllStartTags(
				StartTagType.XML_DECLARATION);
		if (xmlDeclarationTags.size() == 0) {
			return;
		}

		StartTag xmlDeclarationTag = xmlDeclarationTags.get(0);
		String encoding = xmlDeclarationTag.getAttributeValue("encoding");

		if (encoding == null || encoding.equals("")) {
			return;
		}

        for (HtmlParameter param: params) {
            if (encoding.equalsIgnoreCase(param.getValue())) {
            	raiseAlert(msg, id, "\\?xml", "encoding", param, encoding);
            }
        }
	}

	private void checkContentTypeCharset(HttpMessage msg, int id, Set<HtmlParameter> params) {
    	String charset = msg.getResponseHeader().getCharset();
        if (charset == null || charset.equals("")) {
            return;
        }

        for (HtmlParameter param: params) {
            if (charset.equalsIgnoreCase(param.getValue())) {
            	raiseAlert(msg, id, "Content-Type HTTP header", "charset", param, charset);
            }
        }
	}

	// TODO: Fix up to support other variations of text/html.
	// FIX: This will match Atom and RSS feeds now, which set text/html but
	// use <?xml> in content

    // TODO: these methods have been extracted from CharsetMismatchScanner
    // I think we should create helper methods for them
	private boolean isResponseHTML(HttpMessage message, Source source) {
		String contentType = message.getResponseHeader().getHeader(
				HttpHeader.CONTENT_TYPE);
		if (contentType == null) {
			return false;
		}

		return contentType.indexOf("text/html") != -1 ||
				contentType.indexOf("application/xhtml+xml") != -1 ||
				contentType.indexOf("application/xhtml") != -1;
	}

	private boolean isResponseXML(Source source) {
		return source.isXML();
	}

	private void raiseAlert(HttpMessage msg, int id, String tag, String attr,
			HtmlParameter param, String charset) {
		Alert alert = new Alert(getPluginId(), Alert.RISK_MEDIUM, Alert.CONFIDENCE_MEDIUM,
				getName());

		alert.setDetail(getDescriptionMessage(), msg.getRequestHeader()
				.getURI().toString(), param.getName(), getExploitMessage(msg),
				getExtraInfoMessage(msg, tag, attr, param, charset),
				getSolutionMessage(), getReferenceMessage(),
				"",	// No evidence
				0,	// TODO CWE Id
				0,	// TODO WASC Id
				msg);

		parent.raiseAlert(id, alert);
	}

	@Override
	public int getPluginId() {
		return 10030;
	}

	@Override
	public void setParent(PassiveScanThread parent) {
		this.parent = parent;
	}

	/*
	 * Rule-associated messages
	 */

	private String getDescriptionMessage() {
		return Constant.messages.getString(MESSAGE_PREFIX + "desc");
	}

	private String getSolutionMessage() {
		return Constant.messages.getString(MESSAGE_PREFIX + "soln");
	}

	private String getReferenceMessage() {
		return Constant.messages.getString(MESSAGE_PREFIX + "refs");
	}

	// XXX Consider removing the parameter msg, it's never used.
	private String getExploitMessage(HttpMessage msg) {
        return Constant.messages.getString(MESSAGE_PREFIX + "exploit");
	}

	// XXX Consider removing the parameter msg, it's never used.
	private String getExtraInfoMessage(HttpMessage msg, String tag, String attr,
			HtmlParameter param, String charset) {
        return Constant.messages.getString(MESSAGE_PREFIX + "extrainfo",
        		tag, attr, param.getName(), param.getValue(), charset);
	}
}


text:
layout: post
title: " English... "
date: 2005-01-09 01:54:00 +0000
comments: true
published: true
categories: ["blog", "archives"]
tags: ["On Software", "On Generalities"]
alias: ["/mattsblog/archive/2005/01/09/246.aspx"]
---

Just downloading FireFox (which is a different internet browser, like Internet Explorer, but stupider), and i came across the download link:

But... I thought the British invented English...

Nerd bit: If you must know, I'm downloading cos of the increasing number of people using it, and when you write websites, you need to test in all of them. I must say, for a browser that is supposedly so much better that IE, and fully standards compliant, I've found quite a few non-standard-compliant issues with it already - enough to drive me up the proverbial wall.


text:
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt

package com.google.appinventor.components.annotations;

/**
 * Categories for Simple properties.  This is used only for documentation.
 *
 */
public enum PropertyCategory {
  // TODO(user): i18n category names
  BEHAVIOR("Behavior"),
  APPEARANCE("Appearance"),
  DEPRECATED("Deprecated"),
  UNSET("Unspecified");

  private String name;

  PropertyCategory(String categoryName) {
    name = categoryName;
  }

  public String getName() {
    return name;
  }
}


text:
  document.addEventListener("DOMContentLoaded", pageLoad)
  document.addEventListener("submit", submit)

function pageLoad(event){
    var cedula=document.getElementById("cedula");
    var nombre=document.getElementById("nombre");
    cedula.addEventListener("focus",doFocus);
    cedula.addEventListener("blur",doBlur);
    nombre.addEventListener("focus",doFocus);
    nombre.addEventListener("blur",doBlur);
}

function doFocus(event){
    event.target.className="focus";
}

function doBlur(event){
    event.target.className="nofocus";
}

function submit(event) {
    validaCampos(event);

}

function validaCampos(event) {
    var ced = document.getElementById("cedula");
    var nom = document.getElementById("nombre");
    var radios = document.getElementsByName("sexo");

    if(!isCedFilled(ced)) {
        ced.setAttribute("class", ".error");
    }
    if(!isNomFilled(nom)) {
        nom.setAttribute("class", ".error");
    }
    if(!isRadioChecked(radios)) {
        radios.setAttribute("class", ".error");
    }
}

function isRadioChecked(radios) {
    for (var i = 0; i < radios.length; i++)
        if(radios[i].checked)
            return true;
    return false;
}
function isCedFilled(ced) {
    if(ced.value == null || ced.value == "")
        return false;
    return true;
}
function isNomFilled(nom) {
    if(nom.value == null || nom.value == "")
        return false;
    return true;
}

function enviar() {

}


text:
package net.falappa.wwind.helpers;

import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.layers.RenderableLayer;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Material;
import gov.nasa.worldwind.render.SurfacePolygon;
import java.util.ArrayList;
import name.gano.astro.AstroConst;
import name.gano.astro.GeoFunctions;
import name.gano.astro.MathUtils;
import name.gano.astro.bodies.Sun;

/**
 * A SurfacePolygon object representing the night earth area.
 *

 * Derived from SunTerminatorPolyLineTimeDep from JSatTrack.
 *
 * @author Shawn Gano
 * @author Alessandro Falappa
 */
public class EarthNightSurfacePolygon extends SurfacePolygon {

    private static final int POLY_POINTS = 6;
    // angle offset to generate a sliglthly less than a great circle (to make the surface polygon interior be in the right night zone)
    private static final double GREAT_CIRCLE_OFFSET = 0.0001;
    private final Sun sun;
    private final RenderableLayer rl;
    private final ArrayList<LatLon> llVec = new ArrayList<>(POLY_POINTS);

    public EarthNightSurfacePolygon(Sun sun, RenderableLayer rl) {
        super();
        this.sun = sun;
        this.rl = rl;
        // default attributes
        BasicShapeAttributes attrs = new BasicShapeAttributes();
        attrs.setEnableAntialiasing(true);
        attrs.setInteriorOpacity(0.5);
        attrs.setInteriorMaterial(Material.BLACK);
        attrs.setOutlineMaterial(Material.GRAY);
        attrs.setOutlineWidth(1.0);
        this.setAttributes(attrs);
        // create initial dummy positions
        for (int i = 0; i < POLY_POINTS; i++) {
            llVec.add(LatLon.ZERO);
        }
        // substitute calculated positions
        calcPositions(sun.getCurrentLLA()[0], sun.getCurrentLLA()[1], sun.getCurrentLLA()[2]);
        // set llVec as the polygon point storage
        setLocations(llVec);
    }

    /**
     * Recalculate the shape depending on the current sun position.
     */
    public void updateFromSun() {
        calcPositions(sun.getCurrentLLA()[0], sun.getCurrentLLA()[1], sun.getCurrentLLA()[2]);
        //fire points changed event
        onShapeChanged();
    }

// code from SunTerminatorPolyLineTimeDep kept in case lighting is added to WWindPanel
//    @Override
//    public void render(DrawContext dc) {
//        javax.media.opengl.GL gl = dc.getGL();
//        gl.glPushAttrib(javax.media.opengl.GL.GL_TEXTURE_BIT | javax.media.opengl.GL.GL_ENABLE_BIT | javax.media.opengl.GL.GL_CURRENT_BIT);
//        // Added so that the colors wouldn't depend on sun shading
//        gl.glDisable(GL.GL_TEXTURE_2D);
//        super.render(dc);
//        gl.glPopAttrib();
//    }
    /**
     * Recalculates the list of lat/lon (radians) for the polygon.
     *

     * Note: assumes the Earth is a perfect sphere!
     *
     * @param lat sun latitude in radians
     * @param lon sun longitude in radians
     * @param alt sun altitude in meters
     */
    private void calcPositions(double lat, double lon, double alt) {
        double lambda0 = Math.acos(AstroConst.R_Earth / (AstroConst.R_Earth + alt));
        // TODO - convert first geodetic/geographic!!?
        double beta = (90 * Math.PI / 180.0 - lat); // latitude center (pitch)
        double gamma = -lon + 180.0 * Math.PI / 180.0; // longitude (yaw)
        // rotation matrix
        double[][] M = new double[][]{{Math.cos(beta) * Math.cos(gamma), Math.sin(gamma), -Math.sin(beta) * Math.cos(gamma)},
        {-Math.cos(beta) * Math.sin(gamma), Math.cos(gamma), Math.sin(beta) * Math.sin(gamma)},
        {Math.sin(beta), 0.0, Math.cos(beta)}};
        double theta = 0 + Math.PI / 2.0; // with extra offset of pi/2 so circle starts left of center going counter clockwise
        double phi = lambda0 + GREAT_CIRCLE_OFFSET;
        // position
        double[] pos = new double[3];
        pos[0] = AstroConst.R_Earth * Math.cos(theta) * Math.sin(phi);
        pos[1] = AstroConst.R_Earth * Math.sin(theta) * Math.sin(phi);
        pos[2] = AstroConst.R_Earth * Math.cos(phi);
        // rotate to center around satellite sub point
        pos = MathUtils.mult(M, pos);
        // calculate Lat Long of point (first time save it)
        double[] lla0 = GeoFunctions.ecef2lla_Fast(pos);
        // add to vector
        llVec.set(0, LatLon.fromRadians(lla0[0], lla0[1]));
        // other polygon points
        double[] lla; // prepare array
        double dt = 2.0 * Math.PI / (POLY_POINTS - 1.0);
        for (int j = 1; j < POLY_POINTS; j++) {
            theta = j * dt + Math.PI / 2.0; // +Math.PI/2.0 // offset so it starts at the side
            //phi = lambda0;
            // find position - unrotated about north pole
            pos[0] = AstroConst.R_Earth * Math.cos(theta) * Math.sin(phi);
            pos[1] = AstroConst.R_Earth * Math.sin(theta) * Math.sin(phi);
            pos[2] = AstroConst.R_Earth * Math.cos(phi);
            // rotate to center around satellite sub point
            pos = MathUtils.mult(M, pos);
            // find lla
            lla = GeoFunctions.ecef2lla_Fast(pos);
            // add to vector
            llVec.set(j, LatLon.fromRadians(lla[0], lla[1]));
        } // for each point around footprint
        // return
//        Collections.reverse(llVec);
    }

}


text:
"""A module used for downloading files."""
import hashlib
import os
import shutil
import subprocess as sp
import tempfile
from ftplib import FTP

import requests
from tqdm import tqdm

from .logger import geoparse_logger as logger

try:
    from urlparse import urlparse
except ImportError:
    from urllib.parse import urlparse

class Downloader(object):
    """Downloader class."""

    def __init__(self, url, outdir=None, filename=None):
        self.url = url
        if outdir is None:
            self.outdir = os.getcwd()
        else:
            self.outdir = outdir
        if filename is None:
            self.filename = self._get_filename()
        else:
            self.filename = filename

        with tempfile.NamedTemporaryFile(delete=True) as tmpf:
            self._temp_file_name = tmpf.name

    @property
    def destination(self):
        """Get the destination path.

        This is the property should be calculated every time it is used because
        a user could change the outdir and filename dynamically.
        """
        return os.path.join(os.path.abspath(self.outdir), self.filename)

    def download(self, force=False, silent=False):
        """Download from URL."""

        def _download():
            if self.url.startswith("http"):
                self._download_http(silent=silent)
            elif self.url.startswith("ftp"):
                self._download_ftp(silent=silent)
            else:
                raise ValueError("Invalid URL %s" % self.url)
            logger.debug("Moving %s to %s" % (self._temp_file_name, self.destination))
            shutil.copyfile(self._temp_file_name, self.destination)
            logger.debug("Successfully downloaded %s" % self.url)

        try:
            is_already_downloaded = os.path.isfile(self.destination)
            if is_already_downloaded:
                if force:
                    try:
                        os.remove(self.destination)
                    except Exception:
                        logger.error("Cannot delete %s" % self.destination)
                    logger.info("Downloading %s to %s" % (self.url, self.destination))
                    logger.debug(
                        "Downloading %s to %s" % (self.url, self._temp_file_name)
                    )
                    _download()
                else:
                    logger.info(
                        (
                            "File %s already exist. Use force=True if you"
                            " would like to overwrite it."
                        )
                        % self.destination
                    )
            else:
                _download()
        finally:
            try:
                os.remove(self._temp_file_name)
            except OSError:
                pass

    def download_aspera(self, user, host, silent=False):
        """Download file with Aspera Connect.

        For details see the documentation ov Aspera Connect

        Args:
            user (:obj:`str`): FTP user.
            host (:obj:`str`): FTP host. Defaults to "ftp-trace.ncbi.nlm.nih.gov".
        """
        aspera_home = os.environ.get("ASPERA_HOME", None)
        if not aspera_home:
            raise ValueError("environment variable $ASPERA_HOME not set")
        if not os.path.exists(aspera_home):
            raise ValueError(
                "$ASPERA_HOME directory {} does not exist".format(aspera_home)
            )
        ascp = os.path.join(aspera_home, "connect/bin/ascp")
        key = os.path.join(aspera_home, "connect/etc/asperaweb_id_dsa.openssh")
        if not os.path.exists(ascp):
            raise ValueError("could not find ascp binary")
        if not os.path.exists(key):
            raise ValueError("could not find openssh key")

        parsed_url = urlparse(self.url)

        cmd = "{} -i {} -k1 -T -l400m {}@{}:{} {}".format(
            ascp, key, user, host, parsed_url.path, self._temp_file_name
        )
        logger.debug(cmd)
        try:
            pr = sp.Popen(cmd, shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
            stdout, stderr = pr.communicate()
            if not silent:
                logger.debug("Aspera stdout: " + str(stdout))
                logger.debug("Aspera stderr: " + str(stderr))
            if pr.returncode == 0:
                logger.debug(
                    "Moving %s to %s" % (self._temp_file_name, self.destination)
                )
                shutil.move(self._temp_file_name, self.destination)
                logger.debug("Successfully downloaded %s" % self.url)
            else:
                logger.error("Failed to download %s using Aspera Connect" % self.url)
        finally:
            try:
                os.remove(self._temp_file_name)
            except OSError:
                pass

    def _get_filename(self):
        filename = os.path.basename(urlparse(self.url).path).strip(" \n\t.")
        if len(filename) == 0:
            raise Exception("Cannot parse filename from %s" % self.url)
        return filename

    def _download_ftp(self, silent=False):
        parsed_url = urlparse(self.url)
        try:
            ftp = FTP(parsed_url.netloc)
            ftp.login()
            total_size = ftp.size(parsed_url.path)
            if total_size is None:
                total_size = 0
            wrote = list()  # cannot add in the callback, has to be a list
            with open(self._temp_file_name, "wb") as f:
                if silent:

                    def _write(data):
                        f.write(data)
                        wrote.append(len(data))

                    ftp.retrbinary("RETR %s" % parsed_url.path, _write)
                else:
                    with tqdm(
                        total=total_size,
                        unit="B",
                        unit_scale=True,
                        unit_divisor=1024,
                        leave=True,
                    ) as pbar:

                        def _write(data):
                            data_length = len(data)
                            pbar.update(data_length)
                            f.write(data)
                            wrote.append(data_length)

                        ftp.retrbinary("RETR %s" % parsed_url.path, _write)
            ftp.quit()
        except Exception:
            try:
                ftp.quit()
                logger.error(
                    "Error when trying to retreive %s." % self.url, exc_info=True
                )
            except Exception:
                logger.error("Error when quiting FTP server.", exc_info=True)

        if total_size != 0:
            if sum(wrote) != total_size:
                raise ValueError(
                    "Downloaded size do not match the expected size for %s" % (self.url)
                )
            else:
                logger.debug("Size validation passed")

    def _download_http(self, silent=False):
        r = requests.get(self.url, stream=True)
        r.raise_for_status()
        # Total size in bytes.
        total_size = int(r.headers.get("content-length", 0))
        logger.debug("Total size: %s" % total_size)
        md5_header = r.headers.get("Content-MD5")
        logger.debug("md5: %s" % str(md5_header))
        chunk_size = 1024
        wrote = 0
        with open(self._temp_file_name, "wb") as f:
            if silent:
                for chunk in r.iter_content(chunk_size):
                    if chunk:
                        f.write(chunk)
                        wrote += len(chunk)
            else:
                with tqdm(
                    total=total_size,
                    unit="B",
                    unit_scale=True,
                    unit_divisor=1024,
                    leave=True,
                ) as pbar:
                    for chunk in r.iter_content(chunk_size):
                        if chunk:
                            f.write(chunk)
                            pbar.update(len(chunk))
                            wrote += len(chunk)
        if total_size != 0:
            if wrote != total_size:
                raise ValueError(
                    "Downloaded size do not match the expected size for %s" % (self.url)
                )
            else:
                logger.debug("Size validation passed")
        if md5_header:
            logger.debug("Validating MD5 checksum...")
            if md5_header == Downloader.md5sum(self._temp_file_name):
                logger.debug("MD5 checksum passed")
            else:
                raise ValueError("MD5 checksum do NOT passed")

    @staticmethod
    def md5sum(filename, blocksize=8192):
        """Get the MD5 checksum of a file."""
        with open(filename, "rb") as fh:
            m = hashlib.md5()
            while True:
                data = fh.read(blocksize)
                if not data:
                    break
                m.update(data)
        return m.hexdigest()


text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_selected="true"
        android:drawable="@drawable/skyblue_platform_list_item_selected"
        />
    <item
        android:state_pressed="true"
        android:drawable="@drawable/skyblue_platform_list_item_selected"
        />
    <item
        android:drawable="@drawable/skyblue_platform_list_item"
        />
</selector>


text:
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { settings } from 'carbon-components';

const { prefix } = settings;

const SkeletonPlaceholder = ({ className, ...other }) => {
  const skeletonPlaceholderClasses = classNames({
    [`${prefix}--skeleton__placeholder`]: true,
    [className]: className,
  });

  return <div className={skeletonPlaceholderClasses} {...other} />;
};

SkeletonPlaceholder.propTypes = {
  /**
   * the class to be applied to the component
   */
  className: PropTypes.string,
};

export default SkeletonPlaceholder;


text:
import config from "../../config/firebase";
import Firebase from "firebase/app";
import "firebase/auth";
import "firebase/database";

const app = Firebase.initializeApp(config);
export const firebase = Firebase;
export const auth = firebase.auth();
export const database = firebase.database();


text:
<!DOCTYPE html>

    <title>Release Metrics</title>


text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="yd.miwu.testdemo.ViewPagerInfiniteSlidingActivity">

    <RelativeLayout
        android:id="@+id/titleBar"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:background="#FF5077">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Viewpager无限滑动"
            android:textColor="@android:color/white"
            android:textSize="17dp" />

    </RelativeLayout>

    <yd.miwu.view.PicRollingDisplayView.PicRollingDisplayView
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/picRollingDisplayView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/titleBar"
        custom:defaultPicSrc="@mipmap/ic_launcher"
        custom:timeGap="2"/>

    <yd.miwu.view.PicRollingDisplayView.PositionIndicatorView
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/indicatorView"
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:layout_alignBottom="@id/picRollingDisplayView"
        android:layout_marginBottom="35dp"
        android:gravity="center_horizontal"
        custom:defaultSrc="@drawable/cricle_white"
        custom:gap="4dip"
        custom:indicatorHeight="8dip"
        custom:indicatorWidth="8dip"
        custom:selectedSrc="@drawable/cricle_red" />

</RelativeLayout>


text:
[![Travis](https://img.shields.io/travis/mbenford/ngTagsInput.svg?style=flat)](https://travis-ci.org/mbenford/ngTagsInput)
[![Coveralls](https://img.shields.io/coveralls/mbenford/ngTagsInput.svg?style=flat)](https://coveralls.io/r/mbenford/ngTagsInput?branch=master)
[![David](https://img.shields.io/david/dev/mbenford/ngTagsInput.svg?style=flat)](https://david-dm.org/mbenford/ngTagsInput#info=devDependencies)
[![GitHub release](https://img.shields.io/github/release/mbenford/ngTagsInput.svg)](https://github.com/mbenford/ngTagsInput/releases)
[![Gratipay](https://img.shields.io/gratipay/mbenford.svg)](https://gratipay.com/mbenford)

Tags input directive for AngularJS. Check out the [ngTagsInput website](http://mbenford.github.io/ngTagsInput) for more information.

## Requirements

 - AngularJS 1.2.24+ (v1.3 isn't officially supported yet)
 - A modern browser (Chrome 31+, Firefox 29+, Safari 7+, Opera 12+, IE 10+)

## Installing

All files are available from a variety of sources. Choose the one that best fits your needs:

- Direct download (https://github.com/mbenford/ngTagsInput/releases)
- NPM (`npm install ng-tags-input --save`)
- Bower (`bower install ng-tags-input --save`)
- CDNJS (http://cdnjs.com/libraries/ng-tags-input)

You can also grab the [latest build](http://shrub.appspot.com/ng-tags-input) generated by Travis. It's fully functional and may contain new features and bugfixes not yet published to the services listed above.

Now all you have to do is add the scripts to your application. Just make sure the `ng-tags-input.js` file is inserted **after** the `angular.js` script:

```html
<script src="angular.js"></script>
<script src="ng-tags-input.js"></script>
<link rel="stylesheet" type="text/css" href="ng-tags-input.css">
```

## Usage

 1. Add the `ngTagsInput` module as a dependency in your AngularJS app;
 2. Add the custom directive `<tags-input>` to the HTML file where you want to use an input tag control and bind it to a property of your model. That property, if it exists, must be an array of objects and each object must have a property named `text` containing the tag text;
 3. Set up the options that make sense to your application;
 4. Enable autocomplete, if you want to use it, by adding the directive `<auto-complete>` inside the `<tags-input>` tag, and bind it to a function of your model. That function must return either an array of objects or a promise that eventually resolves to an array of objects (same rule from step 2 applies here);
 5. Customize the CSS classes, if you want to.
 6. You're done!

**Note:** There's a more detailed [getting started](http://mbenford.github.io/ngTagsInput/gettingstarted) guide on the ngTagsInput website.

## Example

```html
<html>
<head>
    <script src="angular.min.js"></script>
    <script src="ng-tags-input.min.js"></script>
    <link rel="stylesheet" type="text/css" href="ng-tags-input.min.css">
    <script>
        angular.module('myApp', ['ngTagsInput'])
            .controller('MyCtrl', function($scope, $http) {
                $scope.tags = [
                    { text: 'just' },
                    { text: 'some' },
                    { text: 'cool' },
                    { text: 'tags' }
                ];
                $scope.loadTags = function(query) {
                     return $http.get('/tags?query=' + query);
                };
            });
    </script>
</head>
<body ng-app="myApp" ng-controller="MyCtrl">
    <tags-input ng-model="tags">
        <auto-complete source="loadTags($query)"></auto-complete>
    </tags-input>
</body>
</html>
```

## Options

Check out the [documentation](http://mbenford.github.io/ngTagsInput/documentation/api) page for a detailed view of all available options.

## Demo

You can see the directive in action in the [demo page](http://mbenford.github.io/ngTagsInput/demos).

## Contributing

Before posting an issue or sending a pull request, make sure to read the [CONTRIBUTING](https://github.com/mbenford/ngTagsInput/blob/master/CONTRIBUTING.md) file.

## License

See the [LICENSE](https://github.com/mbenford/ngTagsInput/blob/master/LICENSE) file.

## Changelog

See the [CHANGELOG](https://github.com/mbenford/ngTagsInput/blob/master/CHANGELOG.md) page.

## Alternatives

The following are some alternatives to ngTagsInput you may want to check out:

- [angular-tags](http://decipherinc.github.io/angular-tags): Pure AngularJS tagging widget with typeahead support courtesy of ui-bootstrap
- [angular-tagger](https://github.com/monterail/angular-tagger): Pure Angular autocomplete with tags, no jQuery
- [jsTag](https://github.com/eranhirs/jstag): Open source project for editing tags (aka tokenizer) based on AngularJS
- [bootstrap-tagsinput](http://timschlechter.github.io/bootstrap-tagsinput/examples): jQuery plugin providing a Twitter Bootstrap user interface for managing tags (provides Angular support)


text:
package io.fundrequest.social.gitter.connect;

import io.fundrequest.social.gitter.api.Gitter;
import io.fundrequest.social.gitter.api.Me;
import org.springframework.social.ApiException;
import org.springframework.social.connect.ApiAdapter;
import org.springframework.social.connect.ConnectionValues;
import org.springframework.social.connect.UserProfile;
import org.springframework.social.connect.UserProfileBuilder;

public class GitterAdapter implements ApiAdapter<Gitter> {

	public boolean test(final Gitter gitter) {
		try {
            Me me = gitter.userResource().me();
            return me != null;
		} catch (ApiException e) {
			return false;
		}
	}

    @Override
    public void setConnectionValues(final Gitter gitter, final ConnectionValues values) {
        final Me me = gitter.userResource().me();
        values.setProviderUserId(me.getId());
        values.setDisplayName(me.getDisplayName());
        values.setProfileUrl(me.getUrl());
        values.setImageUrl(me.getAvatarUrl());
    }

    @Override
    public UserProfile fetchUserProfile(Gitter gitter) {
        final Me me = gitter.userResource().me();
        return new UserProfileBuilder().setId(me.getId()).setUsername(me.getUsername()).setName(me.getDisplayName()).build();
    }

    @Override
    public void updateStatus(Gitter gitter, String message) {
        //Intentionally blank
    }
}


text:
var Router = require('routes');
var through = require('through2');
var inherits = require('inherits');
var EventEmitter = require('events').EventEmitter;
var prefix = require('route-prefix');

module.exports = Server;
inherits(Server, EventEmitter);

function Server (compute, opts) {
    if (!(this instanceof Server)) return new Server(compute, opts);
    if (!opts) opts = {};
    this.compute = compute;
    this.router = this._createRouter(opts);
}

Server.prototype.exec = function (req, res) {
    var m = this.match(req.url);
    if (m) m.fn(req, res, m);
    return m;
};

Server.prototype.match = function (uri) {
    return this.router.match(uri);
};

Server.prototype._createRouter = function (opts) {
    var self = this;
    if (!opts) opts = {};
    var router = opts.prefix
        ? prefix(opts.prefix, Router())
        : Router()
    ;

    router.addRoute('/create', function (req, res, m) {
        if (req.method !== 'POST') return error(404, res, 'not a POST');
        req.pipe(self.compute.add(function (err, jobkey) {
            if (err) error(500, res, err);
            else res.end(jobkey + '\n')
        }));
    });

    router.addRoute('/list/:type', function (req, res, m) {
        if (req.method !== 'GET') return error(404, res, 'not a GET');
        var s = self.compute.list(m.params.type);
        if (!s) return error(404, res, 'unknown list type');
        s.pipe(through.obj(function (row, enc, next) {
            this.push(JSON.stringify(row) + '\n');
            next();
        })).pipe(res);
    });

    router.addRoute('/blob/:id', function (req, res, m) {
        if (req.method !== 'GET') return error(404, res, 'not a GET');
        var s = self.compute.getResult(m.params.id);
        s.on('error', function (err) { error(500, res, err) });
        s.pipe(res);
    });

    return router;
};

function error (code, res, err) {
    res.statusCode = code;
    res.end(err + '\n');
}


text:
"On va faire la bêta Romu"
 (mais c'est vrai, c'est Romu en version bêta)

"Qu'est-ce que je fais? Je roule une pelle."
  (en roulant une pelle)

"Dit moi Paul, tes plaquards ne sont pas vide ou bien?"
 Theo


text:
+++
date = "2018-02-20T17:00:00"
title = "The Ultimate Teaching Time Savers"
abstract = "Hosting The Ultimate Teaching Time Savers event. Organised by The UvA-FMG Knowledge Sharing Matchmakers"
abstract_short = ""
event = "The Ultimate Teaching Time Savers - UvA"
event_url = "https://medewerker.uva.nl/en/fmg/content-secured/events/events/2018/02/the-ultimate-teaching-time-savers.html?date=20022018&origin=0s96z%2F%2BRSPy%2FHuk0vGqOrA"
location = "Amsterdam, Netherlands"

selected = false
math = true

url_pdf = ""
url_slides = ""
url_video = ""

# Optional featured image (relative to `static/img/` folder).
[header]
image = "headers/bubbles-wide.jpg"
caption = "My caption :smile:"

+++


text:
package com.rhomobile.rhodes.extmanager;

import android.graphics.Rect;

public interface IRhoExtension {

    interface IAlertResult {
        void setPending();
        void confirm();
        void cancel();
    }
    interface IPromptResult {
        void setPending();
        void confirm(String res);
        void cancel();
    }
    interface IAuthRequest {
        //void setPending();
        void proceed(String user, String pass);
        void cancel();
        String type();
        String host();
        String realm();
    }

    public enum LoadErrorReason { STOP, INTERNAL_ERROR, BAD_LICENSE }

    IRhoWebView onCreateWebView(IRhoExtManager extManager, int tabIndex);
    boolean onWebViewCreated(IRhoExtManager extManager, IRhoWebView ext, boolean res);
    boolean onSetPropertiesData(IRhoExtManager extManager, String propId, String data, int pos, int total, IRhoWebView ext, boolean res);
    boolean onSetPropertiesDataEnd(IRhoExtManager extManager, IRhoWebView ext, boolean res);
    boolean onSetProperty(IRhoExtManager extManager, String name, String value, IRhoWebView ext, boolean res);
    boolean onBeforeNavigate(IRhoExtManager extManager, String url, IRhoWebView ext, boolean res);
    boolean onNavigateStarted(IRhoExtManager extManager, String url, IRhoWebView ext, boolean res);
    boolean onNavigateProgress(IRhoExtManager extManager, String url, int pos, int total, IRhoWebView ext, boolean res);
    boolean onNavigateComplete(IRhoExtManager extManager, String url, IRhoWebView ext, boolean res);
    boolean onDocumentComplete(IRhoExtManager extManager, String url, IRhoWebView ext, boolean res);
    boolean onAlert(IRhoExtManager extManager, String message, IRhoWebView ext, IAlertResult alertResult, boolean res);
    boolean onConfirm(IRhoExtManager extManager, String message, IRhoWebView ext, IAlertResult confirmResult, boolean res);
    boolean onPrompt(IRhoExtManager extManager, String message, String defaultResponse, IRhoWebView ext, IPromptResult promptResult, boolean res);
    boolean onSelect(IRhoExtManager extManager, String[] items, int selected, IRhoWebView ext, boolean res);
    boolean onStatus(IRhoExtManager extManager, String status, IRhoWebView ext, boolean res);
    boolean onTitle(IRhoExtManager extManager, String title, IRhoWebView ext, boolean res);
    boolean onConsole(IRhoExtManager extManager, String message, IRhoWebView ext, boolean res);
    boolean onInputMethod(IRhoExtManager extManager, boolean enabled, String type, Rect area, IRhoWebView ext, boolean res);
    boolean onNavigateError(IRhoExtManager extManager, String url, LoadErrorReason reason, IRhoWebView ext, boolean res);
    boolean onAuthRequest(IRhoExtManager extManager, IAuthRequest request, IRhoWebView ext, boolean res);

    void onAppActivate(IRhoExtManager extManager, boolean bActivate);

    //EkiohLocation getCachedLocation(IRhoExtManager extManager, IRhoExtData ext);
    boolean startLocationUpdates(IRhoExtManager extManager, boolean highAccuracy, IRhoWebView ext, boolean res);
    boolean stopLocationUpdates(IRhoExtManager extManager, IRhoWebView ext, boolean res);

    boolean onNewConfig(IRhoExtManager extManager, IRhoConfig config, String name, boolean res);

    String onGetProperty(IRhoExtManager extManager, String name);
}


text:
<?php

define('ROOT', dirname(__DIR__) . DIRECTORY_SEPARATOR);
define('APP', ROOT . 'app' . DIRECTORY_SEPARATOR);

require_once ROOT . 'vendor/autoload.php';

$app = require APP . 'bootstrap.php';
$app->run();


text:
package org.zstack.test.configuration;

import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.zstack.core.componentloader.ComponentLoader;
import org.zstack.core.db.DatabaseFacade;
import org.zstack.header.configuration.InstanceOfferingDuration;
import org.zstack.header.configuration.InstanceOfferingInventory;
import org.zstack.header.configuration.InstanceOfferingVO;
import org.zstack.test.Api;
import org.zstack.test.ApiSenderException;
import org.zstack.test.BeanConstructor;
import org.zstack.test.DBUtil;
import org.zstack.test.image.TestAddImage;
import org.zstack.utils.Utils;
import org.zstack.utils.data.SizeUnit;
import org.zstack.utils.logging.CLogger;

public class TestDeleteInstanceOffering {
    CLogger logger = Utils.getLogger(TestAddImage.class);
    Api api;
    ComponentLoader loader;
    DatabaseFacade dbf;

    @Before
    public void setUp() throws Exception {
        DBUtil.reDeployDB();
        BeanConstructor con = new BeanConstructor();
        /* This loads spring application context */
        loader = con.addXml("PortalForUnitTest.xml").addXml("ConfigurationManager.xml").addXml("HostAllocatorManager.xml").addXml("AccountManager.xml").build();
        dbf = loader.getComponent(DatabaseFacade.class);
        api = new Api();
        api.startServer();
    }

    @After
    public void tearDown() throws Exception {
        api.stopServer();
    }

    @Test
    public void test() throws ApiSenderException {
        InstanceOfferingInventory inv = new InstanceOfferingInventory();
        inv.setName("TestInstanceOffering");
        inv.setCpuNum(2);
        inv.setCpuSpeed(1000);
        inv.setMemorySize(SizeUnit.GIGABYTE.toByte(1));
        inv.setDescription("TestInstanceOffering");
        inv = api.addInstanceOffering(inv);

        InstanceOfferingVO vo = dbf.findByUuid(inv.getUuid(), InstanceOfferingVO.class);
        Assert.assertNotNull(vo);
        Assert.assertEquals(InstanceOfferingDuration.Permanent, vo.getDuration());

        api.deleteInstanceOffering(vo.getUuid());
        vo = dbf.findByUuid(inv.getUuid(), InstanceOfferingVO.class);
        Assert.assertEquals(null, vo);
    }

}


text:
package org.onosproject.net.intf;

import java.util.Set;

import com.google.common.collect.ImmutableSet;
import org.apache.commons.lang.NotImplementedException;
import org.onlab.packet.IpAddress;
import org.onlab.packet.VlanId;
import org.onosproject.event.ListenerService;
import org.onosproject.net.ConnectPoint;

/**
 * Service for interacting with interfaces.
 */

public interface InterfaceService
        extends ListenerService<InterfaceEvent, InterfaceListener> {

    /**
     * Returns the set of all interfaces in the system.
     *
     * @return set of interfaces
     */
    Set<Interface> getInterfaces();

    /**
     * Returns the interface with the given name.
     *
     * @param connectPoint connect point of the interface
     * @param name name of the interface
     * @return interface if it exists, otherwise null
     */
    Interface getInterfaceByName(ConnectPoint connectPoint, String name);

    /**
     * Returns the set of interfaces configured on the given port.
     *
     * @param port connect point
     * @return set of interfaces
     */
    Set<Interface> getInterfacesByPort(ConnectPoint port);

    /**
     * Returns the set of interfaces with the given IP address.
     *
     * @param ip IP address
     * @return set of interfaces
     */
    Set<Interface> getInterfacesByIp(IpAddress ip);

    /**
     * Returns the set of interfaces in the given VLAN.
     *
     * @param vlan VLAN ID of the interfaces
     * @return set of interfaces
     */
    Set<Interface> getInterfacesByVlan(VlanId vlan);

    /**
     * Returns an interface that has an address that is in the same subnet as
     * the given IP address.
     *
     * @param ip IP address to find matching subnet interface for
     * @return interface
     */
    Interface getMatchingInterface(IpAddress ip);

    /**
     * Returns all interfaces that have an address that is in the same
     * subnet as the given IP address.
     *
     * @param ip IP address to find matching subnet interface for
     * @return a set of interfaces
     */
    Set<Interface> getMatchingInterfaces(IpAddress ip);

    /**
     * Returns untagged VLAN configured on given connect point.
     *

     * Only returns the first match if there are multiple untagged VLAN configured
     * on the connect point.
     *
     * @param connectPoint connect point
     * @return untagged VLAN or null if not configured
     */
    default VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
        return null;
    }

    /**
     * Returns tagged VLAN configured on given connect point.
     *

     * Returns all matches if there are multiple tagged VLAN configured
     * on the connect point.
     *
     * @param connectPoint connect point
     * @return tagged VLAN or empty set if not configured
     */
    default Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
        return ImmutableSet.of();
    }

    /**
     * Returns native VLAN configured on given connect point.
     *

     * Only returns the first match if there are multiple native VLAN configured
     * on the connect point.
     *
     * @param connectPoint connect point
     * @return native VLAN or null if not configured
     */
    default VlanId getNativeVlanId(ConnectPoint connectPoint) {
        return null;
    }

    /**
     * Returns true if given connectPoint has an IP address or vlan configured
     * on any of its interfaces.
     *
     * @param connectPoint the port on a device
     * @return true if connectpoint has a configured interface
     */
    default boolean isConfigured(ConnectPoint connectPoint) {
        throw new NotImplementedException("isConfigured is not implemented");
    }

    /**
     * Returns true if given vlanId is in use due to configuration on any of the
     * interfaces in the system.
     *
     * @param vlanId the vlan id being queried
     * @return true if vlan is configured on any interface
     */
    default boolean inUse(VlanId vlanId) {
        throw new NotImplementedException("isConfigured is not implemented");
    }
}


text:
package Paws::ElastiCache::CopySnapshotResult;
  use Moose;
  has Snapshot => (is => 'ro', isa => 'Paws::ElastiCache::Snapshot');

  has _request_id => (is => 'ro', isa => 'Str');
1;

### main pod documentation begin ###

=head1 NAME

Paws::ElastiCache::CopySnapshotResult

=head1 ATTRIBUTES

=head2 Snapshot => L<Paws::ElastiCache::Snapshot>

=head2 _request_id => Str

=cut


text:
Collection best of Sass Mixins and Function.If you have seen good and meaningful Mixins or Functions, please send us your code.Let more students share your results.

收集优秀的Sass Mixins和Function。如果您有看到优秀的、有意义的Mixins或Functions,请向我们提交您的代码吧。让更多的同学分享您的成果。

© [w3cplus](http://www.w3cplus.com).作者:大漠。QQ讨论群:78142855


text:
import sys
from twingo import Twingo
from moto import Moto
from e_r6 import ER6
from gladius import Gladius

class Parking():
    """ New class Parking"""

    global_message = "Bienvenu dans votre parking favoris"

    def __init__(self):
        self.vehicules = list()

        twingo = Twingo("twingo")
        twingo.owner = "Mark"

        er6 = ER6("er6")
        er6.owner = "Julian"
        glad = Gladius("gladius")
        glad.owner = "Pierre"

        self.vehicules.append(twingo)
        self.vehicules.append(er6)
        self.vehicules.append(glad)

    def display_vehicules_owner(self):
        """DocString : display the list of vehicules owner"""
        print("List of vehicule owners : ")
        for vehicule in self.vehicules:
            print("    " + vehicule.owner)

    def display_vehicules_features(self):
        for vehicule in self.vehicules:
            if isinstance(vehicule, Moto):
                vehicule.print_features()
            else:
                print("Car has different features")

    def print_global_message():
        print(Parking.global_message)


text:
package endpoint

import (
	"fmt"
	"net/http"

	"github.com/go-martini/martini"
	"github.com/go-msf/msf-tools/common"
	"github.com/martini-contrib/binding"
	"github.com/martini-contrib/render"
)

// ServiceRegistryWebService is the service stub for the service registry
type ServiceRegistryWebService struct {
	Service common.IServiceRegistry
}

// Route return the route handler for this
func (s ServiceRegistryWebService) Route() martini.Router {
	r := martini.NewRouter()
	r.Group("/apiversions/", func(r martini.Router) {
		r.Get("", s.getAPIVersions)
		r.Post("", binding.Bind(common.APIVersion{}), s.createAPIVersion)
	})

	r.Group("/apiversions/:version/", func(r martini.Router) {
		r.Get("", s.getAPIVersion)
		r.Get("services/", s.getAPIServices)
		r.Post("services/", binding.Bind(common.ServiceVersion{}), s.createAPIService)
	})

	r.Group("/services/", func(r martini.Router) {
		r.Get("", s.getServices)
		r.Post("", binding.Bind(common.ServiceDefinition{}), s.createService)
	})

	r.Group("/services/:service/versions", func(r martini.Router) {
		r.Get("", s.getServiceVersions)
		r.Post("", binding.Bind(common.ServiceVersion{}), s.createServiceVersion)
	})

	r.Group("/services/:service/:version/hosts/", func(r martini.Router) {
		r.Get("", s.getServiceHosts)
		r.Post("", binding.Bind(common.ServiceHost{}), s.attachServiceHost)
	})

	return r
}

// NewServiceRegistryWebService returns a new instance of service registry stub
func NewServiceRegistryWebService(service common.IServiceRegistry) *ServiceRegistryWebService {
	return &ServiceRegistryWebService{
		Service: service,
	}
}

func (s *ServiceRegistryWebService) getServiceVersions(r render.Render, parms martini.Params) {
	var result, err = s.Service.GetServiceVersions(parms["service"])
	if err != nil {
		r.JSON(http.StatusNotFound, nil)
		return
	}

	r.JSON(http.StatusOK, result)
}

func (s *ServiceRegistryWebService) createServiceVersion(r render.Render, sv common.ServiceVersion, parms martini.Params) {
	var err = s.Service.AddServiceVersion(sv.ServiceIdentifier, sv.Version)
	if err != nil {
		r.JSON(http.StatusNotFound, nil)
		return
	}

	r.JSON(http.StatusOK, nil)
}

func (s *ServiceRegistryWebService) getAPIVersion(r render.Render, parms martini.Params) {
	var result, err = s.Service.GetAPIVersion(parms["version"])
	if err != nil {
		r.JSON(http.StatusNotFound, nil)
		return
	}
	r.JSON(http.StatusOK, result)
}

func (s *ServiceRegistryWebService) getAPIVersions(r render.Render) {
	var result, err = s.Service.GetAPIVersions()
	if err != nil {
		r.JSON(http.StatusNotFound, nil)
		return
	}
	r.JSON(http.StatusOK, result)
}

func (s *ServiceRegistryWebService) getAPIServices(r render.Render, parms martini.Params) {
	fmt.Println("Getting Api Version")
	var result, err = s.Service.GetAPIVersion(parms["version"])
	if err != nil {
		r.JSON(http.StatusNotFound, err.Error())
		return
	}

	r.JSON(http.StatusOK, result)
}

func (s *ServiceRegistryWebService) createAPIService(r render.Render, sv common.ServiceVersion, parms martini.Params) int {
	var err = s.Service.LinkServiceToAPI(sv.ServiceIdentifier, sv.Version, parms["version"])
	if err != nil {
		r.JSON(http.StatusNotFound, err.Error())
		return http.StatusNotFound
	}

	return http.StatusOK
}

func (s *ServiceRegistryWebService) createAPIVersion(r render.Render, apiV common.APIVersion) {
	var result, err = s.Service.AddAPIVersion(apiV.Version, apiV.Name)
	if err != nil {
		r.JSON(http.StatusNotFound, nil)
		return
	}

	r.JSON(http.StatusOK, result)
}

func (s *ServiceRegistryWebService) getServices(r render.Render, parms martini.Params) {
	var result, err = s.Service.GetServiceDefinitions()
	if err != nil {
		r.JSON(http.StatusInternalServerError, nil)
		return
	}

	r.JSON(http.StatusOK, result)
}

func (s *ServiceRegistryWebService) createService(sd common.ServiceDefinition, r render.Render) {
	var result, err = s.Service.AddServiceDefinition(sd.Identifier, sd.Prefix, sd.DisplayName)
	if err != nil {
		r.JSON(http.StatusInternalServerError, nil)
		return
	}

	r.JSON(http.StatusOK, result)
}

func (s *ServiceRegistryWebService) getServiceHosts(r render.Render, parms martini.Params) {
	var result, err = s.Service.GetServiceHosts(parms["service"], parms["version"])
	if err != nil {
		r.JSON(http.StatusNotFound, nil)
		return
	}

	r.JSON(http.StatusOK, result)
}

func (s *ServiceRegistryWebService) attachServiceHost(r render.Render, host common.ServiceHost, parms martini.Params) {
	var result, err = s.Service.AddServiceHost(parms["service"], parms["version"], host.URI)
	if err != nil {
		r.JSON(http.StatusNotFound, err.Error())
		return
	}

	r.JSON(http.StatusOK, result)
}


text:
function gX = expKernGradX(kern, x, x2)

% EXPKERNGRADX Gradient of EXP kernel with respect to a point x.
% FORMAT
% DESC computes the gradient of the exponentiated
% kernel with respect to the input positions.
% ARG kern : kernel structure for which gradients are being
% computed.
% ARG x : locations against which gradients are being computed.
% RETURN g : the returned gradients. The gradients are returned in
% a matrix which is numData x numInputs x numData. Where numData is
% the number of data points and numInputs is the number of input
% dimensions in X.
%
% FORMAT
% DESC computes the gradident of the exponentiated
% kernel with respect to the input positions where both the row
% positions and column positions are provided separately.
% ARG kern : kernel structure for which gradients are being
% computed.
% ARG x1 : row locations against which gradients are being computed.
% ARG x2 : column locations against which gradients are being computed.
% RETURN g : the returned gradients. The gradients are returned in
% a matrix which is numData2 x numInputs x numData1. Where numData1 is
% the number of data points in X1, numData2 is the number of data
% points in X2 and numInputs is the number of input
% dimensions in X.
%
% SEEALSO expKernParamInit, kernGradX, expKernDiagGradX
%
% COPYRIGHT : Neil D. Lawrence, 2006

% KERN

[K, argK, kDiagdMat] = expKernCompute(kern, x, x2);
gX = kernGradX(kern.argument, x, x2);
if kern.isStationary
  for i = 1:size(gX, 3)
    gX(:, :, i) = gX(:, :, i)...
        .*repmat(exp(argK(i, :)'), 1, size(x, 2))...
        .*exp(kDiagMat)*kern.variance;
  end
else

%   for i = 1:size(gX, 3)
%     gX(:, :, i) = gX(:, :, i) ...
%         .*(repmat(exp(argK(i, :)').*exp(kDiagMat(i, :)'), 1, size(x, 2))...
%         + repmat(((exp(argK(i, :)')-1).*exp(kDiagMat(i, :))*kern.variance;
%   end

end


text:
package com.jivesoftware.os.amza.service.take;

import com.jivesoftware.os.amza.api.partition.VersionedPartitionName;
import com.jivesoftware.os.amza.api.ring.RingHost;
import com.jivesoftware.os.amza.api.ring.RingMember;
import com.jivesoftware.os.amza.api.ring.TimestampedRingHost;

public interface AvailableRowsTaker {

    void availableRowsStream(RingMember localRingMember,
        TimestampedRingHost localTimestampedRingHost,
        RingMember remoteRingMember,
        RingHost remoteRingHost,
        boolean system,
        long takeSessionId,
        long takeSharedKey,
        long timeoutMillis,
        AvailableStream availableStream,
        PingStream pingStream) throws Exception;

    interface AvailableStream {

        void available(VersionedPartitionName versionedPartitionName, long txId) throws Exception;
    }

    interface PingStream {

        void ping() throws Exception;
    }

}


text:
html, table, tr, td, div, ul, ol, li, h1, h2, h3, p, a, dd, dt{
	font-family: "Helvetica", sans-serif, "Arial";
}
html, body{ height: 100%; width: 100%; }
body{
	margin: 0;
	padding: 0;
	font-family: "Helvetica", sans-serif, "Arial";
	font-size: 12px;
	color: #484848;
	background-color: #EEE;
}
.clearfix:after, ui-helper-clearfix:after{
	content: ".";
	display: block;
	clear: both;
	visibility: hidden;
	line-height: 0;
	height: 0;
}
.clearfix, .ui-helper-clearfix{
	display: inline-block;
}
html[xmlns] .clearfix, html[xmlns] .ui-helper-clearfix{
	display: block;
}
* html .clearfix, * html .ui-helper-clearfix {
	height: 1%;
}
form{
	display: inline;
}
.logininstruction{
	width: 362px;
	margin: 20px auto 0 auto;
	padding: 0 0 0 10px;
	font-weight: bold;
}
.loginerror{
	width: 362px;
	margin: auto;
	padding: 0 0 0 10px;
	font-size: 11px;
	color: #FF0000;
}
.logincontainer{
	position: relative;
	background: url( ../../images/auth/auth-login.gif ) no-repeat #EEE;
	width: 362px;
	height: 226px;
	margin: 10px auto;
}
.formarea{
	position: absolute;
	top: 20px;
	left: 25px;
	width: 200px;
}
.formarea div{
	margin: 7px 0;
}
.formarea div.signin{
	margin: 18px 0;
}
.formarea label{
	font-weight: bold;
}
.formarea input[type=text], .formarea input[type=password] {
	width: 200px;
}
.formarea input[type=checkbox]{
	vertical-align: -2px;
}
.formarea input[type=submit]{
	width: 100px;
	padding: 3px;
}
.languagecontainer{
	font-weight: bold;
  margin: auto;
	padding: 0;
	font-size: 1px;
  width: 362px;
	position: relative;
}
.languagecontainer form{
	position: absolute;
	right: 10px;
	top: -5px;
}


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<title>Digital Land Charges</title>

	<link rel="stylesheet" href="/public/stylesheets/styles.css">
	<link rel="stylesheet" href="/public/stylesheets/elements.css">
	<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,700' rel='stylesheet' type='text/css'>

{{>includes/header}}

Home

Bankruptcies

List of verification applications

Writ or Order verification

Writ or Order verification

Confirmation

		         	The backruptcy registration application
have been successfully registered

Reference: 123456

What is next?

We will print and send a confirmation letter to the applicant.

					Back to verification applications

					Back to bankruptcies

					Back to home

{{>includes/footer}}


text:
<readable><title>2275372714_017c269742</title><content>
A group of boys pouring soda into a white plastic bucket while a man pumps a bicycle pump .
A man , adolescent and two boys are mixing a bucket with liquid on a farm .
A person pours water into a work bucket near a shovel and three other people .
Four guys are in a field .
The kids watched as the soda bottle launched into the air .
</content></readable>


text:
// Copyright (c) Gurmit Teotia. Please see the LICENSE file in the project root for license information.
using System.Threading;
using System.Threading.Tasks;
using Amazon.SimpleWorkflow;
using Amazon.SimpleWorkflow.Model;

namespace Guflow.Worker
{
    internal class HeartbeatSwfApi : IHeartbeatSwfApi
    {
        private readonly IAmazonSimpleWorkflow _client;

        public HeartbeatSwfApi(IAmazonSimpleWorkflow client)
        {
            _client = client;
        }

        public async Task<bool> RecordHearbeatAsync(string token, string details, CancellationToken cancellationToken)
        {
            var request = HeartbeatRequest(details, token);
            var response = await _client.RecordActivityTaskHeartbeatAsync(request, cancellationToken);
            return IsCancellationRequested(response);
        }
        private static bool IsCancellationRequested(RecordActivityTaskHeartbeatResponse response)
        {
            return response.ActivityTaskStatus.CancelRequested;
        }

        private static RecordActivityTaskHeartbeatRequest HeartbeatRequest(string details, string token)
        {
            return new RecordActivityTaskHeartbeatRequest
            {
                TaskToken = token,
                Details = details
            };
        }
    }
}


text:
package org.apache.calcite.sql2rel;

import org.apache.calcite.rel.RelHomogeneousShuttle;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.CorrelationId;
import org.apache.calcite.rex.RexBuilder;
import org.apache.calcite.rex.RexCorrelVariable;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexShuttle;
import org.apache.calcite.rex.RexSubQuery;

import com.google.common.collect.ImmutableSet;

/**
 * Rewrites relations to ensure the same correlation is referenced by the same
 * correlation variable.
 */
public class DeduplicateCorrelateVariables extends RelHomogeneousShuttle {
  private final RexShuttle dedupRex;

  /** Creates a DeduplicateCorrelateVariables. */
  private DeduplicateCorrelateVariables(RexBuilder builder,
      CorrelationId canonicalId, ImmutableSet<CorrelationId> alternateIds) {
    dedupRex = new DeduplicateCorrelateVariablesShuttle(builder,
        canonicalId, alternateIds, this);
  }

  /**
   * Rewrites a relational expression, replacing alternate correlation variables
   * with a canonical correlation variable.
   */
  public static RelNode go(RexBuilder builder, CorrelationId canonicalId,
      Iterable<? extends CorrelationId> alternateIds, RelNode r) {
    return r.accept(
        new DeduplicateCorrelateVariables(builder, canonicalId,
            ImmutableSet.copyOf(alternateIds)));
  }

  @Override public RelNode visit(RelNode other) {
    RelNode next = super.visit(other);
    return next.accept(dedupRex);
  }

  /**
   * Replaces alternative names of correlation variable to its canonical name.
   */
  private static class DeduplicateCorrelateVariablesShuttle extends RexShuttle {
    private final RexBuilder builder;
    private final CorrelationId canonicalId;
    private final ImmutableSet<CorrelationId> alternateIds;
    private final DeduplicateCorrelateVariables shuttle;

    private DeduplicateCorrelateVariablesShuttle(RexBuilder builder,
        CorrelationId canonicalId, ImmutableSet<CorrelationId> alternateIds,
        DeduplicateCorrelateVariables shuttle) {
      this.builder = builder;
      this.canonicalId = canonicalId;
      this.alternateIds = alternateIds;
      this.shuttle = shuttle;
    }

    @Override public RexNode visitCorrelVariable(RexCorrelVariable variable) {
      if (!alternateIds.contains(variable.id)) {
        return variable;
      }

      return builder.makeCorrel(variable.getType(), canonicalId);
    }

    @Override public RexNode visitSubQuery(RexSubQuery subQuery) {
      if (shuttle != null) {
        RelNode r = subQuery.rel.accept(shuttle); // look inside sub-queries
        if (r != subQuery.rel) {
          subQuery = subQuery.clone(r);
        }
      }
      return super.visitSubQuery(subQuery);
    }
  }
}

// End DeduplicateCorrelateVariables.java


text:
"""
Enable pyglet to be used interacive by setting PyOS_InputHook.

Authors
-------

* Nicolas P. Rougier
* Fernando Perez
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2008-2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import os
import sys
import time
from timeit import default_timer as clock
import pyglet

#-----------------------------------------------------------------------------
# Platform-dependent imports and functions
#-----------------------------------------------------------------------------

if os.name == 'posix':
    import select

    def stdin_ready():
        infds, outfds, erfds = select.select([sys.stdin],[],[],0)
        if infds:
            return True
        else:
            return False

elif sys.platform == 'win32':
    import msvcrt

    def stdin_ready():
        return msvcrt.kbhit()

# On linux only, window.flip() has a bug that causes an AttributeError on
# window close.  For details, see:
# http://groups.google.com/group/pyglet-users/browse_thread/thread/47c1aab9aa4a3d23/c22f9e819826799e?#c22f9e819826799e

if sys.platform.startswith('linux'):
    def flip(window):
        try:
            window.flip()
        except AttributeError:
            pass
else:
    def flip(window):
        window.flip()

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------

def inputhook_pyglet():
    """Run the pyglet event loop by processing pending events only.

    This keeps processing pending events until stdin is ready.  After
    processing all pending events, a call to time.sleep is inserted.  This is
    needed, otherwise, CPU usage is at 100%.  This sleep time should be tuned
    though for best performance.
    """
    # We need to protect against a user pressing Control-C when IPython is
    # idle and this is running. We trap KeyboardInterrupt and pass.
    try:
        t = clock()
        while not stdin_ready():
            pyglet.clock.tick()
            for window in pyglet.app.windows:
                window.switch_to()
                window.dispatch_events()
                window.dispatch_event('on_draw')
                flip(window)

            # We need to sleep at this point to keep the idle CPU load
            # low.  However, if sleep to long, GUI response is poor.  As
            # a compromise, we watch how often GUI events are being processed
            # and switch between a short and long sleep time.  Here are some
            # stats useful in helping to tune this.
            # time    CPU load
            # 0.001   13%
            # 0.005   3%
            # 0.01    1.5%
            # 0.05    0.5%
            used_time = clock() - t
            if used_time > 5*60.0:
                # print 'Sleep for 5 s'  # dbg
                time.sleep(5.0)
            elif used_time > 10.0:
                # print 'Sleep for 1 s'  # dbg
                time.sleep(1.0)
            elif used_time > 0.1:
                # Few GUI events coming in, so we can sleep longer
                # print 'Sleep for 0.05 s'  # dbg
                time.sleep(0.05)
            else:
                # Many GUI events coming in, so sleep only very little
                time.sleep(0.001)
    except KeyboardInterrupt:
        pass
    return 0


text:
package metamorphic.dsl

import metamorphic.Settings
import metamorphic.dsl.generator._
import metamorphic.dsl.matcher._
import metamorphic.dsl.matcher.model._

import scala.annotation.StaticAnnotation
import scala.reflect.macros.blackbox.Context

class app extends StaticAnnotation {

  def macroTransform(annottees: Any*): Any = macro AppImplementation.app
}

class AppImplementation(val context: Context) {

  implicit val c: Context = context
  import c.universe._

  def app(annottees: c.Expr[Any]*): c.Expr[Any] = {

    validateDependencies

    val appTree = annottees.map(_.tree).head

    val model = ModelMatcher.eval(c)(appTree)
    val application = ApplicationMatcher.eval(c)(appTree, model)

    if (Settings.macroDebug) {
      println("App expansion: " + application.name)
      println("Model: " + model)
      println("Application: " + application)
    }

    if (model.entities.length == 0) {
      c.abort(c.enclosingPosition, s"Unable to generate '${application.name}'. No entities.")
      annottees.head
    } else {
      val result = ApplicationGenerator.generate(model, application)

      if (Settings.macroDebug) {
        println("App result: " + result)
      }

      result
    }
  }

  def validateDependencies: Unit = {
    Dependencies.repositoryGenerator match {
      case None => c.abort(c.enclosingPosition, "Repository generator library not found.")
      case Some(_) =>
    }

    Dependencies.serviceGenerator match {
      case None => c.abort(c.enclosingPosition, "Service generator library not found.")
      case Some(_) =>
    }
  }
}


text:
require 'ansible/ruby/modules/base'

module Ansible
  module Ruby
    module Modules
      # Get facts for virtual machine images.
      class Azure_rm_virtualmachineimage_facts < Base
        # @return [String] Azure location value (ie. westus, eastus, eastus2, northcentralus, etc.). Supplying only a location value will yield a list of available publishers for the location.
        attribute :location
        validates :location, presence: true, type: String

        # @return [String, nil] Name of an image publisher. List image offerings associated with a particular publisher.
        attribute :publisher
        validates :publisher, type: String

        # @return [String, nil] Name of an image offering. Combine with sku to see a list of available image versions.
        attribute :offer
        validates :offer, type: String

        # @return [String, nil] Image offering SKU. Combine with offer to see a list of available versions.
        attribute :sku
        validates :sku, type: String

        # @return [String, nil] Specific version number of an image.
        attribute :version
        validates :version, type: String
      end
    end
  end
end


text:
<?php

namespace Flowcode\ShopBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class FlowcodeShopBundle extends Bundle
{
}