System.out.format not working in java for loop - java

Below is the concerned code. Basically what the code is supposed to do is output the URL, name and version of each GitHub release defined by GetUpdateInfo.getInfo().
GetUpdateInfo.getInfo (NOTE Strings login, oauth and password omitted for security reasons.):
import java.util.List;
import org.kohsuke.github.*;
import org.apache.commons.lang3.ArrayUtils;
public class GetUpdateInfo {
public static getInfo() {
String version = "";
String url = "";
String[] urls = {};
String[] names = {};
String[] versions = {};
String[] releases = {};
GitHub github = GitHubBuilder.fromEnvironment(login, password, oauth).build();
//Get the repo name from the organization
GHOrganization gho = github.getOrganization("NuovoLauncher-Mods");
PagedIterable<GHRepository> repos = gho.listRepositories();
List<GHRepository> repos_list = repos.asList();
for(int i=0; i < repos_list.size(); i++) {
GHRepository repo_test = repos_list.get(i);
GHRelease latest = repo_test.getLatestRelease();
ArrayUtils.add(releases, latest.toString());
ArrayUtils.add(names, latest.getName());
ui.setName(names);
ui.setRelease(releases);
List<GHAsset> assets = latest.getAssets();
for( int x = 0; x < assets.size(); x++ ) {
GHAsset asset = assets.get(x);
url = asset.getBrowserDownloadUrl();
version = url.split("/")[7];
System.out.format("URL: %s, Name: %s, Latest Release: %s. Version %s\n", url, latest.getName(), latest, version);
ArrayUtils.add(urls, url);
ArrayUtils.add(versions, version);
ui.setURL(urls);
ui.setVersion(versions);
}
}
return ui;
}
public static void main(String[] args) throws Exception {
GetUpdateInfo.getInfo();
}
}
DownloadUpdate.runner:
public static void runner() throws Exception {
String system_type = System.getProperty("os.name");
File fpath = new File("");
UpdateInfo ui = GetUpdateInfo.getInfo();
for(int i = 0; i < ui.getName().length; i++) {
System.out.format("URL: %s, Name %s, Version, %s", ui.getURL()[i], ui.getName()[i], ui.getVersion()[i]);
System.out.format("Downloading %s-%s", ui.getName()[i], ui.getVersion()[i]);
System.out.print("\n");
if(system_type.equals("Linux")) {
fpath = new File(System.getProperty("user.home") + "/.minecraft/mods/" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
} else if(system_type.equals("Windows")) {
fpath = new File(System.getProperty("user.home") + "/AppData/Roaming/.minecraft/mods" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
} else {
fpath = new File(System.getProperty("user.home") + "/.minecraft/mods/" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
}
String url = ui.getURL()[i];
FileUtils.copyURLToFile(new URL(url), fpath);
}
}
public static void main(String[] args) throws Exception {
System.out.println("DEBUG START");
DownloadUpdate.runner();
}
}
Looking at the code, I cannot see a reason why the code is not outputting like expected; I am getting zero output on console, simply the line stating that the code is being executed. No exceptions either.
EDIT: variable ui is not being returned properly. For example, ui.getName[0] throws an ArrayIndexOutOfBoundsException, due to the length being zero. Seeing this, I now understand why the for loop isn't behaving as expected. Is this a scope issue? What am I missing here?

An obvious problem of your code is the use of ArrayUtils.add: you have to reassign its result to the input array, as you cannot modify arrays like lists in Java.
Use it like this:
releases = ArrayUtils.add(releases, latest.toString());
names = ArrayUtils.add(names, latest.getName());
and later in the for-loop:
urls = ArrayUtils.add(urls, url);
versions = ArrayUtils.add(versions, version);
Also you don't need to set the elements in each loop cycle to the result:
ui.setURL(urls);
ui.setVersion(versions);
Those would be sufficient once the for-loop has completed.
An alternative would be to first use List<String> instead of the arrays. If you have control over the UpdateInfo class, change it there to be lists too, otherwise create an array from the lists before you set it in UpdateInfo.
As a general advice I would recommend that you get rid of your static methods. Create instances and use your credentials (login, password, oauth) as member fields OR pass in even the whole GitHub instance. This way you would have an easier time writing proper tests.

Related

Implementing language locales into array to be used in a loop

I'm trying to read every file in a directory, clean up with java util.locale, then write to a new directory. The reading and writing methods work, the Locale.SPANISH might be the issue as I have read in other posts.
I iterated through the available languages in the java.util.locale, spanish was in there.
First, the array issue: the following extract of code below is the long way of entering the Locale.(LANGUAGE) into the array. This seems to work fine. However, I can't understand why the 'short' way doesn't seem to work.
String[] languageLocale = new String[fileArray.length];
languageLocale[0] = "Locale.ENGLISH";
languageLocale[1] = "Locale.FRENCH";
languageLocale[2] = "Locale.GERMAN";
languageLocale[3] = "Locale.ITALIAN";
languageLocale[4] = "Locale.SPANISH";
The short way:
String[] languageLocale = new String[("Locale.ENGLISH" , "Locale.FRENCH" , "Locale.GERMAN" , "Locale.ITALIAN" , "Locale.SPANISH")];
I need to input the Locale.(langauge) into a string so they can be called in the following:
File file = new File("\\LanguageGuessing5.0\\Learning\\");
File[] fileArray = file.listFiles();
ArrayList<String> words = new ArrayList<String>();
for (int i = 0; i < fileArray.length; i++) {
if (fileArray[i].isFile()) {
if (fileArray[i].isHidden()) {
continue;
} else {
String content = readUTF8File("\\LanguageGuessing5.0\\Learning\\"+fileArray[i].getName());
words = extractWords(content, languageLocale[i]);
outputWordsToUTF8File("\\LanguageGuessing5.0\\Model\\"+ fileArray[i].getName() + "out.txt", words);
}
} else if (fileArray[i].isDirectory()) {
System.out.println("Directory " + fileArray[i].getName());
}
}
The following method call:
words = extractWords(content, languageLocale[i]);
also presents the following error:
The method extractWords(String, Locale) in the type CleaningText(the class name) is not applicable for the arguments (String, String)
My understanding is that while the array argument is not a locale, the string holds the correct text to make it valid. I'm clearly incorrect, I'm hoping someone could explain how this works.
The input types of the methods are below for context:
public static String readUTF8File(String filePath)
public static ArrayList extractWords(String inputText, Locale currentLocale)
public static void outputWordsToUTF8File(String filePath, ArrayList wordList)
Many thanks in advance

Java - Variable number of variables

I wrote a code to find all URLs within a PDF file and replace the one(s) that matches the parameters that was passed from a PHP script.
It is working fine when a single URL is passed. But I don't know how to handle more than one URL, I'm guessing I would need a loop that reads the array length, and call the changeURL method passing the correct parameters.
I actually made it work with if Statements (if myarray.lenght < 4 do this, if it is < 6, do that, if < 8.....), but I am guessing this is not the optimal way. So I removed it and want to try something else.
Parameters passed from PHP (in this order):
args[0] - Location of original PDF
args[1] - Location of new PDF
args[2] - URL 1 (URL to be changed)
args[3] - URL 1a (URL that will replace URL 1)
args[4] - URL 2 (URL to be changed)
args[5] - URL 2a - (URL that will replace URL 2)
args...
and so on... up to maybe around 16 args, depending on how many URLs the PDF file contains.
Here's the code:
Main.java
public class Main {
public static void main(String[] args) {
if (args.length >= 4) {
URLReplacer.changeURL(args);
} else {
System.out.println("PARAMETER MISSING FROM PHP");
}
}
}
URLReplacer.java
public class URLReplacer {
public static void changeURL(String... a) {
try (PDDocument doc = PDDocument.load(a[0])) {
List<?> allPages = doc.getDocumentCatalog().getAllPages();
for (int i = 0; i < allPages.size(); i++) {
PDPage page = (PDPage) allPages.get(i);
List annotations = page.getAnnotations();
for (int j = 0; j < annotations.size(); j++) {
PDAnnotation annot = (PDAnnotation) annotations.get(j);
if (annot instanceof PDAnnotationLink) {
PDAnnotationLink link = (PDAnnotationLink) annot;
PDAction action = link.getAction();
if (action instanceof PDActionURI) {
PDActionURI uri = (PDActionURI) action;
String oldURL = uri.getURI();
if (a[2].equals(oldURL)) {
//System.out.println("Page " + (i + 1) + ": Replacing " + oldURL + " with " + a[3]);
uri.setURI(a[3]);
}
}
}
}
}
doc.save(a[1]);
} catch (IOException | COSVisitorException e) {
e.printStackTrace();
}
}
}
I have tried all sort of loops, but with my limited Java skills, did not achieve any success.
Also, if you notice any dodgy code, kindly let me know so I can learn the best practices from more experienced programmers.
Your main problem - as I understand -, is the "variable number of variables". And you have to send from PHP to JAVA.
1 you can transmit one by one as your example
2 or, in a structure.
there are several structures.
JSON is rather simple at PHP: multiple examples here:
encode json using php?
and for java you have: Decoding JSON String in Java.
or others (like XML , which seems too complex for this).
I'd structure your method to accept specific parameters. I used map to accept URLs, a custom object would be another option.
Also notice the way loops are changed, might give you a hint on some Java skills.
public static void changeURL(String originalPdf, String targetPdf, Map<String, String> urls ) {
try (PDDocument doc = PDDocument.load(originalPdf)) {
List<PDPage> allPages = doc.getDocumentCatalog().getAllPages();
for(PDPage page: allPages){
List annotations = page.getAnnotations();
for(PDAnnotation annot : page.getAnnotations()){
if (annot instanceof PDAnnotationLink) {
PDAnnotationLink link = (PDAnnotationLink) annot;
PDAction action = link.getAction();
if (action instanceof PDActionURI) {
PDActionURI uri = (PDActionURI) action;
String oldURL = uri.getURI();
for (Map.Entry<String, String> url : urls.entrySet()){
if (url.getKey().equals(oldURL)) {
uri.setURI(url.getValue());
}
}
}
}
}
}
doc.save(targetPdf);
} catch (IOException | COSVisitorException e) {
e.printStackTrace();
}
}
If you have to get the URL and PDF locations from command line, then call the changeURL function like this:
public static void main(String[] args) {
if (args.length >= 4) {
String originalPdf = args[0];
String targetPdf = args[1];
Map<String, String> urls = new HashMap<String, String>();
for(int i = 2; i< args.length; i+=2){
urls.put(args[i], args[i+1]);
}
URLReplacer.changeURL(originalPdf, targetPdf, urls);
} else {
System.out.println("PARAMETER MISSING FROM PHP");
}
}
Of the top of my head, you could do something like this
public static void main(String[] args) {
if (args.length >= 4 && args.length % 2 == 0) {
for(int i = 2; i < args.length; i += 2) {
URLReplacer.changeURL(args[0], args[1], args[i], args[i+1]);
args[0] = args[1];
}
} else {
System.out.println("PARAMETER MISSING FROM PHP");
}
}

URI - getHost returns null. Why?

Why is the 1st one returning null, while the 2nd one is returning mail.yahoo.com?
Isn't this weird? If not, what's the logic behind this behavior?
Is the underscore the culprit? Why?
public static void main(String[] args) throws Exception {
java.net.URI uri = new java.net.URI("http://broken_arrow.huntingtonhelps.com");
String host = uri.getHost();
System.out.println("Host = [" + host + "].");
uri = new java.net.URI("http://mail.yahoo.com");
host = uri.getHost();
System.out.println("Host = [" + host + "].");
}
As mentioned in the comments by #hsz it is a known bug.
But, let's debug and look inside the sources of URI class. The problem is inside the method:
private int parseHostname(int start, int n):
parsing first URI fails at lines if ((p < n) && !at(p, n, ':')) fail("Illegal character in hostname", p);
this is because _ symbol isn't foreseen inside the scan block, so it allows only alphas, digits and -symbol (L_ALPHANUM, H_ALPHANUM, L_DASH and H_DASH).
And yes, this is not yet fixed in Java 7.
It's because of underscore in base uri.
Just Remove underscore to check that out.It's working.
Like given below :
public static void main(String[] args) throws Exception {
java.net.URI uri = new java.net.URI("http://brokenarrow.huntingtonhelps.com");
String host = uri.getHost();
System.out.println("Host = [" + host + "].");
uri = new java.net.URI("http://mail.yahoo.com");
host = uri.getHost();
System.out.println("Host = [" + host + "].");
}
I don't think it's a bug in Java, I think Java is parsing hostnames correctly according to the spec, there are good explanations of the spec here: http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names and here: http://www.netregister.biz/faqit.htm#1
Specifically hostnames MUST NOT contain underscores.
Consider using: new java.net.URL("http://broken_arrow.huntingtonhelps.com").getHost() instead. It has alternative parsing implementation. If you have an URI myUri instance, then call myUri.toURL().getHost().
I faced this URI issue in OpenJDK 1.8 and it worked fine with URL.
As mentioned, it is a known JVM bug.
Although, if you want to do an HTTP request to such a host, you still can try to use a workaround.
The main idea is to construct request basing on the IP, not on the 'wrong' hostname. But in that case you also need to add "Host" header to the request, with the correct (original) hostname.
1: Cut hostname from the URL (it's a rough example, you can use some more smart way):
int n = url.indexOf("://");
if (n > 0) { n += 3; } else { n = 0; }
int m = url.indexOf(":", n);
int k = url.indexOf("/", n);
if (-1 == m) { m = k; }
String hostHeader;
if (k > -1) {
hostHeader = url.substring(n, k);
} else {
hostHeader = url.substring(n);
}
String hostname;
if (m > -1) {
hostname = url.substring(n, m);
} else {
hostname = url.substring(n);
}
2: Get hostname's IP:
String IP = InetAddress.getByName(hostname).getHostAddress();
3: Construct new URL basing on the IP:
String newURL = url.substring(0, n) + IP + url.substring(m);
4: Now use an HTTP library for preparing request on the new URL (pseudocode):
HttpRequest req = ApacheHTTP.get(newUrl);
5: And now you should add "Host" header with the correct (original) hostname:
req.addHeader("Host", hostHeader);
6: Now you can do the request (pseudocode):
String resp = req.getResponse().asString();

Problems with the import statement

I've created the following java file:
import java.awt.*;
public class Text {
public static void main(String[] args) {
String str = "I AM A SENTENCE";
String[] lines = wrap(str, 5);
for (int i=0;i<lines.length;i++) {
if (lines[i] != null) System.out.println(lines[i]);
}
Font myFont = new Font("Impact", Font.BOLD, 36);
System.out.println(String.valueOf(charToPixel(str, myFont)));
}
public static String[] wrap(String str, int w) {
char[] string = str.toCharArray();
System.out.println("string.length: " + String.valueOf(string.length));
int charCounter = 0;
String[] line = new String[20];
String work = "";
int x = 0;
for (int i=0;i<string.length;i++) {
charCounter++;
System.out.println("charCounter: " + String.valueOf(charCounter));
System.out.println("i: " + string[i]);
if (charCounter > w) {
charCounter = 0;
System.out.println(String.valueOf(x));
line[x] = work;
x++;
work = "";
i--;
}
else {
work += string[i];
}
}
line[x] = work;
return line;
}
}
Now, I also created a simple applet that I want to use to receive the String[] and then one by one output it using Graphics.drawString().
I created a .jar file using the default manifest and the previous class file. The class file's directory is as follows within the jar: Dennis\Text.class.
I added my jar into the CLASSPATH.
I used the import statement as follows: import Dennis.*;
However when I compile the applet (btw the Text.class had compiled perfectly)
I get the following compilation error:
bad class file: B:\Apps\Java\JDK\lib\Text.jar(Dennis/Text.class)
class file contains wrong class: Text
Please remove or make sure it appears in the correct subdirectory of the classpath.
As far as I can tell, I put everything in the right place and the import statement was successful.
So what am I doing wrong?
The class file's directory is as follows within the jar: Dennis\Text.class.
It shouldn't be. It's not in any package, so it should just be directly within the root directory of the jar file. Ideally put it within a package (not Dennis, which violates Java naming conventions) and then make your jar file structure match the package structure.

Java Generate software configuration

I am working on a project which has some .properties configuration files for datasource, MQ and some other stuff. We do also have launch shell scripts and user profile scripts. The problem i am facing is that we do actually deploy this software on 5 different environments, and of course the configuration is different for each of them. It's being a little bit hard to maintain about 30 plain text files with the configuration. Most of them are pretty much equal, like shell scripts that only have some different path references on.
Do you guys know any kind of tool i could integrate on our build script that might grab these properties from a single file or an embedded database and then generate the proper environment configuration? If it could also generate the scripts it would be even more interesting.
Thanks
Maven provides this out-of-the-box: http://maven.apache.org/guides/mini/guide-building-for-different-environments.html.
I am the maintainer of Config4*, which is a configuration-file parser library in C++ and Java flavours. Most of the contents in a Config4* configuration file are name=value statements, but you can reference environment variables and the standard output of executing some commands like hostname. You can also have if-then-else statements in a configuration file. For example (keywords are prefixed with "#"):
#if (exec("hostname") #in ["host1", "host2", "host3"]) {
... # set variables to values for production environment
} #elseIf (exec("hostname") #in ["host4", "host5", "host6"]) {
... # set variables to values for staging environment
} #else {
#error "Unknown host";
}
I call this adaptable configuration because a single configuration file can adapt its contents for a variety of hosts, user names, and so on. Config4* provides a trivial way to integrate command-line options with a configuration file, so it is possible to have a configuration file that adapts its contents based on the presence of a command-line option such as -env production or -env staging. For example:
env ?= ""; # set by a command-line option
if (env == "production") {
... # set variables to values for production environment
} #elseIf (env == "staging") {
... # set variables to values for staging environment
} #else {
#error "You must specify '-env production' or '-env staging' as a command-line option";
}
I can think of two possible ways that Config4* might be of help to you.
One option is for you to embed the Config4* parser in your applications. However, although I think that is a good approach when developing new applications, I think might be tedious to retrofit Config4* to an existing application (not because the Config4* is difficult to use, but just because you will be modifying existing code that uses, say, the Java properties API or an XML API to use a different API, and such modifications tend to be tedious).
The second option better fits with the specifics of your question. You write template versions of your shell scripts and property files. These template files will use a particular syntax, such as '${variable.name}' to specify where values from a configuration file should be used. You then write a small utility application that reads a template file and a configuration file, performs the required substitutions, and then writes the transformed file to disk. You could run that utility application from your build system.
You could have a look at newly announced tools4j-config which lets you handle configuration at runtime rather than build time.
In a previous answer, I outlined how Config4* could satisfy your needs. I decided to eat my own dog food, so I knocked up a ready-to-compile-and-run Config4*-based application that will do what you want. I am providing the code inline in this answer. Rather than reading the code via the StackOverview webpage, you might find it easier to copy-and-paste the code into files so you can view it with a text editor.
First, we need a configuration file that defines three variables:
deploymentType (specified as a command-line argument to have the value dev, staging or prod);
files (pairs of template files and output files);
searchAndReplace (pairs of search and replace strings to be applied to the template files to produce the output files). The pairs of strings used depend on the value of deploymentType.
Here is an example of such a file (copy-and-paste this into templates.cfg):
deploymentType ?= ""; # specified with a command-line argument
files = [
# template file output file
# ----------------------------------------------------
"log4j-template.properties", "log4j.properties",
"hello-template.sh", "hello.sh",
];
#if (deploymentType == "dev") {
searchAndReplace = [
"${db.host}", "localhost",
"${db.user}", "guest",
"${db.log.level}", "2",
];
} #elseIf (deploymentType == "staging") {
searchAndReplace = [
"${db.host}", exec("hostname"),
"${db.user}", getenv("USERNAME"),
"${db.log.level}", "0",
];
} #elseIf (deploymentType == "prod") {
searchAndReplace = [
"${db.host}", "production.example.com",
"${db.user}", getenv("USERNAME"),
"${db.log.level}", "0",
];
} #else {
#error "deploymentType must be 'dev', 'staging' or 'prod'";
}
Here is the main-line of the application. You should cut-n-paste the following into InstantiateTemplateFiles.java:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.config4j.Configuration;
import org.config4j.SchemaValidator;
import org.config4j.ConfigurationException;
public class InstantiateTemplateFiles
{
public static void main(String[] args)
{
Configuration cfg = Configuration.create();
SchemaValidator sv = new SchemaValidator();
String[] searchAndReplace;
String[] files;
String contents;
String modifiedContents;
String templateFile;
String outputFile;
int i;
String[] schema = new String[] {
"deploymentType = string",
"searchAndReplace=table[string,search, string,replace]",
"files=table[string,template-file, string,output-file]",
};
if (args.length != 2) {
System.err.println("\nusage: java InstantiateTemplateFiles"
+ " meta-config-file.cfg deploymentType\n");
System.exit(1);
}
try {
//--------
// Parse the configuration file, perform schema validation
// and retrieve the required configuration variables.
//--------
cfg.insertString("", "deploymentType", args[1]);
cfg.parse(args[0]);
sv.parseSchema(schema);
sv.validate(cfg, "", "");
searchAndReplace = cfg.lookupList("", "searchAndReplace");
files = cfg.lookupList("", "files");
//--------
// Do the real work
//--------
for (i = 0; i < files.length; i += 2) {
Util.searchAndReplaceInFile(files[i + 0], files[i + 1],
searchAndReplace);
}
} catch(IOException ex) {
System.err.println("\n" + ex.getMessage() + "\n");
System.exit(1);
} catch(ConfigurationException ex) {
System.err.println("\n" + ex.getMessage() + "\n");
System.exit(1);
}
}
}
Finally, here is the code to perform the search-and-replace on files. This code is independent of Config4*, so you might find it useful even if you decide to build a non-Config4*-based utility. You should cut-n-paste this code into Util.java:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Util
{
public static void searchAndReplaceInFile(
String inputFile,
String outputFile,
String[] searchAndReplacePairs) throws IOException
{
String contents;
String modifiedContents;
contents = Util.readTextFile(inputFile);
modifiedContents = Util.replace(contents, searchAndReplacePairs);
Util.writeTextFile(outputFile, modifiedContents);
}
public static String readTextFile(String fileName) throws IOException
{
BufferedReader in;
StringBuffer result;
String line;
result = new StringBuffer();
in = new BufferedReader(new FileReader(fileName));
while ((line = in.readLine()) != null) {
result.append(line).append("\n");
}
in.close();
return result.toString();
}
public static void writeTextFile(String fileName, String contents)
throws IOException
{
PrintWriter out;
StringBuffer result;
String line;
out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
out.print(contents);
out.close();
}
public static String replace(
String origStr,
String searchStr,
String replacementStr)
{
StringBuffer result;
int origStrLen;
int searchStrLen;
int currStart;
int pIndex;
result = new StringBuffer();
origStrLen = origStr.length();
searchStrLen = searchStr.length();
currStart = 0;
pIndex = origStr.indexOf(searchStr, currStart);
while (pIndex != -1) {
result.append(origStr.substring(currStart, pIndex));
result.append(replacementStr);
currStart = pIndex + searchStrLen;
pIndex = origStr.indexOf(searchStr, currStart);
}
result.append(origStr.substring(currStart));
return result.toString();
}
public static String replace(
String origStr,
String[] searchAndReplacePairs)
{
int i;
int currIndex;
String subStr;
String replaceStr;
StringBuffer result;
SearchAndReplacePair[] pairs;
SearchAndReplacePair nextPair;
pairs = new SearchAndReplacePair[searchAndReplacePairs.length / 2];
for (i = 0; i < searchAndReplacePairs.length; i += 2) {
pairs[i/2] = new SearchAndReplacePair(origStr,
searchAndReplacePairs[i + 0],
searchAndReplacePairs[i + 1]);
}
result = new StringBuffer();
currIndex = 0;
nextPair = findNextPair(origStr, currIndex, pairs);
while (nextPair != null) {
subStr = origStr.substring(currIndex, nextPair.indexOf);
result.append(subStr);
result.append(nextPair.replace);
currIndex = nextPair.indexOf + nextPair.length;
for (i = 0; i < pairs.length; i++) {
pairs[i].findNext(currIndex);
}
nextPair = findNextPair(origStr, currIndex, pairs);
}
subStr = origStr.substring(currIndex);
result.append(subStr);
return result.toString();
}
private static SearchAndReplacePair findNextPair(
String origStr,
int currIndex,
SearchAndReplacePair[] pairs)
{
int i;
SearchAndReplacePair bestSoFar;
SearchAndReplacePair item;
bestSoFar = null;
for (i = 0; i < pairs.length; i++) {
item = pairs[i];
if (item.indexOf == -1) {
continue;
}
if (bestSoFar == null) {
bestSoFar = item;
continue;
}
if (bestSoFar.indexOf < item.indexOf) {
continue;
}
if (bestSoFar.indexOf > item.indexOf) {
bestSoFar = item;
continue;
}
if (bestSoFar.length < item.length) {
bestSoFar = item;
}
}
return bestSoFar;
}
}
class SearchAndReplacePair
{
String source;
String search;
String replace;
int length;
int indexOf;
int sourceLength;
public SearchAndReplacePair(String source, String search, String replace)
{
this.source = source;
this.sourceLength = source.length();
this.search = search;
this.replace = replace;
this.length = search.length();
this.indexOf = source.indexOf(search);
}
public void findNext(int fromIndex)
{
if (indexOf == -1 || indexOf + 1 == sourceLength) {
indexOf = -1;
} else {
indexOf = source.indexOf(search, fromIndex);
}
}
}
Assuming you have downloaded and installed Config4J (from the Config4* website), you can compile the utility with the following:
CLASSPATH=.:/path/to/config4j.jar;
export CLASSPATH
javac -classpath .:/ag/projects/config4j/lib/config4j.jar *.java
Here is an example of running it:
java InstantiateTemplateFiles templates.cfg prod
If the file hello-template.sh looks like:
#!/bin/sh
DB_HOST=${db.host}
DB_USER=${db.user}
DB_LOG_LEVEL=${db.log.level}
echo Hello from $DB_USER at log level $DB_LOG_LEVEL on host $DB_HOST
then the generated hello.sh file will look like:
#!/bin/sh
DB_HOST=production.example.com
DB_USER=cjmchale
DB_LOG_LEVEL=0
echo Hello from $DB_USER at log level $DB_LOG_LEVEL on host $DB_HOST

Categories

Resources