Пример #1
0
  @Override
  protected void prepare(Activity context, LinearLayout items) {

    final RedditPost post = getArguments().getParcelable("post");

    items.addView(
        propView(
            context,
            R.string.props_title,
            StringEscapeUtils.unescapeHtml4(post.title.trim()),
            true));
    items.addView(propView(context, R.string.props_author, post.author, false));
    items.addView(
        propView(context, R.string.props_url, StringEscapeUtils.unescapeHtml4(post.url), false));
    items.addView(
        propView(
            context,
            R.string.props_created,
            RRTime.formatDateTime(post.created_utc * 1000, context),
            false));

    if (post.edited instanceof Long) {
      items.addView(
          propView(
              context,
              R.string.props_edited,
              RRTime.formatDateTime((Long) post.edited * 1000, context),
              false));
    } else {
      items.addView(propView(context, R.string.props_edited, R.string.props_never, false));
    }

    items.addView(propView(context, R.string.props_subreddit, post.subreddit, false));
    items.addView(
        propView(
            context,
            R.string.props_score,
            String.format(
                "%d (%d %s, %d %s)",
                post.score,
                post.ups,
                context.getString(R.string.props_up),
                post.downs,
                context.getString(R.string.props_down)),
            false));
    items.addView(
        propView(context, R.string.props_num_comments, String.valueOf(post.num_comments), false));

    if (post.selftext != null && post.selftext.length() > 0) {
      items.addView(
          propView(
              context,
              R.string.props_self_markdown,
              StringEscapeUtils.unescapeHtml4(post.selftext),
              false));
    }
  }
  @Test
  public void testClensing() {
    String pageTitle =
        StringEscapeUtils.unescapeHtml4(Jsoup.clean("Jeppistä jee", Whitelist.simpleText()));

    assertTrue("Jeppistä jee".equals(pageTitle));
  }
Пример #3
0
 /** Parse a Mediawiki heading of the form "==heading==" and return the resulting HTML output. */
 public String parse(JFlexLexer lexer, String raw, Object... args) throws ParserException {
   if (logger.isTraceEnabled()) {
     logger.trace("heading: " + raw + " (" + lexer.yystate() + ")");
   }
   // the wikiheading tag may match a preceding newline, so strip it
   raw = raw.trim();
   int level = this.generateTagLevel(raw, args);
   String tagText = this.generateTagText(raw, args);
   String tocText = this.buildTocText(lexer, tagText);
   String tagName = this.buildTagName(lexer, tocText);
   if (lexer.getMode() <= JFlexParser.MODE_SLICE) {
     String sectionName = StringEscapeUtils.unescapeHtml4(tocText);
     lexer.getParserOutput().setSectionName(sectionName);
     return raw;
   }
   if (!(lexer instanceof JAMWikiLexer)) {
     throw new IllegalStateException(
         "Cannot parse heading tags except with instances of JAMWikiLexer or in slice/splice mode");
   }
   JAMWikiLexer jamwikiLexer = (JAMWikiLexer) lexer;
   if (jamwikiLexer.paragraphIsOpen()) {
     // close any open paragraph
     jamwikiLexer.popTag("p");
   }
   return this.generateOutput(jamwikiLexer, tagName, tocText, tagText, level, raw, args);
 }
Пример #4
0
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if (convertView == null) v = inflater.inflate(R.layout.sem_details_listrow, null);

    semSubName = (TextView) v.findViewById(R.id.semSubName);
    semSubName.setSelected(true);
    semCCode = (TextView) v.findViewById(R.id.semCCode);
    semSubCredits = (TextView) v.findViewById(R.id.semSubCredits);
    semSubGrade = (TextView) v.findViewById(R.id.semSubGrade);

    HashMap<String, String> row = new HashMap<String, String>();

    row = adapterkeylist.get(position);

    temp = row.get("sub");
    temp = StringEscapeUtils.unescapeHtml4(temp);

    semSubName.setText(temp);
    semCCode.setText("Subject Code: " + row.get("subcode"));
    semSubCredits.setText("Subject Credits: " + row.get("credits"));
    semSubGrade.setText("Subject Grade: " + row.get("grade"));

    return v;
  }
