private void writeElement(OutputNode thisNode, Element anyElement) throws Exception {
    thisNode.setName(anyElement.getLocalName());
    thisNode.getAttributes().remove("class");
    if (!getCurrentNamespace(thisNode).equals(anyElement.getNamespaceURI().toString())) {
      thisNode.setAttribute("xmlns", anyElement.getNamespaceURI().toString());
      thisNode.setReference(anyElement.getNamespaceURI().toString());
    }
    NodeList childList = anyElement.getChildNodes();
    boolean childElements = false;
    for (int i = 0; i < childList.getLength(); i++) {
      Node n = childList.item(i);
      if (n instanceof Attr) {
        thisNode.setAttribute(n.getNodeName(), n.getNodeValue());
      }
      if (n instanceof Element) {
        childElements = true;
        Element e = (Element) n;
        writeElement(thisNode.getChild(e.getLocalName()), e);
      }
    }
    if (anyElement.getNodeValue() != null) {
      thisNode.setValue(anyElement.getNodeValue());
    }

    // added to work with harmony ElementImpl... getNodeValue doesn't seem to work!!!
    if (!childElements && anyElement.getTextContent() != null) {
      thisNode.setValue(anyElement.getTextContent());
    }
  }
Пример #2
0
  /** Get value of a given node. */
  public static String getNodeValue(final Element node) {
    if (node == null) return null;
    final String result = node.getNodeValue();
    if (result != null) return result;

    final NodeList list = node.getChildNodes();
    if (list == null || list.getLength() == 0) return null;
    final StringBuffer buff = new StringBuffer();
    boolean counter = false;

    for (int k = 0, listCount = list.getLength(); k < listCount; k++) {
      final Node child = list.item(k);
      if (child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE) {
        buff.append(child.getNodeValue());
        counter = true;
      } else if (child.getNodeType() == Node.ENTITY_REFERENCE_NODE
          || child.getNodeType() == Node.ENTITY_NODE) {
        final Node child2 = child.getFirstChild();
        if (child2 != null && child2.getNodeValue() != null) buff.append(child2.getNodeValue());
        counter = true;
      }
    }

    if (counter == false) return null;
    return buff.toString();
  }
Пример #3
0
  @Override
  public String toString() {
    StringBuffer buffer = new StringBuffer();
    buffer.append("Statename: " + stateName + "\n");
    buffer.append(
        "<" + element.getNodeName() + " " + DomUtils.getAllElementAttributes(element) + ">");
    if (element.getNodeValue() != null) {
      buffer.append(element.getNodeValue());
    }

    buffer.append("\n");
    // buffer.append("</" + element.getNodeName() + ">\n");

    // buffer.append(Helper.getDocumentToString(element.getOwnerDocument()));

    return buffer.toString();
  }
 private boolean isAppleReview(final NodeList reviewNode) {
   if (reviewNode == null) {
     return false;
   }
   Element node = (Element) reviewNode.item(0);
   if (node != null) {
     String flag = node.getNodeValue();
     if (flag != null && flag.equals("true")) {
       return true;
     }
   }
   return false;
 }
  private void parseHtmlTitleTag(Document doc) {

    NodeList headNodeList = doc.getDocumentElement().getElementsByTagName(HTML_TAG_HEAD);
    XRLog.render(Level.FINEST, "headNodeList=" + headNodeList);
    Element rootHeadNodeElement = (Element) headNodeList.item(0);
    NodeList titleNodeList = rootHeadNodeElement.getElementsByTagName(HTML_TAG_TITLE);
    XRLog.render(Level.FINEST, "titleNodeList=" + titleNodeList);
    Element titleElement = (Element) titleNodeList.item(0);
    if (titleElement != null) {
      XRLog.render(Level.FINEST, "titleElement=" + titleElement);
      XRLog.render(Level.FINEST, "titleElement.name=" + titleElement.getTagName());
      XRLog.render(Level.FINEST, "titleElement.value=" + titleElement.getNodeValue());
      XRLog.render(Level.FINEST, "titleElement.content=" + titleElement.getTextContent());
      String titleContent = titleElement.getTextContent();
      PdfName pdfName = PdfName.TITLE;
      PdfString pdfString = new PdfString(titleContent);
      this.pdfInfoValues.put(pdfName, pdfString);
    }
  }
 private boolean isOmitEntity(final NodeList omitConfig, String appVersion) {
   if (appVersion == null || omitConfig == null) {
     return false;
   }
   Element omitConfigNode = (Element) omitConfig.item(0);
   if (omitConfigNode != null) {
     NodeList versionNode =
         omitConfigNode.getElementsByTagNameNS(
             EntityListParser.ENTITY_LIST_XSD_NS, EntityListParser.VERSION);
     if (versionNode != null) {
       Element verisonElement = (Element) versionNode.item(0);
       String versionRegex = verisonElement.getNodeValue();
       if (isMatch(versionRegex, appVersion)) {
         return true;
       }
     }
   }
   return false;
 }
