Ejemplo n.º 1
0
  /**
   * Gets the string content of a node via xpath
   *
   * @param nodeObj
   * @param xpath
   * @return
   */
  private static String getNodeContent(final Object nodeObj, final String xpath) {
    Node node = XPath.selectNode(xpath, nodeObj);
    if (node == null) {
      if (Logger.isDebugEnabled() == true) {
        Logger.debug("Could not find node by xpath: " + xpath + " in node: " + nodeObj.toString());
      }
      return StringUtils.EMPTY;
    }

    return StringUtils.trimToEmpty(node.getTextContent());
  }
Ejemplo n.º 2
0
  /**
   * Reads the data from the given node and generates an AmazonResult from it.
   *
   * @param nodeObj
   * @return
   */
  private static AmazonResult getResultFromNode(final Object nodeObj) {

    Node itemAttrNode = XPath.selectNode("./ItemAttributes", nodeObj);

    String title = getNodeContent(itemAttrNode, "./Title");

    String copyType = getNodeContent(itemAttrNode, "./Binding");
    if (copyTypeMatches.containsKey(copyType) == false) {
      if (Logger.isErrorEnabled() == true) {
        Logger.error("No copytype matching configured for amazon copytype: " + copyType);
      }
      return null;
    }
    copyType = copyTypeMatches.get(copyType);
    if (copyType.equals("BLURAY") == true && StringUtils.contains(title, "[Blu-ray 3D]") == true) {
      copyType = "BLURAY3D";
    }

    title = StringUtils.substringBefore(title, "(");

    if (CollectionUtils.isEmpty(removeFromTitleList) == false) {
      for (String removeFromTitle : removeFromTitleList) {
        title = StringUtils.remove(title, removeFromTitle);
      }
    }
    title = StringUtils.trim(title);

    String asin = getNodeContent(nodeObj, "./ASIN");
    String ean = getNodeContent(itemAttrNode, "./EAN");
    String rating = getNodeContent(itemAttrNode, "./AudienceRating");
    if (ageRatingMatches.containsKey(rating) == false) {
      if (Logger.isErrorEnabled() == true) {
        Logger.error("No agerating matching configured for amazon rating: " + rating);
      }
    } else {
      rating = ageRatingMatches.get(rating);
    }

    final Set<String> audioTypes = new HashSet<String>();

    final NodeList audioTypeNodes =
        XPath.selectNodes("./Languages/Language[AudioFormat and Type = 'Original']", itemAttrNode);
    for (int i = 0; i < audioTypeNodes.getLength(); i++) {
      Node langItem = audioTypeNodes.item(i);
      final Node nameNode = XPath.selectNode("./Name", langItem);
      final Node formatNode = XPath.selectNode("./AudioFormat", langItem);
      String audioType = StringUtils.EMPTY;
      if (nameNode != null && StringUtils.isEmpty(nameNode.getTextContent()) == false) {
        audioType = nameNode.getTextContent();
      }

      if (formatNode != null && StringUtils.isEmpty(formatNode.getTextContent()) == false) {
        if (StringUtils.isEmpty(audioType) == false) {
          audioType += " - ";
        }
        audioType += formatNode.getTextContent();
      }

      audioTypes.add(audioType);
    }

    final NodeList otherLanguageNodes =
        XPath.selectNodes("./Languages/Language[Type = 'Published']/Name", itemAttrNode);
    for (int i = 0; i < otherLanguageNodes.getLength(); i++) {
      String language = otherLanguageNodes.item(i).getTextContent();

      // check if the language is already known :)
      boolean alreadyKnown = false;

      for (final String audioType : audioTypes) {
        if (StringUtils.startsWithIgnoreCase(audioType, language) == true) {
          alreadyKnown = true;
          break;
        }
      }

      if (alreadyKnown == false) {
        audioTypes.add(language);
      }
    }

    final String imageUrl = getNodeContent(nodeObj, "./MediumImage/URL");

    final AmazonResult result =
        new AmazonResult(title, rating, copyType, asin, ean, audioTypes, imageUrl);

    return result;
  }