code: private static int checkEntity(final StringBuilder out, final String in, final int start)
      {
          final int pos = Utils.readUntil(out, in, start, ';');
          if (pos < 0 || out.length() < 3)
          {
              return -1;
          }
          if (out.charAt(1) == '#')
          {
              if (out.charAt(2) == 'x' || out.charAt(2) == 'X')
              {
                  if (out.length() < 4)
                  {
                      return -1;
                  }
                  for (int i = 3; i < out.length(); i++)
                  {
                      final char c = out.charAt(i);
                      if ((c < '0' || c > '9') && ((c < 'a' || c > 'f') && (c < 'A' || c > 'F')))
                      {
                          return -1;
                      }
                  }
              }
              else
              {
                  for (int i = 2; i < out.length(); i++)
                  {
                      final char c = out.charAt(i);
                      if (c < '0' || c > '9')
                      {
                          return -1;
                      }
                  }
              }
              out.append(';');
          }
          else
          {
              for (int i = 1; i < out.length(); i++)
              {
                  final char c = out.charAt(i);
                  if (!Character.isLetterOrDigit(c))
                  {
                      return -1;
                  }
              }
              out.append(';');
              return HTML.isEntity(out.toString()) ? pos : -1;
          }
  
          return pos;
      }
docs: Check if this is a valid XML/HTML entity.
  
  @param out
  The StringBuilder to write to.
  @param in
  Input String.
  @param start
  Starting position
  @return The new position or -1 if this entity in invalid.