Modifying xml using Java cli - java

Modifying xml using Java cli. Values in the xml were blank and we pass them from the java code. The file is on a Linux system.The method i am using is this
public String modifySufPlaylist(Cli cli, String file, String[] parms)
throws RemoteCliException {
String parmlist = "";
for (String s : parms) {
parmlist += " \"" + s + "\"";
}
String cmd = "for i in " + parmlist + "; do echo -e \"/<value><\\/value>/\\ns/></>$i</\\nw\\nq\\n\" | ed "
+ file + "; done >/dev/null 2>&1";
return cli.send(cmd);
}
It works fine when My xml has
<value></value>
Now a few things were changed and the xml looks like this
<value>Enter Param 1</value>
<value>Enter IP</value>
<value>Enter password</value>
i am stuck on how to modify my script so that is replaces the default value with paramlist values.

Y dont you use xsh:
for my $file in { glob "*.xml" } {
open $file ;
for //SomeTag set #another 'new value' ;
save :b ;
}

Related

Including function code snippet form a groovy file in JMeter groovy script

I have a big Groovy script in JMeter and I want few methods to be re-used in different places of my script. Below is what I tried.
This is a groovy script where I have written a function that I want to call from Jmeter.
Tools.groovy
public void AssertValuesF(float Expected, float Actual, String PassMessage, String FailureMessage){
if(Expected==Actual){
log.info("****Assertion Successful****");
log.info("Actual: "+Actual+" Expected: "+Expected +"\n");
log.info(PassMessage);
}
else{
vars.put("AssertionFailure","true");
AssertionResult.setFailure(true);
vars.put("FailureMsg",vars.get("FailureMsg") + "\n****ASSERTION FAILURE****** \n"+FailureMessage + " || EXPECTED: "+ Expected + " || ACTUAL: "+Actual + "\n");
log.info("****ASSERTION FAILURE******");
// AssertionResult.setFailureMessage("****Assertion Failure****** "+FailureMessage + " Expected: "+ Expected + " Actual: "+Actual+"\n");
log.info(FailureMessage);
log.info("Actual: "+Actual+"Expected: "+Expected);
}
}
Below is my JMeter Groovy code where I am calling the function.
File sourceFile = new File("D://TestScript//Tools.groovy");
Class groovyClass = new GroovyClassLoader(getClass().getClassLoader()).parseClass(sourceFile);
GroovyObject myObject = (GroovyObject) groovyClass.newInstance();
myObject.AssertValues("s","s","asdf","asdf");
The output gives this error, javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: log for class: Tools
This is probably because of 'log' object not available from Groovy. How can I solve this issue?
log shorthand is available only for JSR223 Elements, in order to be able to use it you need to define it manually like it's done in JSR223TestElement class
So amend your code to look like:
import org.slf4j.Logger
import org.slf4j.LoggerFactory
public void AssertValuesF(float Expected, float Actual, String PassMessage, String FailureMessage) {
final Logger log = LoggerFactory.getLogger(getClass());
if (Expected == Actual) {
log.info("****Assertion Successful****");
log.info("Actual: " + Actual + " Expected: " + Expected + "\n");
log.info(PassMessage);
} else {
vars.put("AssertionFailure", "true");
AssertionResult.setFailure(true);
vars.put("FailureMsg", vars.get("FailureMsg") + "\n****ASSERTION FAILURE****** \n" + FailureMessage + " || EXPECTED: " + Expected + " || ACTUAL: " + Actual + "\n");
log.info("****ASSERTION FAILURE******");
// AssertionResult.setFailureMessage("****Assertion Failure****** "+FailureMessage + " Expected: "+ Expected + " Actual: "+Actual+"\n");
log.info(FailureMessage);
log.info("Actual: " + Actual + "Expected: " + Expected);
}
}
And you will be able to use it from Groovy scripts your way:
Also be aware that there is groovy.utilities property which can be used to re-use your custom scripts in __groovy() function, you will need either add the next line to user.properties file:
groovy.utilities=D:/TestScript/Tools.groovy
or pass it via -J command-line argument like:
jmeter -Jgroovy.utilities=D:/TestScript/Tools.groovy -n -t test.jmx -l result.jtl
References:
Configuring JMeter
Overriding Properties Via The Command Line
Apache JMeter Properties Customization Guide

Replace blank spaces from arguments to run command

I'm using Runtime.getRuntime.exec(String) to cut some songs with ffmpeg.
But when my song has a name with a blankspace it doesn't work ...
So before I cut the song, I want to replace every blank space of my songs by "\ ".
I did that :
String in = directory+songs.get(i);
String out = directory+"trimed_"+songs.get(i);
in.replaceAll(" "," \\ ");
out.replaceAll(" ", "\\ ");
String str = "ffmpeg -t 1 -i "+in+" -vcodec copy "+out;
Runtime.getRuntime().exec(str);
But it doesn't replace anything at all when I print str, am I missing something ?
Update : I tried every ideas given bellow and I didn't find a way to fix the problem. Hence, I replaced the blankspaces by "_" and it's working great.
Try
String in = directory+songs.get(i);
String out = directory+"trimed_"+songs.get(i);
/* in = in.replaceAll("\\s","\\\\ ");
out = out.replaceAll("\\s","\\\\ ");
*/
in = "\"" + in + "\"";
out = "\"" + out + "\"";
String str = "ffmpeg -t 1 -i " + in + " -vcodec copy " + out;
Runtime.getRuntime().exec(str);
System.out.println("Command executed " + str);
Note: I tested this code myself its working fine.
If it still not working then execute the command manually by copying the str from log and trace the error

"missing ) after argument list" [duplicate]

