Exemple #1
0
    @Override
    public Maybe<String> findVariable(String key) {
      String value;
      if (key.equals("PAGE_NAME")) value = namedPage.getName();
      else if (key.equals("PAGE_PATH")) value = namedPage.getPath();
      else return Maybe.noString;

      return new Maybe<String>(value);
    }
 public String buildLink(String pageSuffix, String originalName) {
   if (currentPage.targetExists(wikiWordPath)) {
     return makeLinkToExistingWikiPage(qualifiedName + pageSuffix, linkBody, null);
   } else if ("FitNesse".equals(originalName)) {
     return "<span class=\"fitnesse\">" + originalName + "</span>";
   } else {
     return makeLinkToNonExistentWikiPage(originalName, currentPage.makeUrl(wikiWordPath));
   }
 }
 public String makeEditabeLink(String originalName) {
   if (currentPage.targetExists(wikiWordPath)) {
     return makeLinkToExistingWikiPage(qualifiedName, linkBody, null)
         + " "
         + makeLinkToExistingWikiPage(
             qualifiedName + "?edit&amp;redirectToReferer=true&amp;redirectAction=",
             "(edit)",
             "edit");
   } else {
     return makeLinkToNonExistentWikiPage(originalName, currentPage.makeUrl(wikiWordPath));
   }
 }
Exemple #4
0
 private Maybe<String> lookInParentPages(String name) {
   for (SourcePage sourcePage : page.getAncestors()) {
     if (!inCache(sourcePage)) {
       // The cache is passed along... page is rendered as a normal page.
       Parser.make(copyForPage(sourcePage), sourcePage.getContent()).parse();
       putVariable(sourcePage, "", Maybe.noString);
     }
     Maybe<String> result = findVariableInCache(sourcePage, name);
     if (!result.isNothing()) return result;
   }
   return Maybe.noString;
 }
Exemple #5
0
  private SourceSite getSite() {

    SourcePattern pattern = this;

    SourcePage page = pattern.getProperty(sourcePageProperty);
    while (page == null) {

      pattern = pattern.getProperty(parentPatternProperty);

      if (pattern != null) {
        page = pattern.getProperty(sourcePageProperty);
      }
    }

    return page.getProperty(SourcePage.site);
  }
Exemple #6
0
 private Maybe<String> findSpecialVariableValue(String key) {
   String value;
   if (key.equals("RUNNING_PAGE_NAME")) value = page.getName();
   else if (key.equals("RUNNING_PAGE_PATH")) value = page.getPath();
   else if (key.equals("PAGE_NAME")) value = namedPage.getName();
   else if (key.equals("PAGE_PATH")) value = namedPage.getPath();
   else if (key.equals("FITNESSE_PORT")) {
     Maybe<String> port = findVariableInContext("FITNESSE_PORT");
     value = port.isNothing() ? "-1" : port.getValue();
   } else if (key.equals("FITNESSE_ROOTPATH")) {
     Maybe<String> path = findVariableInContext("FITNESSE_ROOTPATH");
     value = path.isNothing() ? "" : path.getValue();
   } else if (key.equals("FITNESSE_VERSION")) {
     Maybe<String> version = findVariableInContext("FITNESSE_VERSION");
     value = version.isNothing() ? "" : version.getValue();
   } else return Maybe.noString;
   return new Maybe<String>(value);
 }
  /**
   * @param driver the web driver to use for the test
   * @param wait the implicit wait value for this run
   * @param waiter handles driver specific waits
   * @return always returns true
   */
  private boolean fathersNavigationExercise(
      final WebDriver driver, final long wait, final PageWaiter waiter) {
    try {
      driver.manage().timeouts().implicitlyWait(wait, TimeUnit.SECONDS);

      // Grandpop
      PersonPage currentPerson = new PersonPage(driver, "I11", null, waiter);
      currentPerson.open();
      assertEquals("Person ID mismatch", "I11", currentPerson.getId());
      assertEquals("Person failed check", "", currentPerson.check());

      // Fred
      currentPerson = currentPerson.navigateFather();
      assertEquals("Person ID mismatch", "I32", currentPerson.getId());
      assertEquals("Person failed check", "", currentPerson.check());

      final SourcePage currentSource = new SourcePage(driver, "S21", currentPerson, waiter);
      assertTrue("Title mismatch", currentSource.titleCheck());

      currentPerson = currentSource.back();
      assertEquals("Person ID mismatch", "I32", currentPerson.getId());

      // Johannes
      currentPerson = currentPerson.navigateFather();
      assertEquals("Person ID mismatch", "I99", currentPerson.getId());
      assertEquals("Person failed check", "", currentPerson.check());

      // Matthias
      currentPerson = currentPerson.navigateFather();
      assertEquals("Person ID mismatch", "I180", currentPerson.getId());
      assertEquals("Person failed check", "", currentPerson.check());
    } finally {
      // Close the browser
      driver.quit();
    }
    return true;
  }
 private String makeChildPath(SourcePage page, String content) {
   return String.format("%s.%s", page.getName(), content.substring(1));
 }
 private String makeParentPath(SourcePage page, String content) {
   return page.findParentPath(content.substring(1));
 }
 public WikiWordBuilder(SourcePage currentPage, String pagePath, String linkBody) {
   this.currentPage = currentPage;
   this.linkBody = linkBody;
   this.wikiWordPath = makePath(currentPage, pagePath);
   this.qualifiedName = currentPage.makeFullPathOfTarget(wikiWordPath);
 }
