Eclipse Plug-in use standard Format function in the code - java

I'm working on a plugin for Eclipse and I created a StructuredTextEditor. The editor contains an XML. And I want to align the code nicely (like indent etc.). I search a possibility to apply the standard function "Format" of Eclipse SHIFT+Ctrl+F.
I found a code snippet that does exaclty this but I didn't get it to work:
String commandId = IJavaEditorActionDefinitionIds.FORMAT;
IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
try {
handlerService.executeCommand(commandId, null);
} catch (Exception e1) {
e1.printStackTrace();
}
I always get the following Exception:
org.eclipse.core.commands.NotHandledException: There is no handler to execute for command org.eclipse.jdt.ui.edit.text.java.format
Does anyone can help me get running this code, or got en other solution to format the xml content, its important to use the same format like the eclipse formatter uses.

Thanks to greg-449 I searched the correct function to call and found it.
Here is my function that works with StructuredTextEditor.
private void formatString() {
String commandId = "org.eclipse.wst.sse.ui.format.document";
IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
try {
handlerService.executeCommand(commandId, null);
} catch (Exception e1) {
}
}

Related

No output for JSoup file

When running the following code:
try {
Document doc = Jsoup.connect("https://pomofocus.io/").get();
Elements text = doc.select("div.sc-kEYyzF");
System.out.println(text.text());
}
catch (IOException e) {
e.printStackTrace();
}
No output occurs. When changing the println to:
System.out.println(text.first().text());
I get a NullPointerException but nothing else.
jsoup doesn't execute javascript - it parses the HTML that the server returns. You can check View Source (vs Inspect) to see the response from the server, and what is selectable.

image injection to saucelabs with appium

I faced a problem when saucelabs image injection. I use Appium and TestNG. These are device logs;
2093513:13:21 W ImageReader_JNI : Unable to acquire a buffer item, very likely client tried to acquire more than maxImages buffers
I also set true sauceLabsImageInjectionEnabled and autoGrantPermissions parameters true. Below code is used for injecting the image.
String qrCodeImage = null;
try {
qrCodeImage = Base64.getEncoder().encodeToString(toByteArray(
getClass().getResourceAsStream("/images/qrcode.png")));
((JavascriptExecutor)driver).executeScript("sauce:inject-image=" + qrCodeImage);
Thread.sleep(10000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
When Appium logs are examined,I see no related thing in there. I don't figure it out how can solve that. Can anyone help?
ANSWER
try {
((AndroidDriver) driver).pushFile(remotePathForImg, new File(localPathForImg));
} catch (IOException e) {
e.printStackTrace();
}
You must assign remote path and image path.
You can find here:
https://github.com/saucelabs-training/demo-java/tree/master/appium-examples/src/test/java/com/realdevice/unifiedplatform/image_injection
an image injection example in Java + JUnit4

Java JSON Object Request - How to receive the JZON parts I need (android studio)

due to the fact that I am a newbie, I am sorry in advance for making you angry ;-)
I am currently working on a project and today I reached a milestone. My API-Request works.
I am using this URL:
https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=turkey
My problem is , that for my app I only need the "Extract" part of this JSON (at the moment i receive everything). How can I go on?
Try this one:
try {
JSONObject jsonResult = new JSONObject(yourline);
String loudScreaming = jsonResult.getJSONObject("query").getJSONObject("pages").getJSONObject("11125639").getString("extract");
System.out.println(loudScreaming);
} catch (JSONException e) {
e.printStackTrace();
}
try this one:
public String getExtract(String json) {
try {
JSONObject object = new JSONObject(json).getJSONObject("query").getJSONObject("pages");
object = object.getJSONObject(object.names().get(0).toString());
return object.getString("extract");
} catch (Exception e) { return ""; }
}
in different wikis, pages object might have different page ids. But every JSON result has query, pages, and extract.
so we use the first item in pages object and return extract text from there.

Java XML Read with WSIL file

at the moment I am trying to program a program which is able to render a link of an xml-file. I use Jsoup, my current code is the following
public static String XmlReader() {
InputStream is = RestService.getInstance().getWsilFile();
try {
Document doc = Jsoup.parse(fis, null, "", Parser.xmlParser());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
I would like to read the following part from a XML file:
<wsil:service>
<wsil:abstract>Read the full documentation on: https://host/sap/bc/mdrs/cdo?type=psm_isi_r&objname=II_QUERY_PROJECT_IN&saml2=disabled</wsil:abstract>
<wsil:name>Query Projects</wsil:name>
<wsil:description location="host/sap/bc/srt/wsdl/srvc_00163E5E1FED1EE897C188AB4A5723EF/wsdl11/allinone/ws_policy/document?sap-vhost=host&saml2=disabled" referencedNamespace="http://schemas.xmlsoap.org/wsdl/"/>
</wsil:service>
I want to return the following URL as String
host/sap/bc/srt/wsdl/srvc_00163E5E1FED1EE897C188AB4A5723EF/wsdl11/allinone/ws_policy/document?sap-vhost=host&saml2=disabled
How can I do that ?
Thank you
If there is only one tag wsil:description then you can use this code:
doc.outputSettings().escapeMode(EscapeMode.xhtml);
String val = doc.select("wsil|description").attr("location");
Escape mode should be changed, since you are not working on regular html, but xml.
If you have more than one tag with given name you can search for distinct neighbour element, and find required tag with respect to it:
String val = doc.select("wsil|name:contains(Query Projects)").first().parent().select("wsil|description").attr("location");

How to execute the Windows 'printto' verb using Java?

In the past I have used the 'printto' verb to print PDFs from with a .Net application. It looked something like this:
ProcessStartInfo psi = new ProcessStartInfo(file);
psi.Verb = "printto"; // print to given printer
psi.Arguments = "LPT1";
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.ErrorDialog = true;
Process.Start(psi);
How can I do this from a Java application? Or is there an alternative approach? Note that the target platform will always be Windows.
Please try this.
public void print() {
//The desktop api can help calling native applications in windows
Desktop desktop = Desktop.getDesktop();
try {
desktop.print(new File("yourFile.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
}
Please Note : This is the easy fix. You can also use java's Print API to achieve the same thing

Categories

Resources