I want to port this Configuration.class object (from http://pastebin.com/dZeV27XB) into python and it seems hard to port the org.apache.commons.configuration object
In the java class, there were multiple functions that returns a Configuration.getString or Configuration.getInt e.g.
public int getDumpEndDir()
{
return this.config.getInt("wiki.dump.endDir");
}
public String getDocDir()
{
return this.config.getString("wiki.dump.docDir");
}
Any clue to what such function return?
Is there a python library similar to org.apache.commons?
Especially if there is one with the org.apache.commons.configuration library.
Is there a reason why this is in Java but not python?
You can use python's inbuild ConfigParser module which is very similar to Java's properties file loader. But here, the configuration parameters will be split section-wise.
check this,
https://docs.python.org/2/library/configparser.html
If it is an inmemory config structure, then you can simply use a dictionary object...
Related
I am developing springboot with GraphQL. Since the data structure is already declared within Protobuf, I tried to use it. This is example of my code.
#Service
public class Query implements GraphQLQueryResolver {
public MyProto getMyProto() {
/**/
}
}
I want make code like upper structure. To to this, I divided job into 2 sections.
Since ".proto file" can be converted to java class, I will use this class as return type.
And The second section is a main matter.
Also Schema is required. At first, I tried to code schema with my hand. But, the real size of proto is about 1000 lines. So, I want to know Is there any way to convert ".proto file" to ".graphqls file".
There is a way. I am using the a protoc plugin for that purpose: go-proto-gql
It is fairly simple to be used, for example:
protoc --gql_out=paths=source_relative:. -I=. ./*.proto
Hope this is working for you as well.
My java project uses the Apache Thrift framework and it has a similar Thrift object structure as the following:
struct MyStruct {
1: required string something;
2: optional OptionEnum option;
}
enum OptionEnum {
VALUE_A = 0,
VALUE_B = 1
}
So when my project compiles it builds a Java class for this structure (ie: class MyStruct).
What I am trying to do is to serialize this into a string Json.
I've tried using TSerializer:
TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
return serializer.toString(instanceOfMyStruct);
This generates a json but loses the string name of the enum (it converts into a numeric value instead):
{
something: 'value',
option: 1
}
Is there a way to keep the enum name (I mean option being VALUE_B instead of 1 in the above example) ?
The issue here is that the conversion is baked into the code parts generated by the Thrift compiler. The protocol level classes only know about a few very basic data types - when the data reach that level it is already too late.
So, unless you want to fork and implement your own (incompatible) version of the code generator, I'm afraid there is no way.
PS: I might add that the main purpose driving the design is efficiency, not human readibility.
Motivation:
In our Android project we have many verifications like str != null && !str.isEmpty(), so I decided to refactor them to a helper method.
For a moment I use following class as a helper:
public class StringUtil {
public static boolean isNullOrEmpty(#Nullable String str) {
return str == null || str.isEmpty();
}
}
Problem:
We already have a string's helper class, written in Kotlin (say, String.kt).
So, this is not clear to have two helpers (one in Java and one in Kotlin).
What I tried:
Naive approach to copy-past isNullOrEmpty() inside String.kt do not successed, because $reciever is null, so it crashed.
Secondly, I tried to used Kotlin native isNullOrEmpty() from kotlin.text (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-null-or-empty.html):
public inline fun CharSequence?.isNullOrEmpty(): Boolean
but I cannot figure out how to call it from Java. This page (https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html) do not provide any suggestions.
The problem is not about Accessing Kotlin extension functions from Java. My extension is perfectly visibly, but it crash because of null-receivier. As I mentioned below, question is more about accessing native library code, not my own extension.
Any help please ?
Some standard library functions, including this one, are marked with the #InlineOnly annotation, which makes them inaccessible from Java. For most other functions, you can access them exactly as described in the question linked as a duplicate.
After suggestion from #yole, I finally manage to convert code correctly to extension:
fun String?.isNullOrEmpty(): Boolean = (this == null || this.isEmpty())
So, problem was in defining extension as String, not as String? which leads to crash on $receiver. (Kotlin generates hidden not-null verifications when translating to Java, see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#null-safety)
Alternatively, standard library functions marked with #InlineOnly can be accessed from Java via custom extension.
fun String?.isVoid(): Boolean = this.isNullOrEmpty()
My problem here was in misunderstanding of ?. I tried this first:
fun String?.isVoid(): Boolean? = this?.isNullOrEmpty()
As #Moria mentioned, please note behaviour of ? - b?.method will return b.method if b is not null, and null otherwise (https://kotlinlang.org/docs/reference/null-safety.html#safe-calls)
Intro:
I'm asking this before I try, fail and get frustrated as I have 0 experience with Apache Ant. A simple 'yes this will work' may suffice, or if it won't please tell me what will.
Situation:
I'm working on a project that uses JavaFX to create a GUI. JavaFX relies on Java Bean-like objects that require a lot of boilerplate code for it's properties. For example, all functionality I want to have is a String called name with default value "Unnamed", or in a minimal Java syntax:
String name = "Unnamed";
In JavaFX the minimum amount of code increases a lot to give the same functionality (where functionality in this case means to me that I can set and get a certain variable to use in my program):
private StringProperty name = new StringProperty("Unnamed");
public final String getName() { return name.get(); }
public final void setName(String value) { name.set(value); }
Question: Can I use Ant to generate this boilerplate code?
It seems possible to make Ant scripts that function as (Java) preprocessors. For instance by using the regex replace (https://ant.apache.org/manual/Tasks/replaceregexp.html) functions. I'm thinking of lines of code similar to this in my code, which then will be auto-replaced:
<TagToSignifyReplaceableLine> StringProperty person "Unnamed"
Final remark: As I've said before I have never used Ant before, so I want to check with you if 1) this can be done and 2) if this is a good way to do it or if there are better ways.
Thanks!
Yes, possible. You can even implement your own Ant task, that does this job very easily.
Something like so in ant:
<taskdef name="codegen" classpath="bin/" classname="com.example.CodeGen" />
and then
<codegen className="Test.java">
<Property name="StringProperty.name" value="Unnamed"/>
</codegen>
The CodeGen.java then like so:
public class CodeGen extends Task {
private String className = null;
private List properties = new ArrayList();
public void setClassName(String className) {
this.className = className;
}
/**
* Called by ant for every <property> tag of the task.
*
* #param property The property.
*/
public void addConfiguredProperty(Property property) {
properties.add(property);
}
public void execute() throws BuildException {
// here we go!
}
}
I know it can be done because my previous firm used ant to generate model objects in java.
The approach they used was to define model objects in an XML file and run an ant task to generate the pojo and dto.
I quickly googled and saw that there are tools that allow you to generate java from XML. You could probably give your schema/default values etc in XML and have an nt task to run the tool.
I would look at JSR-269 specifically: genftw which makes JSR-269 easier...
And yes it will work with Ant with out even having to write a plugin and will work better than a brittle RegEx.
The other option if your really adventurous is to check out XText for code generation but it is rather complicated.
Yes, it can be done :-)
I once wrote a webservices adapter that used a WSDL document (XML file describing a SOAP based webservice) to generate the POJO Java class that implemented the functional interface to my product. What lead me to do this was the mindlessly repetitive Java code which was necessary to talk to our proprietary system.
The technical solution used an XSLT stylesheet to transform the input XML document into an output Java text file which was subsequently compiled by ANT.
<!-- Generate the implementation classes -->
<xslt force="true" style="${resources.dir}/javaServiceStub.xsl" in="${src.dir}/DemoService.wsdl" out="${build.dir}/DemoService/src/com/myspotontheweb/DemoServiceSkeleton.java" classpathref="project.path">
<param name="package" expression="com.myspotontheweb"/>
..
..
</xslt>
Unfortunately XSLT is the closest thing to a templating engine supported by native ANT.
Best of luck!
I'm looking to use the Jython in a Java project I'm working on.
And I'm wondering 2 things.
Can I disable access to Java classes from Python scripts. IE. stop scripts from being able to do things like from java.util import Date?
And can I change the output stream that print "Hello" etc write to, so that I can redirect a scripts output for my implementation?
Or will I have to edit the actual Jython classes to disable and change this?
In order to restrict access to specific Java classes you could implement a custom class loader and register it to Jython:
this.pyInterpreter.getSystemState().setClassLoader(this.userCodeClassLoader);
If you are doing this because of security issues (disallow some actions on server machine that runs user code) you have to notice that Jython also provides built-in function implementations that won't be caught by your class loader:
Built-in Python function implementations
One additional approach is to analyze all imports in Python parse tree. I think it's better having more than one security measure:
String code = "import sys\n"+"from java.io import File\n"+"sys.exit()";
AnalyzingParser anPar = new AnalyzingParser(new ANTLRStringStream(code), "", "ascii");
mod tree=anPar.parseModule();
Visitor vis = new Visitor() {
#Override
public Object visitImport(Import node) throws Exception {
System.out.println(node);
return node;
}
#Override
public Object visitImportFrom(ImportFrom node) throws Exception {
System.out.println(node);
return node;
}
};
List<PythonTree> children=tree.getChildren();
for (PythonTree c : children){
vis.visit(c);
}