Exemple #11
0
  @Export
  public void extract(final Map<String, Object> parameters) throws FrameworkException {

    final SourcePage page = getProperty(sourcePageProperty);

    if (page == null) {
      throw new FrameworkException(422, "Pattern has no source page, exiting.");
    }

    final String selector = getProperty(selectorProperty);
    if (selector == null) {
      throw new FrameworkException(422, "Pattern has no selector, exiting.");
    }

    final Long from = getProperty(fromProperty);
    final Long to = getProperty(toProperty);

    final List<SourcePattern> subPatterns = getProperty(subPatternsProperty);

    Document doc = null;
    NodeInterface parentObj = null;

    if (parameters.containsKey("object")) {

      parentObj = (NodeInterface) parameters.get("object");
    }

    if (parameters.containsKey("document")) {

      doc = (Document) parameters.get("document");

    } else {

      final String url = page.getProperty(SourcePage.url);
      if (url == null) {
        throw new FrameworkException(422, "This pattern's source page has no URL, exiting.");
      }

      // Get the content from the URL
      final String content = getContent(url);

      // Parse the document with Jsoup and extract the elements matched by the given selector
      doc = Jsoup.parse(content);
    }

    final String mappedType = getProperty(mappedTypeProperty);
    if (mappedType == null) {
      throw new FrameworkException(422, "No mapped type given, exiting.");
    }

    final Elements parts = doc.select(selector);

    // Loop through all elements found for this pattern; if a start index is given, start at this
    // element
    for (int i = (from != null ? from.intValue() : 1); i <= (to != null ? to : parts.size()); i++) {

      // If no object was given (from a higher-level pattern), create a new object of the given type
      final NodeInterface obj = (parentObj == null ? create(mappedType) : parentObj);

      if (subPatterns.size() > 0) {

        // Loop through the sub patterns of this pattern
        for (final SourcePattern subPattern : subPatterns) {

          final String subSelector =
              selector
                  + ":nth-child("
                  + i
                  + ") > "
                  + subPattern.getProperty(SourcePattern.selectorProperty);

          final String subPatternMappedAttribute =
              subPattern.getProperty(SourcePattern.mappedAttributeProperty);
          final String subPatternMappedAttributeFormat =
              subPattern.getProperty(SourcePattern.mappedAttributeFormatProperty);
          final SourcePage subPatternSubPage =
              subPattern.getProperty(SourcePattern.subPageProperty);

          extractAndSetValue(
              obj,
              doc,
              subSelector,
              mappedType,
              subPatternMappedAttribute,
              subPatternMappedAttributeFormat,
              subPatternSubPage);
        }

      } else {

        final String mappedAttribute = getProperty(mappedAttributeProperty);
        final String mappedAttributeFormat = getProperty(mappedAttributeFormatProperty);

        extractAndSetValue(
            obj, doc, selector, mappedType, mappedAttribute, mappedAttributeFormat, null);
      }
    }
  }
