@Override
  public String getFirstNumber(String locator) {
    WebElement webElement = getWebElement(locator);

    String text = webElement.getText();

    if (text == null) {
      return StringPool.BLANK;
    }

    StringBundler sb = new StringBundler();

    char[] chars = text.toCharArray();

    for (char c : chars) {
      boolean digit = false;

      if (Validator.isDigit(c)) {
        sb.append(c);

        digit = true;
      }

      String s = sb.toString();

      if (Validator.isNotNull(s) && !digit) {
        return s;
      }
    }

    return sb.toString();
  }
  public static int validateFriendlyURL(String friendlyURL) {
    if (friendlyURL.length() < 2) {
      return LayoutFriendlyURLException.TOO_SHORT;
    }

    if (!friendlyURL.startsWith(StringPool.SLASH)) {
      return LayoutFriendlyURLException.DOES_NOT_START_WITH_SLASH;
    }

    if (friendlyURL.endsWith(StringPool.SLASH)) {
      return LayoutFriendlyURLException.ENDS_WITH_SLASH;
    }

    if (friendlyURL.indexOf(StringPool.DOUBLE_SLASH) != -1) {
      return LayoutFriendlyURLException.ADJACENT_SLASHES;
    }

    for (char c : friendlyURL.toCharArray()) {
      if ((!Validator.isChar(c))
          && (!Validator.isDigit(c))
          && (c != CharPool.DASH)
          && (c != CharPool.PERCENT)
          && (c != CharPool.PERIOD)
          && (c != CharPool.PLUS)
          && (c != CharPool.SLASH)
          && (c != CharPool.STAR)
          && (c != CharPool.UNDERLINE)) {

        return LayoutFriendlyURLException.INVALID_CHARACTERS;
      }
    }

    return -1;
  }
  protected void validate(List<Element> children, Set<String> elNames) throws PortalException {

    for (Element el : children) {
      if (el.getName().equals("meta-data")) {
        continue;
      }

      String elName = el.attributeValue("name", StringPool.BLANK);
      String elType = el.attributeValue("type", StringPool.BLANK);

      if (Validator.isNull(elName) || elName.startsWith(JournalStructureConstants.RESERVED)) {

        throw new StructureXsdException();
      } else {
        char[] c = elName.toCharArray();

        for (int i = 0; i < c.length; i++) {
          if ((!Validator.isChar(c[i]))
              && (!Validator.isDigit(c[i]))
              && (c[i] != CharPool.DASH)
              && (c[i] != CharPool.UNDERLINE)) {

            throw new StructureXsdException();
          }
        }

        String completePath = elName;

        Element parent = el.getParent();

        while (!parent.isRootElement()) {
          completePath =
              parent.attributeValue("name", StringPool.BLANK) + StringPool.SLASH + completePath;

          parent = parent.getParent();
        }

        String elNameLowerCase = completePath.toLowerCase();

        if (elNames.contains(elNameLowerCase)) {
          throw new StructureXsdException();
        } else {
          elNames.add(elNameLowerCase);
        }
      }

      if (Validator.isNull(elType)) {
        throw new StructureXsdException();
      }

      validate(el.elements(), elNames);
    }
  }
  @Override
  public String getAUICompatibleId(String text) {
    if (Validator.isNull(text)) {
      return text;
    }

    StringBundler sb = null;

    int lastReplacementIndex = 0;

    for (int i = 0; i < text.length(); i++) {
      char c = text.charAt(i);

      if (((c <= 127) && (Validator.isChar(c) || Validator.isDigit(c)))
          || ((c > 127)
              && (c != CharPool.FIGURE_SPACE)
              && (c != CharPool.NARROW_NO_BREAK_SPACE)
              && (c != CharPool.NO_BREAK_SPACE))) {

        continue;
      }

      if (sb == null) {
        sb = new StringBundler();
      }

      if (i > lastReplacementIndex) {
        sb.append(text.substring(lastReplacementIndex, i));
      }

      sb.append(StringPool.UNDERLINE);

      if (c != CharPool.UNDERLINE) {
        sb.append(StringUtil.toHexString(c));
      }

      sb.append(StringPool.UNDERLINE);

      lastReplacementIndex = i + 1;
    }

    if (sb == null) {
      return text;
    }

    if (lastReplacementIndex < text.length()) {
      sb.append(text.substring(lastReplacementIndex));
    }

    return sb.toString();
  }
  public static int validateFriendlyURL(String friendlyURL, boolean checkMaxLength) {

    if (friendlyURL.length() < 2) {
      return LayoutFriendlyURLException.TOO_SHORT;
    }

    if (checkMaxLength && (friendlyURL.length() > LayoutConstants.FRIENDLY_URL_MAX_LENGTH)) {

      return LayoutFriendlyURLException.TOO_LONG;
    }

    if (!friendlyURL.startsWith(StringPool.SLASH)) {
      return LayoutFriendlyURLException.DOES_NOT_START_WITH_SLASH;
    }

    if (friendlyURL.endsWith(StringPool.SLASH)) {
      return LayoutFriendlyURLException.ENDS_WITH_SLASH;
    }

    if (friendlyURL.contains(StringPool.DOUBLE_SLASH)) {
      return LayoutFriendlyURLException.ADJACENT_SLASHES;
    }

    for (char c : friendlyURL.toCharArray()) {
      if (!Validator.isChar(c)
          && !Validator.isDigit(c)
          && (c != CharPool.DASH)
          && (c != CharPool.PERCENT)
          && (c != CharPool.PERIOD)
          && (c != CharPool.PLUS)
          && (c != CharPool.SLASH)
          && (c != CharPool.STAR)
          && (c != CharPool.UNDERLINE)) {

        return LayoutFriendlyURLException.INVALID_CHARACTERS;
      }
    }

    return -1;
  }
  protected String fixSessionKey(String fileName, String content, Pattern pattern) {

    Matcher matcher = pattern.matcher(content);

    if (!matcher.find()) {
      return content;
    }

    String newContent = content;

    do {
      String match = matcher.group();

      String s = null;

      if (pattern.equals(sessionKeyPattern)) {
        s = StringPool.COMMA;
      } else if (pattern.equals(taglibSessionKeyPattern)) {
        s = "key=";
      }

      int x = match.indexOf(s);

      if (x == -1) {
        continue;
      }

      x = x + s.length();

      String substring = match.substring(x).trim();

      String quote = StringPool.BLANK;

      if (substring.startsWith(StringPool.APOSTROPHE)) {
        quote = StringPool.APOSTROPHE;
      } else if (substring.startsWith(StringPool.QUOTE)) {
        quote = StringPool.QUOTE;
      } else {
        continue;
      }

      int y = match.indexOf(quote, x);
      int z = match.indexOf(quote, y + 1);

      if ((y == -1) || (z == -1)) {
        continue;
      }

      String prefix = match.substring(0, y + 1);
      String suffix = match.substring(z);
      String oldKey = match.substring(y + 1, z);

      boolean alphaNumericKey = true;

      for (char c : oldKey.toCharArray()) {
        if (!Validator.isChar(c)
            && !Validator.isDigit(c)
            && (c != CharPool.DASH)
            && (c != CharPool.UNDERLINE)) {

          alphaNumericKey = false;
        }
      }

      if (!alphaNumericKey) {
        continue;
      }

      String newKey = TextFormatter.format(oldKey, TextFormatter.O);

      newKey = TextFormatter.format(newKey, TextFormatter.M);

      if (newKey.equals(oldKey)) {
        continue;
      }

      String oldSub = prefix.concat(oldKey).concat(suffix);
      String newSub = prefix.concat(newKey).concat(suffix);

      newContent = StringUtil.replaceFirst(newContent, oldSub, newSub);
    } while (matcher.find());

    return newContent;
  }