Java HTML Parse Jsoup getImageCredit(String html)

Here you can find the source of getImageCredit(String html)

Description

get Image Credit

License

Apache License

Declaration

public static String getImageCredit(String html) 

Method Source Code

//package com.java2s;
/*//from w w w  .ja v a 2 s .c om
 Copyright 2015 Joseph Tranquillo {name of copyright owner}

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.util.ArrayList;
import org.jsoup.Jsoup;

public class Main {
    private static final String EXPLANATION = "Explanation:";
    private final static String[] CREDIT_STRINGS = { "Image Credit:",
            "Picture Credit:", "Credit and Copyright",
            "Image Credit & Copyright", "Credit & Copyright",
            "Credit & License", "Credit", "Copyright", "Courtesy",
            "Image Data", };
    private static ArrayList<Integer> indices;

    public static String getImageCredit(String html) {
        String text = Jsoup.parse(html).text();

        indices = new ArrayList<>();

        int credit_index = -1;
        int credit_length = -1;

        for (String credit : CREDIT_STRINGS) {
            indices.add(new Integer(credit_index = text.indexOf(credit)));

            if (credit_index != -1) {
                credit_length = credit.length() + 1;

                break;
            }
        }

        if (credit_index == -1) {
            return null;
        }

        int indexof_explanation = text.indexOf(EXPLANATION);

        String credit = null;

        credit = text.substring(credit_index + credit_length,
                indexof_explanation);

        credit = credit.replace("\n", " ");
        credit = credit.replace("[s]+", " ");
        credit = credit.trim();

        return credit;
    }
}

Related

  1. getFirstSentence(final String html)
  2. getFirstTableFromHTML(String result)
  3. getHtml(String url, String ruta_fich)
  4. getHtmlBodyContent(String html)
  5. getHtmlInTag(String html, String tag)
  6. getJSFileLinks(String html)
  7. getMetaValue(String html, String metaKey)
  8. getPlainText(String htmlText)
  9. getPlainTextFromHtml(String html)