ATTENTION: WiBit.Net will be temporarily taken offline for routine maintenance on 9/22/2018. The site is expected to be down for 2-3 hours.
We apologize for any inconvenience.

WiBisodeMirthJava



package net.wibit;

import java.net.*;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.xpath.*;


public class WiBisodeMirthJava {
	// *******************************************************
	// *******************************************************
	public String getHTML() throws Exception {
		String htmlCode = "";
		
		String getCurriculumList_URL = "https://www.wibit.net/labWebService/rest/getCurriculumList";
		byte[] getCurriculumList_Bytes = httpGET(getCurriculumList_URL);
		Document getCurriculumList_Document = getDocumentFromBytes(getCurriculumList_Bytes);
		
		NodeList curriculumNodeList = getXPath(getCurriculumList_Document, "/WiBitNet/Curriculums/Curriculum");
		
		htmlCode += "<html>" + "\r\n";
		htmlCode += "\t" +  "<head>" + "\r\n";
		htmlCode += "\t\t" +  "<title>WiBit.Net Courses</title>" + "\r\n";
		htmlCode += "\t\t" +  "<base href=\"https://www.wibit.net\" />" + "\r\n";
		htmlCode += "\t\t" +  "<link rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/favicon-32x32.png\" />" + "\r\n";
		htmlCode += "\t\t" +  "<link rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/favicon-16x16.png\" />" + "\r\n";
		htmlCode += "\t\t" +  "<link href=\"http://fonts.googleapis.com/css?family=Open+Sans:400,300,600&amp;subset;=latin,cyrillic\" rel=\"stylesheet\" type=\"text/css\" />" + "\r\n";
		// 
		htmlCode += "\t" +  "</head>" + "\r\n";
		
		htmlCode += "\t" +  "<body style=\"font-family:'Open Sans';\">" + "\r\n";
		
		htmlCode += "\t\t" + "<a href=\"/\" target=\"_blank\" title=\"Goto:WiBit.Net\"><img src=\"/images/logo.learntocode.250.png\" /></a>" + "\r\n";
		htmlCode += "\t\t" + "<hr />" + "\r\n"; 
		
		for(int i = 0; i < curriculumNodeList.getLength(); i++) {
			Element curriculum = (Element)curriculumNodeList.item(i);
			String curriculum_id = ((Element)curriculum.getElementsByTagName("CurriculumID").item(0)).getTextContent();
			String curriculum_title = ((Element)curriculum.getElementsByTagName("Title").item(0)).getTextContent();
			String curriculum_description = ((Element)curriculum.getElementsByTagName("Description").item(0)).getTextContent();
			
			htmlCode += "\t\t" + "<h1>" + escapeHTML(curriculum_title) + "</h1>" + "\r\n";
			htmlCode += "\t\t" + "<img src=\"/images/curriculum/" + curriculum_id + ".200x200.png\" /><br />" + "\r\n";
			String[] descriptionLines = curriculum_description.replaceAll("\r\n", "\n").split("\\n");
			for(int l = 0; l < descriptionLines.length; l++) {
				htmlCode += "\t\t" + "<p style=\"font-weight:100;text-align:justify;text-justify:inter-word;\">" + escapeHTML(descriptionLines[l]) + "</p>" + "\r\n";
			}
			
			String getCoursesForCurriculum_URL = "https://www.wibit.net/labWebService/rest/getCoursesForCurriculum/" + curriculum_id;
			byte[] getCoursesForCurriculum_Bytes = httpGET(getCoursesForCurriculum_URL);
			Document getCoursesForCurriculum_Document = getDocumentFromBytes(getCoursesForCurriculum_Bytes);
			
			NodeList courseNodeList = getXPath(getCoursesForCurriculum_Document, "/WiBitNet/Courses/Course");
			htmlCode += "\t\t" + "<div style=\"padding-left:100px;\">" + "\r\n";
			for(int j = 0; j < courseNodeList.getLength(); j++) {
				Element course = (Element)courseNodeList.item(j);
				String course_id = ((Element)course.getElementsByTagName("CourseID").item(0)).getTextContent();
				String course_title = ((Element)course.getElementsByTagName("Title").item(0)).getTextContent();
				String course_nickname = ((Element)course.getElementsByTagName("Nickname").item(0)).getTextContent();
				String course_description = ((Element)course.getElementsByTagName("Description").item(0)).getTextContent();
				
				htmlCode += "\t\t\t" + "<hr style=\"border-top:dotted 1px\" />" + "\r\n";
				
				String courseURL = "/course/" + escapeHTML(course_nickname.replaceAll(" ", "_"));
				htmlCode += "\t\t\t" + "<table><tr><td width=\"55\"><a href=\"" + courseURL + "\" target=\"_blank\"><img src=\"/images/course/" + course_id + ".50x50.png\" /></a></td><td><h2><a href=\"" + courseURL + "\" target=\"_blank\" style=\"color:blue;\">" + escapeHTML(course_title) + "</a><h2></td></tr></table>" + "\r\n";
				descriptionLines = course_description.replaceAll("\r\n", "\n").split("\\n");
				htmlCode += "\t\t\t" + "<div style=\"font-size:14px;\">" + "\r\n"; 
				for(int l = 0; l < descriptionLines.length; l++) {
					htmlCode += "\t\t\t\t" + "<p style=\"font-weight:100;text-align:justify;text-justify:inter-word;\">" + escapeHTML(descriptionLines[l]) + "</p>" + "\r\n";
				}
				htmlCode += "\t\t\t" + "</div>" + "\r\n";
			}
			htmlCode += "\t\t" + "</div>" + "\r\n";
			getCoursesForCurriculum_Bytes = null;
			getCoursesForCurriculum_Document = null;
			
			if(i + 1 < curriculumNodeList.getLength()) {
				htmlCode += "<hr />";
			}
		}
		
		getCurriculumList_Bytes = null;
		getCurriculumList_Document = null;
		
		htmlCode += "\t" +  "</body>" + "\r\n";
		
		htmlCode += "</html>" + "\r\n";
		
		return htmlCode;
	}
	// *******************************************************
	// *******************************************************
    private byte[] httpGET(String HTTPUrl) throws Exception {        
        URL url = new URL(HTTPUrl);
        HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
        
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestMethod("GET");
        DataInputStream dis = new DataInputStream(urlConn.getInputStream());
        
        byte[] transferBuffer = new byte[1024 * 1024];
        int bytesRead = -1;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        do
        {
            transferBuffer = new byte[1024 * 1024];
            bytesRead = dis.read(transferBuffer);
            if(bytesRead > 0) {
				out.write(transferBuffer, 0, bytesRead);
            }
        } while(bytesRead > 0);
        dis.close();
        dis = null;
        out.close();
        
        byte[] response = out.toByteArray();
        out = null;
        url = null;
        urlConn.disconnect();
        urlConn = null;
        transferBuffer = null;
        
        return response;
    }
	// *******************************************************
	// *******************************************************
    private String escapeHTML(String s) {
        StringBuilder out = new StringBuilder(Math.max(16, s.length()));
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&' || c == '\'') {
                out.append("&#");
                out.append((int) c);
                out.append(';');
            } else {
                out.append(c);
            }
        }
        return out.toString();
    }
	// *******************************************************
	// *******************************************************
    private Document getDocumentFromBytes(byte[] xmlBytes) throws Exception {
        InputStream is = new ByteArrayInputStream(xmlBytes);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(is);    
        is.close();
        doc.setXmlStandalone(true);
        return doc;
    }
	// *******************************************************
	// *******************************************************
    private NodeList getXPath(Document xmlDocument, String xPath) throws Exception {
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile(xPath);

        return (NodeList) expr.evaluate(xmlDocument, XPathConstants.NODESET);
    }
}