Java Code Examples for org.apache.commons.text.StringSubstitutor

The following examples show how to use org.apache.commons.text.StringSubstitutor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: DataflowTemplates   Source File: BigQueryConverters.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@code String} using Key/Value style formatting.
 *
 * @param formatTemplate a String with bracketed keys to apply "I am a {key}"
 * @param row is a TableRow object which is used to supply key:values to the template
 *
 * <p> Extracts TableRow fields and applies values to the formatTemplate.
 * ie. formatStringTemplate("I am {key}"{"key": "formatted"}) -> "I am formatted"
 */
public static String formatStringTemplate(String formatTemplate, TableRow row) {
    // Key/Value Map used to replace values in template
    Map<String, String> values = new HashMap<>();

    // Put all column/value pairs into key/value map
    Set<String> rowKeys = row.keySet();
    for (String rowKey : rowKeys) {
      // Only String types can be used in comparison
      if(row.get(rowKey) instanceof String) {
        values.put(rowKey, (String) row.get(rowKey));
      }
    }
    // Substitute any templated values in the template
    String result = StringSubstitutor.replace(formatTemplate, values, "{", "}");
    return result;
}
 
Example 2
Source Project: DataflowTemplates   Source File: BigQueryConverters.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return a formatted String Using Key/Value Style formatting
 * from the TableRow applied to the Format Template.
 * ie. formatStringTemplate("I am {key}"{"key": "formatted"}) -> "I am formatted"
 */
public static String formatStringTemplate(String formatTemplate, TableRow row) {
    // Key/Value Map used to replace values in template
    Map<String, String> values = new HashMap<>();

    // Put all column/value pairs into key/value map
    Set<String> rowKeys = row.keySet();
    for (String rowKey : rowKeys) {
      // Only String types can be used in comparison
      if(row.get(rowKey) instanceof String) {
        values.put(rowKey, (String) row.get(rowKey));
      }
    }
    // Substitute any templated values in the template
    String result = StringSubstitutor.replace(formatTemplate, values, "{", "}");
    return result;
}
 
Example 3
public String build() {
    Map<String, String> variables = new HashMap<>();
    variables.put("tempTable", TAG_GROUP_TEMP_TABLE);
    variables.put("dimensions", dimensions);
    variables.put("passed", String.valueOf(StatusType.PASSED.getId()));
    variables.put("conditionallyPassed", String.valueOf(StatusType.CONDITIONALLY_PASSED.getId()));
    variables.put("failed", String.valueOf(StatusType.FAILED.getId()));
    variables.put("nestedDimensions", nestedDimensions);
    variables.put("totalTimeField", getTotalTimeField());
    variables.put("nestedResultIds", joinType.nestedResultFields);
    variables.put("nestedJoinTables", joinType.nestedJoinTables);
    variables.put("nestedGroupFields", joinType.nestedGroupFields);
    variables.put("joinTables", joinType.resultJoinTables);
    variables.put("where", buildWhereClause());
    variables.put("groupCount", groupCount);
    variables.put("groupFields", groupFields);
    variables.put("orderFields", orderFields);
    return new StringSubstitutor(variables).replace(MAIN_QUERY_PATTERN);
}
 
Example 4
Source Project: component-runtime   Source File: ApiMockUpdate.java    License: Apache License 2.0 6 votes vote down vote up
private static CompletableFuture<Void> capture(final Map<String, byte[]> files, final Executor executor,
        final String path, final String base, final Map<String, String> templates,
        final Function<WebTarget, byte[]> target) {
    return CompletableFuture.runAsync(() -> {
        final String outputPath = new StringSubstitutor(templates, "{", "}").replace(path);
        log.info("Trying to grab {}", outputPath);
        final Client client = ClientBuilder.newClient();
        try {
            WebTarget webTarget = client.target(base).path(path);
            for (final Map.Entry<String, String> tpl : templates.entrySet()) {
                webTarget = webTarget.resolveTemplate(tpl.getKey(), tpl.getValue());
            }
            webTarget.property("http.connection.timeout", 30000L).property("http.receive.timeout", 60000L);
            files.put(outputPath, target.apply(webTarget));
            log.info("Grabbed to grab {}", outputPath);
        } catch (final ProcessingException | WebApplicationException ex) {
            log.error("Error on {}", outputPath, ex);
            throw ex;
        } finally {
            client.close();
        }
    }, executor);
}
 
