Example usage for org.jsoup.nodes Document head

List of usage examples for org.jsoup.nodes Document head

Introduction

In this page you can find the example usage for org.jsoup.nodes Document head.

Prototype

public Element head() 

Source Link

Document

Accessor to the document's head element.

Usage

From source file:cognition.common.utils.StringTools.java

public static String addMetaDataToHtml(String htmlText, String metaData) {
    Document doc = Jsoup.parse(htmlText);
    doc.head().append(metaData);

    return doc.html();
}

From source file:cognition.common.utils.StringTools.java

public static String getMetaDataFromHTML(String html) {
    Document doc = Jsoup.parse(html);
    return doc.head().html();
}

From source file:com.greenpepper.maven.plugin.SpecificationRunnerMojo.java

public static void recoverLinkInResult(String specification, String htmlString, RepositoryIndex repositoryIndex)
        throws IOException {
    RepositoryIndex.SpecificationInfo specificationInfo = repositoryIndex.getNameToInfo().get(specification);
    if (isBlank(specificationInfo.getLink()) && isNotBlank(htmlString)) {
        LOGGER.trace("got new missing link in index for '{}'. trying to find it in the result output",
                specification);//from www  .ja va2 s.  c om
        org.jsoup.nodes.Document resultOutput = Jsoup.parse(htmlString);
        Elements metaTags = resultOutput.head().getElementsByTag("meta");
        String link = metaTags.select("[name=\"external-link\"]").attr("content");
        if (isNotBlank(link)) {
            LOGGER.trace("Found {}", link);
            specificationInfo.setLink(link);
            repositoryIndex.dump();
        }
    }
}

From source file:com.gistlabs.mechanize.document.html.HtmlDocument.java

@Override
protected void loadPage() throws Exception {
    Document jsoup = Jsoup.parse(getInputStream(), getContentEncoding(response), getUri());
    setBaseUri(jsoup.head().baseUri());
    this.htmlElements = new HtmlElements(this, jsoup);
}

From source file:nl.detoren.ijsco.io.OSBOLoader.java

public Spelers laadWebsite(String url) {
    try {//w w  w  . j a va  2  s.  c o  m
        //Document doc = Jsoup.connect("http://osbo.nl/jeugd/jrating.htm").get();
        //String url = "http://osbo.nl/jeugd/jrating.htm";
        //String url = "http://ijsco.schaakverenigingdetoren.nl/ijsco1718/IJSCOrating1718.htm";
        Document doc = Jsoup.connect(url).get();
        doc.head().appendElement("meta").attr("charset", "UTF-8");
        doc.head().appendElement("meta").attr("http-equiv", "Content-Type").attr("content", "text/html");
        //Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
        //URI baseURI=new URI(url);
        //String content=IOUtils.toString(stream,"utf-8");
        //Document doc=Jsoup.parse(content,baseurl);      
        return load(doc);
    } catch (Exception e) {
        logger.log(Level.WARNING, "Error loading OSBO spelers \" + e.getMessage()");
        System.out.println("Error loading OSBO spelers " + e.getMessage());
    }
    return null;
}

From source file:org.jboss.tools.windup.ui.internal.issues.IssueDetailsView.java

public static void addPrism(Document doc) {
    try {// w ww  .j a v a  2  s .  com
        Bundle bundle = WindupUIPlugin.getDefault().getBundle();
        Elements codeElements = doc.getElementsByTag("code");
        codeElements.forEach(element -> {
            Set<String> classNames = element.classNames();
            Set<String> newNames = Sets.newHashSet();
            classNames.forEach(className -> {
                // prismjs requires prefix, i'm not sure about another/easier workaround.
                newNames.add("language-" + className);
            });
            element.classNames(newNames);
        });

        DocumentType type = new DocumentType("html", "", "", "");
        doc.insertChildren(0, Lists.newArrayList(type));

        Element head = doc.head();
        Element css = doc.createElement("link");

        URL fileURL = FileLocator.find(bundle, new Path("html/prism.css"), null);
        String srcPath = FileLocator.resolve(fileURL).getPath();

        css.attr("href", srcPath);
        css.attr("rel", "stylesheet");
        head.appendChild(css);

        Element body = doc.body();
        Element script = doc.createElement("script");

        fileURL = FileLocator.find(bundle, new Path("html/prism.js"), null);
        srcPath = FileLocator.resolve(fileURL).getPath();

        script.attr("src", srcPath);
        body.appendChild(script);
    } catch (Exception e) {
        WindupUIPlugin.log(e);
    }
}

From source file:org.uberfire.server.locale.GWTLocaleHeaderFilter.java

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {

    final CharResponseWrapper wrappedResponse = getWrapper((HttpServletResponse) response);
    chain.doFilter(request, wrappedResponse);

    final String output;

    final Locale locale = getLocale(request);
    final String injectedScript = "<meta name=\"gwt:property\" content=\"locale=" + locale.toString() + "\">";

    final Document document = Jsoup.parse(wrappedResponse.toString());
    document.head().append(injectedScript);
    output = document.html();/*from  ww  w .ja v a  2  s. c o m*/

    final byte[] outputBytes = output.getBytes("UTF-8");
    response.setContentLength(outputBytes.length);
    response.getWriter().print(output);
}

From source file:pe.chalk.takoyaki.target.NaverCafe.java