Пример #5
0
 private void makeDigest(KCPosting posting) {
   if (null == posting) {
     return;
   }
   digest = posting.content;
   int len = digest.length();
   if (len > 250) len = 250;
   digest = digest.substring(0, len);
   digest = digest.replaceAll("[\n\r\u0085\u2028\u2029]", " ").replaceAll(" +", " ").trim();
   digest = StringEscapeUtils.unescapeHtml4(digest);
   digest = digest.replaceAll("<span class=\"spoiler\">.+?</span>", "");
   digest = digest.replaceAll("\\<.*?\\>", " ");
   digest = digest.replaceAll("https?://.+? ", " ");
   digest = digest.replaceAll(" +", " ");
   len = digest.length();
   if (len > 200) len = 200;
   digest = digest.substring(0, len);
   digest = digest.replaceAll("\\<.*", "");
   int pos = digest.length() - 1;
   char c = digest.charAt(pos);
   while ((c != ' ') && (pos > 150)) {
     pos--;
     c = digest.charAt(pos);
   }
   digest = digest.trim();
   for (String img : posting.thumbs) {
     if ((img != null) && (img.length() > 0)) {
       digest += "\n   " + img;
     }
   }
 }
Пример #6
0
  public static ImageInfo parseImgur(final JsonBufferedObject object)
      throws IOException, InterruptedException {

    final JsonBufferedObject image = object.getObject("image");
    final JsonBufferedObject links = object.getObject("links");

    String urlOriginal = null;
    String urlBigSquare = null;
    String title = null;
    String caption = null;
    String type = null;
    boolean isAnimated = false;
    Long width = null;
    Long height = null;
    Long size = null;

    if (image != null) {
      title = image.getString("title");
      caption = image.getString("caption");
      type = image.getString("type");
      isAnimated = "true".equals(image.getString("animated"));
      width = image.getLong("width");
      height = image.getLong("height");
      size = image.getLong("size");
    }

    if (links != null) {
      urlOriginal = links.getString("original");
      if (urlOriginal != null && isAnimated) urlOriginal = urlOriginal.replace(".gif", ".mp4");

      urlBigSquare = links.getString("big_square");
    }

    if (title != null) {
      title = StringEscapeUtils.unescapeHtml4(title);
    }

    if (caption != null) {
      caption = StringEscapeUtils.unescapeHtml4(caption);
    }

    return new ImageInfo(
        urlOriginal, urlBigSquare, title, caption, type, isAnimated, width, height, size);
  }
Пример #7
0
  @Override
  protected void createFeed(SyndFeed feed, Map model) {
    @SuppressWarnings("unchecked")
    List<PreparedUserEvent> list = (List<PreparedUserEvent>) model.get("topicsList");
    String s = "Ответы на комментарии пользователя " + model.get("nick");
    feed.setTitle(s);
    feed.setLink("http://www.linux.org.ru");
    feed.setUri("http://www.linux.org.ru");
    feed.setAuthor("");
    feed.setDescription(s);

    Date lastModified;
    if (!list.isEmpty()) {
      lastModified = list.get(0).getEvent().getEventDate();
    } else {
      lastModified = new Date();
    }
    feed.setPublishedDate(lastModified);

    List<SyndEntry> entries = new ArrayList<>();
    feed.setEntries(entries);
    for (PreparedUserEvent preparedUserEvent : list) {
      UserEvent item = preparedUserEvent.getEvent();

      SyndEntry feedEntry = new SyndEntryImpl();
      feedEntry.setPublishedDate(item.getEventDate());
      feedEntry.setTitle(StringEscapeUtils.unescapeHtml4(item.getSubj()));

      String link;

      if (item.getCid() != 0) {
        feedEntry.setAuthor(preparedUserEvent.getAuthor().getNick());

        link =
            String.format(
                "http://www.linux.org.ru/jump-message.jsp?msgid=%s&cid=%s",
                String.valueOf(item.getTopicId()), String.valueOf(item.getCid()));
      } else {
        link =
            String.format(
                "http://www.linux.org.ru/view-message.jsp?msgid=%s",
                String.valueOf(item.getTopicId()));
      }

      feedEntry.setLink(link);
      feedEntry.setUri(link);

      if (preparedUserEvent.getMessageText() != null) {
        SyndContent message = new SyndContentImpl();
        message.setValue(StringUtil.removeInvalidXmlChars(preparedUserEvent.getMessageText()));
        message.setType("text/html");
        feedEntry.setDescription(message);
      }
      entries.add(feedEntry);
    }
  }
 public String getStrippedDescription(boolean load) {
   if (strippedDescription == null && load) {
     // All complete words up to 100 characters
     String result = getDescription().replaceAll("\\<.*?\\>", "").trim();
     result = StringEscapeUtils.unescapeHtml4(result);
     int lastSpace = result.lastIndexOf(" ", 100);
     strippedDescription = result.substring(0, lastSpace);
   }
   return strippedDescription;
 }
