public Macro getReplacer() { if (replacer == null) return replacer = new Macro(this, getMacroDomains()); else return replacer; }
private String doCommands(String key) { String[] args = commands.split(key); if (args == null || args.length == 0) return null; for (int i = 0; i < args.length; i++) if (args[i].indexOf('\\') >= 0) args[i] = args[i].replaceAll("\\\\;", ";"); Processor rover = domain; while (rover != null) { String result = doCommand(rover, args[0], args); if (result != null) return result; rover = rover.getParent(); } for (int i = 0; targets != null && i < targets.length; i++) { String result = doCommand(targets[i], args[0], args); if (result != null) return result; } return doCommand(this, args[0], args); }
public String _filter(String args[]) { return filter(args, false); }
public String _version(String args[]) { verifyCommand(args, _versionHelp, null, 2, 3); String mask = args[1]; Version version = null; if (args.length >= 3) version = new Version(args[2]); return version(version, mask); }
public String process(String line) { return process(line,domain); }
public String _env(String args[]) { verifyCommand(args, "${env;<name>}, get the environmet variable", null, 2, 2); try { return System.getenv(args[1]); } catch (Throwable t) { return null; } }
if (c1 == end) { if (--nesting == 0) { result.append(replace(variable.toString(), link)); return index; } else if (c1 == '$' && index < line.length() - 2) { char c2 = line.charAt(index); char terminator = getTerminator(c2); if (terminator != 0) { index = process(line, index + 1, c2, terminator, variable, link); continue outer;
protected String replace(String key, Link link) { if (link != null && link.contains(key)) return "${infinite:" + link.toString() + "}"; if (key != null) { key = key.trim(); if (key.length() > 0) { String value = (String) properties.getProperty(key); if (value != null) return process(value, new Link(link, key)); value = doCommands(key); if (value != null) return process(value, new Link(link, key)); if (key != null && key.trim().length() > 0) { value = System.getProperty(key); if (value != null) return value; } if (!flattening) domain.warning("No translation found for macro: " + key); } else { domain.warning("Found empty macro key"); } } else { domain.warning("Found null macro key"); } return "${" + key + "}"; }
/** * Get the contents of a file. * * @param in * @return * @throws IOException */ public String _cat(String args[]) throws IOException { verifyCommand(args, "${cat;<in>}, get the content of a file", null, 2, 2); File f = domain.getFile(args[1]); if (f.isFile()) { InputStream in = new FileInputStream(f); return getString(in); } else if (f.isDirectory()) { return Arrays.toString(f.list()); } else { try { URL url = new URL(args[1]); InputStream in = url.openStream(); return getString(in); } catch (MalformedURLException mfue) { // Ignore here } return null; } }
/** * Wildcard a directory. The lists can contain Instruction that are matched * against the given directory * * ${lsr;<dir>;<list>(;<list>)*} ${lsa;<dir>;<list>(;<list>)*} * * @author aqute * */ public String _lsr(String args[]) { return ls(args, true); }
/** * Return the properties but expand all macros. This always returns a new * Properties object that can be used in any way. * * @return */ public Properties getFlattenedProperties() { return getReplacer().getFlattenedProperties(); }
boolean scan( ICharacterScanner scanner, StringBuffer buffer ) { int c = scanner.read(); if (c == ICharacterScanner.EOF) return false; int terminator = Macro.getTerminator((char) c); if (terminator == 0) return false; while(true) { c = scanner.read(); buffer.append((char)c); if ( c == terminator ) return true; else if ( c == '$') { if ( !scan(scanner, buffer) ) return false; } else if ( c == '\\') { c = scanner.read(); if ( c == ICharacterScanner.EOF) return false; buffer.append((char)c); } } } }
public String process(String line) { return process(line, null); }
public String _if(String args[]) { verifyCommand(args, _ifHelp, null, 3, 4); String condition = args[1].trim(); if (condition.length() != 0) return args[2]; if (args.length > 3) return args[3]; else return ""; }
if (c1 == end) { if (--nesting == 0) { result.append(replace(variable.toString(), link)); return index; } else if (c1 == '$' && index < line.length() - 2) { char c2 = line.charAt(index); char terminator = getTerminator(c2); if (terminator != 0) { index = process(line, index + 1, c2, terminator, variable, link); continue outer;
return process(value, new Link(source,link, key)); value = doCommands(key, link); if (value != null) return process(value, new Link(source, link, key));
public String _range(String args[]) { verifyCommand(args, _rangeHelp, _rangePattern, 2, 3); Version version = null; if (args.length >= 3) version = new Version(args[2]); String spec = args[1]; Matcher m = RANGE_MASK.matcher(spec); m.matches(); String floor = m.group(1); String floorMask = m.group(2); String ceilingMask = m.group(3); String ceiling = m.group(4); StringBuilder sb = new StringBuilder(); sb.append(floor); sb.append(version(version, floorMask)); sb.append(","); sb.append(version(version, ceilingMask)); sb.append(ceiling); String s = sb.toString(); VersionRange vr = new VersionRange(s); if (!(vr.includes(vr.getHigh()) || vr.includes(vr.getLow()))) { domain.error("${range} macro created an invalid range %s from %s and mask %s", s, version, spec); } return sb.toString(); }
verifyCommand(args, "${system;<command>[;<in>]}, execute a system command", null, 2, 3); String s = getString(process.getInputStream()); process.getInputStream().close(); int exitValue = process.waitFor();
/** * Wildcard a directory. The lists can contain Instruction that are matched * against the given directory * * ${lsr;<dir>;<list>(;<list>)*} * ${lsa;<dir>;<list>(;<list>)*} * * @author aqute * */ public String _lsr(String args[]) { return ls(args, true); }
/** * Return the properties but expand all macros. This always returns a new * Properties object that can be used in any way. * * @return */ public Properties getFlattenedProperties() { return getReplacer().getFlattenedProperties(); }