|
From: <sa...@us...> - 2004-02-25 15:43:10
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11738/org/jrobin/core Modified Files: RrdDefTemplate.java Util.java XmlTemplate.java Log Message: XML templates revisited (80% done) Index: RrdDefTemplate.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdDefTemplate.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** RrdDefTemplate.java 23 Feb 2004 15:49:46 -0000 1.1 --- RrdDefTemplate.java 25 Feb 2004 15:28:05 -0000 1.2 *************** *** 211,269 **** } ! /* public static void main(String[] args) throws RrdException, IOException { ! String s = ! "<rrd_def> " + ! " <path>${path}</path> " + ! " <start>1000000000</start> " + ! " <step>${step}</step> " + ! " <datasource> " + ! " <name>input</name> " + ! " <type>COUNTER</type> " + ! " <heartbeat>300</heartbeat> " + ! " <min>10</min> " + ! " <max>U</max> " + ! " </datasource> " + ! " <datasource> " + ! " <name>temperature</name> " + ! " <type>GAUGE</type> " + ! " <heartbeat>400</heartbeat> " + ! " <min>U</min> " + ! " <max>1000</max> " + ! " </datasource> " + ! " <archive> " + ! " <cf>AVERAGE</cf> " + ! " <xff>0.5</xff> " + ! " <steps>1</steps> " + ! " <rows>${rows}</rows> " + ! " </archive> " + ! " <archive> " + ! " <cf>MAX</cf> " + ! " <xff>0.6</xff> " + ! " <steps>6</steps> " + ! " <rows>7000</rows> " + ! " </archive> " + ! "</rrd_def> "; ! RrdDefTemplate t = new RrdDefTemplate(s); ! // works as well: ! // File f = new File("test.xml"); ! // RrdDefTemplate t = new RrdDefTemplate(f); t.setMapping("path", "test1.rrd"); t.setMapping("step", 310); ! t.setMapping("rows", 888); RrdDef def = t.getRrdDef(); System.out.println(def.dump()); - new RrdDb(def).close(); - - t.setMapping("path", "test2.rrd"); - t.setMapping("step", 320); - t.setMapping("rows", 999); - def = t.getRrdDef(); - System.out.println(def.dump()); - new RrdDb(def).close(); } - */ } --- 211,226 ---- } ! public static void main(String[] args) throws RrdException, IOException { ! File f = new File("work/test.xml"); ! RrdDefTemplate t = new RrdDefTemplate(f); t.setMapping("path", "test1.rrd"); t.setMapping("step", 310); ! t.setMapping("hb", 123); RrdDef def = t.getRrdDef(); System.out.println(def.dump()); } } Index: Util.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Util.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** Util.java 23 Feb 2004 15:48:42 -0000 1.9 --- Util.java 25 Feb 2004 15:28:05 -0000 1.10 *************** *** 43,46 **** --- 43,47 ---- import java.util.ArrayList; import java.io.*; + import java.awt.*; /** *************** *** 136,139 **** --- 137,163 ---- } + /** + * Returns <code>GregorianCalendar</code> object for the given timestamp + * (in seconds, without milliseconds) + * @param timestamp Timestamp in seconds. + * @return Corresponding GregorianCalendar object. + */ + public static GregorianCalendar getGregorianCalendar(long timestamp) { + GregorianCalendar gc = new GregorianCalendar(); + gc.setTimeInMillis(timestamp * 1000L); + return gc; + } + + /** + * Returns <code>GregorianCalendar</code> object for the given Date object + * @param date Date object + * @return Corresponding GregorianCalendar object. + */ + public static GregorianCalendar getGregorianCalendar(Date date) { + GregorianCalendar gc = new GregorianCalendar(); + gc.setTime(date); + return gc; + } + /** * Returns timestamp (unix epoch) for the given Date object *************** *** 142,146 **** */ public static long getTimestamp(Date date) { ! return (date.getTime() + 500L) / 1000L; } --- 166,170 ---- */ public static long getTimestamp(Date date) { ! return (date.getTime() + 499L) / 1000L; } *************** *** 190,193 **** --- 214,225 ---- } + static boolean parseBoolean(String valueStr) { + return valueStr.equalsIgnoreCase("true") || + valueStr.equalsIgnoreCase("on") || + valueStr.equalsIgnoreCase("yes") || + valueStr.equalsIgnoreCase("y") || + valueStr.equalsIgnoreCase("1"); + } + private static final File homeDirFile; private static final String homeDirPath; *************** *** 256,260 **** --- 288,299 ---- } + /** + * Various DOM utility functions + */ static class Xml { + static Node[] getChildNodes(Node parentNode) { + return getChildNodes(parentNode, null); + } + static Node[] getChildNodes(Node parentNode, String childName) { ArrayList nodes = new ArrayList(); *************** *** 262,266 **** for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); ! if (node.getNodeName().equals(childName)) { nodes.add(node); } --- 301,305 ---- for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); ! if (childName == null || node.getNodeName().equals(childName)) { nodes.add(node); } *************** *** 277,280 **** --- 316,324 ---- } + static boolean hasChildNode(Node parentNode, String childName) { + Node[] childs = getChildNodes(parentNode, childName); + return childs.length > 0; + } + static String getChildValue(Node parentNode, String childName) throws RrdException { NodeList children = parentNode.getChildNodes(); *************** *** 282,286 **** Node child = children.item(i); if (child.getNodeName().equals(childName)) { ! return child.getFirstChild().getNodeValue().trim(); } } --- 326,330 ---- Node child = children.item(i); if (child.getNodeName().equals(childName)) { ! return getValue(child); } } *************** *** 288,291 **** --- 332,339 ---- } + static String getValue(Node node) { + return node.getFirstChild().getNodeValue().trim(); + } + static int getChildValueAsInt(Node parentNode, String childName) throws RrdException { String valueStr = getChildValue(parentNode, childName); *************** *** 293,296 **** --- 341,349 ---- } + static int getValueAsInt(Node node) { + String valueStr = getValue(node); + return Integer.parseInt(valueStr); + } + static long getChildValueAsLong(Node parentNode, String childName) throws RrdException { String valueStr = getChildValue(parentNode, childName); *************** *** 298,301 **** --- 351,359 ---- } + static long getValueAsLong(Node node) { + String valueStr = getValue(node); + return Long.parseLong(valueStr); + } + static double getChildValueAsDouble(Node parentNode, String childName) throws RrdException { String valueStr = getChildValue(parentNode, childName); *************** *** 303,306 **** --- 361,379 ---- } + static double getValueAsDouble(Node node) { + String valueStr = getValue(node); + return Util.parseDouble(valueStr); + } + + static boolean getChildValueAsBoolean(Node parentNode, String childName) throws RrdException { + String valueStr = getChildValue(parentNode, childName); + return Util.parseBoolean(valueStr); + } + + static boolean getValueAsBoolean(Node node) { + String valueStr = getValue(node); + return Util.parseBoolean(valueStr); + } + static Element getRootElement(InputSource inputSource) throws RrdException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Index: XmlTemplate.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/XmlTemplate.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** XmlTemplate.java 23 Feb 2004 15:49:47 -0000 1.1 --- XmlTemplate.java 25 Feb 2004 15:28:05 -0000 1.2 *************** *** 32,80 **** import java.io.File; import java.util.HashMap; /** * Class used as a base class for various XML template related classes. Class provides * methods for XML source parsing and XML tree traversing. XML source may have unlimited ! * number of placeholders in the format <code>${placeholder_name}</code>. Methods are provided ! * to specify values of placeholders at runtime. Note that this class has limited functionality: ! * XML source gets parsed, and placeholder values are collected. You have to extend this class ! * to do anything more useful.<p> */ public abstract class XmlTemplate { - /** - * root element of the DOM hierarchy representing XML source - */ protected Element root; private HashMap valueMap = new HashMap(); - /** - * Creates XmlTemplate object from any parsable XML input source - * @param xmlSource Any parsable XML input source - * @throws IOException thrown in case of I/O error - * @throws RrdException thrown (in most cases) when the source has invalid XML syntax - * (cannot be parsed at all) - */ protected XmlTemplate(InputSource xmlSource) throws IOException, RrdException { root = Util.Xml.getRootElement(xmlSource); } - /** - * Creates XmlTemplate object from a string containing properly formatted XML - * @param xmlString parsable XML string - * @throws IOException thrown in case of I/O error - * @throws RrdException thrown (in most cases) when the source has invalid XML syntax - * (cannot be parsed at all) - */ protected XmlTemplate(String xmlString) throws IOException, RrdException { root = Util.Xml.getRootElement(xmlString); } - /** - * Creates XmlTemplate object from a file containing properly formatted XML - * @param xmlFile File object representing file containing parsable XML source - * @throws IOException thrown in case of I/O error - * @throws RrdException thrown (in most cases) when the source has invalid XML syntax - * (cannot be parsed at all) - */ protected XmlTemplate(File xmlFile) throws IOException, RrdException { root = Util.Xml.getRootElement(xmlFile); --- 32,61 ---- import java.io.File; import java.util.HashMap; + import java.util.HashSet; + import java.util.Date; + import java.util.GregorianCalendar; + import java.awt.*; /** * Class used as a base class for various XML template related classes. Class provides * methods for XML source parsing and XML tree traversing. XML source may have unlimited ! * number of placeholders (variables) in the format <code>${variable_name}</code>. ! * Methods are provided to specify variable values at runtime. ! * Note that this class has limited functionality: XML source gets parsed, and variable ! * values are collected. You have to extend this class to do something more useful.<p> */ public abstract class XmlTemplate { protected Element root; private HashMap valueMap = new HashMap(); + private HashSet validatedNodes = new HashSet(); protected XmlTemplate(InputSource xmlSource) throws IOException, RrdException { root = Util.Xml.getRootElement(xmlSource); } protected XmlTemplate(String xmlString) throws IOException, RrdException { root = Util.Xml.getRootElement(xmlString); } protected XmlTemplate(File xmlFile) throws IOException, RrdException { root = Util.Xml.getRootElement(xmlFile); *************** *** 89,97 **** /** ! * Sets value for the given placeholder. Placeholder should be supplied by its name. ! * For example, for a placeholder <code>${param}</code> specify just <code>param</code> ! * for its name. ! * @param name Name of the placeholder (placeholder without leading '${' and ending '}'). ! * @param value Value to replace placeholder with. */ public void setMapping(String name, String value) { --- 70,78 ---- /** ! * Sets value for a single XML template variable. Variable name should be specified ! * without leading '${' and ending '}' placeholder markers. For example, for a placeholder ! * <code>${start}</code>, specify <code>start</start> for the <code>name</code> parameter. ! * @param name variable name ! * @param value value to be set in the XML template */ public void setMapping(String name, String value) { *************** *** 100,108 **** /** ! * Sets value for the given placeholder. Placeholder should be supplied by its name. ! * For example, for a placeholder <code>${param}</code> specify just <code>param</code> ! * for its name. ! * @param name Name of the placeholder (placeholder without leading '${' and ending '}'). ! * @param value Value to replace placeholder with. */ public void setMapping(String name, int value) { --- 81,89 ---- /** ! * Sets value for a single XML template variable. Variable name should be specified ! * without leading '${' and ending '}' placeholder markers. For example, for a placeholder ! * <code>${start}</code>, specify <code>start</start> for the <code>name</code> parameter. ! * @param name variable name ! * @param value value to be set in the XML template */ public void setMapping(String name, int value) { *************** *** 111,119 **** /** ! * Sets value for the given placeholder. Placeholder should be supplied by its name. ! * For example, for a placeholder <code>${param}</code> specify just <code>param</code> ! * for its name. ! * @param name Name of the placeholder (placeholder without leading '${' and ending '}'). ! * @param value Value to replace placeholder with. */ public void setMapping(String name, long value) { --- 92,100 ---- /** ! * Sets value for a single XML template variable. Variable name should be specified ! * without leading '${' and ending '}' placeholder markers. For example, for a placeholder ! * <code>${start}</code>, specify <code>start</start> for the <code>name</code> parameter. ! * @param name variable name ! * @param value value to be set in the XML template */ public void setMapping(String name, long value) { *************** *** 122,130 **** /** ! * Sets value for the given placeholder. Placeholder should be supplied by its name. ! * For example, for a placeholder <code>${param}</code> specify just <code>param</code> ! * for its name. ! * @param name Name of the placeholder (placeholder without leading '${' and ending '}'). ! * @param value Value to replace placeholder with. */ public void setMapping(String name, double value) { --- 103,111 ---- /** ! * Sets value for a single XML template variable. Variable name should be specified ! * without leading '${' and ending '}' placeholder markers. For example, for a placeholder ! * <code>${start}</code>, specify <code>start</start> for the <code>name</code> parameter. ! * @param name variable name ! * @param value value to be set in the XML template */ public void setMapping(String name, double value) { *************** *** 133,178 **** /** ! * Returns all child nodes with the given tag ! * belonging to the specified parent Node ! * @param parentNode Parent node ! * @param childName Child node tag ! * @return Array of child nodes with the specified parent and with the given tag name */ ! protected static Node[] getChildNodes(Node parentNode, String childName) { ! return Util.Xml.getChildNodes(parentNode, childName); } /** ! * Returns the first child node with the given parent and the given tag. ! * @param parentNode Parent node ! * @param childName Child node tag ! * @return First child node with the given parent node and the given tag. ! * @throws RrdException Thrown if no such child can be found */ ! protected static Node getFirstChildNode(Node parentNode, String childName) throws RrdException { ! return Util.Xml.getFirstChildNode(parentNode, childName); } /** ! * Returns the 'value' of the child node with the given parent and the given tag name. ! * For example, in a DOM-tree created from the following XML source:<p> ! * <pre> ! * ...<root><branch>abc</branch></root>... ! * </pre> ! * and assuming that <code>parentNode</code> points to the <code><root></code> element, ! * the following call:<p> ! * <pre> ! * getChildValue(parentNode, "branch"); ! * </pre> ! * returns:<p> ! * <code>abc</code><p> ! * @param parentNode Parent DOM node ! * @param childName Child node tag ! * @return XML 'value' of the child node (trimmed content between <childName> and </childName> ! * tags) ! * @throws RrdException Thrown if no such child node exists. */ protected String getChildValue(Node parentNode, String childName) throws RrdException { String value = Util.Xml.getChildValue(parentNode, childName); if(value.startsWith("${") && value.endsWith("}")) { // template variable found, remove leading "${" and trailing "}" --- 114,187 ---- /** ! * Sets value for a single XML template variable. Variable name should be specified ! * without leading '${' and ending '}' placeholder markers. For example, for a placeholder ! * <code>${start}</code>, specify <code>start</start> for the <code>name</code> parameter. ! * @param name variable name ! * @param value value to be set in the XML template */ ! public void setMapping(String name, Color value) { ! valueMap.put(name, "#" + Integer.toHexString(value.getRGB() & 0xFFFFFF)); } /** ! * Sets value for a single XML template variable. Variable name should be specified ! * without leading '${' and ending '}' placeholder markers. For example, for a placeholder ! * <code>${start}</code>, specify <code>start</start> for the <code>name</code> parameter. ! * @param name variable name ! * @param value value to be set in the XML template */ ! public void setMapping(String name, Date value) { ! setMapping(name, Util.getTimestamp(value)); } /** ! * Sets value for a single XML template variable. Variable name should be specified ! * without leading '${' and ending '}' placeholder markers. For example, for a placeholder ! * <code>${start}</code>, specify <code>start</start> for the <code>name</code> parameter. ! * @param name variable name ! * @param value value to be set in the XML template */ + public void setMapping(String name, GregorianCalendar value) { + setMapping(name, Util.getTimestamp(value)); + } + + /** + * Sets value for a single XML template variable. Variable name should be specified + * without leading '${' and ending '}' placeholder markers. For example, for a placeholder + * <code>${start}</code>, specify <code>start</start> for the <code>name</code> parameter. + * @param name variable name + * @param value value to be set in the XML template + */ + public void setMapping(String name, boolean value) { + valueMap.put(name, "" + value); + } + + protected static Node[] getChildNodes(Node parentNode, String childName) { + return Util.Xml.getChildNodes(parentNode, childName); + } + + protected static Node[] getChildNodes(Node parentNode) { + return Util.Xml.getChildNodes(parentNode, null); + } + + protected static Node getFirstChildNode(Node parentNode, String childName) throws RrdException { + return Util.Xml.getFirstChildNode(parentNode, childName); + } + + protected boolean hasChildNode(Node parentNode, String childName) { + return Util.Xml.hasChildNode(parentNode, childName); + } + protected String getChildValue(Node parentNode, String childName) throws RrdException { String value = Util.Xml.getChildValue(parentNode, childName); + return resolveMappings(value); + } + + protected String getValue(Node parentNode) { + String value = Util.Xml.getValue(parentNode); + return resolveMappings(value); + } + + private String resolveMappings(String value) { if(value.startsWith("${") && value.endsWith("}")) { // template variable found, remove leading "${" and trailing "}" *************** *** 184,188 **** else { // no mapping found - this is illegal ! throw new RrdException("No mapping found for template variable " + value); } } --- 193,197 ---- else { // no mapping found - this is illegal ! throw new IllegalArgumentException("No mapping found for template variable " + value); } } *************** *** 190,265 **** } ! /** ! * Returns the 'value' of the child node with the given parent and the given tag name. ! * For example, in a DOM-tree created from the following XML source:<p> ! * <pre> ! * ...<root><branch>123</branch></root>... ! * </pre> ! * and assuming that <code>parentNode</code> points to the <code><root></code> element, ! * the following call:<p> ! * <pre> ! * getChildValue(parentNode, "branch"); ! * </pre> ! * returns:<p> ! * <code>123</code><p> ! * @param parentNode Parent DOM node ! * @param childName Child node tag ! * @return XML 'value' of the child node (trimmed content between <childName> and </childName> ! * tags) ! * @throws RrdException Thrown if no such child node exists. ! */ ! protected int getChildValueAsInt(Node parentNode, String childName) ! throws RrdException, NumberFormatException { String valueStr = getChildValue(parentNode, childName); return Integer.parseInt(valueStr); } ! /** ! * Returns the 'value' of the child node with the given parent and the given tag name. ! * For example, in a DOM-tree created from the following XML source:<p> ! * <pre> ! * ...<root><branch>123</branch></root>... ! * </pre> ! * and assuming that <code>parentNode</code> points to the <code><root></code> element, ! * the following call:<p> ! * <pre> ! * getChildValue(parentNode, "branch"); ! * </pre> ! * returns:<p> ! * <code>123</code><p> ! * @param parentNode Parent DOM node ! * @param childName Child node tag ! * @return XML 'value' of the child node (trimmed content between <childName> and </childName> ! * tags) ! * @throws RrdException Thrown if no such child node exists. ! */ ! protected long getChildValueAsLong(Node parentNode, String childName) ! throws RrdException, NumberFormatException { String valueStr = getChildValue(parentNode, childName); return Long.parseLong(valueStr); } ! /** ! * Returns the 'value' of the child node with the given parent and the given tag name. ! * For example, in a DOM-tree created from the following XML source:<p> ! * <pre> ! * ...<root><branch>123.45</branch></root>... ! * </pre> ! * and assuming that <code>parentNode</code> points to the <code><root></code> element, ! * the following call:<p> ! * <pre> ! * getChildValue(parentNode, "branch"); ! * </pre> ! * returns:<p> ! * <code>123.45</code><p> ! * @param parentNode Parent DOM node ! * @param childName Child node tag ! * @return XML 'value' of the child node (trimmed content between <childName> and </childName> ! * tags) ! * @throws RrdException Thrown if no such child node exists. ! */ protected double getChildValueAsDouble(Node parentNode, String childName) throws RrdException { String valueStr = getChildValue(parentNode, childName); return Util.parseDouble(valueStr); } } --- 199,268 ---- } ! protected int getChildValueAsInt(Node parentNode, String childName) throws RrdException { String valueStr = getChildValue(parentNode, childName); return Integer.parseInt(valueStr); } ! protected int getValueAsInt(Node parentNode) { ! String valueStr = getValue(parentNode); ! return Integer.parseInt(valueStr); ! } ! ! protected long getChildValueAsLong(Node parentNode, String childName) throws RrdException { String valueStr = getChildValue(parentNode, childName); return Long.parseLong(valueStr); } ! protected long getValueAsLong(Node parentNode) { ! String valueStr = getValue(parentNode); ! return Long.parseLong(valueStr); ! } ! protected double getChildValueAsDouble(Node parentNode, String childName) throws RrdException { String valueStr = getChildValue(parentNode, childName); return Util.parseDouble(valueStr); } + + protected double getValueAsDouble(Node parentNode) { + String valueStr = getValue(parentNode); + return Util.parseDouble(valueStr); + } + + protected boolean getChildValueAsBoolean(Node parentNode, String childName) throws RrdException { + String valueStr = getChildValue(parentNode, childName); + return Util.parseBoolean(valueStr); + } + + protected boolean getValueAsBoolean(Node parentNode) { + String valueStr = getValue(parentNode); + return Util.parseBoolean(valueStr); + } + + protected boolean isEmptyNode(Node node) { + // comment node or empty text node + return node.getNodeName().equals("#comment") || + (node.getNodeName().equals("#text") && node.getNodeValue().trim().length() == 0); + } + + protected void validateOnce(Node parentNode, String[] allowedChildNames) throws RrdException { + // validate node only once + if(validatedNodes.contains(parentNode)) { + //System.out.println("Already validated"); + return; + } + Node[] childs = getChildNodes(parentNode); + main: + for(int i = 0; i < childs.length; i++) { + String childName = childs[i].getNodeName(); + for(int j = 0; j < allowedChildNames.length; j++) { + if(childName.equals(allowedChildNames[j])) { + continue main; + } + } + if(!isEmptyNode(childs[i])) { + throw new RrdException("Unexpected tag encountered: <" + childName + ">"); + } + } + validatedNodes.add(parentNode); + } } |