How to develop a content assist like the one of Eclipse IDE? - java

I want to develop a content assist for my own IDE like the one that Eclipse offers for java.
My problem is:
How to display the information of a completion proposal has an HTML text(javadoc in the case of eclipse)?
I know only how to use a string to show the information of a method in content assist:
new CompletionProposal("catch(a)", index + 1, offset - (index + 1), "catch(a)", img, keyword + " ", null, "Catches an exception a");
Tks for the time, regards Ramos

Related

How to compile and run some String entered in a TextArea?

My intentions are to ask the user to write down some code in a TextArea, then see if that code compiles, and if it does, print out the results to another TextArea, which acts like a console.
Edit:Solving this via online compilers is the priority.
To accomplish this, I've tried using online compilers (i.e. compilerjava.net) and used the library HtmlUnit, but the library came in with a lot of errors, especially when reading the JavaScript code and returned me pages of 'warnings' that increase the compile & run time for about 20 seconds. I will leave the code below with explanations if anyone has intentions about trying to fix it.
I've also tried using the JavaCompiler interface, which did succeed in compiling, but under the conditions that I have provided the exact location of the .java file, which is something I have to create using the information I get from the TextArea. So again, a dead end.
I decided to come back to online compilers, since if I can manage to just return the data from the compiled program, I am set. The only issue is I haven't yet found an online compiler that allows a user to access its fields via Java code ( since its something too specific). I would appreciate any help on this if anyone can provide a way to send and retrieve data from an online compiler.
Here is the code using the HtmlUnit library, on the site 'compilerjava.net'. It is so close to working that the only 2 issues I have is that,
1) Run-time is too long.
2) I cannot access the console output of the code. Reasoning is that, when you hit 'compile', the output text-area's text turns into "executing". After a few seconds, it turns into the output of the code. When I try to access it, the data I retrieve is always "executing" and not the desired output. Here is the code;
public class Test {
public static void main(String[] args) {
try {
// Prevents the program to print thousands of warning codes.
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
// Initializes the web client and yet again stops some warning codes.
WebClient webClient = new WebClient( BrowserVersion.CHROME);
webClient.getOptions().setThrowExceptionOnFailingStatusCode( false);
webClient.getOptions().setThrowExceptionOnScriptError( false);
webClient.getOptions().setJavaScriptEnabled( true);
webClient.getOptions().setCssEnabled( true);
// Gets the html page, which is the online compiler I'm using.
HtmlPage page = webClient.getPage("https://www.compilejava.net/");
// Succesfully finds the form which has the required buttons etc.
List<HtmlForm> forms = page.getForms();
HtmlForm form = forms.get( 0);
// Finds the textarea which will hold the code.
HtmlTextArea textArea = form.getTextAreaByName( "code");
// Finds the textarea which will hold the result of the compilation.
HtmlTextArea resultArea = page.getHtmlElementById( "execsout");
// Finds the compile button.
HtmlButtonInput button = form.getInputByName( "compile");
System.out.println( button.getValueAttribute());
// Simple code to run.
textArea.setText( "public class HelloWorld\n" +
"{\n" +
" // arguments are passed using the text field below this editor\n" +
" public static void main(String[] args)\n" +
" {\n" +
" System.out.print( \"Hello\");\n" +
" }\n" +
"}");
// Compiles.
button.click();
// Result of the compilation.
System.out.println( resultArea.getText());
} catch ( Exception e) {
e.printStackTrace();
}
}
}
As I said, this final code System.out.println( resultArea.getText()); prints out "executing", which implies that I have succeeded in pressing the compile button on the webpage via code.
So after this long wall of text, I'm either looking for a way to fix the code I presented, which is so darn close to my answer but not quite, or just an entirely different solution to the problem I presented at the beginning.
P.S. Maven is the last hope.

Jsoup with JavaScript dependent web page?

