Java HTML Jsoup Element getNameFromAttributes(org.jsoup.nodes.Element e)

Here you can find the source of getNameFromAttributes(org.jsoup.nodes.Element e)

Description

checks whether the attributes values to be used as variable name

License

Apache License

Parameter

Parameter Description
id a parameter
name a parameter

Declaration

public static String getNameFromAttributes(org.jsoup.nodes.Element e) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/* ww w  . j  av  a2s  .c  om*/
     * checks whether the attributes values
     * to be used as variable name
     * @param id
     * @param name
     */
    public static String getNameFromAttributes(org.jsoup.nodes.Element e) {

        String id = e.attr("id");
        String name = e.attr("name");
        String value = e.attr("value");
        String _class = e.attr("class");
        String action = e.attr("action");

        String text = e.text();
        text = text.replaceAll("\t", "_");
        text = text.replaceAll("\n", "_");
        text = text.replaceAll(" ", "_");
        text = text.replaceAll("-", "_");

        if (id.length() != 0) {
            id = id.replace("-", "_");
            return id;
        } else if (name.length() != 0) {
            return name;
        } else if (value.length() != 0) {
            return value;
        } else if (_class.length() != 0) {
            return _class;
        } else if (action.length() != 0) {
            // retrieves the page name 
            action = action.substring(action.lastIndexOf('/') + 1, action.length());
            // removes the php extension
            action = action.replace(".php", "");
            //removes the & and ?
            if (action.contains("?"))
                action = action.substring(0, action.indexOf('?'));
            // camel case the string
            action = toSentenceCase(action);
            return action;
        } else if (text.length() != 0) {
            if (text.length() > 35)
                text.substring(0, 10);
            return text;
        } else {
            System.err.println("[WARNING] Name not found: UtilsStaticAnalyzer.getNameFromAttributes");
            return "";
        }

    }

    /**
     * BETA VERSION: converts an URL to UpperCamelCase
     * TODO: test the correct behavior
     */
    public static String toSentenceCase(String inputString) {
        String result = "";
        if (inputString.length() == 0) {
            return result;
        }
        char firstChar = inputString.charAt(0);
        char firstCharToUpperCase = Character.toUpperCase(firstChar);
        result = result + firstCharToUpperCase;
        boolean terminalCharacterEncountered = false;
        char[] terminalCharacters = { '.', '?', '!', '_', ' ', '-' };
        for (int i = 1; i < inputString.length(); i++) {
            char currentChar = inputString.charAt(i);
            if (terminalCharacterEncountered) {
                if (currentChar == ' ') {
                    result = result + currentChar;
                } else {
                    char currentCharToUpperCase = Character.toUpperCase(currentChar);
                    result = result + currentCharToUpperCase;
                    terminalCharacterEncountered = false;
                }
            } else {
                char currentCharToLowerCase = Character.toLowerCase(currentChar);
                result = result + currentCharToLowerCase;
            }
            for (int j = 0; j < terminalCharacters.length; j++) {
                if (currentChar == terminalCharacters[j]) {
                    terminalCharacterEncountered = true;
                    break;
                }
            }
        }
        result = result.replaceAll("-", "");
        return result;
    }
}

Related

  1. getFormattedText(Element element)
  2. getIndustry(Element element)
  3. getInt(Elements td, int i)
  4. getLink(Element element, int index)
  5. getMoney(final Element container)
  6. getOneSubItemOfClass(Element parent, String className)
  7. getReviewAuthorIDFromMultipleSelect(Element doc, String location, String locationValue)
  8. getSingleElement(final Elements elements)
  9. getSrcOrRelFromImageElement(Element imgElm)