In an application that I'm developing my goal would be to have highlighted code for some language, i.e. Java, and make so that some elements of code (functions, parameters) are clickable.
I understood that I need some js-based syntax highlighter capable to output raw html instead of linking a CSS stylesheet to the code.
What I still don't get is:
Is there any highlighter that ouputs highlighted code in such format?
How to make just some words clickable?
Is there any alternative to this strategy?
UPDATE1: As first attempt, I downloaded the source code of the android-codepad project, and edited the HTMLViewerPlusPlus class so that it highlights the HTML and, instead of showing it in a WebView, it shows it in a TextView, but since codepad uses css styling, I'm not getting the proper results. I'd need a new alternative that generates hard-styled html code.
UPDATE2: Using jsoup library with this method I managed to parse the code so that I get inline style attribute for each html element, but how to convert css styles to html tags?
Related
The functionality for the project that I am currently working is to get data from a WYSIWYG editor and convert all the input to a PDF document. The problem is sometimes there is necessary to add wider tables and this produces a truncated visualization of them.
To solve this problem, I added to the editor (specifically, CKEditor) a HR button but I renamed it to "Change page orientation", so users can click that before inserting a table. In Java, I used iText 7 to detect this element (<hr>) and change the page orientation. This works like a charm.
Example using iText with a simple table
Now, requirements changed and for license purposes we need to replace iText for another HTML to PDF converter, but we need to keep this functionality.
I found OpenHTMLToPdf and I liked it, but I didn't find the way to replicate this page orientation when a hr (or another specific element) is found.
How can I solve that? I can use whatever library as long as they are open source.
Is there a way for me to render and parse the SVG element from a html page rendered by javascript in Java,
for example: http://bl.ocks.org/mbostock/raw/4063269/, which in the case is using d3.js.
If I understood you question correctly, your problem is that tools you used (HtmlUnit) cannot handle complex JS (d3.js).
In this case there is nothing better than using an actual browser. You can use Selenium to open your page with a remote controlled browser instance and get JS rendered html from there.
This tutorial contains pretty much all you need. Except the getting html part, you can find it in this SO answer.
After that you can feed the html to any parser you want.
EDIT
Just thought of another way, you can try WebKit Html2Pdf. Its purpose is to create PDF files but it uses WebKit under the hood and you can inject custom script (like document.onload callback) that will post SVG contents to you service after page is loaded.
But I wouldnt go that road, it has many limitations (basically only works for direct urls) and overall is pretty messed up.
If what you're trying to do is get the SVG content as a String, Selenium is your best choice, like #chimmi said. But, you might get away without a real browser window opening by using PhantomJS instead.
In theory, it should work like this:
System.setProperty("phantomjs.binary.path", "/path/to/phantomjs");
WebDriver driver = new PhantomJSDriver();
// Open your page with SVG
driver.get("http://localhost:8080/svgpage");
// Find the SVG
WebElement svg = driver.findElement(By.tageName("svg"));
// Get its XML content
String xml = svg.getAttribute("outerHTML");
From here, you could use Batik if you want to actually render the SVG on screen in your non-web app.
Or, if all you wanted was to make assertions on the SVG contents for testing purposes, remember you can select sub-elements using normal CSS or XPath selectors:
//Select all <path> elements within the SVG
Lis<WebElement> pathElementsInSVG = svg.findElements(By.tagName("path"));
//Assert there is 4 <path>s
assert pathElementsInSVG.size() == 4
I am developing a small application with SWT Browser widget. I am highlighting a search text word with
<a id="xyz" href=''><mark>test</mark></a>
in a HTML document. and replace all the search words in HTML Text in this way so we get all the search words highlighted.
htmltext.replaceAll("(?i)"+Pattern.quote(searchword), "\\<a id='xyz' href=''> <mark>$0\\</mark></a>
I want to implement functionality that if I click on next button, next highlighted word should get focus and if I click on previous button previous highlighted text should get focus. how can I accomplish Next and Previous Hit using Javascript in Eclipse RCP application.
This is best solved by combining JavaScript with Java code. It depends what kind of HTML content are you going to handle, if it's stateful (e.g. cannot reload), dynamic with lot of JS code, or plain static. In most cases, the best solution would involve most of logic to be written in JS and just minimal code in Java to bind JS actions to SWT GUI.
There's several things you need to implement:
keyword searching
toggling highlighting
toggling highlight from one word to another
1. Search: you realise that you won't be able to search for words that span through many HTML elements, like W<span>o</span>rd? If that's ok then you can just search and replace from Java as you do now. I'd go for individually tagging each word match with id: <span id="match1"> and remembering how many matches in total were found.
You could likely do such search on JS side as well by adding a function that iterates through DOM and searches for specific text and wraps it with another DOM object.
2. Toggling highlighting: It's best done in JavaScript. Append to your HTML a JS code fragment that toggles DOM element style. Something like:
`
function highlight(id) {
document.getElementById(id).className = 'highlighted'
}
You'll be able to call this JS from SWT by invoking swtBrowser.execute("highlight('match1')")
Further you should implement function that takes off highlighting.
3. Toggling highlighting between elements:
This can be done both on Java side and on JS side. I would probably go with JS and add two more functions: highlightNext() and highlightPrev() that would just call highlight() function with proper ids.
Then in Java you could make SWT buttons that call JS functions through SWTBrowser.execute().
I have an android app with a search functionality. The search functionality loops through locally stored html files and appends a span with a background color to words that equal the imputed word, the same as if you press ctrl -f on your desktop. The problem i am having is that if the user searches for head, body, div, span etc it adds a span to the html tags. My question. Is there an android validation library that deals with this issue or do i need to make my own blacklist? I am aware of Android form validator's libraries but but i am not sure that they are built for what i am looking for.
I've use jsoup before to strip out unwanted html tags. You could do this in order to make the html data more "searchable". Also look at Android's Html.escapeHtml(CharSequence) that converts html into a String.
Is there a way to change the css style(defined in the page source) dynamically with Java? I know it is possible to do it with JavaScript. If there isn't, are there other situations where JavaScript is the only choice developing a web app?
Matthew is right. The question should be specified better.
If you are about applet that is running on current page your can call any javascript including javascript code that changes style of any element.
You just have to add attribute mayscript to applet tag and then use code like the following:
JSObject win = (JSObject) JSObject.getWindow(this);
win.eval("documeent.getElementById('myelem').style='border-color: red'");
If you are asking about sevlet/jsp you can
1. generate full html code including css
2. bind style element to URL that is mapped to servlet or JSP that generates CSS.
where styles URL brings us to servlet that generates css dynamically using parameter "id".
I hope it helps. Otherwise please try to specify you question.
Why don't you use JS in the JSP page like you would in a regular HTML page?