Example 5
Source Project: rdf4j   Source File: BuildURI.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length < 1) {
		throw new ValueExprEvaluationException("Incorrect number of arguments");
	}
	if (!(args[0] instanceof Literal)) {
		throw new ValueExprEvaluationException("First argument must be a string");
	}
	Literal s = (Literal) args[0];
	String tmpl = s.getLabel();
	Map<String, String> mappings = new HashMap<>(args.length);
	for (int i = 1; i < args.length; i++) {
		mappings.put(Integer.toString(i), args[i].stringValue());
	}
	String newValue = StringSubstitutor.replace(tmpl, mappings, "{?", "}");
	if (tmpl.charAt(0) == '<' && tmpl.charAt(tmpl.length() - 1) == '>') {
		return valueFactory.createURI(newValue.substring(1, newValue.length() - 1));
	}
	throw new ValueExprEvaluationException("Invalid URI template: " + tmpl);
}
 
Example 6
Source Project: rdf4j   Source File: BuildString.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException {
	if (args.length < 1) {
		throw new ValueExprEvaluationException("Incorrect number of arguments");
	}
	if (!(args[0] instanceof Literal)) {
		throw new ValueExprEvaluationException("First argument must be a string");
	}
	Literal s = (Literal) args[0];
	String tmpl = s.getLabel();
	Map<String, String> mappings = new HashMap<>(args.length);
	for (int i = 1; i < args.length; i++) {
		mappings.put(Integer.toString(i), args[i].stringValue());
	}
	String newValue = StringSubstitutor.replace(tmpl, mappings, "{?", "}");
	return valueFactory.createLiteral(newValue);
}
 
Example 7
Source Project: cuba   Source File: App.java    License: Apache License 2.0 6 votes vote down vote up
protected void initHomeDir() {
    String homeDir = System.getProperty(DesktopAppContextLoader.HOME_DIR_SYS_PROP);
    if (StringUtils.isBlank(homeDir)) {
        homeDir = getDefaultHomeDir();
    }
    homeDir = StringSubstitutor.replaceSystemProperties(homeDir);
    System.setProperty(DesktopAppContextLoader.HOME_DIR_SYS_PROP, homeDir);

    File file = new File(homeDir);
    if (!file.exists()) {
        boolean success = file.mkdirs();
        if (!success) {
            System.out.println("Unable to create home dir: " + homeDir);
            System.exit(-1);
        }
    }
    if (!file.isDirectory()) {
        System.out.println("Invalid home dir: " + homeDir);
        System.exit(-1);
    }
}
 
Example 8
private static String addRelationshipWhereClause( Long relationshipTypeId, RelationshipEntity relationshipEntity )
{
    String sql = new StringSubstitutor(
        ImmutableMap.<String, Long> builder().put( "relationshipid", relationshipTypeId ).build() )
            .replace( RELATIONSHIP_JOIN );

    sql += " AND ";

    switch ( relationshipEntity )
    {
    case TRACKED_ENTITY_INSTANCE:
        return sql + "tei.uid = ax.tei ";
    case PROGRAM_STAGE_INSTANCE:
        return sql + "psi.uid = ax.psi ";
    case PROGRAM_INSTANCE:
        return sql + "pi.uid = ax.pi ";
    default:
        throw new IllegalQueryException( new ErrorMessage( ErrorCode.E7227, relationshipEntity.name() ) );
    }
}
 
Example 9
Source Project: elasticsearch-beyonder   Source File: SettingsReader.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Read a file content from the classpath
 * @param file filename
 * @return The file content
 */
public static String readFileFromClasspath(String file) {
	logger.trace("Reading file [{}]...", file);
	String content = null;

	try (InputStream asStream = SettingsReader.class.getClassLoader().getResourceAsStream(file)) {
		if (asStream == null) {
			logger.trace("Can not find [{}] in class loader.", file);
			return null;
		}
		content = IOUtils.toString(asStream, "UTF-8");
	} catch (IOException e) {
		logger.warn("Can not read [{}].", file);
	}

	return StringSubstitutor.replace(content, System.getenv());
}
 
