From: <tim...@us...> - 2007-08-20 14:02:21
|
Revision: 5049 http://smartfrog.svn.sourceforge.net/smartfrog/?rev=5049&view=rev Author: tim_doering Date: 2007-08-20 07:02:17 -0700 (Mon, 20 Aug 2007) Log Message: ----------- SFOS-399, 295: Removed redundancy of the XML code. Modified Paths: -------------- trunk/core/extras/avalanche/AvalancheGUI/web/help_get.jsp trunk/core/extras/avalanche/AvalancheGUI/web/host_actions.jsp trunk/core/extras/avalanche/AvalancheGUI/web/host_list.jsp trunk/core/extras/avalanche/AvalancheGUI/web/host_status_feed.jsp trunk/core/extras/avalanche/AvalancheGUI/web/host_status_get.jsp Added Paths: ----------- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/XMLHelper.java Added: trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/XMLHelper.java =================================================================== --- trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/XMLHelper.java (rev 0) +++ trunk/core/extras/avalanche/AvalancheCore/src/org/smartfrog/avalanche/shared/XMLHelper.java 2007-08-20 14:02:17 UTC (rev 5049) @@ -0,0 +1,106 @@ +/** +(C) Copyright 1998-2007 Hewlett-Packard Development Company, LP + +This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +For more information: www.smartfrog.org +*/ +package org.smartfrog.avalanche.shared; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import java.io.StringWriter; +import java.io.IOException; +import java.text.DateFormat; +import java.util.Date; + +public class XMLHelper { + + public static void addRSSChannelInformation (Document XMLDocument, Node channelRoot, String title, String link, String description) { + // Title node + Node entry = XMLDocument.createElement("title"); + entry.appendChild(XMLDocument.createTextNode(title)); + channelRoot.appendChild(entry); + + // Link node + entry = XMLDocument.createElement("link"); + entry.appendChild(XMLDocument.createTextNode(link)); + channelRoot.appendChild(entry); + + // Description node + entry = XMLDocument.createElement("description"); + entry.appendChild(XMLDocument.createTextNode(description)); + channelRoot.appendChild(entry); + + // lastBuildDate node + entry = XMLDocument.createElement("lastBuildDate"); + entry.appendChild(XMLDocument.createTextNode(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()))); + channelRoot.appendChild(entry); + + // language node + entry = XMLDocument.createElement("language"); + entry.appendChild(XMLDocument.createTextNode("en-gb")); + channelRoot.appendChild(entry); + } + + public static void addRSSItem(Document XMLDocument, Node channelRoot, String title, String link, String guid, String pubDate, String text) { + // Create <item> node + Node entry = XMLDocument.createElement("item"); + + // Set title node beneath <item> + Node subentry = XMLDocument.createElement("title"); + subentry.appendChild(XMLDocument.createTextNode(title)); + entry.appendChild(subentry); + + // Set link node beneath <item> + subentry = XMLDocument.createElement("link"); + subentry.appendChild(XMLDocument.createTextNode(link)); + entry.appendChild(subentry); + + // Set guid node beneath <item> + subentry = XMLDocument.createElement("guid"); + subentry.appendChild(XMLDocument.createTextNode(guid)); + entry.appendChild(subentry); + + // Set pubDate beneath <item> + subentry = XMLDocument.createElement("pubDate"); + subentry.appendChild(XMLDocument.createTextNode(pubDate)); + entry.appendChild(subentry); + + // Set last message node beneath <host> + subentry = XMLDocument.createElement("description"); + subentry.appendChild(XMLDocument.createTextNode(text)); + entry.appendChild(subentry); + + channelRoot.appendChild(entry); + } + + public static String XMLToString(Document XMLDocument) { + String xmlString = null; + try { + // Convert DOM to XML string + StringWriter sw = new StringWriter(); + StreamResult result = new StreamResult(sw); + Transformer trans = TransformerFactory.newInstance().newTransformer(); + trans.setOutputProperty(OutputKeys.INDENT, "yes"); + trans.transform(new DOMSource(XMLDocument), result); + xmlString = sw.toString(); + sw.close(); + } catch (TransformerConfigurationException tcex) { + + } catch (TransformerException tex) { + + } catch (IOException iex) { + + } + return xmlString; + } +} Modified: trunk/core/extras/avalanche/AvalancheGUI/web/help_get.jsp =================================================================== --- trunk/core/extras/avalanche/AvalancheGUI/web/help_get.jsp 2007-08-20 13:25:10 UTC (rev 5048) +++ trunk/core/extras/avalanche/AvalancheGUI/web/help_get.jsp 2007-08-20 14:02:17 UTC (rev 5049) @@ -19,11 +19,8 @@ <%@ page language="java" contentType="text/xml" %> <%@ page import="org.apache.xerces.parsers.DOMParser" %> <%@ page import="javax.xml.parsers.*" %> -<%@ page import="javax.xml.transform.*" %> -<%@ page import="javax.xml.transform.stream.*" %> -<%@ page import="javax.xml.transform.dom.*" %> -<%@ page import="java.io.*" %> <%@ page import="org.w3c.dom.*" %> +<%@ page import="org.smartfrog.avalanche.shared.XMLHelper" %> <% String pageId = request.getParameter("id"); @@ -69,24 +66,15 @@ // Create type-Node Element entry = xdoc.createElement("type"); - entry.appendChild(xdoc.createTextNode(((text!=null)?"success":"error"))); + entry.appendChild(xdoc.createTextNode(((text != null) ? "success" : "error"))); root.appendChild(entry); // Create message-Node entry = xdoc.createElement("message"); - entry.appendChild(xdoc.createTextNode(((text!=null)?text:"Sorry, but there is no help on this topic."))); + entry.appendChild(xdoc.createTextNode(((text != null) ? text : "Sorry, but there is no help on this topic."))); root.appendChild(entry); - // Convert DOM to XML string - StringWriter sw = new StringWriter(); - StreamResult result = new StreamResult(sw); - Transformer trans = TransformerFactory.newInstance().newTransformer(); - trans.setOutputProperty(OutputKeys.INDENT, "yes"); - trans.transform(new DOMSource(xdoc), result); - String xmlString = sw.toString(); - sw.close(); - // Print output out.clear(); - out.write(xmlString); + out.write(XMLHelper.XMLToString(xdoc)); %> \ No newline at end of file Modified: trunk/core/extras/avalanche/AvalancheGUI/web/host_actions.jsp =================================================================== --- trunk/core/extras/avalanche/AvalancheGUI/web/host_actions.jsp 2007-08-20 13:25:10 UTC (rev 5048) +++ trunk/core/extras/avalanche/AvalancheGUI/web/host_actions.jsp 2007-08-20 14:02:17 UTC (rev 5049) @@ -25,11 +25,8 @@ <%@ page import="org.smartfrog.avalanche.server.engines.*" %> <%@ page import="org.smartfrog.avalanche.core.host.HostType" %> <%@ page import="javax.xml.parsers.*" %> -<%@ page import="javax.xml.transform.*" %> -<%@ page import="javax.xml.transform.stream.*" %> -<%@ page import="javax.xml.transform.dom.*" %> -<%@ page import="java.io.*" %> <%@ page import="org.w3c.dom.*" %> +<%@ page import="org.smartfrog.avalanche.shared.XMLHelper" %> <% HostManager manager = factory.getHostManager(); @@ -52,7 +49,7 @@ BootStrap bs = new BootStrap(factory); try { bs.ignite(hosts); - } catch (HostIgnitionException e) { + } catch (HostIgnitionException e) { message = "Host Ignition failed, please check the hosts' settings. " + e.getMessage(); } // STOP SMARTFROG ON SELECTED HOSTS @@ -108,16 +105,7 @@ entry.appendChild(xdoc.createTextNode(message)); root.appendChild(entry); - // Convert DOM to XML string - StringWriter sw = new StringWriter(); - StreamResult result = new StreamResult(sw); - Transformer trans = TransformerFactory.newInstance().newTransformer(); - trans.setOutputProperty(OutputKeys.INDENT, "yes"); - trans.transform(new DOMSource(xdoc), result); - String xmlString = sw.toString(); - sw.close(); - // Print output out.clear(); - out.write(xmlString); + out.write(XMLHelper.XMLToString(xdoc)); %> \ No newline at end of file Modified: trunk/core/extras/avalanche/AvalancheGUI/web/host_list.jsp =================================================================== --- trunk/core/extras/avalanche/AvalancheGUI/web/host_list.jsp 2007-08-20 13:25:10 UTC (rev 5048) +++ trunk/core/extras/avalanche/AvalancheGUI/web/host_list.jsp 2007-08-20 14:02:17 UTC (rev 5049) @@ -18,7 +18,7 @@ For more information: www.smartfrog.org */ --%> -<%@ page contentType="text/html" language="java" %> +<%@ page language="java" contentType="text/html" %> <%@ include file="header.inc.jsp" %> <%@ page import="org.smartfrog.avalanche.server.*" %> <%@ page import="org.smartfrog.avalanche.core.host.*" %> @@ -112,20 +112,8 @@ <td><%= host %> </td> <td> - <table cellspacing="0" cellpadding="0"> - <tr> - <td> - <a href="log_view.jsp?pageAction=viewSelected&hostId=<%= host %>"> - [Logs] - </a> - </td> - <td class="data"> - <a href="host_setup_bs.jsp?hostId=<%= host %>"> - [Settings] - </a> - </td> - </tr> - </table> + <a href="log_view.jsp?pageAction=viewSelected&hostId=<%= host %>">[Logs]</a> + <a href="host_setup_bs.jsp?hostId=<%= host %>">[Settings]</a> </td> <td><%=os%>, <%=arch %> </td> Modified: trunk/core/extras/avalanche/AvalancheGUI/web/host_status_feed.jsp =================================================================== --- trunk/core/extras/avalanche/AvalancheGUI/web/host_status_feed.jsp 2007-08-20 13:25:10 UTC (rev 5048) +++ trunk/core/extras/avalanche/AvalancheGUI/web/host_status_feed.jsp 2007-08-20 14:02:17 UTC (rev 5049) @@ -23,15 +23,12 @@ <%@ page import="org.smartfrog.avalanche.server.*" %> <%@ page import="org.w3c.dom.*" %> <%@ page import="javax.xml.parsers.*" %> -<%@ page import="javax.xml.transform.*" %> -<%@ page import="javax.xml.transform.stream.*" %> -<%@ page import="javax.xml.transform.dom.*" %> -<%@ page import="java.io.*" %> <%@ page import="org.smartfrog.avalanche.shared.ActiveProfileUpdater"%> <%@ page import="org.smartfrog.avalanche.core.activeHostProfile.ActiveProfileType"%> <%@ page import="java.text.DateFormat"%> <%@ page import="java.util.Date"%> <%@ page import="org.smartfrog.avalanche.core.activeHostProfile.MessageType"%> +<%@ page import="org.smartfrog.avalanche.shared.XMLHelper" %> <% // Be able to query ActiveProfile @@ -51,9 +48,13 @@ Element channelRoot = xdoc.createElement("channel"); rssRoot.appendChild(channelRoot); - Element entry = null; - Element subentry = null; + String title = ""; + String link = ""; + String description = ""; + String lastMsgText = ""; + String lastMsgTime = ""; + String hostName = request.getParameter("host"); if (hostName != null) { hostName = hostName.trim().toLowerCase(); @@ -67,199 +68,60 @@ HostManager manager = factory.getHostManager(); String[] hosts = manager.listHosts(); - // Title node - entry = xdoc.createElement("title"); - entry.appendChild(xdoc.createTextNode("Avalanche Host Status Feed")); - channelRoot.appendChild(entry); + title = "Avalanche Host Status Feed"; + link = pathToAvalancheWebApp + "/host_list.jsp?active=true"; + description = "This feed shows the status of all hosts known to Avalanche."; - // Link node - entry = xdoc.createElement("link"); - entry.appendChild(xdoc.createTextNode(pathToAvalancheWebApp + "/host_list.jsp?active=true")); - channelRoot.appendChild(entry); + XMLHelper.addRSSChannelInformation(xdoc, channelRoot, title, link, description); - // Description node - entry = xdoc.createElement("description"); - entry.appendChild(xdoc.createTextNode("This feed shows the status of all hosts known to Avalanche.")); - channelRoot.appendChild(entry); - - // lastBuildDate node - entry = xdoc.createElement("lastBuildDate"); - entry.appendChild(xdoc.createTextNode(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()))); - channelRoot.appendChild(entry); - - // language node - entry = xdoc.createElement("language"); - entry.appendChild(xdoc.createTextNode("en-gb")); - channelRoot.appendChild(entry); - // For each host for (String host : hosts) { - String lastMsgText = ""; - String lastMsgTime = ""; - try { // Query ActiveProfile type = updater.getActiveProfile(host); if (type.getMessagesHistoryArray().length != 0) { - MessageType msg = type.getMessagesHistoryArray(type.getMessagesHistoryArray().length-1); + MessageType msg = type.getMessagesHistoryArray(type.getMessagesHistoryArray().length - 1); lastMsgText = msg.getMsg(); lastMsgTime = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date(Long.parseLong(msg.getTime()))); } else { lastMsgText = "No message received yet."; lastMsgTime = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()); } + link = pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + host; - // Create <item> node - entry = xdoc.createElement("item"); - - // Set title node beneath <item> - subentry = xdoc.createElement("title"); - subentry.appendChild(xdoc.createTextNode(host + ": " + type.getHostState())); - entry.appendChild(subentry); - - // Set link node beneath <item> - subentry = xdoc.createElement("link"); - subentry.appendChild(xdoc.createTextNode(pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + host)); - entry.appendChild(subentry); - - // Set guid node beneath <item> - subentry = xdoc.createElement("guid"); - subentry.appendChild(xdoc.createTextNode(pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + host)); - entry.appendChild(subentry); - - // Set pubDate beneath <item> - subentry = xdoc.createElement("pubDate"); - subentry.appendChild(xdoc.createTextNode(lastMsgTime)); - entry.appendChild(subentry); - - // Set last message node beneath <host> - subentry = xdoc.createElement("description"); - subentry.appendChild(xdoc.createTextNode("Last Message: " + lastMsgText + "\nReceived on: " +lastMsgTime)); - entry.appendChild(subentry); - - // Append <host> node to hostlist - channelRoot.appendChild(entry); + XMLHelper.addRSSItem(xdoc, channelRoot, host + ": " + type.getHostState(), link, link, lastMsgTime, "Last Message: " + lastMsgText + "\nReceived on: " + lastMsgTime); } catch (Exception e) { // do nothing } } } else { - // Title node - entry = xdoc.createElement("title"); - entry.appendChild(xdoc.createTextNode("Avalanche: " + hostName + " Status")); - channelRoot.appendChild(entry); + title = "Avalanche: " + hostName + " Status"; + link = pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + hostName; + description = "This feed shows the status of " + hostName; - // Link node - entry = xdoc.createElement("link"); - entry.appendChild(xdoc.createTextNode(pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + hostName)); - channelRoot.appendChild(entry); + XMLHelper.addRSSChannelInformation(xdoc, channelRoot, title, link, description); - // Description node - entry = xdoc.createElement("description"); - entry.appendChild(xdoc.createTextNode("This feed shows the status of " + hostName)); - channelRoot.appendChild(entry); - - // lastBuildDate node - entry = xdoc.createElement("lastBuildDate"); - entry.appendChild(xdoc.createTextNode(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()))); - channelRoot.appendChild(entry); - - // language node - entry = xdoc.createElement("language"); - entry.appendChild(xdoc.createTextNode("en-gb")); - channelRoot.appendChild(entry); - type = updater.getActiveProfile(hostName); MessageType[] messages = type.getMessagesHistoryArray(); - String lastMsgText = ""; - String lastMsgTime = ""; - if (messages.length != 0) { - for (int i = messages.length-1; i >= 0; i--) { - + for (int i = messages.length - 1; i >= 0; i--) { MessageType msg = messages[i]; lastMsgText = msg.getMsg(); lastMsgTime = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date(Long.parseLong(msg.getTime()))); - // Create <item> node - entry = xdoc.createElement("item"); - - // Set title node beneath <item> - subentry = xdoc.createElement("title"); - subentry.appendChild(xdoc.createTextNode(lastMsgText)); - entry.appendChild(subentry); - - // Set link node beneath <item> - subentry = xdoc.createElement("link"); - subentry.appendChild(xdoc.createTextNode(pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + hostName)); - entry.appendChild(subentry); - - // Set guid node beneath <item> - subentry = xdoc.createElement("guid"); - subentry.appendChild(xdoc.createTextNode(pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + hostName)); - entry.appendChild(subentry); - - // Set pubDate beneath <item> - subentry = xdoc.createElement("pubDate"); - subentry.appendChild(xdoc.createTextNode(lastMsgTime)); - entry.appendChild(subentry); - - // Set last message node beneath <host> - subentry = xdoc.createElement("description"); - subentry.appendChild(xdoc.createTextNode(lastMsgText)); - entry.appendChild(subentry); - - // Append <host> node to hostlist - channelRoot.appendChild(entry); - } + XMLHelper.addRSSItem(xdoc, channelRoot, lastMsgText, link, link, lastMsgTime, lastMsgText); + } } else { lastMsgText = "No message received yet."; lastMsgTime = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()); + link = pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + hostName; - // Create <item> node - entry = xdoc.createElement("item"); - - // Set title node beneath <item> - subentry = xdoc.createElement("title"); - subentry.appendChild(xdoc.createTextNode(lastMsgText)); - entry.appendChild(subentry); - - // Set link node beneath <item> - subentry = xdoc.createElement("link"); - subentry.appendChild(xdoc.createTextNode(pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + hostName)); - entry.appendChild(subentry); - - // Set guid node beneath <item> - subentry = xdoc.createElement("guid"); - subentry.appendChild(xdoc.createTextNode(pathToAvalancheWebApp + "/log_xmpp.jsp?host=" + hostName)); - entry.appendChild(subentry); - - // Set pubDate beneath <item> - subentry = xdoc.createElement("pubDate"); - subentry.appendChild(xdoc.createTextNode(lastMsgTime)); - entry.appendChild(subentry); - - // Set last message node beneath <host> - subentry = xdoc.createElement("description"); - subentry.appendChild(xdoc.createTextNode(lastMsgText)); - entry.appendChild(subentry); - - // Append <host> node to hostlist - channelRoot.appendChild(entry); + XMLHelper.addRSSItem(xdoc, channelRoot, lastMsgText, link, link, lastMsgTime, lastMsgText); } } - // Convert DOM to XML string - StringWriter sw = new StringWriter(); - StreamResult result = new StreamResult(sw); - Transformer trans = TransformerFactory.newInstance().newTransformer(); - trans.setOutputProperty(OutputKeys.INDENT, "yes"); - trans.transform(new DOMSource(xdoc), result); - String xmlString = sw.toString(); - sw.close(); - // Print output out.clear(); - out.write(xmlString); + out.write(XMLHelper.XMLToString(xdoc)); %> \ No newline at end of file Modified: trunk/core/extras/avalanche/AvalancheGUI/web/host_status_get.jsp =================================================================== --- trunk/core/extras/avalanche/AvalancheGUI/web/host_status_get.jsp 2007-08-20 13:25:10 UTC (rev 5048) +++ trunk/core/extras/avalanche/AvalancheGUI/web/host_status_get.jsp 2007-08-20 14:02:17 UTC (rev 5049) @@ -24,12 +24,9 @@ <%@ page import="org.smartfrog.avalanche.core.host.*" %> <%@ page import="org.w3c.dom.*" %> <%@ page import="javax.xml.parsers.*" %> -<%@ page import="javax.xml.transform.*" %> -<%@ page import="javax.xml.transform.stream.*" %> -<%@ page import="javax.xml.transform.dom.*" %> -<%@ page import="java.io.*" %> <%@ page import="org.smartfrog.avalanche.shared.ActiveProfileUpdater"%> <%@ page import="org.smartfrog.avalanche.core.activeHostProfile.ActiveProfileType"%> +<%@ page import="org.smartfrog.avalanche.shared.XMLHelper" %> <% // Get all hosts @@ -69,7 +66,7 @@ type = updater.getActiveProfile(host); active = type.getHostState().equals("Available"); if (type.getMessagesHistoryArray().length != 0) { - lastMsg = type.getMessagesHistoryArray(type.getMessagesHistoryArray().length-1).getMsg(); + lastMsg = type.getMessagesHistoryArray(type.getMessagesHistoryArray().length - 1).getMsg(); } else { lastMsg = "false"; } @@ -105,16 +102,7 @@ } } - // Convert DOM to XML string - StringWriter sw = new StringWriter(); - StreamResult result = new StreamResult(sw); - Transformer trans = TransformerFactory.newInstance().newTransformer(); - trans.setOutputProperty(OutputKeys.INDENT, "yes"); - trans.transform(new DOMSource(xdoc), result); - String xmlString = sw.toString(); - sw.close(); - // Print output out.clear(); - out.write(xmlString); + out.write(XMLHelper.XMLToString(xdoc)); %> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |