JAVA - Write into html files - java

I am looking to write inside an html file using java.
I have my index.html page ready and I would like to use this template and add a name list (with hyperlinks to go to their pages) at a certain place in this page.
Is it possible to use beacons or tags to tell java to write to this exact location in the html file?
I will use this type of java code to write, the array will be a names array btw, but it's in this mind:
String[] labelEquipment = { "thing1", "thing2", "thing3", "thing4",
"thing5", "thing6", "thing7", "thing8", "thing9",
"thing10" };
PrintWriter f0 = new PrintWriter(new FileWriter("filename.txt"));
for (String string : labelEquipment) {
f0.println(string);
}
f0.close();

You can create a html file like this :
<body>
{{my_placeholder}}
</body>
Using java, you can read this file as string and then use .replace('{{my_placeholder}}',your_content) to replace the place holder with individual label equipments. The variable your_content will have to be group of html tags that will be placed in your html in place of my_placeholder

Related

How to write data to pdf file which contains html tags using itext lib in Java

I have String which contains some html tags and it is coming from database, i want to write that in PDF file with same styling present in the String in the form of HTML tag. I tried to use XMLWorkerHelper like this
String html = What is the equation of the line passing through the
point (2,-3) and making an angle of -45<sup>2</sup> with the positive
X-axis?
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new
StringReader(html));
but it only reads the data which is inside the html tag(in this case only 2) other string it simply ignores. But i want the entire String with HTML formating.
With HTMLWorker it works perfectly but that is deprecated so please let me know how to achieve this.
I am using iText 5 lib

JavaFx WebEngine - Overwriting a website's stylesheet with (local) files

I'd like to customise the appearance of a website that I am loading, so I created a little test.css file that does nothing but changing the look of all table rows:
tr {
height: 22px;
background-image: url("test.png");
}
How do I get he WebEngine to load this file and replace the page's own CSS rules with mine?
Also, i'd like to be able to load page-specific css files and not one huge file for all pages.
I found this page, but it only shows how to run through the DOM and assign a new style to the desired elements by hand. This is, of course, not what I want. Instead, I'd like the browser to use my files as 'user defaults'.
Thx for any help :)
First of I have to state, that I hope you know what you are doing, as these things can seriously damage a web site.
So here is what you can do:
You grab the Document from the WebEngine, retrieve the head element and add a style child element to it, containing the src location of the stylesheet you want to add.
Document doc = webView.getEngine().getDocument();
URL scriptUrl = getClass().getResource(pathToAttachedDocument); // pathToAttachedDocument = CSS file you want to attach
String content = IOUtils.toString(scriptUrl); // Use Apache IO commons for conveniance
Element appendContent = doc.createElement("style");
appendContent.appendChild(doc.createTextNode(content));
doc.getElementsByTagName("head").item(0).appendChild(appendContent);
By the way, JavaScript can be added in a similar way, it's just 'script' instead of 'style'
I would do like this to ADD or REPLACE any rules :
String css = getFileAsString("style.css");
Document doc = webEngine.getDocument();
Element e = doc.getElementById("my_style");
e.setTextContent(css);
... given a
<style id="my_style"></style>
tag in the HTML document.
setUserStyleSheetLocation()was designed for that very purpose: to let the user of the web page, style it as they want.
Usage:
webEngine.setUserStyleSheetLocation(styleSheetURL.toString());

Add a new string in between existing string

I have read the content of HTML into string, now I need to add a new attribute inside the body tag, I was thinking of using StringBuilder for this. But I am unable to frame the logic. Any help would be really appreciated.
Existing HTML
<body class="temporaryrevision">
HTML that I want to create
<body class="temporaryrevision" bgcolor="#FFFFFF">
String htmlString="<html>...<body class="temporaryrevision">..</body>...</html>";
String[] tempData=htmlString.split("<body class=/"temporaryrevision/"");
String data = tempData[0]+"bgcolor=/"#FFFFFF/""+tempData[1];
You can use jQUery for this:
$( ".temporaryrevision" ).attr( "bgcolor", "#FFFFFF" );
Ill suggets a way that will be generic:
Use a XML parser for the same.
Load the file into java and pass it to and instance of XML Parser(SAXParser or something like that)
Traverse the parsed Object tree to your required element tag name. in this example HTML
use Library method to add attribute to the element.
convert the object back to xml format(Basic HTML).
Write and replace the file contents with your updated content.
This way is complex but will be generic to all ur such needs..

process HTML files using hadoop map reduce

I have a input folder in hdfs which contain thousands of HTML files :
/data/htmls/1/(HTML files)
/data/htmls/2/(HTML files)
.
.
/data/htmls/n/(HTML files)
I have a java function which takes HTML file as input and parse it, I want to read these HTML files in mapper function and feed them as input to parser function. Because Input files are processed line by line by map function, is there a way to work with HTML file?
I'm not sure how well it would work, but the Mahout XmlInputFormat is a decent XML reader. You might be able to adapt that to working for HTML.
In the configuration set the following before creating the Job object:
conf.set("xmlinput.start", "<tag>");
conf.set("xmlinput.end", "</tag>");
Then set the input class by the following after creating a job object:
job.setInputFormatClass(XmlInputFormat.class);
This will select everything inside of the specified tag as a single input string.
for instance if you select <html> and </html> (or <body> </body> or any other matched pair of tags) as start and end tags you should get everything inside of that as a single record, passed to the mapper.
Hope this is helpful.

Merging HTML files in Java

I want to merge multiple HTML files into one. For example, if I have two HTML files which prints WELCOME and XYZ respectively, can i merge these two file into one which can show together WELCOME XYZ? These operation I want to do for multiple, suppose 1500, files.
Appreciate any help.
You might use an HTML parsing/manipulation API such as JSoup.
create one html file and keep on including several files using below command...
<!--#include virtual="insertthisfile1.html" -->
<!--#include virtual="insertthisfile2.html" -->
<!--#include virtual="insertthisfile3.html" -->
<!--#include virtual="insertthisfile4.html" -->
You can get this done by iterating over all your files and appending the contents of <body>...</body> tag together programmatically.
Get all the html file names in to an ArrayList<String>
Create one StringBuilder
Read each HTML file line by line till you find a line with the body tag
Read from that tag start till you find a line with the body tag closing
Append this content to the StringBuilder
After all files are read, write the StringBuilder content to one file.
At the end you will have the one single HTML file

Categories