Example 10
Source Project: aem-component-generator   Source File: CommonUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Method to read the content of the provided template file as string.
 *
 * @param filePath Path to the template file in the project
 * @param generationConfig The {@link GenerationConfig} object with all the populated values
 * @return String return content of the resource file as string or null when file not exists
 */
public static String getTemplateFileAsString(String filePath, GenerationConfig generationConfig) {
    try (InputStream inputStream = CommonUtils.class.getClassLoader().getResourceAsStream(filePath)) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        Map<String, String> stringsToReplaceValueMap = getStringsToReplaceValueMap(generationConfig);
        StringSubstitutor stringSubstitutor = new StringSubstitutor(stringsToReplaceValueMap);
        String content = reader.lines().collect(Collectors.joining(System.lineSeparator()));
        return stringSubstitutor.replace(content);
    } catch (IOException e) {
        LOG.error("Failed to read " + filePath + " from the classpath.", e);
    }
    return null;
}
 
Example 11
Source Project: aem-core-cif-components   Source File: UrlProviderImpl.java    License: Apache License 2.0 5 votes vote down vote up
private String toUrl(SlingHttpServletRequest request, Page page, Map<String, String> params, String template, String selectorFilter) {
    if (page != null && params.containsKey(selectorFilter)) {
        Resource pageResource = page.adaptTo(Resource.class);
        boolean deepLink = !WCMMode.DISABLED.equals(WCMMode.fromRequest(request));
        if (deepLink) {
            Resource subPageResource = toSpecificPage(pageResource, params.get(selectorFilter));
            if (subPageResource != null) {
                pageResource = subPageResource;
            }
        }

        params.put(PAGE_PARAM, pageResource.getPath());
    }

    String prefix = "${", suffix = "}"; // variables have the format ${var}
    if (template.contains("{{")) {
        prefix = "{{";
        suffix = "}}"; // variables have the format {{var}}
    }

    StringSubstitutor sub = new StringSubstitutor(params, prefix, suffix);
    String url = sub.replace(template);
    url = StringUtils.substringBeforeLast(url, "#" + prefix); // remove anchor if it hasn't been substituted

    if (url.contains(prefix)) {
        LOGGER.warn("Missing params for URL substitution. Resulted URL: {}", url);
    }

    return url;
}
 
Example 12
@Override
public String apply(PubsubMessage message) {
  final String batchKey = StringSubstitutor.replace(template,
      DerivedAttributesMap.of(message.getAttributesMap()));
  if (batchKey.contains("$")) {
    throw new IllegalArgumentException("Element did not contain all the attributes needed to"
        + " fill out variables in the configured template: " + template);
  }
  return batchKey;
}
 
Example 13
Source Project: gcp-ingestion   Source File: SchemaStore.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Return the parsed schema corresponding to doctype and namespace parsed from attributes.
 *
 * @throws SchemaNotFoundException if schema matching the attributes exists
 */
public T getSchema(Map<String, String> attributes) throws SchemaNotFoundException {
  ensureSchemasLoaded();
  if (attributes == null) {
    throw new SchemaNotFoundException("No schema for message with null attributeMap");
  }
  // This is the path provided by mozilla-pipeline-schemas
  final String path = StringSubstitutor.replace(
      "${document_namespace}/${document_type}/${document_type}.${document_version}", attributes)
      + schemaSuffix();
  return getSchema(path);
}
 
Example 14
Source Project: gushici   Source File: ConvertUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getSvg(String content, Double fontSize, Double spacing) {
    int length = content.length();
    double realWidth = length * fontSize + (length-1) * spacing;
    double realFontSize = fontSize;
    Map<String, Object> templateValue  = new HashMap<>();
    templateValue.put("width", realWidth);
    templateValue.put("font-size", realFontSize);
    templateValue.put("spacing", spacing);
    templateValue.put("content", content);
    templateValue.put("height", realFontSize * 1.1);
    StringSubstitutor sub = new StringSubstitutor(templateValue);
    return sub.replace(svgTemplate);
}
 