Пример #9
0
  public WebResource resolve() {
    HttpGet get = null;
    HttpResponse getResponse = null;
    try {
      HttpContext localContext = new BasicHttpContext();
      get = new HttpGet(shortenedUrl);
      getResponse = httpClient.execute(get, localContext);

      if (getResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        hadErrors = true;
        return this;
      }

      url = getUrlAfterRedirects(localContext);
      URL u = new URL(url);

      String pageContent = EntityUtils.toString(getResponse.getEntity());

      Matcher m = pageTitlePattern.matcher(pageContent);
      if (m.find()) {
        title = m.group(1);
        title = title.replace("\n", " ");
        title = title.replace("\t", " ");
        title = title.replaceAll(" {2,}", " ");
        title = StringEscapeUtils.unescapeHtml4(title.trim());
      }

      for (Entry<String, Pattern> scraper : imageScrapers.entrySet()) {
        if (u.getHost().contains(scraper.getKey())) {
          Matcher sm = scraper.getValue().matcher(pageContent);
          if (sm.find()) {
            imageUrl = sm.group(1);
            type = Type.image;
          }
        }
      }

      return this;
    } catch (Exception e) {
      log.warn("Failed to resolve URL " + url);
      if (get != null) {
        get.abort();
      }
      if (getResponse != null) {
        try {
          EntityUtils.consume(getResponse.getEntity());
        } catch (IOException e1) {
          log.debug("Failed to clean up", e1);
        }
      }
    }
    hadErrors = true;
    return this;
  }
Пример #10
0
 public static String getPlainHtml(String text) {
   if (text == null) return "";
   String res = "", tmp;
   Scanner scanner = new Scanner(text);
   while (scanner.hasNextLine()) {
     tmp = scanner.nextLine();
     String t = StringEscapeUtils.unescapeHtml4(tmp).replaceAll("[\\s\\u00A0]+$", "");
     if (t.trim().equals("")) continue;
     else {
       res += tmp + "<br>\n";
     }
   }
   return res;
 }
Пример #11
0
 private String getRefAndLevel(AuditStatistics auditStatistics, Locale locale) {
   ResourceBundle refBundle = ResourceBundle.getBundle(refBundleName, locale);
   Collection<ResourceBundle> refAndlevelValueBundleList = new ArrayList();
   for (String bundle : refAndLevelValueBundleNameList) {
     refAndlevelValueBundleList.add(ResourceBundle.getBundle(bundle, locale));
   }
   ResourceBundle levelBundle = ResourceBundle.getBundle(levelBundleName, locale);
   StringBuilder refAndLevel = new StringBuilder();
   refAndLevel.append(StringEscapeUtils.unescapeHtml4(refBundle.getString(REF_KEY)));
   refAndLevel.append(DOUBLE_DOT_KEY);
   refAndLevel.append(
       StringEscapeUtils.unescapeHtml4(
           retrieveI18nValue(
               auditStatistics.getParametersMap().get(REF_KEY), refAndlevelValueBundleList)));
   refAndLevel.append(SEPARATOR_KEY);
   refAndLevel.append(StringEscapeUtils.unescapeHtml4(levelBundle.getString(LEVEL_KEY)));
   refAndLevel.append(DOUBLE_DOT_KEY);
   refAndLevel.append(
       StringEscapeUtils.unescapeHtml4(
           retrieveI18nValue(
               auditStatistics.getParametersMap().get(LEVEL_KEY).replace(";", "-"),
               refAndlevelValueBundleList)));
   return refAndLevel.toString();
 }
