Gerry's Home Page CILT XML Proposal & Report XML Document Type Definitions XML Data Export Functions Translation Functions Printing Application XML Client-Server Application

XML Button Source Code

The user opens certain parts of the hierarchy of note titles and pushes a "print visible" button. For all the notes whose titles are visible in the Java window, their contents are now displayed in the HTML window. The HTML display is properly indented and formatted with the author's name, the title and other related information based on an XSL style sheet. 

Step 1: When user presses button, get a Vector of the open titles and then go to step 2

/**
printDisplayedButton -- in GulchDisplayPane
*/
void jButton18_actionPerformed(ActionEvent e) {
	//get Vector of TreeDataRows which are currently open
    	Vector v = treePane.getOpenRows();
    	//send Vector to server and then call CGI script
    	callCgi(v);
}

Step 2: Traverse the tree of notes and add open ones into the Vector

/**
* getOpenRows() -- in GulchTreePane
* return a vector of TreeDataRows which are open (visible) in the
* current tree structure
*/
public Vector getOpenRows() {
	TreePath path;
	DefaultMutableTreeNode node;
	Vector v = new Vector();
	// Traverse the JTree looking for open nodes
	int rowCount = tree.getRowCount();
	for (int i = 0; i < rowCount; i++) {
		path = tree.getPathForRow(i);
		node = (DefaultMutableTreeNode)path.getLastPathComponent();
		v.addElement(node.getUserObject());
	}
	return v;
}

Step 3. Send Vector of notes from Client to Server. When it returns, call CGi script to put XML data file into browser window.

/**
callCgi() -- in GulchDisplayPane
*/
void callCgi(Vector v) {
	try {
		//send Vector to server
		tabbedPane.interfaceMgr1.multiNodeDisplay(v, xmlRadioButton.isSelected());
		//return from server -- new xml or html file has been created
		//now call cgi script to display file in browser
		URL cgiScript = new URL("http://webguide.cs.colorado.edu/cgi-bin/xml/xmlWebGuide.cgi?webguide.html");
		if (parentPanel.parent.isApplet()) {
			parentPanel.parent.getTheApp().getAppletContext().showDocument(cgiScript, "target");
		}
	}
	catch (Exception ex) {
		messageTextArea.setText(blankMessage);
		messageTextArea.setText("WARNING: Caught an exception while displaying rows.");
		return;
	}
}

Step 4. Encode notes from the Vector into an XML string. Retrieve their full content from the database. Indent them recursively depending on their parent links. Check for special characters in the title and content. Convert the XML to HTML if the user does not have a browser that reads XSL. Then write XML or HTML out to a file on the server that the CGI script know about.

/**
multiNodeDisplay() -- in InterfaceMgrServer
*/
public int multiNodeDisplay(Vector v, boolean xml) {
	int returnInt = 0;
try {
	Connection ColabDatabase = openDatabase();
	Statement stmt = ColabDatabase.createStatement();
	//translate Vector of selected tree nodes into XML or HTML string
	String s = "";
	s = translateXml(v, xml);
	//store file on server
	String fileName = "webguide.xml";
	FileOutputStream fos = new FileOutputStream(fileName);
	PrintWriter pw = new PrintWriter(fos, true);
	pw.print(s);
	pw.close();
	fos.close();
	//output s as a file and call CGI from client
	ColabDatabase.close();
}
catch (Exception e) {
	e.printStackTrace();
}
	return returnInt;
}

/**
translateXml() translate a Vector of TreeDataRows into an XML String
*/
	public String translateXml(Vector v, boolean xml) {
	java.util.Date now = new java.util.Date();
	String returnString = "";
	String xmlString = "";
	StringBuffer s = new StringBuffer("<!-- XML generated by WebGuide -->");
	s.append("<DATASET SYSTEM='WebGuide_2000' DESCRIPTION='tree of data for a perspective' " +
		" CREATOR='" + databaseMgr1.state1.getCurrentAuthorName() + "' " +
		"CREATE_DATE='" + now.toString().substring(0, 11) + "' XML_VERSION='1.0'> ");
	int i = 0;
	while (i < v.size()) {
		i = addNodeXml(v, i, s);
		i++;
	}
	s.append("</DATASET>");
	xmlString = s.toString();

	// Convert the string to HTML if the user so desires
	if (! xml) {
		try {
			XSLTProcessor processor = XSLTProcessorFactory.getProcessor();

			// Have the XSLTProcessor processor object transform "foo.xml" to
			// System.out, using the XSLT instructions found in "foo.xsl".
			processor.process(new XSLTInputSource(xmlString),
			new XSLTInputSource("webguide.xsl"),
			new XSLTResultTarget(returnString));
		} catch (org.xml.sax.SAXException e) {
			System.err.println("Error from XSL processor");
			System.err.println(e);
		}
	}
	return returnString;
}

/**
addNodeXml() append a Node with its recursive sub-nodes
*/
public int addNodeXml(Vector v, int i, StringBuffer s) {
	TreeDataRow row = (TreeDataRow) v.elementAt(i);
	int currentNodeNid = row.getNid();
	int authorNid = 0;
	s.append(" <NOTE NOTE_ID='" + currentNodeNid);
	s.append("' NOTE_KIND='" + row.getKindName());
	s.append("' TITLE='" + markup(row.getName()));
	if (row.getCreated() == null) { s.append(" ' CREATE_DATE=' "); }
	else { s.append(" ' CREATE_DATE='" + row.getCreated()); }
	s.append("' AUTHOR='" + row.getAuthorName());
	s.append("'>");
	//add content and color for this node from database
	try {
		Connection ColabDatabase = openDatabase();
		Statement stmt = ColabDatabase.createStatement();
		String content = databaseMgr1.getMemo(currentNodeNid, stmt);
		s.append(" <CONTENT>'" + markup(content) + "'</CONTENT>");
		authorNid = row.getAuthor();
		String color = databaseMgr1.getColor(authorNid, stmt);
		s.append(" <FIELD> NAME=color VALUE='" + color + "' </FIELD>");
		String perspective = row.getPerspectiveName();
		s.append(" <FIELD> NAME=perspective VALUE='" + perspective + "' </FIELD>");
		ColabDatabase.close();
	}
	catch (Exception e) {
		s.append(" <CONTENT>'" + row.getDescription() + "'</CONTENT>");
		e.printStackTrace();
	}
	while (i+1 < v.size()) { //use explicit while loop because value of i may change
		row = (TreeDataRow) v.elementAt(i+1);
		if (row.getParent() == currentNodeNid) { //if current node is parent of new node
			i = addNodeXml(v, i+1, s); //then recures and append new node within current node
		}
		else {
			s.append("</NOTE>");
			return i;
		}
	}
	s.append("</NOTE>");
	return i;
}

/**
markup()
replace special characters with XML markup for them
& &amp
< &lt
> &gt
" &quot
' &apos
*/
private String markup(String s) {
	StringBuffer t = new StringBuffer();
	for (int i=0; i<s.length(); i++) {
		if (s.charAt(i) == '&') { t.append("&amp;"); }
		else {
			if (s.charAt(i) == '<') { t.append("&lt;"); }
			else {
				if (s.charAt(i) == '>') { t.append("&gt;"); }
				else {
					if (s.charAt(i) == '\"') { t.append("&quot;"); }
					else {
						if (s.charAt(i) == '\'') { t.append("&apos;"); }
						else {
							t.append(s.charAt(i));
						}
					}
				}
			}
		}
	}
	return t.toString();
}

Go to top of this page

Return to Gerry Stahl's Home Page

Send email to Gerry.Stahl@drexel.edu

This page last modified on August 01, 2003