So I'm having trouble formulating the correct syntax for selecting this element from a webpage. Here is what the path looks like on the Inspect Element Interface on Firefox
And here's what my current code looks like:
Element prices = doc.select("body[class =en page-type-search page-type-group-shelf og ress] " +
"div#wrap " +
"div#main-wrap " +
"div#jalapeno-template " +
"div[class=zone zone3 wgrid-10of12 wgrid-6of8 wgrid-4of4] " +
"section#shelf-page " +
"div#shelf-thumbs " +
"div.shelf-thumbs " +
"div.price-current " +
"span.product-price-analytics").first();
String priceOne = prices.attr("data-analytics-value");
And just to be incredibly clear, the attribute that I'm wanting is the 'data-analytics-value' because it gives an exact price.
I think that I have all the correct syntax so what am I doing wrong? When I run the program it gives me a nullPointerException. Any help is appreciated!
[Update] I changed princeOne to doc.toString() and its saying the the web browser is not running javascript and that JavaScript is required to view the walmart website, any work arounds?
After trying with no luck using Android's WebView, I accidentally found a solution in setting my userAgent, all I did was change the
Jsoup.connect(url).get();
line to
Jsoup.connect(url).userAgent("YOUR_USER_AGENT_HERE").get();
and it worked like a charm. Thanks for the reply anyway Fred!

Eclipse RCP: How to programmatically get Problems View records

On Eclipse Luna, I need to programmatically build java projects and then retrieve the Problems View's records. I use the following code
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IResource resource = workspace.getRoot();
IMarker[] markers = resource.findMarkers(IMarker.MARKER, true, IResource.DEPTH_INFINITE);
for (IMarker m : markers) {
System.out.println("Id: " + m.getId());
System.out.println("Message: " + m.getAttribute(IMarker.MESSAGE));
System.out.println("Source ID: " + m.getAttribute(IMarker.SOURCE_ID));
System.out.println("Location: " + m.getAttribute(IMarker.LOCATION));
System.out.println("Line Number: " + m.getAttribute(IMarker.LINE_NUMBER));
System.out.println("Marker: " + m.getAttribute(IMarker.MARKER));
}
The message and line number are printed correctly. But IMarker.SOURCE_ID returns "JDT" and IMarker.LOCATION is always null.
Anybody knows how can I get the data shown as "Resource" and "Path" on the Problems View? I cannot create any custom Marker view using MarkerSupportView. I need to access the existing Problems View in a programmatic way. Thank you for any suggestion.
Got it. Use getResource() instead of getAttribute().
The markers API is pretty flexible, you should read the documentation.
Long story short, there will be other attributes that you're not looking at. Try calling getAttributes and dumping them out.

Getting mime type Description/Name/Friendly Format

Since I don't know how it is called the name of the Friendly format of the Mime Types like for example:
image/bmp - > Bitmap
image/x-icon - > Icon
I would like to ask how I can retrieve that information in some kind of Map with Key->Value (I am using Java)
Or some library where I can get them from.
I am using apache tika and there with MimeTypes.forName("application/pdf").getDescription(); is not returning me anything.
Anyways I think this question is irrelevant for every programming language.
I've found one website where I can see them but it is made with Table and I cannot extract them so I can receive them in key->value type HERE
You could just scrape that page.
If you're using Firefox with Firebug or Chrome then paste the following Javascript into an eval window and run it.
(The right hand side panel in Firebug's Console tab, or in Chrome; Tools > Developer Tools and paste in Console tab)
jQuery('#mime-types-list tbody tr').each(function(i, tr){
var tds = jQuery(tr).children('td');
console.log("map.put(\"" + jQuery(tds[1]).text() + "\", \"" + jQuery(tds[0]).text() + "\");");
});
This will generate 684 entries, to copy 'n' paste, such as:
map.put("text/turtle", "Turtle (Terse RDF Triple Language)");
Just convert the string inside console.log to suit your syntax/format needs.

How can i send an query from java to lpsolve as String

Hi i formulated a linear programing problem using java
and i want to send it to be solved by lpsolve without the need to create each constraint seperatlly.
i want to send the entire block (which if i insert it to the ide works well) and get a result
so basically instead of using something like
problem.strAddConstraint("", LpSolve.EQ, 9);
problem.strAddConstraint("", LpSolve.LE, 5);
i want to just send as one string
min: 0*x11 + 0*x12 + 0*x13
x11 + x12 + x13= 9;
x12 + x12<5;
can it be done if so how?
LpSolve supports LP files as well as MPS files. Everything is thoroughly detailed in the API documentation (see http://lpsolve.sourceforge.net/5.5/).
You can do your job like this in java :
lp = LpSolve.readLP("model.lp", NORMAL, "test model");
LpSolve.solve(lp)
What is sad with file based approaches is that you will not be able to use warm start features. I would not suggest you to use such approach if you want to optimize successive similar problems.
Cheers

Categories