Esempio n. 1
0
 /**
  * @param suffix Suffix.
  * @return Rule for the next element according to the suffix.
  */
 Rule getRule(String suffix) {
   for (Rule rule : rules) {
     if ((suffix.compareTo(rule.getFrom()) >= 0) &&
         (suffix.compareTo(rule.getTo()) <= 0)) {
       return rule;
     }
   }
   return null;
 }
Esempio n. 2
0
  /**
   * @param isbn ISBN.
   * @return Information about ISBN.
   */
  public static ISBNInformation getInformation(String isbn) {
    if (isbn == null) {
      return null;
    }

    // Retrieve information about ISBN
    isbn = PageElementISBN.cleanISBN(isbn);
    boolean isbn10 = false;
    if (isbn.length() == 10) {
      isbn = "978" + isbn;
      isbn10 = true;
    }
    Range eanPrefix = getEANPrefix(isbn);
    Range registrationGroup = getRegistrationGroup(isbn);

    // Build information
    ISBNInformation isbnInfo = new ISBNInformation();
    if ((eanPrefix != null) && !isbn10) {
      isbnInfo.texts.add(eanPrefix.getPrefix() + " - " + eanPrefix.getAgency());
    }
    String prefix = null;
    String cleanPrefix = null;
    if (registrationGroup != null) {
      prefix = registrationGroup.getPrefix();
      cleanPrefix = registrationGroup.getCleanPrefix();
      if (isbn10 && (prefix != null) && (prefix.length() > 4)) {
        // Remove "978-"
        prefix = prefix.substring(4);
      }
      if ((prefix != null) && (prefix.length() > 0)) {
        isbnInfo.texts.add(prefix + "-" + registrationGroup.getAgency());
      }
    }

    // Suggest a formatted ISBN
    if ((registrationGroup != null) && (cleanPrefix != null)) {
      String suffix = isbn.substring(cleanPrefix.length());
      Rule rule = registrationGroup.getRule(suffix);
      if (rule == null) {
        isbnInfo.texts.add(GT._("No range found for ISBN"));
        isbnInfo.unknownRange = true;
      } else {
        int nextLength = rule.getLength();
        if (nextLength > 0) {
          if (suffix.length() > nextLength + 1) {
            String suggestedISBN = prefix + "-" +
                suffix.substring(0, nextLength) + "-" +
                suffix.substring(nextLength, suffix.length() - 1) + "-" +
                suffix.substring(suffix.length() - 1);
            isbnInfo.texts.add(GT._("Suggested format: {0}", suggestedISBN));
          } else {
            isbnInfo.texts.add(GT._("ISBN length incoherent with range found"));
          }
        } else {
          isbnInfo.texts.add(GT._(
              "ISBN is in a reserved range {0}",
              prefix + "-(" + rule.getFrom() + "-" + rule.getTo() + ")"));
          isbnInfo.reservedRange = true;
        }
      }
    } else {
      isbnInfo.texts.add(GT._("No range found for ISBN"));
      isbnInfo.unknownRange = true;
    }

    return isbnInfo;
  }