Example 15
Source Project: DataflowTemplates   Source File: MergeStatementBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public String buildMergeStatement(
    String replicaTable,
    String stagingTable,
    List<String> primaryKeyFields,
    List<String> allFields) {
  // Key/Value Map used to replace values in template
  Map<String, String> mergeQueryValues = new HashMap<>();

  mergeQueryValues.put("replicaTable", replicaTable);
  mergeQueryValues.put("replicaAlias", REPLICA_TABLE_NAME);
  mergeQueryValues.put("stagingAlias", STAGING_TABLE_NAME);
  mergeQueryValues.put("deleteColumn", configuration.deletedFieldName()); // TODO require config options

  mergeQueryValues.put(
    "stagingViewSql",
    buildLatestViewOfStagingTable(
        stagingTable, allFields, primaryKeyFields,
        configuration.timestampFieldName(), configuration.deletedFieldName(),
        configuration.partitionRetention()));

  mergeQueryValues.put("joinCondition", buildJoinConditions(primaryKeyFields, REPLICA_TABLE_NAME, STAGING_TABLE_NAME));
  mergeQueryValues.put("timestampCompareSql", buildTimestampCheck(configuration.timestampFieldName()));
  mergeQueryValues.put("mergeUpdateSql", buildUpdateStatement(allFields));
  mergeQueryValues.put("mergeInsertSql", buildInsertStatement(allFields));

  String mergeStatement = StringSubstitutor.replace(configuration.mergeQueryTemplate(), mergeQueryValues, "{", "}");
  return mergeStatement;
}
 
Example 16
Source Project: DataflowTemplates   Source File: MergeStatementBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public String buildMergeStatement(
    String replicaTable,
    String stagingTable,
    List<String> primaryKeyFields,
    List<String> allFields) {
  // Key/Value Map used to replace values in template
  Map<String, String> mergeQueryValues = new HashMap<>();

  mergeQueryValues.put("replicaTable", replicaTable);
  mergeQueryValues.put("replicaAlias", REPLICA_TABLE_NAME);
  mergeQueryValues.put("stagingAlias", STAGING_TABLE_NAME);
  mergeQueryValues.put("deleteColumn", configuration.deletedFieldName()); // TODO require config options

  mergeQueryValues.put(
    "stagingViewSql",
    buildLatestViewOfStagingTable(
        stagingTable, allFields, primaryKeyFields,
        configuration.timestampFieldName(), configuration.deletedFieldName(),
        configuration.partitionRetention()));

  mergeQueryValues.put("joinCondition", buildJoinConditions(primaryKeyFields, REPLICA_TABLE_NAME, STAGING_TABLE_NAME));
  mergeQueryValues.put("timestampCompareSql", buildTimestampCheck(configuration.timestampFieldName()));
  mergeQueryValues.put("mergeUpdateSql", buildUpdateStatement(allFields));
  mergeQueryValues.put("mergeInsertSql", buildInsertStatement(allFields));

  String mergeStatement = StringSubstitutor.replace(configuration.mergeQueryTemplate(), mergeQueryValues, "{", "}");
  return mergeStatement;
}
 
Example 17
Source Project: kayenta   Source File: QueryConfigUtils.java    License: Apache License 2.0 5 votes vote down vote up
private static String expandTemplate(
    String templateToExpand, Map<String, String> templateBindings) {
  try {
    log.debug("Expanding template='{}' with variables={}", templateToExpand, templateBindings);
    StringSubstitutor substitutor = new StringSubstitutor(templateBindings);
    substitutor.setEnableUndefinedVariableException(true);
    String expandedTemplate = substitutor.replace(templateToExpand);
    log.debug("Expanded template='{}'", expandedTemplate);

    return expandedTemplate;
  } catch (Exception e) {
    throw new IllegalArgumentException(
        "Problem evaluating custom filter template: " + templateToExpand, e);
  }
}
 
Example 18
Source Project: Rails   Source File: Slack.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void sendMessage(String player) {
    setConfig();
    if ( webhook == null ) {
        return;
    }
    Map<String, String> keys = new HashMap<>();
    keys.put("game", root.getGameName());
    //keys.put("gameName", StringUtils.defaultIfBlank(root.getGameData().getUsersGameName(), "[none]"));
    keys.put("round", gameUiManager.getCurrentRound().getRoundName());
    keys.put("current", StringUtils.defaultIfBlank(playerNameMappings.get(player), player));
    keys.put("previous", StringUtils.defaultIfBlank(observer.getFormerPlayer().getId(), "[none]"));

    String msgBody = StringSubstitutor.replace(body, keys);
    log.debug("Sending message '{}' to Slack for user {}", msgBody, player);

    HttpPost httpPost = new HttpPost(webhook);
    try {
        httpPost.setEntity(new StringEntity(msgBody));
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        httpPost.setHeader(HttpHeaders.USER_AGENT, "18xx Rails");
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if ( response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT ) {
            // TODO: verify result
            log.debug("Unexpected Slack response: {}", response);
        }
        response.close();
    }
    catch (IOException e) {
        log.error("Error sending message to Slack", e);
    }
}
 