I want to initialize a String in Java, but that string needs to include quotes; for example: "ROM". I tried doing:
String value = " "ROM" ";
but that doesn't work. How can I include "s within a string?
In Java, you can escape quotes with \:
String value = " \"ROM\" ";
In reference to your comment after Ian Henry's answer, I'm not quite 100% sure I understand what you are asking.
If it is about getting double quote marks added into a string, you can concatenate the double quotes into your string, for example:
String theFirst = "Java Programming";
String ROM = "\"" + theFirst + "\"";
Or, if you want to do it with one String variable, it would be:
String ROM = "Java Programming";
ROM = "\"" + ROM + "\"";
Of course, this actually replaces the original ROM, since Java Strings are immutable.
If you are wanting to do something like turn the variable name into a String, you can't do that in Java, AFAIK.
Not sure what language you're using (you didn't specify), but you should be able to "escape" the quotation mark character with a backslash: "\"ROM\""
\ = \\
" = \"
new line = \r\n OR \n\r OR \n (depends on OS) bun usualy \n enough.
taabulator = \t
Just escape the quotes:
String value = "\"ROM\"";
In Java, you can use char value with ":
char quotes ='"';
String strVar=quotes+"ROM"+quotes;
Here is full java example:-
public class QuoteInJava {
public static void main (String args[])
{
System.out.println ("If you need to 'quote' in Java");
System.out.println ("you can use single \' or double \" quote");
}
}
Here is Out PUT:-
If you need to 'quote' in Java
you can use single ' or double " quote
Look into this one ... call from anywhere you want.
public String setdoubleQuote(String myText) {
String quoteText = "";
if (!myText.isEmpty()) {
quoteText = "\"" + myText + "\"";
}
return quoteText;
}
apply double quotes to non empty dynamic string. Hope this is helpful.
This tiny java method will help you produce standard CSV text of a specific column.
public static String getStandardizedCsv(String columnText){
//contains line feed ?
boolean containsLineFeed = false;
if(columnText.contains("\n")){
containsLineFeed = true;
}
boolean containsCommas = false;
if(columnText.contains(",")){
containsCommas = true;
}
boolean containsDoubleQuotes = false;
if(columnText.contains("\"")){
containsDoubleQuotes = true;
}
columnText.replaceAll("\"", "\"\"");
if(containsLineFeed || containsCommas || containsDoubleQuotes){
columnText = "\"" + columnText + "\"";
}
return columnText;
}
suppose ROM is string variable which equals "strval"
you can simply do
String value= " \" "+ROM+" \" ";
it will be stored as
value= " "strval" ";

envirnoment variable set on terminal but System.getenv returns null. java problems

I've changed my ~/.bashrc file. I've changed /etc/environment. I've done export WNHOME="/usr/local/WordNet-3.0". I tried everything here and more. (I'm running arch linux, in case that's of any consequence).
I think the environment variable must be set on my machine, if I check it with echo $WNHOME I get the correct result.
However when I call System.out.println(System.getenv("WNHOME")); in my java program I keep getting null, what could be the reason for this?
The output looks like this:
Path is 'null/dict'
null
Exception in thread "main" java.io.IOException: Dictionary directory does not exist: null/dict
at edu.mit.jwi.data.FileProvider.open(FileProvider.java:306)
at edu.mit.jwi.DataSourceDictionary.open(DataSourceDictionary.java:92)
at edu.mit.jwi.CachingDictionary.open(CachingDictionary.java:133)
at MITJavaWordNetInterface.main(MITJavaWordNetInterface.java:30)
The code looks like this:
public static void main(String[] args) throws IOException
{
// construct the URL to the Wordnet dictionary directory
String wnhome = System.getenv("WNHOME");
String path = wnhome + File.separator + "dict";
System.out.println("Path is '" + path + "'");
URL url = new URL ("file", null , path );
System.out.println(System.getenv("WNHOME"));
//final URL url = Paths.get(wnhome, "dict").toUri().toURL();
// construct the dictionary object and open it
IDictionary dict = new Dictionary ( url ) ;
dict . open () ;
// look up first sense of the word "dog "
IIndexWord idxWord = dict . getIndexWord ("dog", POS . NOUN ) ;
IWordID wordID = idxWord . getWordIDs () . get (0) ;
IWord word = dict . getWord ( wordID ) ;
System . out . println ("Id = " + wordID ) ;
System . out . println (" Lemma = " + word . getLemma () ) ;
System . out . println (" Gloss = " + word . getSynset () . getGloss () ) ;
}
Set the environment variable in ~/.profile file
If we set the environment variable in ~/.bashrc then those variable are accessable only to the application started from shell. For desktop application to access the environment variable set it into ~/.profile file.

Read JSCON file with PHP

I'm trying to read a JSON with a php array. The JSON file is using a format that I am not familiar with and I don't know how to write my PHP array so it could read the file.
JSON file in question can be found here:
http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/ANA/iphone/clubroster.json
The format is giving me hard time because 1) it is starting with a time stamp that my array cannot read and 2) the file is separated between the value position, means that I have a closing statement ' }] ' before the end of the file - seems like they separated category
My PHP array that work with more standard array :
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>";
for(i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].position +
"</td><td>" +
arr[i].weight +
"</td><td>" +
arr[i].height +
"</td></tr>";
}
out += "</table>"
document.getElementById("id01").innerHTML = out;
}
thank you
It is called JSON, not JSCON. The code you posted is JavaScript, not PHP.
Here is an example of how you could read the JSON you provided with PHP:
<?php
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/ANA/iphone/clubroster.json");
$json = json_decode($result);
foreach ($json->goalie as $player) {
echo $player->name . '</br>';
}
?>
This will print each goalie's name.
it looks like you need JavaScript(not php) to read this json file?
in JavaScript try
arr = val('(' + response + ')')
and in php try
arr = json_decode(response)

Categories

Resources