Пример #12
0
  public String getQ() {
    HTTPQuery q;
    try {
      q = HTTPQuery.create("http://questionparty.com/questions/rand/");
    } catch (MalformedURLException e1) {
      return null;
    }

    q.connect(true, false);
    String html = null;
    try {
      html = q.readWhole();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      q.close();
    }

    if (html == null) return null;

    Matcher matcher = headerPattern.matcher(html);
    if (!matcher.find()) return "";
    String question = matcher.group(1);

    matcher = itemPattern.matcher(html);
    ArrayList<String> answers = new ArrayList<String>();
    while (matcher.find()) {
      answers.add(matcher.group(1));
    }

    StringBuilder sb = new StringBuilder();
    int i = Math.min(answers.size(), 5);
    Random rnd = new Random();
    while (i-- > 0) {
      if (sb.length() != 0) sb.append(" | ");
      int n = rnd.nextInt(answers.size());
      sb.append(answers.get(n).trim());
      answers.remove(n);
    }
    sb.insert(0, '\n');
    sb.insert(0, question);
    return StringEscapeUtils.unescapeHtml4(sb.toString());
  }
Пример #13
0
  /**
   * Clean the given input text based on the original configuration Options. Optionally, don't
   * escape special characters.
   *
   * @param oinput The text to be cleaned. Can be any object. JSoup nodes are handled specially.
   * @param normalText If false, don't escape special characters. This is usually only used for
   *     inline code or code blocks, because they don't need to be escaped.
   * @return The cleaned text.
   */
  private String clean(Object oinput, boolean normalText) {
    String input;
    if (oinput instanceof TextNode) {
      input = getTextNodeText((TextNode) oinput, normalText);
    } else if (oinput instanceof Element) {
      if (normalText) {
        input = ((Element) oinput).text();
      } else {
        input = getPreformattedText((Element) oinput);
      }
    } else {
      input = oinput.toString();
    }
    String result;
    if (input.length() == 0) {
      // not seen, so just return an empty string.
      result = "";
    } else if (normalText) {
      // For non-code text, newlines are _never_ allowed.
      // Replace one or more set of whitespace chars followed by a newline with a single space.
      input = LINEBREAK_REMOVER.matcher(input).replaceAll(" ");

      // now escape special characters.
      for (final Escape rep : escapes) {
        input = rep.pattern.matcher(input).replaceAll(rep.replacement);
      }
      StringBuffer output = doReplacements(input, entityReplacementsPattern);
      if (unicodeReplacementsPattern != null) {
        output = doReplacements(output, unicodeReplacementsPattern);
      }
      result = output.toString();
    } else {
      // we have to revert ALL HTML entities for code, because they will end up
      // double-encoded by markdown
      // we also don't need to worry about escaping anything
      // note: we have to manually replace &apos; because it is ignored by StringEscapeUtils for
      // some reason.
      result = StringEscapeUtils.unescapeHtml4(input.replace("&apos;", "'"));
    }
    return result;
  }
Пример #14
0
 /**
  * 缩略字符串(不区分中英文字符)
  *
  * @param str 目标字符串
  * @param length 截取长度
  * @return
  */
 public static String abbr(String str, int length) {
   if (str == null) {
     return "";
   }
   try {
     StringBuilder sb = new StringBuilder();
     int currentLength = 0;
     for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) {
       currentLength += String.valueOf(c).getBytes("GBK").length;
       if (currentLength <= length - 3) {
         sb.append(c);
       } else {
         sb.append("...");
         break;
       }
     }
     return sb.toString();
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   }
   return "";
 }
Пример #15
0
 private String getInputString(SampleResult result) {
   String inputString =
       useUrl()
           ? result.getUrlAsString() // Bug 39707
           : useHeaders()
               ? result.getResponseHeaders()
               : useRequestHeaders()
                   ? result.getRequestHeaders()
                   : useCode()
                       ? result.getResponseCode() // Bug 43451
                       : useMessage()
                           ? result.getResponseMessage() // Bug 43451
                           : useUnescapedBody()
                               ? StringEscapeUtils.unescapeHtml4(result.getResponseDataAsString())
                               : useBodyAsDocument()
                                   ? Document.getTextFromDocument(result.getResponseData())
                                   : result.getResponseDataAsString() // Bug 36898
       ;
   if (log.isDebugEnabled()) {
     log.debug("Input = " + inputString);
   }
   return inputString;
 }