Example 19
Source Project: Rails   Source File: Discord.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void sendMessage(String player) {
    setConfig();
    if ( webhook == null ) {
        return;
    }
    Map<String, String> keys = new HashMap<>();
    keys.put("game", root.getGameName());
    //keys.put("gameName", StringUtils.defaultIfBlank(root.getGameData().getUsersGameName(), "[none]"));
    keys.put("round", gameUiManager.getCurrentRound().getRoundName());
    keys.put("current", StringUtils.defaultIfBlank(playerNameMappings.get(player), player));
    keys.put("previous", StringUtils.defaultIfBlank(observer.getFormerPlayer().getId(), "[none]"));

    String msgBody = StringSubstitutor.replace(body, keys);
    log.debug("Sending message '{}' to Discord for user {}", msgBody, player);

    HttpPost httpPost = new HttpPost(webhook);
    try {
        httpPost.setEntity(new StringEntity(msgBody));
        httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        httpPost.setHeader(HttpHeaders.USER_AGENT, "18xx Rails");
        CloseableHttpResponse response = httpClient.execute(httpPost);
        if ( response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT ) {
            log.debug("Unexpected Discord  response: {}", response);
        }
        response.close();
    }
    catch (IOException e) {
        log.error("Error sending message to Discord", e);
    }
}
 
Example 20
Source Project: cuba   Source File: AppProperties.java    License: Apache License 2.0 5 votes vote down vote up
private String handleInterpolation(String value) {
    StringSubstitutor substitutor = new StringSubstitutor(key -> {
        String property = getSystemOrAppProperty(key);
        return property != null ? property : System.getProperty(key);
    });
    return substitutor.replace(value);
}
 
Example 21
private static String createQuery( ExportParams params )
{
    IdSchemes idSchemes = params.getOutputIdSchemes() != null ? params.getOutputIdSchemes() : new IdSchemes();

    ImmutableMap.Builder<String, String> namedParamsBuilder = ImmutableMap.<String, String>builder()
            .put( DATA_SET_SCHEME, idSchemes.getDataSetIdScheme().getIdentifiableString().toLowerCase() )
            .put( ORG_UNIT_SCHEME, idSchemes.getOrgUnitIdScheme().getIdentifiableString().toLowerCase() )
            .put( ATTR_OPT_COMBO_SCHEME, idSchemes.getAttributeOptionComboIdScheme().getIdentifiableString().toLowerCase() );

    String sql =
        "SELECT ds.${dsScheme} AS dsid, pe.startdate AS pe_start, pt.name AS ptname, ou.${ouScheme} AS ouid, " +
            "aoc.${aocScheme} AS aocid, cdsr.storedby AS storedby, cdsr.date AS created, cdsr.completed AS completed " +
            "FROM completedatasetregistration cdsr " +
            "INNER JOIN dataset ds ON ( cdsr.datasetid=ds.datasetid ) " +
            "INNER JOIN period pe ON ( cdsr.periodid=pe.periodid ) " +
            "INNER JOIN periodtype pt ON ( pe.periodtypeid=pt.periodtypeid ) " +
            "INNER JOIN organisationunit ou ON ( cdsr.sourceid=ou.organisationunitid ) " +
            "INNER JOIN categoryoptioncombo aoc ON ( cdsr.attributeoptioncomboid = aoc.categoryoptioncomboid ) ";

    sql += createOrgUnitGroupJoin( params );
    sql += createDataSetClause( params, namedParamsBuilder );
    sql += createOrgUnitClause( params, namedParamsBuilder );
    sql += createPeriodClause( params, namedParamsBuilder );
    sql += createCreatedClause( params, namedParamsBuilder );
    sql += createLimitClause( params, namedParamsBuilder );

    sql = new StringSubstitutor( namedParamsBuilder.build(), "${", "}" ).replace( sql );

    log.debug( "CompleteDataSetRegistrations query: " + sql );

    return sql;
}
 
