Search in sources :

Example 36 with Element

use of net.htmlparser.jericho.Element in project zaproxy by zaproxy.

the class ExtensionAntiCSRF method getTokenValue.

public String getTokenValue(HttpMessage tokenMsg, String tokenName) {
    Source source = new Source(tokenMsg.getResponseBody().toString());
    List<Element> formElements = source.getAllElements(HTMLElementName.FORM);
    if (formElements != null && formElements.size() > 0) {
        for (Element formElement : formElements) {
            List<Element> inputElements = formElement.getAllElements(HTMLElementName.INPUT);
            if (inputElements != null && inputElements.size() > 0) {
                // Loop through all of the INPUT elements
                for (Element inputElement : inputElements) {
                    String id = inputElement.getAttributeValue("ID");
                    if (id != null && id.equalsIgnoreCase(tokenName)) {
                        return inputElement.getAttributeValue("VALUE");
                    }
                    String name = inputElement.getAttributeValue("NAME");
                    if (name != null && name.equalsIgnoreCase(tokenName)) {
                        return inputElement.getAttributeValue("VALUE");
                    }
                }
            }
        }
    }
    return null;
}
Also used : Element(net.htmlparser.jericho.Element) Source(net.htmlparser.jericho.Source)

Example 37 with Element

use of net.htmlparser.jericho.Element in project zaproxy by zaproxy.

the class SpiderHtmlFormParser method parseResource.

@Override
public boolean parseResource(HttpMessage message, Source source, int depth) {
    getLogger().debug("Parsing an HTML message for forms...");
    // If form processing is disabled, don't parse anything
    if (!param.isProcessForm()) {
        return false;
    }
    // Prepare the source, if not provided
    if (source == null) {
        source = new Source(message.getResponseBody().toString());
    }
    // Get the context (base url)
    String baseURL = message.getRequestHeader().getURI().toString();
    uri = message.getRequestHeader().getURI();
    // Try to see if there's any BASE tag that could change the base URL
    Element base = source.getFirstElement(HTMLElementName.BASE);
    if (base != null) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Base tag was found in HTML: " + base.getDebugInfo());
        }
        String href = base.getAttributeValue("href");
        if (href != null && !href.isEmpty()) {
            baseURL = URLCanonicalizer.getCanonicalURL(href, baseURL);
        }
    }
    // Go through the forms
    List<Element> forms = source.getAllElements(HTMLElementName.FORM);
    for (Element form : forms) {
        // Clear the attributes for each form and store their key and values
        envAttributes.clear();
        for (Attribute att : form.getAttributes()) {
            envAttributes.put(att.getKey(), att.getValue());
        }
        // Get method and action
        String method = form.getAttributeValue("method");
        String action = form.getAttributeValue("action");
        getLogger().debug("Found new form with method: '" + method + "' and action: " + action);
        // If no action, skip the form
        if (action == null) {
            getLogger().debug("No form 'action' defined. Using base URL: " + baseURL);
            action = baseURL;
        }
        // If POSTing forms is not enabled, skip processing of forms with POST method
        if (!param.isPostForm() && method != null && method.trim().equalsIgnoreCase(METHOD_POST)) {
            getLogger().debug("Skipping form with POST method because of user settings.");
            continue;
        }
        // Clear the fragment, if any, as it does not have any relevance for the server
        if (action.contains("#")) {
            int fs = action.lastIndexOf("#");
            action = action.substring(0, fs);
        }
        url = URLCanonicalizer.getCanonicalURL(action, baseURL);
        FormData formData = prepareFormDataSet(source, form);
        // Process the case of a POST method
        if (method != null && method.trim().equalsIgnoreCase(METHOD_POST)) {
            // Build the absolute canonical URL
            String fullURL = URLCanonicalizer.getCanonicalURL(action, baseURL);
            if (fullURL == null) {
                return false;
            }
            getLogger().debug("Canonical URL constructed using '" + action + "': " + fullURL);
            for (String submitData : formData) {
                notifyPostResourceFound(message, depth, fullURL, submitData);
            }
        } else // Process anything else as a GET method
        {
            // Process the final URL
            if (action.contains("?")) {
                if (action.endsWith("?")) {
                    processGetForm(message, depth, action, baseURL, formData);
                } else {
                    processGetForm(message, depth, action + "&", baseURL, formData);
                }
            } else {
                processGetForm(message, depth, action + "?", baseURL, formData);
            }
        }
    }
    return false;
}
Also used : Attribute(net.htmlparser.jericho.Attribute) Element(net.htmlparser.jericho.Element) Source(net.htmlparser.jericho.Source)

Example 38 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class RelicsIndexPageParser method extractIcon.

private String extractIcon(Element td) {
    Element src = JerichoHtmlUtils.findElementByTagName(td, HTMLElementName.IMG);
    String iconPath = src.getAttributeValue("src");
    int index = iconPath.indexOf("/");
    if (index != -1) {
        iconPath = iconPath.substring(index + 1).trim();
    }
    return iconPath;
}
Also used : Element(net.htmlparser.jericho.Element)