public NaverCafe(JSONObject properties) {
    super(properties.getString("prefix"), properties.getLong("interval"));
    this.getFilters()
            .addAll(Utils.buildStream(String.class, properties.getJSONArray("filters")).map(filterName -> {
                switch (filterName) {
                case ArticleFilter.NAME:
                    return new ArticleFilter(this);

                case CommentaryFilter.NAME:
                    return new CommentaryFilter(this);

                case VisitationFilter.NAME:
                    return new VisitationFilter(this);

                default:
                    return null;
                }/*w w  w .java 2  s.  co  m*/
            }).filter(filter -> filter != null).collect(Collectors.toList()));

    this.staff = new Staff(this.getLogger(), properties.getInt("timeout"),
            properties.getJSONObject("naverAccount"));
    this.address = properties.getString("address");
    this.contentUrl = String.format(STRING_CONTENT, this.getAddress());

    try {
        Document contentDocument = Jsoup.parse(this.getStaff().parse(this.contentUrl));
        this.setName(contentDocument.select("h1.d-none").text());

        Matcher clubIdMatcher = NaverCafe.PATTERN_CLUB_ID
                .matcher(contentDocument.head().select("script:not([type]):not([src])").first().html());
        if (!clubIdMatcher.find()) {
            throw new IllegalArgumentException(" ID ?  : " + this.getName());
        }

        this.clubId = Integer.parseInt(clubIdMatcher.group(1));
        this.menus = contentDocument.select("a[id^=menuLink]").stream()
                .map(element -> new Menu(this, Integer.parseInt(element.id().substring(8)), element.text()))
                .collect(Collectors.toList());

        this.articleUrl = String.format(STRING_ARTICLE, this.getClubId());

        Files.write(Paths.get("Takoyaki-menus-" + this.getAddress() + ".log"),
                this.getMenus().stream().map(Menu::toString).collect(Collectors.toList()),
                StandardCharsets.UTF_8);
    } catch (IOException | JSONException e) {
        String errorMessage = "?? : " + e.getClass().getName() + ": "
                + e.getMessage();

        this.getLogger().error(errorMessage);
        throw new IllegalStateException(errorMessage);
    }
}

From source file:poe.trade.assist.SearchForm.java

private String removeAllExceptSearchForm(String html) {
    String htmlDirectory = htmlDirectory();
    Document doc = Jsoup.parse(html);

    // Remove stuff outside of id="main"
    //      doc.body().children().stream().filter(e -> !"main".equalsIgnoreCase(e.id())).forEach(e -> e.remove());

    Element head = doc.head();

    // Replace everthing in the <head>
    head.children().stream().forEach(e -> e.remove());
    head.appendElement("meta").attr("charset", "utf-8");
    head.appendElement("meta").attr("name", "viewport").attr("content", "width=device-width");
    head.appendElement("title").text("poe.trade.assist");
    head.appendElement("script").attr("type", "text/javascript").attr("src", htmlDirectory + "packed.js");
    head.appendElement("link").attr("rel", "stylesheet").attr("href", htmlDirectory + "packed_dark.css");

    // Show search form
    Optional.ofNullable(doc.getElementById("search-form")).ifPresent(e -> e.attr("style", ""));

    Optional.ofNullable(doc.getElementById("search"))
            .ifPresent(e -> e.attr("action", "http://poe.trade/search"));
    //      //w  ww  .  j  a  va 2 s .c  o m
    //      Element mainElement = doc.getElementById("main");
    //      Element topDivContainer = mainElement.child(0);
    //
    //      // Remove everthing that is not id="content" or h2
    //      topDivContainer.children().stream()
    //         .filter(e -> !"content".equalsIgnoreCase(e.id()))
    //         .filter(e -> !e.tag().getName().equalsIgnoreCase("h2"))
    //         .forEach(e -> e.remove());
    //      
    //      // Clean up stuff inside id="content"
    //      
    //         // Remove "Show search form", "search/import"
    //         Optional<Element> searchFormElem = doc.getElementsByTag("a").stream().filter(e -> e.hasClass("button") && e.hasClass("secondary") && e.hasClass("expand")).findFirst();
    //         searchFormElem.ifPresent(e -> e.remove());
    //         
    //         Optional<Element> searchOrImportDiv = doc.getElementsByTag("div").stream().filter(e -> e.hasClass("row") && e.hasClass("form-choose-action")).findFirst();
    //         searchOrImportDiv.ifPresent(e -> e.remove());
    //
    //         // Remove search results
    Elements searchResultBlocks = doc.getElementsByClass("search-results-block");
    if (searchResultBlocks.size() > 0) {
        searchResultBlocks.get(0).remove();
    }

    // append assist as the last element in body
    //       doc.body().appendElement("script").attr("type", "text/javascript").attr("src", htmlDirectory + "assist.js");

    String cleanHtml = doc.toString();
    return cleanHtml;
}

From source file:poe.trade.assist.SearchView.java

private String addHeadElements(String html) {
    String htmlDirectory = htmlDirectory();
    Document doc = Jsoup.parse(html);
    Element head = doc.head();

    // Replace everthing in the <head>
    head.children().stream().forEach(e -> e.remove());
    head.appendElement("meta").attr("charset", "utf-8");
    head.appendElement("meta").attr("name", "viewport").attr("content", "width=device-width");
    head.appendElement("title").text("poe.trade.assist");
    head.appendElement("script").attr("type", "text/javascript").attr("src", htmlDirectory + "packed.js");
    head.appendElement("link").attr("rel", "stylesheet").attr("href", htmlDirectory + "packed_dark.css");

    doc.body().appendElement("script").attr("type", "text/javascript").attr("src", htmlDirectory + "assist.js");

    String cleanHTML = doc.toString();
    //      try {
    //         FileUtils.writeStringToFile(new File("test"), cleanHTML);
    //      } catch (IOException e1) {
    //         // TODO Auto-generated catch block
    //         e1.printStackTrace();
    //      }//ww  w . ja v  a2  s  .c o  m
    return cleanHTML;
}