Пример #1
0
  private void processSelectSource(TagNode formNode, FormFlow formFlow)
      throws XPatherException, ResourceLoaderException {
    Object[] dynamicSelectNodes =
        formNode.evaluateXPath("//select[@" + Constants.SELECT_SOURCE_ATTR + "]");
    for (Object dynamicSelectNodeO : dynamicSelectNodes) {
      TagNode dynamicSelectNode = (TagNode) dynamicSelectNodeO;
      String name = dynamicSelectNode.getAttributeByName(Constants.NAME_ATTR);
      String source = dynamicSelectNode.getAttributeByName(Constants.SELECT_SOURCE_ATTR);
      source = formFlow.resolveResourcePathIfRelative(source);
      String preselectFirstOption =
          dynamicSelectNode.getAttributeByName(Constants.SELECT_PRESELECT_FIRST_OPTION_ATTR);
      dynamicSelectNode.removeAttribute(Constants.SELECT_SOURCE_ATTR);
      dynamicSelectNode.removeAttribute(Constants.SELECT_PRESELECT_FIRST_OPTION_ATTR);
      logger.debug("Found dynamicSelectNode name:{}, source:{}", name, source);

      List<SelectOptionPojo> options = selectOptionHelper.loadOptions(source);
      if (!"true".equals(preselectFirstOption)) {
        options.add(0, new SelectOptionPojo("-- Please Select --", ""));
      }
      for (SelectOptionPojo selectOptionPojo : options) {
        TagNode optionNode = new TagNode("option");
        String value = selectOptionPojo.getValue();
        if (value != null) {
          optionNode.setAttribute("value", value);
        }
        optionNode.addChild(new ContentNode(selectOptionPojo.getText()));
        dynamicSelectNode.addChild(optionNode);
      }
    } // TODO: validate that submitted value comes from the list
  }
Пример #2
0
  private void processInputSourceFields(TagNode formNode, String currentPath, FormFlow formFlow)
      throws XPatherException {
    Object[] autoCompleteNodes =
        formNode.evaluateXPath("//input[@" + Constants.SELECT_SOURCE_ATTR + "]");
    for (Object autoCompleteNodeO : autoCompleteNodes) {
      TagNode autoCompleteNode = (TagNode) autoCompleteNodeO;
      String fieldName = autoCompleteNode.getAttributeByName(Constants.NAME_ATTR);
      String source = autoCompleteNode.getAttributeByName(Constants.INPUT_SOURCE_ATTR);

      FieldSourceProxy fieldSourceProxy =
          proxyFactory.createFlowProxy(currentPath, fieldName, source);
      formFlow.addFieldSourceProxy(fieldSourceProxy);
      autoCompleteNode.removeAttribute(Constants.INPUT_SOURCE_ATTR);
      autoCompleteNode.setAttribute(
          "rf.source", "rhinoforms/proxy/" + fieldSourceProxy.getProxyPath());
    }
  }
  private static TagNode convertVideos(TagNode contentNode) {
    TagNode[] videoNodes = contentNode.getElementsByAttValue("class", "bbcode_video", true, true);
    TagNode[] youtubeNodes =
        contentNode.getElementsByAttValue("class", "youtube-player", true, true);
    for (TagNode youTube : youtubeNodes) {
      String src = youTube.getAttributeByName("src");
      int height = Integer.parseInt(youTube.getAttributeByName("height"));
      int width = Integer.parseInt(youTube.getAttributeByName("width"));
      Matcher youtube = youtubeHDId_regex.matcher(src);
      if (youtube.find()) {
        String videoId = youtube.group(1);
        String link = "http://www.youtube.com/watch?v=" + videoId;
        String image = "http://img.youtube.com/vi/" + videoId + "/0.jpg";
        youTube.setName("a");
        youTube.setAttribute("href", link);
        youTube.removeAttribute("type");
        youTube.removeAttribute("frameborder");
        youTube.removeAttribute("src");
        youTube.removeAttribute("height");
        youTube.removeAttribute("width");
        youTube.setAttribute(
            "style",
            "background-image:url("
                + image
                + ");background-size:cover;background-repeat:no-repeat;background-position:center; position:relative;display:block;text-align:center; width:"
                + width
                + "; height:"
                + height);
        TagNode img = new TagNode("img");
        img.setAttribute("class", "nolink videoPlayButton");
        img.setAttribute("src", "file:///android_res/drawable/ic_menu_video.png");
        img.setAttribute(
            "style", "position:absolute;top:50%;left:50%;margin-top:-16px;margin-left:-16px;");
        youTube.addChild(img);
      }
    }
    for (TagNode node : videoNodes) {
      try {
        String src = null;
        int height = 0;
        int width = 0;
        TagNode[] object = node.getElementsByName("object", false);
        if (object.length > 0) {
          height = Integer.parseInt(object[0].getAttributeByName("height"));
          width = Integer.parseInt(object[0].getAttributeByName("width"));
          TagNode[] emb = object[0].getElementsByName("embed", true);
          if (emb.length > 0) {
            src = emb[0].getAttributeByName("src");
          }
        }
        if (src != null && height != 0 && width != 0) {
          String link = null, image = null;
          Matcher youtube = youtubeId_regex.matcher(src);
          Matcher vimeo = vimeoId_regex.matcher(src);
          if (youtube
              .find()) { // we'll leave in the old youtube code in case something gets reverted
            String videoId = youtube.group(1);
            link = "http://www.youtube.com/watch?v=" + videoId;
            image = "http://img.youtube.com/vi/" + videoId + "/0.jpg";
          } else if (vimeo.find()) {
            String videoId = vimeo.group(1);
            TagNode vimeoXML;
            try {
              vimeoXML = NetworkUtils.get("http://vimeo.com/api/v2/video/" + videoId + ".xml");
            } catch (Exception e) {
              e.printStackTrace();
              continue;
            }
            if (vimeoXML.findElementByName("mobile_url", true) != null) {
              link = vimeoXML.findElementByName("mobile_url", true).getText().toString();
            } else {
              link = vimeoXML.findElementByName("url", true).getText().toString();
            }
            image = vimeoXML.findElementByName("thumbnail_large", true).getText().toString();
          } else {
            node.removeAllChildren();
            TagNode ln = new TagNode("a");
            ln.setAttribute("href", src);
            ln.addChild(new ContentNode(src));
            node.addChild(ln);
            continue;
          }
          node.removeAllChildren();
          node.setAttribute(
              "style",
              "background-image:url("
                  + image
                  + ");background-size:cover;background-repeat:no-repeat;background-position:center; position:relative;text-align:center; width:"
                  + width
                  + "; height:"
                  + height);
          node.setAttribute("onclick", "location.href=\"" + link + "\"");
          TagNode img = new TagNode("img");
          img.setAttribute("class", "nolink videoPlayButton");
          img.setAttribute("src", "file:///android_res/drawable/ic_menu_video.png");
          img.setAttribute(
              "style", "position:absolute;top:50%;left:50%;margin-top:-23px;margin-left:-32px;");
          node.addChild(img);
        }
      } catch (Exception e) {
        continue; // if we fail to convert the video tag, we can still display the rest.
      }
    }

    return contentNode;
  }