Example 39 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class RelicsIndexPageParser method handleTable.

private void handleTable(String categoryName, Segment source, String id, Integer level, RelicType defaultType) {
    Element table = findRelicsTable(source, id);
    if (table != null) {
        RelicsCategory category = _relicsMgr.getRelicCategory(categoryName, true);
        List<Element> trs = JerichoHtmlUtils.findElementsByTagName(table, HTMLElementName.TR);
        for (Element tr : trs) {
            Relic relic = handleTableRow(tr, level, defaultType);
            if (relic != null) {
                category.addRelic(relic);
            }
        }
    } else {
        System.err.println("Cannot find table: " + id);
    }
}
Also used : Relic(delta.games.lotro.lore.items.legendary.relics.Relic) RelicsCategory(delta.games.lotro.lore.items.legendary.relics.RelicsCategory) Element(net.htmlparser.jericho.Element)

Example 40 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class RecipePageParser method parseRecipePage.

/**
 * Parse the recipe page at the given URL.
 * @param url URL of recipe page.
 * @return A list of recipes or <code>null</code> if an error occurred.
 */
public List<Recipe> parseRecipePage(String url) {
    List<Recipe> recipes = null;
    try {
        // url="http://lorebook.lotro.com/wiki/Recipe:Ancient_Steel_Scholar%27s_Glass";
        // url="http://lorebook.lotro.com/wiki/Recipe:Heavy_Cotton_Armour"; // Multiple output
        DownloadService downloader = DownloadService.getInstance();
        String page = downloader.getPage(url);
        // File f=new File("D:\\dam\\dev\\perso\\lotro\\docs\\recipes\\Glass.html");
        // File f=new File("D:\\dam\\dev\\perso\\lotro\\docs\\recipes\\Chisel.html");
        // String page=TextUtils.loadTextFile(f,EncodingNames.ISO8859_1);
        Source source = new Source(page);
        // <div id="lorebookNoedit">
        Element lorebook = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.DIV, "id", "lorebookNoedit");
        if (lorebook != null) {
            // identifier
            // <a id="ca-nstab-recipe" class="lorebook_action_link" href="/wiki/Recipe:Sage%5C%27s_Sharp_Chisel">Article</a></div>
            _key = null;
            Element articleLink = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.A, "id", "ca-nstab-recipe");
            if (articleLink != null) {
                String thisURL = articleLink.getAttributeValue("href");
                if ((thisURL != null) && (thisURL.startsWith(RECIPE_URL_SEED))) {
                    _key = thisURL.substring(RECIPE_URL_SEED.length()).trim();
                }
            }
            int tier = findTier(source);
            recipes = new ArrayList<Recipe>();
            List<Element> recipeSections = JerichoHtmlUtils.findElementsByTagNameAndAttributeValue(lorebook, HTMLElementName.DIV, "class", "lorebookclass");
            if ((recipeSections != null) && (recipeSections.size() > 0)) {
                for (Element recipeSection : recipeSections) {
                    Recipe recipe = parseRecipeSection(recipeSection);
                    if (recipe != null) {
                        recipes.add(recipe);
                        recipe.setKey(_key);
                        if (tier != 0) {
                            recipe.setTier(tier);
                        }
                    }
                }
            }
            findIdentifiers(recipes);
            for (Recipe recipe : recipes) {
                System.out.println("Recipe: ");
                System.out.println(recipe.dump());
            }
        }
    } catch (Exception e) {
        recipes = null;
        _logger.error("Cannot parse quest page [" + url + "]", e);
    }
    return recipes;
}
Also used : Recipe(delta.games.lotro.lore.crafting.recipes.Recipe) Element(net.htmlparser.jericho.Element) DownloadService(delta.games.lotro.utils.DownloadService) InputSource(org.xml.sax.InputSource) Source(net.htmlparser.jericho.Source)

Aggregations

Element (net.htmlparser.jericho.Element)70 Source (net.htmlparser.jericho.Source)17 DownloadService (delta.games.lotro.utils.DownloadService)11 ArrayList (java.util.ArrayList)11 Segment (net.htmlparser.jericho.Segment)6 InputSource (org.xml.sax.InputSource)6 Context (com.cflint.plugins.Context)4 Matcher (java.util.regex.Matcher)4 StartTag (net.htmlparser.jericho.StartTag)4 BasicStatsSet (delta.games.lotro.character.stats.BasicStatsSet)3 Item (delta.games.lotro.lore.items.Item)3 List (java.util.List)3 Attribute (net.htmlparser.jericho.Attribute)3 CFScriptStatement (cfml.parsing.cfscript.script.CFScriptStatement)2 ParseException (cfml.parsing.reporting.ParseException)2 CFLintScanException (com.cflint.exception.CFLintScanException)2 ContextMessage (com.cflint.plugins.Context.ContextMessage)2 Money (delta.games.lotro.common.Money)2 CraftingResult (delta.games.lotro.lore.crafting.recipes.CraftingResult)2 Ingredient (delta.games.lotro.lore.crafting.recipes.Ingredient)2