Пример #7
0
  private static void printElem(Element elem, String endient, boolean toplevel, PrintStream pstrm) {
    if (elem == null) {
      return;
    }

    pstrm.print(endient + "<" + elem.getTagName());
    printAttrs(elem, endient, pstrm);
    int count = printChildElems(elem, endient, toplevel, pstrm);
    if (count == 0) {
      String text = elem.getNodeValue();

      if (is_empty(text) == false) {
        pstrm.print(text);
      }

      pstrm.println("</" + elem.getTagName() + ">");
    } else {
      pstrm.println(endient + "</" + elem.getTagName() + ">");
    }
  }
 /**
  * Returns all possible features for the attribute 'attr'. These are exactValue, and existence
  * features.
  *
  * @param attr
  * @return
  */
 private static List<IAttribute> getAllAttributeFeatures(Element attr) {
   List<IAttribute> attList = new LinkedList<IAttribute>();
   attList.add(new Attr_Exists(attr));
   if (attr.getNodeValue() != null) attList.add(new Attr_ExistsWithExactValue(attr));
   return attList;
 }
Пример #9
0
    private String createDefinition(Object runConfigNode) throws Exception {
      Element benchDefNode =
          (Element) xp.evaluate("fd:benchmarkDefinition", runConfigNode, XPathConstants.NODE);

      if (benchDefNode == null) {
        return null;
      }

      String definingClassName = xp.evaluate("fd:driverConfig/@name", runConfigNode);

      // Get the cycleTime annotation
      Element driverConfigNode =
          (Element) xp.evaluate("fd:driverConfig", runConfigNode, XPathConstants.NODE);
      String requestLagTime = getRequestLagTime(driverConfigNode);

      /**
       * Load the template from file Template file is small, but is there a better way to read this?
       */
      String line = null;
      BufferedReader br = null;
      InputStream is = null;

      try {
        ClassLoader cl = this.getClass().getClassLoader();
        is = cl.getResourceAsStream("driver_template");
        br = new BufferedReader(new InputStreamReader(is));
      } catch (Exception ex) {
        // what to do?
        ex.printStackTrace();
        throw new ConfigurationException(ex);
      }

      StringBuilder sb = new StringBuilder();

      while ((line = br.readLine()) != null) {
        if (!line.equals("\n")) {
          sb.append(line).append("\n");
        }
      }

      String template = sb.toString();

      is.close();

      // Obtain the provider.
      TransportProvider provider = TransportProvider.getProvider();

      /**
       * Get the operation token out of the template. Use this to create multiple
       * operations/functions that will replace the token in the java file
       */
      String opTemplate =
          template.substring((template.indexOf("#operation") + 10), template.indexOf("operation#"));
      StringBuilder operations = new StringBuilder();

      Element operationNode = null;
      int i = 1;

      while ((operationNode =
              (Element)
                  xp.evaluate(
                      ("fd:driverConfig/fd:operation[" + i + "]"),
                      runConfigNode,
                      XPathConstants.NODE))
          != null) {

        /*
         * There are many different options here. First, the operation
         * is either POST or GET. In either case, the subst element
         * indicates whether or not random strings are present in the
         * payload data (for backward compatibility, it is assumed
         * that random data is present).
         *
         * For POST, we can read the data from a file, and we can
         * encode the data as form-encoded or binary (octet-stream)
         */
        boolean isPost = false;
        boolean isBinary = false;
        boolean isFile = false;
        boolean doSubst = true;

        String requestLagTimeOverride = getRequestLagTime(operationNode);
        String operationName = xp.evaluate("fd:name", operationNode);
        String url = xp.evaluate("fd:url", operationNode);
        String max90th = xp.evaluate("fd:max90th", operationNode);
        String kbps = xp.evaluate("fd:kbps", operationNode);
        String accept = xp.evaluate("fd:accept", operationNode);

        String requestString = "";

        Element requestNode = (Element) xp.evaluate("fd:get", operationNode, XPathConstants.NODE);

        if (requestNode == null) {
          // Can't have both post & get either, but if both are there,
          // will assume you meant GET.

          requestNode = (Element) xp.evaluate("fd:post", operationNode, XPathConstants.NODE);

          if (requestNode != null) {
            isPost = true;
            if ("true".equalsIgnoreCase(requestNode.getAttributeNS(null, "file"))) {
              isFile = true;
            }
            if ("true".equalsIgnoreCase(requestNode.getAttributeNS(null, "binary"))) {
              isBinary = true;
            }
          } else {
            throw new ConfigurationException("<operation> " + "must have a either a get/post");
          }
          // Can't have both post & get either, but if there,
          // will assume you meant GET.
        }
        if ("false".equalsIgnoreCase(requestNode.getAttributeNS(null, "subst"))) {
          doSubst = false;
        }
        if (isBinary && doSubst) {
          throw new ConfigurationException(
              "<operation> " + "cannot be both binary and perform substitution");
        }
        requestString = requestNode.getNodeValue();
        if (requestString == null) {
          CDATASection cDataNode = (CDATASection) requestNode.getFirstChild();
          if (cDataNode != null) {
            requestString = cDataNode.getNodeValue();
          }
        }
        if (requestString == null) {
          requestString = "";
        }

        if (requestLagTimeOverride == null) {
          requestLagTimeOverride = "";
        }
        if (operationName == null) {
          throw new ConfigurationException("<operation> must have a <name> ");
        }
        if (url == null) {
          throw new ConfigurationException("<operation> must have a <url>");
        }

        RunInfoDefinition rid;
        if (isPost) {
          if (isFile) {
            rid = new RunInfoPostFileDefinition();
          } else {
            rid = new RunInfoPostDataDefinition();
          }
        } else {
          rid = new RunInfoGetDefinition();
        }
        rid.init(doSubst, isBinary, url, requestString, kbps, accept);

        // Create the benchmark Operation annotation
        StringBuilder bmop =
            new StringBuilder("@BenchmarkOperation(name = \"").append(operationName);
        bmop.append("\", percentileLimits={ ")
            .append(max90th)
            .append(", ")
            .append(max90th)
            .append(", ")
            .append(max90th)
            .append("}, ");
        if (provider == TransportProvider.SUN && url.startsWith("https"))
          bmop.append("timing = com.sun.faban.driver.Timing.MANUAL");
        else bmop.append("timing = com.sun.faban.driver.Timing.AUTO");
        bmop.append(")");

        String opTemplateClone = new String(opTemplate);
        // replace tokens with actual content
        opTemplateClone = opTemplateClone.replaceAll("@RequestLagTime@", requestLagTimeOverride);
        opTemplateClone = opTemplateClone.replaceAll("@Operations@", bmop.toString());
        opTemplateClone = opTemplateClone.replaceAll("@operationName@", operationName);
        opTemplateClone = opTemplateClone.replaceAll("@url@", rid.getURL(i));
        opTemplateClone = opTemplateClone.replaceAll("@postRequest@", rid.getPostRequest(i));
        opTemplateClone = opTemplateClone.replaceAll("@Statics@", rid.getStatics(i));
        opTemplateClone = opTemplateClone.replaceAll("@doKbps@", rid.getKbps(i));
        opTemplateClone = opTemplateClone.replaceAll("@doHeaders@", rid.getHeaders(i));
        opTemplateClone = opTemplateClone.replaceAll("@doTiming@", rid.doTiming(i, provider));

        operations.append(opTemplateClone);

        i++;
      }

      String benchmarkDef = getBenchmarkDefinition(benchDefNode);

      // replace tokens in template
      template = template.replaceFirst("#operation(.*\\n*)*operation#", operations.toString());
      template = template.replaceFirst("@RequestLagTime@", requestLagTime);
      template = template.replaceFirst("@BenchmarkDefinition@", benchmarkDef);
      template = template.replaceAll("@DriverClassName@", definingClassName);
      template = template.replaceFirst("@ProviderClass@", provider.providerClass);
      template = template.replaceFirst("@BenchmarkDriverName@", definingClassName);

      String tmpDir = System.getProperty("faban.tmpdir");

      if (tmpDir == null) {
        tmpDir = System.getProperty("java.io.tmpdir");
      }

      if (!tmpDir.endsWith(File.separator)) {
        tmpDir = tmpDir + File.separator;
      }

      String className =
          new StringBuilder(tmpDir).append(definingClassName).append(".java").toString();

      try {
        // convert class name to filename?
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(className)));
        pw.print(template);
        pw.flush();
      } catch (Exception ex) {
        ex.printStackTrace();
        throw new ConfigurationException(ex);
      }

      String classpath = System.getProperty("java.class.path");

      String arg[] = new String[] {"-classpath", classpath, className};
      int errorCode = com.sun.tools.javac.Main.compile(arg);

      if (errorCode != 0) {
        throw new ConfigurationException(
            "unable to compile generated driver file. " + "check output for errors");
      }

      return definingClassName;
    }