public String getDocument(boolean toScreen) {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   Dom4jUtils.writeDocument(rootElement.getDocument(), baos, PrintType.PRETTY);
   if (toScreen) {
     System.out.println(baos.toString());
   }
   return baos.toString();
 }
Example #2
0
  /**
   * This method generates the time series for the methods and puts it in an HTML string. The
   * methods are sorted according to the greatest average time for each method.
   *
   * @param dataBase the database to aggregate the statistics for
   * @return the html representing the series of timings for the method
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  protected String methodSeries(final IDataBase dataBase) {
    Comparator<Method> comparator =
        new Comparator<Method>() {
          public int compare(Method o1, Method o2) {
            Double o1Average = calculator.averageMethodTime(o1);
            Double o2Average = calculator.averageMethodTime(o2);
            // We want a descending table, i.e. the most expensive at the top
            return o2Average.compareTo(o1Average);
          }
        };
    Set<Method> sortedMethods = new TreeSet<>(comparator);
    List<Method> methods = dataBase.find(Method.class);
    sortedMethods.addAll(methods);

    List<Snapshot<?, ?>> snapshots =
        methods.size() > 0 ? methods.get(0).getSnapshots() : new ArrayList<Snapshot<?, ?>>();
    Element tableElement = tableElement(snapshots);

    for (Method method : sortedMethods) {
      Class<?, ?> klass = (Class<?, ?>) method.getParent();
      String className = klass.getName();
      String methodName = method.getName();

      Element rowElement = addElement(tableElement, "tr", null);
      addElement(rowElement, "td", className);
      addElement(rowElement, "td", methodName);
      addElement(rowElement, "td", Double.toString(calculator.averageMethodTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.averageMethodNetTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.totalMethodTime(method)));
      addElement(rowElement, "td", Double.toString(calculator.totalNetMethodTime(method)));
      addElement(rowElement, "td", Integer.toString(method.getInvocations()));

      Element dataElement = addElement(rowElement, "td", null);
      Element imageElement = addElement(dataElement, "img", null);
      // Add the method series graph for the average and total time for the method
      List<Double> methodSeries = calculator.methodSeries(method);
      String url = buildGraph(IConstants.METHOD_SERIES, method, methodSeries);
      addAttributes(imageElement, new String[] {"src"}, new String[] {url});

      dataElement = addElement(rowElement, "td", null);
      imageElement = addElement(dataElement, "img", null);
      // Add the method change graph, i.e. the change in the average time for the method
      List<Double> methodChangeSeries = calculator.methodChangeSeries(method);
      url = buildGraph(IConstants.METHOD_CHANGE_SERIES, method, methodChangeSeries);
      addAttributes(imageElement, new String[] {"src"}, new String[] {url});
    }

    Document document = tableElement.getDocument();
    return prettyPrint(document);
  }
 protected void decorateDCStream() {
   Element rootElement = DocumentHelper.createElement(new QName("dc", Namespaces.oai_dc));
   rootElement.add(Namespaces.dc);
   rootElement.add(Namespaces.xsi);
   Element title = rootElement.addElement(new QName("title", Namespaces.dc));
   title.addText(getLabel());
   Element identifier = rootElement.addElement(new QName("identifier", Namespaces.dc));
   identifier.setText(getPid());
   Element type = rootElement.addElement(new QName("type", Namespaces.dc));
   type.addText(
       "model:"
           + getModel().getValue().substring(0, 1)
           + getModel().getValue().substring(1).toLowerCase());
   Element rights = rootElement.addElement(new QName("rights", Namespaces.dc));
   rights.addText("policy:" + getPolicy().toString().toLowerCase());
   appendDatastream(DATASTREAM_CONTROLGROUP.X, DATASTREAM_ID.DC, rootElement, null, null);
   dcXmlContent = rootElement.getDocument();
 }
  /**
   * Render with a localized stylesheet. The localized stylesheet is addressed by its name with
   * appended locale. E.g. mystyle.xsl in DE locale is addressed by mystyle_de.xsl
   *
   * @param node The node to render
   * @param styleSheetName The stylesheet to use.
   * @return Results of XSL transformation
   */
  private StringBuilder render(final Element node) {
    try {
      Document doc = node.getDocument();
      if (doc == null) {
        doc = new DOMDocument();
        doc.add(node);
      }
      final DocumentSource xmlsource = new DocumentSource(node);

      // ByteArrayOutputStream baos = new ByteArrayOutputStream();
      final StringWriter sw = new StringWriter();
      final StreamResult result = new StreamResult(sw);
      synchronized (transformer) { // o_clusterOK by:fj transformer is per vm
        transformer.transform(xmlsource, result);
      }
      final String res = sw.toString();
      return new StringBuilder(res); // .append(result.getOutputStream());
    } catch (final Exception e) {
      throw new OLATRuntimeException(LocalizedXSLTransformer.class, "Error transforming XML.", e);
    }
  }