@Override public Properties getProperties() { if (props == null) { props = loadPropertiesFromFile(file); } return props; }
public RoxyTokenReplacer() { super(); setPropertyPrefix("@ml."); addPropertiesSource(new FilePropertiesSource(new File("deploy/default.properties"))); addPropertiesSource(new FilePropertiesSource(new File("deploy/build.properties"))); addPropertiesSource(new FilePropertiesSource(new File("deploy/local.properties"))); }
/** * Initialize the Properties instance based on all the PropertiesSources that have been registered. */ protected void initializeProperties() { properties = new Properties(); for (PropertiesSource source : propertiesSources) { Properties p = source.getProperties(); if (p != null) { properties.putAll(p); } } }
protected TokenReplacer buildModuleTokenReplacer(AppConfig appConfig) { DefaultTokenReplacer r = appConfig.isUseRoxyTokenPrefix() ? new RoxyTokenReplacer() : new DefaultTokenReplacer(); final Map<String, String> customTokens = appConfig.getCustomTokens(); if (customTokens != null) { r.addPropertiesSource(new MapPropertiesSource(customTokens)); } if (appConfig.getModuleTokensPropertiesSources() != null) { for (PropertiesSource ps : appConfig.getModuleTokensPropertiesSources()) { r.addPropertiesSource(ps); } } return r; } }
private TokenReplacer buildModuleTokenReplacer(AppConfig appConfig) { DefaultTokenReplacer r = new DefaultTokenReplacer(); final Map<String, String> customTokens = appConfig.getCustomTokens(); if (customTokens != null && !customTokens.isEmpty()) { r.addPropertiesSource(() -> { Properties p = new Properties(); p.putAll(customTokens); return p; }); } return r; }
@Override public String replaceTokens(String text) { if (properties == null) { initializeProperties(); } if (helper == null) { initializeHelper(); } for (Object key : properties.keySet()) { String skey = propertyPrefix != null ? propertyPrefix + key : key.toString(); if (logger.isTraceEnabled()) { logger.trace("Checking for key in text: " + skey); } if (text.contains(skey)) { String value = properties.getProperty(key.toString()); value = helper.replacePlaceholders(value, properties); if (logger.isDebugEnabled()) { logger.debug(format("Replacing %s with %s", skey, value)); } text = text.replace(skey, value); } } return text; }
/** * Handles reading in the content of a Resource, and then replacing tokens in it if a tokenReplacer has been set. */ protected String readAndReplaceTokens(Resource r) { String content; try { content = new String(FileCopyUtils.copyToByteArray(r.getInputStream())); } catch (IOException e) { throw new RuntimeException("Unable to read content from: " + r.getDescription() + "; cause: " + e.getMessage(), e); } if (tokenReplacer != null) { content = tokenReplacer.replaceTokens(content); } return content; }
/** * Populate the customTokens map in this class with the properties from the given properties source. * @param propertiesSource * @param prefix optional; if set, then each token key that is added has the prefix prepended to it * @param suffix optional; if set, then each token key that is added has the suffix appended to it */ public void populateCustomTokens(PropertiesSource propertiesSource, String prefix, String suffix) { Properties props = propertiesSource.getProperties(); if (props != null) { if (customTokens == null) { customTokens = new HashMap<>(); } for (Object key : props.keySet()) { String skey = (String)key; String value = props.getProperty(skey); if (value != null) { String token = skey; if (prefix != null) { token = prefix + token; } if (suffix != null) { token = token + suffix; } customTokens.put(token, value); } } } }
@Override public DocumentFile processDocumentFile(DocumentFile documentFile) { if (tokenReplacer != null && moduleCanBeReadAsString(documentFile.getFormat())) { String text = documentFile.getModifiedContent(); if (text == null) { Resource resource = documentFile.getResource(); if (resource != null) { try { text = new String(FileCopyUtils.copyToByteArray(resource.getInputStream())); } catch (IOException ie) { logger.warn("Unable to replace tokens in file: " + documentFile.getUri() + "; cause: " + ie.getMessage()); } } } if (text != null) { text = tokenReplacer.replaceTokens(text); documentFile.setModifiedContent(text); } } return documentFile; }