How to tell Java which StringEscapeUtils.escapeXML() to use? - java

I'm trying to use the StringEscapeUtils.escapeXML() function from org.apache.commons.lang...
There are two versions of that function, one which expects (Writer, String) and one which just expects (String)....
http://commons.apache.org/lang/api/org/apache/commons/lang/StringEscapeUtils.html#escapeXml(java.lang.String)
I'm trying to use the version that just expects the String parameter without the Writer, but Java is complaining that I've not given it a Writer.
How do I use this in my program so that I don't need a Writer?
String escXml = StringEscapeUtils.escapeXml(attr.get());
xml = xml.concat("<"+attr.getID()+">"+escXml+"</"+attr.getID()+">");
I've also tried just doing it inline in the string itself.
xml = xml.concat("<"+attr.getID()+">"+StringEscapeUtils.escapeXml(attr.get())+"</"+attr.getID()+">");
Both of these attempts have given me the error about it expecting the Writer though. Can anyone help me with this?
Thanks,
Matt

The error message is telling you that you are passing an Object into the method, not a String.
If you are sure that the Object is a String, then you'll need to cast it to a String first.
If this doesn't work, please post the actual code that is giving you trouble.

You should compile the java class with the specify version if you have install the more than one version of java in you system.
You should compile your file using this way.
javac -target -source
E.g H:>javac -target 1.6 -source 1.6 Testt.java
So your target and Source version is tell to the java so it will call the particular version class at run time..

What exactly is the compiler error message?
is it possible that you're using a different version of the commons library that does not have the 1-parameter method?

Related

Compiling a Java Project using Compiler API

I'm trying to compile a whole java project dynamically using the Compiler API. My initial thoughts of achieving this is to first know how to compile it in one-line using command line, then apply the same principle/parameters on the compiler object. Problem is, I never did, err, compiled using CLI. (Disadvantage of using an IDE? Haha)
So, am I on the right track? Moreover, can the project compilation achieved in one line execution? I'm having a hard time figuring this out because of the fact that it's a project, it contains packages galore.
So you either want to learn javac or Java Compiler API?
If you want CLI compilation look at javac (Linux | windows).
Alternatively for API, then programmatic use of Java's compiler API will definitely require more that one line, this will get you the compiler:
JavaCompilercompiler =ToolProvider.getSystemJavaCompiler();
Then you'll still need to load classes, write out byte code, and possibly package as a JAR.
You should use StandardJavaFileManager as you've probably many classes to manage, there's an example in the top of the JavaCompiler javadoc, but search for StandardJavaFileManager+JavaCompiler+example to find clearer examples like this blog post.

Using ICompilationUnit for ASTParser.setSource() method in eclipse jdt/ast

ASTParser.setSource has polymorphic methods with different input types.
However, when I use ICompilationUnit as an input to setSource method,
I got an error saying I didn't use char[] as an input parameter.
Why this error?
ADDED
I use eclipse jdt/ast as a part of stand alone java program. In the course of doing that, I don't use eclipse project/resources, but load the java source into char[] as a parameter for setSource(). I'm not sure, but the eclipse may recognize that it's not run as a plugin to block the usage of other polymorphic methods.
check whether you have specified the kind as compilation unit.
// Parse the class as a compilation unit.
parser.setKind(ASTParser.K_COMPILATION_UNIT);
I was trying to execute the jdt/ast as a library for stand-alone java program. When I rerun them in plugin context, everything works fine.

Converting from Java 1.4 to Java 1.6

I am currently converting from Java 1.4 to Java 1.6. When I try to recompile my code in Java 1.6 I get the following compilation error:
_getAgentInfoOperation0.setUse(com.ibm.ws.webservices.engine.enum.Use.LITERAL);
_getAgentInfoOperation0.setStyle(com.ibm.ws.webservices.engine.enum.Style.WRAPPED);
I understand that enum is a reserved word in Java 1.6. I was told that I could change the enum to enumtype and that should compile cleanly. I changed the code to enumtype:
_getAgentInfoOperation0.setUse(com.ibm.ws.webservices.engine.enumtype.Use.LITERAL);
_getAgentInfoOperation0.setStyle(com.ibm.ws.webservices.engine.enumtype.Style.WRAPPED);
When I change to enumtype I get the following two errors:
1) The method setUse(Use) in the type OperationDesc is not applicable for the arguments (Use).
2) The method setStyle(Style) in the type OperationDesc is not applicable for the arguments (Style).
Does anyone know what I need to do to get a clean compile?
Thanks,
It looks to me like OperationDesc is your code and com.ibm.ws... is a library you're using. I suspect that, even though you switched to using the new enumtype version of the library, your old code is still expecting the enum version, so it doesn't recognize the new Use or Style objects.
After reading this forum thread, I wonder if perhaps you haven't updated your build path to use com.ibm.ws.webservices.thinclient_7.0.0.jar.

How to create a method out of the text file?

I have a text (.txt) file that contains Java code! I want to create a method that includes this Java code and then call that method through the program.
Can anybody suggest a way to do this?
let consider this example what it does actually load the source code, compile and execute the java code by simpler program by using JavaCompiler API.
Use the JavaCompiler. It can compile code from a String, so I'm sure it could handle code from a text file.
Do you think instead of putting it in the main method I can put it in for example test method and call method like this?
Put it wherever you like. E.G. see the STBC & especially the source code. It provides a GUI and can compile the code in the text area on button click.
this program need tools.jar but jre 7 doesnt have this!!
Did you try reading the documentation that is provided for the STBC? Notably:
System Requirements
STBC will run on any computer with a version 1.6+ Java Plug-In* JDK (AKA SDK).
(*) The API that STBC uses is merely a public interface to the compiler in the tools.jar that is distributed only with JDKs (though the 'public JRE' of the JDK also seems to acquire a tools.jar). This leads to some unusual requirements in running either the native jar, or the web start app.
Or shorter, no JRE will have a JavaCompiler, only JDKs have them.
Change the .txt file to a .java file,
add it to your java project
Compile the code
Execute the methods
Load the file in through standard java IO and then have Groovy evaluate it for you:
http://groovy.codehaus.org/Embedding+Groovy
it's something like quine:
http://www.nyx.org/%7Egthompso/quine.htm

identifiers in java with different versions?

as we "No keyword should be used as an Identifier in java". But there will be some words like asser or enum or any other which have been added as keyword in version 1.4, 1.5 resp. So if any older version code is used to compile with new javac, what happens if that code contains these words as an identifier?
When older code uses an identifier that is a keyword in the current version you are trying to compile the code with, you will simply get an error. The compiler won't be able to tell from the code what version it was written for.
You'll get a compile error. But you can use already compiled classes if you aren't going to make changes in your legacy code.
Thanks for your answer, After searching i got to know the answer as, if any keyword is used as an identifier then we have to compile it with its compliant compiler otherwise it gives a compiler error. So to do that say suppose if my code has a assert as an identifier which works fine with java 1.3. So i can compile it as
javac -source 1.3 example.java
which compiles fine.

Categories