So the case is ultra simple in theory - harder to get it working in real life. I have a property:
my.loggger.test.property=file://some/path/to/log.log
I define it with such pattern in logback.xml and everything is working as expected:
<springProperty name="filePath" source="my.loggger.test.property"/>
Case is that I want to (without editing application.properties) substring / replace some characters on it so I would love to have something like:
filePath = filePath.subString(5)
or
filePath = filePath.replace("file:/", "")
Is this even possible ? I cannot find any information about string manipulation on logback.xml.
Related
I'm redoing the backend of a very basic framework that connects to a completely customizable frontend. It was originally in PHP but for the refactor have been plodding away in F#. Although it seems like PHP might be the more suited language. But people keep telling me you can do everything in F# and I like the syntax and need to learn and this seemingly simple project has me stumped when it comes to JSON. This is a further fleshed out version of my question yesterday, but it got alot more complex than I thought.
Here goes.
The frontend is basically a collection of HTML files, which are simply loaded in PHP and preg_replace() is used to replace things like [var: varName] or [var: array|key] or the troublesome one: [lang: hello]. That needs to be replaced by a variable defined in a translation dictionary, which is stored as JSON which is also editable by a non-programmer.
I can't change the frontend or the JSON files, and both are designed to be edited by non-programmers so it is very likely that there will be errors, calls to language variables that don't exist etc.
So we might have 2 json files, english.json and french.json
english.json contains:
{
"hello":"Hello",
"bye":"Goodbye"
}
french.json:
{
"hello": "Bonjour",
"duck": "Canard"
//Plus users can add whatever else they want here and expect to be able to use it in a template
}
There is a template that contains
<b>[lang: hello]</b>
<span>Favourite Animal: [lang:duck]</span>
In this case, if the language is set to "english" and english.json is being loaded, that should read:
<b>Hello</b>
<span>Favourite Animal: </span>
Or in French:
<b>Bonjour</b>
<span>Favourite Animal: Canard</span>
We can assume that the json format key: value is always string:string but ideally I'd like to handle string: 'T as well but that might be beyond the scope of this question.
So I need to convert a JSON file (called by dynamic name, which gave F# Data a bit of an issue I couldn't solve last night as it only allowed a static filename as a sample, and since these two files have potential to be different from sample and provided, the type provider doesn't work) to a dictionary or some other collection.
Now inside the template parsing function I need to replace [lang: hello] with something like
let key = "duck"
(*Magic function to convert JSON to usable collection*)
let languageString = convertedJSONCollection.[key] (*And obviously check if containsKey first*)
Which means I need to call the key dynamically, and I couldn't figure out how to do that with the type that FSharp.Data provided.
I have played around with some Thoth as well to some promising results that ended up going nowhere. I avoided JSON.NET because I thought it was paid, but just realised I am mistaken there so might be an avenue to explore
For comparison, the PHP function looks something like this:
function loadLanguage($lang='english){
$json = file_get_contents("$lang.json");
return json_decode($json, true);
}
$key = 'duck';
$langVars = loadLanguage();
$duck = $langVars[$key] || "";
Is there a clean way to do this in F#/.NET? JSON seems really painful to work with in comparison to PHP/Javascript and I'm starting to lose my mind. Am I going to have to write my own parser (which means probably going back to PHP)?
Cheers to all you F# geniuses who know the answer :p
open Thoth.Json.Net
let deserialiseDictionary (s: string) =
s
|> Decode.unsafeFromString (Decode.keyValuePairs Decode.string)
|> Map.ofList
let printDictionary json =
json
|> deserialiseDictionary
|> fun m -> printfn "%s" m.["hello"] // Hello
For the question about 'T the question becomes, what can 'T be? For json it very limited, it can be a number of things, string, json-object, number, bool or json array. What should happen if it is bool or a number?
def copy_file(from_file,to_file):
content = open(from_file).read()
target = open(to_file,'w').write(content)
print open(to_file).read()
def user_input(f1):
f1 = raw_input("Enter the source file : ")
user_input(f1)
user_input(f2)
copy_file(user_input(f1),user_input(f2))
What is the mistake in this ? I tried it with argv and it was working.
You're not calling the function user_input (by using ()). (fixed in question by OP).
Also, you need to return a string from user_input. currently you're trying to set a variable f1 which is local to the function user_input. While this is possible using global - I do not recommend this (this beats keeping your code DRY).
It's possible to do something similar with objects by changing their states. String is an object - but since strings are immutable, and you can't have the function change their state - this approach of expecting a function to change the string it's given is also doomed to fail.
def user_input():
return raw_input("Enter the source file :").strip()
copy_file(user_input(),user_input())
You can see user_input does very little, it's actually redundant if you assume user input is valid.
I am struggling to make this work and have trawled for examples on how to fix this to no avail. I am converting a mysql resultset into an xml file to upload to ebaymotorspro. I thought this would be relatively simple and yet I am struggling with the conventions set out by both ebay and the .net framework.
The opening element of the file has to read:
<empro xmlns="urn:de:mobile:emp:inventory:xml:uk:car" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:de:mobile:emp:inventory:xml:uk:car http://www.ebaymotorspro.co.uk/schema/empro-car-uk.xsd">
I am using the xmlwriter class to recreate this and have this so far:
Using writer as XmlWriter = XmlWriter.Create(feedfile, xmlsettings)
writer.WriteStartDocument(True)
writer.WriteStartElement("empro", "urn:de:mobile:inventory:xml:uk:car")
' This Bit is causing the issue
writer.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2001/XMLScema-instance")
End Using
I end up with the following code in the xml file:
<empro p1:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p1="xmlns" xmlns="urn:de:mobile:emp:inventory:xml:uk:car" />
Which isnt correct, can anyone please point me in the right direction to make this output the correct document header?
Many thanks
Graham
The part that causing you a trouble should've been written using WriteAttributeString() overload which accept four parameters :
' This Bit is causing the issue'
writer.WriteAttributeString("xmlns", "xsi", Nothing, "http://www.w3.org/2001/XMLScema-instance")
Anyway, I will personally do this using LINQ-to-XML. VB even has exclusive features which C# doesn't for constructing XML : XML literals with embedded expression support. For example :
Dim contactName As String = "Patrick Hines"
Dim contact As XElement =
<contact><%= contactName %></contact>
Console.WriteLine(contact.ToString())
'output :'
'<contact>Patrick Hines</contact>'
Source : MSDN: Creating XML in Visual Basic
My goal is to find a predefined string in an HTML source of a specific site that I have extracted using c++, but I'm getting some errors. Here is my source code so far:
So after I connect to the internet and the site and all I have this...
addr = InternetOpenUrl...
dmbp = char dmbp[5000]
dba = DWORD dba = 0
while (InternetReadFile(addr, dmbp, 80000, &dba) && dba)
{
string str2 = dmbp;
size_t sf1 = str2.find(string1);
if (sf1!=string::npos)
{printf("found");
// manipulate it...
}else{printf("not found");}
}
My problem is that it never actually confirms that it found the value that I need, it always says that the value is not found, but I even statically insert the page and look at myself and i can see the value that i need, it just doesnt show up. Does anyone with experience in html extraction with c++ know what I'm missing or how I can get this to work?
There is nothing wrong with the string search code as far as I can see, the problem is that we don't know exactly what you are searching for.
As pure HTML can be full of special characters (such as " or ", the string you might be looking for should deal with those characters. Also, strings can contain newlines and html tags (such as <b></b> within a single word), and they should be specified in the search string as string::find looks for an exact match (including any newline).
Also, I suggest debugging your code and see if the website's text/code is actually loaded into str2.
Looking at the information given that's currently the only issue I can think of why your code doesn't work.
I am writing an SQL statement class that will allow a user to create a properly formatted SQL statement string. This class will have a method -(id)initWithFormat:(NSString *)format, ... that needs to work just like the NSString variadic method. The one thing I want to change, however, is any NSString's passed as an argument must be automatically escaped by the initWithFormat.
For example, after initialising a statement like this (notice the string argument has a "'" that needs to be escaped):
MyStatement *statement = [[MyStatement alloc] initWithFormat:#"UPDATE myTable SET myField = %# WHERE myID = %lu", #"David's Room", 1234];
The resulting statement string should be:
#"UPDATE myTable SET myField = "David\'s Room WHERE myID = 1234"
Writing the function to escape a string is easy but I can't work out how to include this in the initWithFormat method. Can anyone tell me how to accomplish this? I have thought about emulating the NSString initWithFormat functionality by stepping through each character of the format string, finding any chars starting with % and somehow using a switch statement to append the correct type to a NSMutableString but this seems overly complicated (i.e. some format specifiers are more than 1 char e.g. Signed 16-bit integer %hi and the function should take into account positional specifiers such as %1$# etc).
All the variadic tutorials I have seen concentrate on nil-terminated lists and don't show how to emulate initWithFormat effectively (including the technical Q&A from Apple that is deceptively titled "How can I write a method that takes a variable number of arguments, like NSString's +stringWithFormat:?").
Thanks in advance.