PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/jrds/configuration/GraphDescBuilder.java

https://gitlab.com/mesophoyx/jrds
Java | 171 lines | 148 code | 19 blank | 4 comment | 41 complexity | b44bf3a18c272cf5f4e7ea97690b72d1 MD5 | raw file
  1. package jrds.configuration;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import jrds.Graph;
  12. import jrds.GraphDesc;
  13. import jrds.GraphNode;
  14. import jrds.PropertiesManager;
  15. import jrds.factories.xml.JrdsDocument;
  16. import jrds.factories.xml.JrdsElement;
  17. public class GraphDescBuilder extends ConfigObjectBuilder<GraphDesc> {
  18. private final BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
  19. private final Graphics2D g2d = img.createGraphics();
  20. public GraphDescBuilder() {
  21. super(ConfigType.GRAPHDESC);
  22. }
  23. @Override
  24. GraphDesc build(JrdsDocument n) throws InvocationTargetException {
  25. try {
  26. return makeGraphDesc(n);
  27. } catch (Exception e) {
  28. throw new InvocationTargetException(e, GraphDescBuilder.class.getName());
  29. }
  30. }
  31. public GraphDesc makeGraphDesc(JrdsDocument n) throws Exception {
  32. JrdsElement subnode = n.getRootElement();
  33. GraphDesc gd = new GraphDesc();
  34. //Identify the optionnal custom GraphDesc class
  35. JrdsElement graphClass = subnode.getElementbyName("graphClass");
  36. if(graphClass != null) {
  37. String className = graphClass.getTextContent().trim();
  38. if(! "".equals(className)) {
  39. @SuppressWarnings("unchecked")
  40. Class<Graph> clazz = (Class<Graph>) pm.extensionClassLoader.loadClass(className);
  41. // Check a valid constructor
  42. Constructor<Graph> c = clazz.getConstructor(GraphNode.class);
  43. if (c !=null )
  44. gd.setGraphClass(clazz);
  45. else
  46. throw new IllegalArgumentException("Invalid constructor");
  47. }
  48. else {
  49. throw new IllegalArgumentException("Empty graphClass");
  50. }
  51. }
  52. setMethod(subnode.getElementbyName("name"), gd, "setName");
  53. setMethod(subnode.getElementbyName("graphName"), gd, "setGraphName");
  54. setMethod(subnode.getElementbyName("verticalLabel"), gd, "setVerticalLabel");
  55. setMethod(subnode.getElementbyName("graphTitle"), gd, "setGraphTitle");
  56. setMethod(subnode.getElementbyName("upperLimit"), gd, "setUpperLimit", Double.TYPE);
  57. setMethod(subnode.getElementbyName("lowerLimit"), gd, "setLowerLimit", Double.TYPE);
  58. setMethod(subnode.getElementbyName("height"), gd, "setHeight", Integer.TYPE);
  59. setMethod(subnode.getElementbyName("width"), gd, "setWidth", Integer.TYPE);
  60. doACL(gd, n, subnode);
  61. //Vertical label should never be empty
  62. if(gd.getVerticalLabel() == null)
  63. gd.setVerticalLabel("");
  64. gd.setWithLegend(subnode.getElementbyName("nolegend") == null);
  65. gd.setWithSummary(subnode.getElementbyName("novalues") == null);
  66. gd.setLogarithmic(subnode.getElementbyName("logarithmic") != null);
  67. JrdsElement unitElem = subnode.getElementbyName("unit");
  68. if(unitElem != null) {
  69. setMethod(unitElem.getElementbyName("base"), gd, "setUnitExponent");
  70. if(unitElem.getElementbyName("binary") != null)
  71. gd.setSiUnit(false);
  72. else if(unitElem.getElementbyName("SI") != null)
  73. gd.setSiUnit(true);
  74. }
  75. for(JrdsElement addnode: subnode.getChildElements()) {
  76. if(! "add".equals(addnode.getNodeName()) && ! "addpath".equals(addnode.getNodeName()) )
  77. continue;
  78. Map<String, String> elements = new HashMap<String, String>(10);
  79. boolean withPath = false;
  80. for(JrdsElement child: addnode.getChildElements()) {
  81. if("path".equals(child.getNodeName())) {
  82. withPath = true;
  83. for(JrdsElement hostchild: child.getChildElements()) {
  84. String key = hostchild.getNodeName();
  85. String value = hostchild.getTextContent();
  86. if(value != null) {
  87. value = value.trim();
  88. }
  89. elements.put("path" + key, value);
  90. }
  91. }
  92. else {
  93. String key = child.getNodeName();
  94. String value = child.getTextContent();
  95. if(value != null) {
  96. value = value.trim();
  97. }
  98. elements.put(key, value);
  99. }
  100. }
  101. String addName = elements.get("name");
  102. String addgraphType = elements.get("graphType");
  103. String addColor = elements.get("color");
  104. String addLegend = elements.get("legend");
  105. String addrpn = elements.get("rpn");
  106. String consFunc = elements.get("cf");
  107. String reversed = elements.get("reversed");
  108. String percentile = elements.get("percentile");
  109. if(elements.containsKey("percentile") && "".equals(percentile))
  110. percentile="95";
  111. String host = null;
  112. String probe = null;
  113. String dsName;
  114. if(withPath) {
  115. host = elements.get("pathhost");
  116. probe = elements.get("pathprobe");
  117. dsName = elements.get("pathdsName");
  118. }
  119. else
  120. dsName = elements.get("dsName");
  121. gd.add(addName, addrpn, addgraphType, addColor, addLegend, consFunc, reversed, percentile, host, probe, dsName);
  122. }
  123. gd.setTree(PropertiesManager.HOSTSTAB, enumerateTree(subnode.getElementbyName("hosttree")));
  124. gd.setTree(PropertiesManager.VIEWSTAB, enumerateTree(subnode.getElementbyName("viewtree")));
  125. for(JrdsElement treenode: subnode.getChildElementsByName("tree")) {
  126. String treetab = treenode.getAttribute("tab");
  127. gd.setTree(treetab, enumerateTree(treenode));
  128. }
  129. // If the class is not jrds.Graph, it's difficult to evaluate dimensions
  130. if(gd.getGraphClass() == jrds.Graph.class) {
  131. gd.initializeLimits(g2d);
  132. }
  133. return gd;
  134. }
  135. List<Object> enumerateTree(JrdsElement roottree) {
  136. if(roottree == null)
  137. return Collections.emptyList();
  138. List<JrdsElement> path = roottree.getChildElements();
  139. if(path.isEmpty())
  140. return Collections.emptyList();
  141. List<Object> pathString = new ArrayList<Object>(path.size());
  142. for(JrdsElement te: path) {
  143. Object value;
  144. if("pathelement".equals(te.getNodeName()))
  145. value = GraphDesc.resolvPathElement(te.getTextContent());
  146. else
  147. value = te.getTextContent();
  148. pathString.add(value);
  149. }
  150. return pathString;
  151. }
  152. }