Exemple #12
0
  private void extractAndSetValue(
      final NodeInterface obj,
      final Document doc,
      final String selector,
      final String mappedType,
      final String mappedAttribute,
      final String mappedAttributeFormat,
      final SourcePage subPage)
      throws FrameworkException {

    // If the sub pattern has a mapped attribute, set the extracted value
    if (StringUtils.isNotEmpty(mappedAttribute)) {

      // Extract the value for this sub pattern's selector
      final String ex = doc.select(selector).text();

      final ConfigurationProvider config = StructrApp.getConfiguration();
      final PropertyKey key = config.getPropertyKeyForJSONName(type(mappedType), mappedAttribute);

      if (key != null) {

        Object convertedValue = ex;

        final PropertyConverter inputConverter = key.inputConverter(securityContext);

        if (inputConverter != null) {

          final String locale = getProperty(mappedAttributeLocaleProperty);
          DecimalFormat decimalFormat = null;

          if (key instanceof DoubleProperty) {

            if (StringUtils.isNotBlank(locale)) {

              decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(new Locale(locale));

            } else if (StringUtils.isNotBlank(mappedAttributeFormat)) {

              decimalFormat = new DecimalFormat(mappedAttributeFormat);
            }

            if (decimalFormat != null) {

              convertedValue = decimalFormat.format(convertedValue);
            }

          } else {

            convertedValue = inputConverter.convert(ex);
          }
        }

        obj.setProperty(key, convertedValue);
      }

      // If the sub pattern has no mapped attribute but a sub page defined, query the patterns of
      // the sub page
    } else if (subPage != null) {

      final String pageUrl = subPage.getProperty(SourcePage.url);
      final URI uri;

      try {
        uri = new URI(pageUrl);
      } catch (URISyntaxException ex) {
        throw new FrameworkException(422, "Unable to parse sub page url: " + pageUrl);
      }

      // This is the URL of the linked page derived from the enclosing selector
      final String subUrl =
          uri.getScheme() + "://" + uri.getAuthority() + doc.select(selector).attr("href");

      // Extract the content of the linked page
      final String subContent = getContent(subUrl);

      // Parse the content into a document
      final Document subDoc = Jsoup.parse(subContent);

      final List<SourcePattern> subPagePatterns = subPage.getProperty(SourcePage.patterns);

      // Loop through all patterns of the sub page
      for (final SourcePattern subPagePattern : subPagePatterns) {

        final Map<String, Object> params = new HashMap<>();
        params.put("document", subDoc);
        params.put("object", obj);

        subPagePattern.extract(params);

        //				final String subPagePatternSelector =
        // subPagePattern.getProperty(SourcePattern.selectorProperty);
        //
        //
        //				// Extract
        //				final String subEx = subDoc.select(subPagePatternSelector).text();
        //				final String subPagePatternType =
        // subPagePattern.getProperty(SourcePattern.mappedTypeProperty);
        //
        //				if (subPagePatternType != null) {
        //
        //
        //					final Elements subParts = subDoc.select(subPagePatternSelector);
        //
        //					final Long j = 1L;
        //
        //					for (final Element subPart : subParts) {
        //
        //						final NodeInterface subObj = create(subPagePatternType);
        //
        //						final List<SourcePattern> subPagePatternPatterns =
        // subPagePattern.getProperty(SourcePattern.subPatternsProperty);
        //
        //						for (final SourcePattern subPageSubPattern : subPagePatternPatterns) {
        //
        //
        //							final String subPagePatternSelector =
        // subPageSubPattern.getProperty(SourcePattern.selectorProperty);
        //
        //
        //
        //							final String subPageSubPatternSelector = subPagePatternSelector + ":nth-child(" + j
        // + ") > " + subPagePatternSelector;
        //
        //							extractAndSetValue(subObj, subDoc, subSelector, mappedType,
        // subPatternMappedAttribute);
        //
        //
        //							final String subSubEx = subDoc.select(subPageSubPatternSelector).text();
        //
        //							if (subSubEx != null && subSubEx != = '' && subPageSubPattern.mappedAttribute !=
        // null) {
        //
        //							final PropertyKey key = config.getPropertyKeyForJSONName(type(mappedType),
        // subPatternMappedAttribute);
        //							if (key != null) {
        //
        //								subObj.setProperty(key, subSubEx);
        //							}
        //
        //						}
        //
        //						final String subPagePatternMappedAttribute =
        // subPagePattern.getProperty(SourcePattern.mappedAttributeProperty);
        //
        //						final PropertyKey key = config.getPropertyKeyForJSONName(type(mappedType),
        // subPagePatternMappedAttribute);
        //						if (key != null) {
        //
        //							obj.setProperty(key, subSubEx);
        //						}
        //
        //					}
        //
        //				} else {
        //
        //					if (subEx != null && subEx != = '' && subPagePattern.mappedAttribute != null) {
        //						obj[subPagePattern.mappedAttribute] = subEx;
        //					}
      }
    }
  }
Exemple #13
0
 private void putVariable(SourcePage page, String name, Maybe<String> value) {
   String key = page.getFullName();
   if (!cache.containsKey(key)) cache.put(key, new HashMap<String, Maybe<String>>());
   cache.get(key).put(name, value);
 }
Exemple #14
0
 private Maybe<String> findVariableInCache(SourcePage page, String name) {
   String key = page.getFullName();
   if (!cache.containsKey(key)) return Maybe.noString;
   if (!cache.get(key).containsKey(name)) return Maybe.noString;
   return cache.get(key).get(name);
 }
Exemple #15
0
 private boolean inCache(SourcePage page) {
   return cache.containsKey(page.getFullName());
 }