Example usage for org.apache.commons.lang3 StringEscapeUtils unescapeHtml4

List of usage examples for org.apache.commons.lang3 StringEscapeUtils unescapeHtml4

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringEscapeUtils unescapeHtml4.

Prototype

public static final String unescapeHtml4(final String input) 

Source Link

Document

Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes.

Usage

From source file:silvertrout.plugins.titlegiver.TitleGiver.java

/**
 *
 * @param url//  w w  w  .ja v  a 2s.c o  m
 * @param connection
 * @param server
 * @param port
 * @param file
 * @return
 */
public static String getTitle(String url, String connection, String server, int port, String file) {

    /* Set current port */
    if (port == 0) {
        if (connection.equals("https")) {
            port = 443;
        } else {
            port = 80;
        }
    }

    // Find title
    String page = ConnectHelper.Connect(connection, server, file, port, maxContentLength * 10, null, null);
    if (page != null) {
        String titlePattern = "(?is)<title>([^<]+)";
        Pattern pt = Pattern.compile(titlePattern);
        Matcher mt = pt.matcher(page);

        if (mt.find()) {
            String title = mt.group(1);
            return StringEscapeUtils.unescapeHtml4(title.replaceAll("\\s+", " ").trim());
        } else if (server.equals("t.co")) {
            // TODO(reggna): Move this to the ConnectHelper, and do proper
            // META "redirects".  Be careful not to not create a redirect
            // loop like this though.
            Pattern pattern = Pattern.compile("<META http-equiv=\"refresh\"(?:[^URL]+)URL=([^\"]+)");
            Matcher matcher = pattern.matcher(page);
            if (matcher.find()) {
                List<String> titles = getTitles(matcher.group(1));
                if (titles.size() > 0) {
                    return titles.get(0);
                }
            }
        }
        System.out.println("No title found");
    }
    System.out.println("Could be no content (if no title found)");
    System.out.println("Failed on fetching title for some reason");
    return null;

}

From source file:silvertrout.plugins.trace.OperatorFinder.java

static private String getOperator(String areaCode, String number) throws Exception {

    // Pre step/*  w ww .  j av  a 2s.  co  m*/
    // ================================================================
    HttpURLConnection l = (HttpURLConnection) (new URL("https://pts.siriusit.net/net/PTS/etjansterStart"
            + "/svenska/Om_Webbplatsen/Nummerkapacitet_r" + "/Enskilt+nummer").openConnection());
    l.setDoInput(true);
    l.setDoOutput(false);

    // Fetch cookies needed for later.
    String cookies = "";
    for (Map.Entry<String, List<String>> header : l.getHeaderFields().entrySet()) {
        if (header.getKey() != null && header.getKey().equals("Set-Cookie")) {
            for (String s : header.getValue()) {
                cookies += s.substring(0, s.indexOf(";")) + "; ";
            }
        }
    }
    cookies = cookies.substring(0, cookies.length() - 2);

    // Lookup step
    // ================================================================
    URL url = new URL(URL_SERVER + "/" + URL_PATH);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    //con.setFollowRedirects(true);
    con.setDoOutput(true);
    con.setRequestProperty("Cookie", cookies);
    con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");

    //con.setRequestMethod("POST");

    // Post request
    OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
    String outdata = "action=search" + "&ndc=" + URLEncoder.encode(areaCode.substring(1), "UTF-8") + "&number="
            + URLEncoder.encode(number, "UTF-8") + "&search=" + URLEncoder.encode("Sk", "UTF-8") + "\r\n";
    out.write(outdata);
    out.close();

    // Read in response
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

    String result = "", decodedString = "";
    while ((decodedString = in.readLine()) != null) {
        result += decodedString;
    }
    in.close();

    // Find number
    int spos = result.indexOf(TAG_START) + TAG_START.length();
    number = result.substring(spos);
    int epos = number.indexOf(TAG_END);
    number = number.substring(0, epos);

    return StringEscapeUtils.unescapeHtml4(number);
}

From source file:silvertrout.plugins.trace.Trace.java

public static String substring(String s, String start, String end) {
    if (s.contains(start))
        s = s.substring(s.indexOf(start) + start.length());
    else//www .j av a 2  s.  c  o m
        return "";

    if (s.contains(end))
        return StringEscapeUtils.unescapeHtml4(s.substring(0, s.indexOf(end)));
    else
        return "";
}

From source file:spntoolsdata.pdf.util.XMLtoHtml.java

