Need api similar to XML_SetUserData(expat) in libxml2 - libxml2

I have used expat and want to convert my code to use libxml2 sax parser. I have 2 questions:
Q1) expat provides api XML_SetUserData(), i want similar api in libxml2.
Q2) Does libxml2 sax parser allow schema validation along with parsing the xml.
Thanks
SM

xmlSAXHandlerinstance.userdata = your_data;
should do the needful.
You will get this data in all your callbacks then.
You can also set user data when you instantiate your Parser context.
Eg:
xmlCreatePushParserCtxt(&xmlSAXHandlerinstance, your_data, NULL, 0,
NULL);

Related

Is there a way to import multiple enumerands in IBM Rhapsody?

I have an enumerand of around 150 entries, which I need to get into IBM Rhapsody.
Doing this by hand is clearly lengthy and error prone. I have google extensively but found only things that tell me how to edit the generated code -- not go the other way.
The question is: How is this done? And if there is no way -- please someone post that as an answer.
David,
I would jump into the Java API (plugin subsystem) and do it that way. If you haven't learned how to use the API, there is a bit of a learning curve. There are two ways to go about it: Implement a Java (or your favorite JVM language--I use Scala) app that realizes the Rhapsody Plugin framework and then you choose to package it up and deploy it so that it gets loaded when you load your model, or, if it is a one off job, do everything up to the point of packaging it up and then run it from within your IDE and you are done. If you are comfortable with Scala, I can post some code.
So what I did in the end was I edited the relevant .sbs file, used a small python program to generate the items I required, and then update the length of the array accordingly.
all_the_literals = ["enum_name = 0x4e", enum_name2 = 0xF2", ... ,]
for field1, waste, field1_value in map(lambda x: x.split(" "),
all_the_literals):
literal_string = f""" {{ IEnumerationLiteral
- _id = GUID {uuid.uuid4()};
- _name = \"{field1}\";
- codeUpdateCGTime = 5.16.2022::19:24:18;
- _modifiedTimeWeak = 5.16.2022::19:24:18;
- _value = \"{field1_value}\";
}}"""
print(literal_string)
Note the above "code" snippet purely prints the items, which you then copy-paste into the relevant field in the sbs file. YMMV -- this was the correct format for an enum in Rhapsody (and note how I fudged the update time, but it worked successfully, so you'll need to do the same if you use this answer).
Also note it's probably better to use bauhaus9's answer, but I definitely didn't have time for it.

How to read a cx_Oracle.OBJECT with type cx_Oracle.ObjectType SYS.XMLTYPE in robotframework

I connected to oracle database and fetched some of the columns from the table.
It gave one of the column as cx_Oracle.OBJECT with type <cx_Oracle.ObjectType SYS.XMLTYPE>.
I need to read the XML Data in this and validate.
I have idea that XML Library is there in robot framework, to parse the Object, it is not accepting cx_Oracle.OBJECT, how to read cx_Oracle.OBJECT of type <cx_Oracle.ObjectType SYS.XMLTYPE>.
My Robot Framework Testcase is as below:
Query From CB_ADDRESS_XML
Connect To Database Using Custom Params cx_Oracle '${Username}/${Password}#${Host}:${Port}/${DatabaseName}'
${result}= Query select address_x from cb_address_xml where ACCOUNT_LINK_CODE_N='21818'
Log ${result[0][0]}
Disconnect From Database
Output came as :
INFO : ${result} = [(<cx_Oracle.OBJECT object at 0x032AE700>,), (<cx_Oracle.OBJECT object at 0x032AE720>,), (<cx_Oracle.OBJECT object at 0x032AE740>,)]
INFO : <cx_Oracle.OBJECT object at 0x032AE700>
<cx_Oracle.OBJECT object at 0x032AE700> is a xml object, i want to read this and validate the values in xml.
Thanks
Sarada
This could be implemented as a python keyword and used in Robot framework.
When you implement execute query in python call as below:
cursor.execute("select XMLType.GetClobVal(address_x) from cb_address_xml where ACCOUNT_LINK_CODE_N='21818'")
Once you receive the result it will be of type cx_Oracle.LOB, so you can use the below to read its contents:
result[column_number].read()
This will return XML data which you are looking for.
NOTE: This should be done before you close the DB connection.
Good Luck!

Marklogic Rest API for directory-query

I have the following XQuery which I use to fetch documents for a directory.
xquery version "1.0-ml";
cts:search(fn:collection(), cts:directory-query("/Path/To/Docs/", "infinity"))
Now I need to translate this into a REST call but I can't seem to crack it following the documentation on this page.
https://docs.marklogic.com/REST/GET/v1/search
Update:
using the Jersey REST API, It tried this but got 406 Error
String query = "{\"queries\":[ {\"directory-query\":{\"uri\":[\"/Path/to/Docs/\"]},\"infinite\":true} ]}";
String encodedQuery = URLEncoder.encode(query, "UTF-8");
WebTarget target = searchWebTarget.queryParam("structuredQuery", encodedQuery);
final Response response = target.request().get();
Any ideas?
As David said, you don't need to use structured query for this purpose, but in case you have future need:
I believe your original issue was that this is not a well-formed structured query:
{\"queries\":[ {\"directory-query\":{\"uri\":[\"/Path/to/Docs/\"]},\"infinite\":true} ]}
You're missing the top level "query" property. You can find an example of a fully formed structured query that uses directory-query here:
http://docs.marklogic.com/guide/search-dev/structured-query#id_97452
Also, you're probably already aware, but there is a native Java API that sits atop the REST API. You can learn more about this API here:
https://docs.marklogic.com/javadoc/client/index.html
http://docs.marklogic.com/guide/java
Constraining by directory is a query parameter directly on the search API. NO other notation needed.
See the docs here: https://docs.marklogic.com/REST/GET/v1/search

Get AST from .Net assembly without source code (IL code)

I'd like to analyze .Net assemblies to be language independent from C#, VB.NET or whatever.
I know Roslyn and NRefactory but they only seem to work on C# source code level?
There is also the "Common Compiler Infrastructure: Code Model and AST API" project on CodePlex which claims to "supports a hierarchical object model that represents code blocks in a language-independent structured form" which sound exactly for what I looking for.
However I'am unable to find any useful documentation or code that is actual doing this.
Any advice how to archive this?
Can Mono.Cecil maybe doing something?
You can do this and there is also one (although tiny) example of this in the source of ILSpy.
var assembly = AssemblyDefinition.ReadAssembly("path/to/assembly.dll");
var astBuilder = new AstBuilder(new DecompilerContext(assembly.MainModule));
decompiler.AddAssembly(assembly);
astBuilder.SyntaxTree...
The CCI Code Model is somewhere between a IL disassembler and full C# decompiler: it gives your code some structure (e.g. if statements and expressions), but it also contains some low level stack operations like push and pop.
CCI contains a sample that shows this: PeToText.
For example, to get Code Model for the first method of the Program type (in the global namespace), you could use code like this:
string fileName = "whatever.exe";
using (var host = new PeReader.DefaultHost())
{
var module = (IModule)host.LoadUnitFrom(fileName);
var type = (ITypeDefinition)module.UnitNamespaceRoot.Members
.Single(m => m.Name.Value == "Program");
var method = (IMethodDefinition)type.Members.First();
var methodBody = new SourceMethodBody(method.Body, host, null, null);
}
To demonstrate, if you decompile the above code and show it using PeToText, you're going to get:
Microsoft.Cci.ITypeDefinition local_3;
Microsoft.Cci.ILToCodeModel.SourceMethodBody local_5;
string local_0 = "C:\\code\\tmp\\nuget tmp 2015\\bin\\Debug\\nuget tmp 2015.exe";
Microsoft.Cci.PeReader.DefaultHost local_1 = new Microsoft.Cci.PeReader.DefaultHost();
try
{
push (Microsoft.Cci.IModule)local_1.LoadUnitFrom(local_0).UnitNamespaceRoot.Members;
push Program.<>c.<>9__0_0;
if (dup == default(System.Func<Microsoft.Cci.INamespaceMember, bool>))
{
pop;
push Program.<>c.<>9.<Main0>b__0_0;
Program.<>c.<>9__0_0 = dup;
}
local_3 = (Microsoft.Cci.ITypeDefinition)System.Linq.Enumerable.Single<Microsoft.Cci.INamespaceMember>(pop, pop);
local_5 = new Microsoft.Cci.ILToCodeModel.SourceMethodBody((Microsoft.Cci.IMethodDefinition)System.Linq.Enumerable.First<Microsoft.Cci.ITypeDefinitionMember>(local_3.Members).Body, local_1, (Microsoft.Cci.ISourceLocationProvider)null, (Microsoft.Cci.ILocalScopeProvider)null, 0);
}
finally
{
if (local_1 != default(Microsoft.Cci.PeReader.DefaultHost))
{
local_1.Dispose();
}
}
Of note are all those push, pop and dup statements and the lambda caching condition.
As far as I know, it's not possible to build AST from binary (without sources) since AST itself generated by parser as part of compilation process from sources.
Mono.Cecil won't help because you can only modify opcodes/metadata with them, not analyze assembly.
But since it's .NET you can dump IL code from dll with help of ildasm. Then you can pass generated sources to any parser with CIL dictionary hooked up and get AST from parser. The problem is that as far as I know there is only one publically available CIL grammar for parser, so you don't really have a choice. And ECMA-355 is big enough so it's bad idea to write your own grammar.
So I can suggest you only one solution:
Pass assembly to ildasm.exe to get CIL.
Then pass CIL to ANTLR v3 parser with this CIL grammar wired up (note it's a little bit outdated - grammar created at 2004 and latest CIL specification is 2006, but CIL doesn't really change to much)
After that you can freely access AST generated by ANTLR
Note that you will need ANTLR v3 not v4, since grammar written for 3rd version, and it's hardly possible to port it to v4 without good knowledge of ANTLR syntax.
Also you can try to look into new Microsoft ryujit compiler sources at github (part of CoreCLR) - I don't sure that it's helps, but in theory it must contains CIL grammar and parser implementations since it works with CIL code. But it's written in CPP, have enormous code base and lacks of documentation since it's in active development stage, so it's may be easier to stuck with ANTLR.
If you treat the .net binary file as a stream of bytes, you ought to be able to "parse" it just fine.
You simply write a grammar whose tokens are essentially bytes. You can certainly build a classical lexer/parser with almost any set of lexer/parser tools by defining the lexer to read single bytes as tokens.
You can then build the AST using standard AST-building machinery for the parsing engine (on your own for YACC, automatically with ANTLR4).
What you will discover, of course, is that "parsing" isn't enough; you'll still need to build symbol tables, and carry out control and data flow analyses if you are going to do serious analysis of the corresponding code. See my essay on LifeAfterParsing.
You will also likely have to take into account "distinguished" functions that provide key runtime facilities to the particular programming languages that actually generated the CIL code. And these will make your analyzers language-dependent. Yes, you still get to share the part of the analysis that works on generic CIL.

Can't access PFObject as dictionary

I am starting to use Parse and in the documentation it has examples to use the PFObject as an NSDitionary like this:
// Create the post
PFObject *myPost = [PFObject objectWithClassName:#"Post"];
myPost[#"title"] = #"I'm Hungry";
but i am getting a compiler error:
"Expected method to write dictionary element not found on object of type "PFObject"
But if I access the myPost PFObject like this, it works:
[myPost setObject:#"I'm Hungry" forKey:#"title"];
What is the problem?
I though that PFObject could be accesses as a dictionary?
thanks
I would check that you are using the latest version of the Parse SDK. This is new functionality and my guess is you haven't downloaded the SDK since this feature was introduced.
when i try your code at my side it gives me success message. I think you are using old parse library.
Thanks.
Downloading latest XCode should help. Subscript syntax is available from Xcode 4.4+

Resources