Question: Part 2 - Generating an HTML K-ary Tree using Java In part two of this assignment you should first run the WebPageGenerator program provided. At

Part 2 - Generating an HTML K-ary Tree using Java

In part two of this assignment you should first run the WebPageGenerator program provided. At the moment it creates and saves an HTML file named index.html, which if you carefully examine the code is done by first loading a mostly empty Web page called template.html into a Document object (which is the root Node of a Web document), then changing some things in the tree and saving it. Note how the Document object called doc is the root of a K-ary tree that stores all the content of the Web page. And note that methods allow one to add nodes and set content in the page. Define the generateRandomlyColoredTable method such that it adds a table to the page and randomly chooses a number of columns (1-100) and rows (1-100) where each cell in the table will be generated such that it is filled with a randomly selected color.

Here is my code.

package part2_kary_tree;

import java.io.File; import java.io.IOException; import java.net.URL; import javax.swing.text.html.HTML; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException;

public class WebPageGenerator { static String templateFileName = "template.html"; static String saveFileName = "index.html"; public static void main(String[] args) { try { // WE CAN LOAD, MANIPULATE, AND SAVE HTML FILES USING THESE CLASSES DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// FIRST WE'LL LOAD THE TEMPLATE FILE File templateFile = new File(templateFileName); URL templateURL = templateFile.toURI().toURL(); Document doc = docBuilder.parse(templateURL.getPath()); addWebPageContent(doc);

// EXPORT THE WEB PAGE saveDocument(doc, saveFileName); } catch(SAXException | ParserConfigurationException | TransformerException | IOException exc) { System.out.println("Error loading " + saveFileName); } } private static void addWebPageContent(Document doc) { // SET THE WEB PAGE TITLE Node titleNode = doc.getElementsByTagName("title").item(0); titleNode.setTextContent("CSE 214 - HW 4, Part 2 - K-ary Trees");

// ADD A PARAGRAPH Node paragraphNode = doc.createElement(HTML.Tag.P.toString()); paragraphNode.setTextContent("Below is our random color table"); Node bodyNode = doc.getElementsByTagName("body").item(0); bodyNode.appendChild(paragraphNode);

// GENERATE A TABLE WITH 1-100 COLUMNS (RANDOM NUMBER) // AND 1-100 ROWS (RANDOM NUMBER) WHERE EACH TABLE // CELL IS FILLED WITH A RANDOM COLOR generateRandomlyColoredTable(); } private static void generateRandomlyColoredTable() { // YOU MUST DEFINE THIS METHOD } private static void saveDocument(Document doc, String outputFilePath) throws TransformerException, TransformerConfigurationException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Result result = new StreamResult(new File(outputFilePath)); Source source = new DOMSource(doc); transformer.transform(source, result); } }

I am using Netbeans. Thank you for help.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!