Пример #16
0
 /** @return {@true} if the input string contains HTML entities, {@code false} otherwise */
 public static boolean containsHtmlEntities(String s) {
   return !StringEscapeUtils.unescapeHtml4(s).equals(s);
 }
Пример #17
0
 public String getParamValue(String paramName) {
   if (paramMap.containsKey(paramName)) {
     return StringEscapeUtils.unescapeHtml4(paramMap.get(paramName).getValue());
   }
   return null;
 }
Пример #18
0
 /** Html 解码. */
 public static String unescapeHtml(String htmlEscaped) {
   return StringEscapeUtils.unescapeHtml4(htmlEscaped);
 }
Пример #19
0
 private String buildTagName(JFlexLexer lexer, String tocText) {
   // re-convert any &uuml; or other (converted by the parser) entities back
   String tagName = StringEscapeUtils.unescapeHtml4(tocText);
   return lexer.getParserInput().getTableOfContents().buildUniqueName(tagName);
 }
Пример #20
0
 public static String unescape(final String string) {
   if (string == null) return null;
   return StringEscapeUtils.unescapeHtml4(string);
 }
Пример #21
0
 /** @param separator the separator to set */
 public void setSeparator(String separator) {
   this.separator = separator == null ? null : separator.intern();
   unescapedSeparator =
       separator == null ? null : StringEscapeUtils.unescapeHtml4(separator).intern();
 }
Пример #22
0
 // Should unescape some weird symbols that gets escaped in HTML
 public static String unescapeHTML(String it) {
   return StringEscapeUtils.unescapeHtml4(it);
 }
Пример #23
0
  public static List<Map<String, String>> annotate(File infile, String... ignoreAuthors) {

    List<Map<String, String>> rows = new ArrayList<Map<String, String>>();
    List<String> blacklist = Arrays.asList(ignoreAuthors);
    ArrayList<String> empty = new ArrayList<String>();

    // String infile = getProperties().getProperty("transcript");
    if (infile.exists()) {
      rows = CSVReader.readCSV(infile);

      Iterator<Map<String, String>> rowIt = rows.iterator();
      String groupName = infile.getName().replaceAll("\\.csv", "");
      // groupName = groupName.replace("chem-f12-", "");
      groupName = groupName.replace("-", "");

      MessageEventLogger.setDefaultName("PostHoc " + groupName, false);

      while (rowIt.hasNext()) {
        Map<String, String> row = rowIt.next();
        String author = row.get("AUTHOR");
        String text = row.get("TEXT");
        String type = row.get("TYPE");
        if (type == null) type = "text";

        String timeString = row.get("DATE") + " " + row.get("TIME");
        Date stamp;
        try {
          stamp = timeFormat.parse(timeString);
        } catch (ParseException e) {
          try {
            stamp = fallbackTimeFormat.parse(timeString);
          } catch (ParseException p) {
            stamp = new Date();
            p.printStackTrace();
          }
        }

        MessageEvent me = new MessageEvent(null, author, text);

        text = text.replaceAll("LINEBREAK", "\n");
        text = StringEscapeUtils.unescapeHtml4(text); // TODO: add this to incoming chat
        me.setText(text);

        if (!text.equals("joins the room") && !text.equals("leaves the room")) {
          if (!blacklist.contains(author)) {
            mickey.preProcessEvent(null, me);
            ricky.preProcessEvent(null, me);
            albert.preProcessEvent(null, me);
            preston.preProcessEvent(null, me);
            alice.preProcessEvent(null, me);
          } else {
            String match = coulson.match(text);
            if (match != null) {
              me.addAnnotation(match, empty);
              me.addAnnotation("AT_MOVE", empty);
            }
          }
        }

        me.addAnnotation("TYPE=" + type, empty);

        // if(me.hasAnnotations())
        //	logger.log("TranscriptAnnotator", Logger.LOG_NORMAL, me.getAnnotationString());
        MessageEventLogger.logMessageEvent(me, stamp, groupName);
      }
    } else System.err.println("no transcript file " + infile);

    return rows;
  }