public static String checkHTML(String htmlString) throws IOException {

    String checkedhtml = null;//from  w ww. j  av a  2 s .com
    try {
        String value = new String(Jsoup.parse(htmlString, "UTF-8").html().getBytes(), "UTF-8");
        checkedhtml = StringEscapeUtils.unescapeHtml4(value);
    } catch (Exception ex) {
        throw ex;
    }

    return checkedhtml;
}

From source file:spring.travel.site.services.news.NewsItemDigestible.java

public void setStandFirst(String standFirst) {
    String unescaped = StringEscapeUtils.unescapeHtml4(standFirst);
    if (unescaped.startsWith("<p>")) {
        unescaped = unescaped.substring(3, unescaped.indexOf('<', 3));
    } else {/*from  ww w  . j a  v a  2s . c o m*/
        unescaped = unescaped.substring(0, unescaped.indexOf('<'));
    }
    this.standFirst = stripWhitespace(unescaped).trim();
}

From source file:sturesy.util.web.WebCommands.java

/**
 * Updates the Lecturetype/*from  w ww  .j  a  v  a  2s  .c  om*/
 * 
 * @param lecturename
 * @param password
 * @param model
 */
public static String updateAnswerCommand(String lecturename, String password, QuestionModel model) {
    final String command = "command=update&name=%s&pwd=%s&type=%s&count=%s&text=%s&answers=%s";

    final String type = model.hasCorrectAnswer() ? "1" : "-1";

    final String questiontext = encode(StringEscapeUtils
            .escapeHtml4(StringEscapeUtils.unescapeHtml4((HTMLStripper.stripHTML2(model.getQuestion())))));
    final String jsonAnswers = encode(getJSONAnswerTexts(model.getAnswers()));
    final String formatted = String.format(command, encode(lecturename), encode(password), type,
            model.getAnswerSize(), questiontext, jsonAnswers);

    return formatted;
}

From source file:sturesy.util.web.WebCommands2.java

public static String prepareForSend(String s) {
    String stripped = HTMLStripper.stripHTML2(s);
    return StringEscapeUtils.escapeHtml4(StringEscapeUtils.unescapeHtml4(stripped));
}

From source file:sturesy.voting.chartcontroller.AChoiceBarController.java

/**
 * Creates a Dataset containing percent values
 * /*  ww w .j  a  v  a 2 s.c o  m*/
 * @param votes
 *            array of votes
 * @param answers
 *            List of answer strings
 * @return CategoryDataset containing percent values
 */
protected CategoryDataset createDatasetPercent(double[] votes, List<String> answers) {
    final DefaultCategoryDataset result = new DefaultCategoryDataset();

    int all = 0;
    for (int i = 0; i < votes.length; i++) {
        all += votes[i];
    }

    for (int i = 0; i < votes.length; i++) {

        double value = votes[i] * 100 / all;

        value = MathUtils.roundToDecimals(value, 1);

        String answer = StringEscapeUtils.unescapeHtml4(HTMLStripper.stripHTML2(answers.get(i)));
        String columnKey = (char) ('A' + i) + ": " + answer;
        result.addValue(value, "SERIES", columnKey);
    }

    return result;
}

From source file:sturesy.voting.chartcontroller.AChoiceBarController.java

/**
 * Creates a Dataset containing absolute values
 * /*from  w  ww.  j  a va2s  . c om*/
 * @param votes
 *            array of votes
 * @param answers
 *            List of answer strings
 * @return CategoryDataset containing absolute values
 */
protected CategoryDataset createDatasetAbsolute(double[] votes, List<String> answers) {
    final DefaultCategoryDataset result = new DefaultCategoryDataset();

    for (int i = 0; i < votes.length; i++) {
        String answer = StringEscapeUtils.unescapeHtml4(HTMLStripper.stripHTML2(answers.get(i)));
        String columnKey = (char) ('A' + i) + ": " + answer;
        result.addValue(votes[i], "SERIES", columnKey);
    }
    return result;
}

From source file:sturesy.voting.chartcontroller.MultipleChoiceBarChartController.java

@Override
public void updateUI() {
    double[] votesarr = prepareVotesForDataSet(_question.getAnswerSize(), _votes);

    CategoryDataset dataset = createDataSet(votesarr, _question.getAnswers());

    final String unescapeHtml4 = StringEscapeUtils
            .unescapeHtml4(HTMLStripper.stripHTML2(_question.getQuestion()));

    _gui.createNewChartPanel(dataset, unescapeHtml4, _backgroundcolor, _showCorrectAnswer,
            _question.getCorrectAnswers(), _showPercent);
}