Example 22
Source Project: ffwd   Source File: OpenCensusPluginSink.java    License: Apache License 2.0 5 votes vote down vote up
private String getOutputMetricName(Metric metric) {
  StringSubstitutor sub = new StringSubstitutor((key) -> {
    if (key.equals("key")) {
      return metric.getKey();
    } else {
      return metric.getTags().get(key);
    }
  });
  return sanitizeName(sub.replace(outputMetricNamePattern));
}
 
Example 23
Source Project: RemoteSupportTool   Source File: AboutDialog.java    License: Apache License 2.0 5 votes vote down vote up
private String replaceVariables(String text) {
    Properties replacements = new Properties();

    replacements.setProperty("app.title", settings.getString("title"));
    replacements.setProperty("app.version", settings.getString("version"));
    replacements.setProperty("openjdk.name", StringUtils.defaultIfBlank(
            SystemUtils.JAVA_RUNTIME_NAME, SystemUtils.JAVA_VM_NAME));
    replacements.setProperty("openjdk.version", StringUtils.defaultIfBlank(
            SystemUtils.JAVA_RUNTIME_VERSION, SystemUtils.JAVA_VERSION));
    return StringSubstitutor.replace(text, replacements);
}
 
Example 24
Source Project: syncope   Source File: ContentLoaderHandler.java    License: Apache License 2.0 5 votes vote down vote up
public ContentLoaderHandler(
        final DataSource dataSource,
        final String rootElement,
        final boolean continueOnError,
        final Environment env) {

    this.jdbcTemplate = new JdbcTemplate(dataSource);
    this.rootElement = rootElement;
    this.continueOnError = continueOnError;
    this.envParamSubstitutor = new StringSubstitutor(key -> {
        String value = env.getProperty(key);
        return StringUtils.isBlank(value) ? null : value;
    });
}
 
Example 25
Source Project: alf.io   Source File: LocationDescriptor.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getMapUrl(String lat, String lng, Map<ConfigurationKeys, ConfigurationManager.MaybeConfiguration> geoConf) {
    Map<String, String> params = new HashMap<>();
    params.put("latitude", lat);
    params.put("longitude", lng);

    ConfigurationKeys.GeoInfoProvider provider = getProvider(geoConf);
    String mapUrl = mapUrl(provider);

    fillParams(provider, geoConf, params);

    return new StringSubstitutor(params).replace(mapUrl);
}
 
Example 26
/**
 * Return the appropriate table destination instance for the given document type and other
 * attributes.
 */