Пример #4
0
  private void processSelectRange(TagNode formNode, JSMasterScope masterScope)
      throws XPatherException {
    Object[] rangeSelectNodes =
        formNode.evaluateXPath("//select[@" + Constants.SELECT_RANGE_START_ATTR + "]");
    if (rangeSelectNodes.length > 0) {
      Scriptable workingScope = masterScope.createWorkingScope();
      Context context = masterScope.getCurrentContext();
      for (Object rangeSelectNodeO : rangeSelectNodes) {
        TagNode rangeSelectNode = (TagNode) rangeSelectNodeO;
        String name = rangeSelectNode.getAttributeByName(Constants.NAME_ATTR);
        String rangeStart = rangeSelectNode.getAttributeByName(Constants.SELECT_RANGE_START_ATTR);
        String rangeEnd = rangeSelectNode.getAttributeByName(Constants.SELECT_RANGE_END_ATTR);
        String preselectFirstOption =
            rangeSelectNode.getAttributeByName(Constants.SELECT_PRESELECT_FIRST_OPTION_ATTR);
        rangeSelectNode.removeAttribute(Constants.SELECT_RANGE_START_ATTR);
        rangeSelectNode.removeAttribute(Constants.SELECT_RANGE_END_ATTR);
        rangeSelectNode.removeAttribute(Constants.SELECT_PRESELECT_FIRST_OPTION_ATTR);

        logger.debug(
            "Found rangeSelectNode name:{}, rangeStart:{}, rangeEnd:{}",
            new String[] {name, rangeStart, rangeEnd});
        boolean rangeStartValid = rangeStart != null && !rangeStart.isEmpty();
        boolean rangeEndValid = rangeEnd != null && !rangeEnd.isEmpty();
        if (rangeStartValid && rangeEndValid) {
          Object rangeStartResult =
              context.evaluateString(
                  workingScope, "{" + rangeStart + "}", Constants.SELECT_RANGE_START_ATTR, 1, null);
          Object rangeEndResult =
              context.evaluateString(
                  workingScope, "{" + rangeEnd + "}", Constants.SELECT_RANGE_END_ATTR, 1, null);
          logger.debug(
              "RangeSelectNode name:{}, rangeStartResult:{}, rangeEndResult:{}",
              new Object[] {name, rangeStartResult, rangeEndResult});

          double rangeStartResultNumber = Context.toNumber(rangeStartResult);
          double rangeEndResultNumber = Context.toNumber(rangeEndResult);
          String comparator;
          String incrementor;
          if (rangeStartResultNumber < rangeEndResultNumber) {
            comparator = "<=";
            incrementor = "++";
          } else {
            comparator = ">=";
            incrementor = "--";
          }

          String rangeStatement =
              "{ var range = []; for( var i = "
                  + rangeStartResult
                  + "; i "
                  + comparator
                  + " "
                  + rangeEndResult
                  + "; i"
                  + incrementor
                  + ") { range.push(i); }; '' + range; }";
          logger.debug("RangeSelectNode name:{}, rangeStatement:{}", name, rangeStatement);
          String rangeResult =
              (String)
                  context.evaluateString(workingScope, rangeStatement, "Calculate range", 1, null);
          logger.debug("RangeSelectNode name:{}, rangeResult:{}", name, rangeResult);

          if (!"true".equals(preselectFirstOption)) {
            TagNode optionNode = new TagNode("option");
            optionNode.setAttribute("value", "");
            optionNode.addChild(new ContentNode("-- Please Select --"));
            rangeSelectNode.addChild(optionNode);
          }

          for (String item : rangeResult.split(",")) {
            TagNode optionNode = new TagNode("option");
            optionNode.addChild(new ContentNode(item));
            rangeSelectNode.addChild(optionNode);
          }

        } else {
          logger.warn(
              "Range select node '{}' not processed because {} is empty.",
              name,
              (rangeStartValid
                  ? Constants.SELECT_RANGE_START_ATTR
                  : Constants.SELECT_RANGE_END_ATTR));
        }
      }
    }
  }