public TableDestination getTableDestination(Map<String, String> attributes) {
  attributes = new HashMap<>(attributes);

  // We coerce all docType and namespace names to be snake_case and to remove invalid
  // characters; these transformations MUST match with the transformations applied by the
  // jsonschema-transpiler and mozilla-schema-generator when creating table schemas in BigQuery.
  final String namespace = attributes.get(Attribute.DOCUMENT_NAMESPACE);
  final String docType = attributes.get(Attribute.DOCUMENT_TYPE);
  if (namespace != null) {
    attributes.put(Attribute.DOCUMENT_NAMESPACE, getAndCacheNormalizedName(namespace));
  }
  if (docType != null) {
    attributes.put(Attribute.DOCUMENT_TYPE, getAndCacheNormalizedName(docType));
  }

  // Only letters, numbers, and underscores are allowed in BigQuery dataset and table names,
  // but some doc types and namespaces contain '-', so we convert to '_'; we don't pass all
  // values through getAndCacheBqName to avoid expensive regex operations and polluting the
  // cache of transformed field names.
  attributes = Maps.transformValues(attributes, v -> v.replaceAll("-", "_"));

  final String tableSpec = StringSubstitutor.replace(tableSpecTemplate.get(), attributes);

  // Send to error collection if incomplete tableSpec; $ is not a valid char in tableSpecs.
  if (tableSpec.contains("$")) {
    throw new IllegalArgumentException("Element did not contain all the attributes needed to"
        + " fill out variables in the configured BigQuery output template: "
        + tableSpecTemplate.get());
  }

  final TableDestination tableDestination = new TableDestination(tableSpec, null,
      new TimePartitioning().setField(partitioningField.get()),
      new Clustering().setFields(clusteringFields.get()));
  final TableReference ref = BigQueryHelpers.parseTableSpec(tableSpec);
  final DatasetReference datasetRef = new DatasetReference().setProjectId(ref.getProjectId())
      .setDatasetId(ref.getDatasetId());

  if (bqService == null) {
    bqService = BigQueryOptions.newBuilder().setProjectId(ref.getProjectId())
        .setRetrySettings(RETRY_SETTINGS).build().getService();
  }

  // Get and cache a listing of table names for this dataset.
  Set<String> tablesInDataset;
  if (tableListingCache == null) {
    // We need to be very careful about settings for the cache here. We have had significant
    // issues in the past due to exceeding limits on BigQuery API requests; see
    // https://bugzilla.mozilla.org/show_bug.cgi?id=1623000
    tableListingCache = CacheBuilder.newBuilder().expireAfterWrite(Duration.ofMinutes(10))
        .build();
  }
  try {
    tablesInDataset = tableListingCache.get(datasetRef, () -> {
      Set<String> tableSet = new HashSet<>();
      Dataset dataset = bqService.getDataset(ref.getDatasetId());
      if (dataset != null) {
        dataset.list().iterateAll().forEach(t -> {
          tableSet.add(t.getTableId().getTable());
        });
      }
      return tableSet;
    });
  } catch (ExecutionException e) {
    throw new UncheckedExecutionException(e.getCause());
  }

  // Send to error collection if dataset or table doesn't exist so BigQueryIO doesn't throw a
  // pipeline execution exception.
  if (tablesInDataset.isEmpty()) {
    throw new IllegalArgumentException("Resolved destination dataset does not exist or has no "
        + " tables for tableSpec " + tableSpec);
  } else if (!tablesInDataset.contains(ref.getTableId())) {
    throw new IllegalArgumentException("Resolved destination table does not exist: " + tableSpec);
  }

  return tableDestination;
}
 
Example 27
Source Project: gcp-ingestion   Source File: DynamicPathTemplate.java    License: Mozilla Public License 2.0 4 votes vote down vote up
/** Return the dynamic part of the output path with placeholders filled in. */
private String replaceDynamicPart(Map<String, String> attributes) {
  return StringSubstitutor.replace(dynamicPart, attributes);
}
 
Example 28
Source Project: sailfish-core   Source File: Tag.java    License: Apache License 2.0 4 votes vote down vote up
public void normalize(StringSubstitutor substitutor) {
       this.name = substitutor.replace(name);
}
 
Example 29
Source Project: pandaria   Source File: VariableExpression.java    License: MIT License 4 votes vote down vote up
@Override
public String evaluate(String raw) {
    return StringSubstitutor.replace(raw, variables.variables());
}
 
Example 30
Source Project: Rails   Source File: LogFile.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public String getPropertyValue() {
    ConfigManager.initConfiguration(false);

    String logDir = Config.get(CONFIG_LOG_DIRECTORY);
    if ( StringUtils.isBlank(logDir) ) {
        logDir = System.getProperty("user.home");
        switch ( SystemOS.get() ) {
            case MAC:
            case UNIX:
                logDir += File.separator + ".rails" + File.separator;
                break;

            case WINDOWS:
                // should point to the Documents directory on Windows
                logDir = FileSystemView.getFileSystemView().getDefaultDirectory().getPath() + File.separator + "Rails" + File.separator;
                break;

            default:
                logDir += File.separator + "Rails" + File.separator;
                // nothing to do
        }
    }

    String fileName = Config.get(CONFIG_LOG_FILENAME_PATTERN);
    if ( StringUtils.isNotBlank(fileName) ) {
        if ( fileName.startsWith(File.separator ) ) {
            // use the whole thing as is
            logDir = "";
        }
        // support inject of date, etc
        StringSubstitutor substitutor = StringSubstitutor.createInterpolator();
        substitutor.setEnableSubstitutionInVariables(true);
        fileName = substitutor.replace(fileName);
        if ( SystemOS.get() == SystemOS.MAC || SystemOS.get() == SystemOS.UNIX ) {
            // sanitize
            fileName = fileName.replace(' ', '_');
        }
    }
    else {
        fileName = "18xx_" + new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".log";
    }

    return logDir + fileName;
}