Search in sources :

Example 6 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project OpenOLAT by OpenOLAT.

the class HTMLEditorController method parsePage.

/**
 * Internal helper to parse the page content
 * @param vfsLeaf
 * @return String containing the page body
 */
private String parsePage(VFSLeaf vfsLeaf) {
    // Load data with given encoding
    InputStream is = vfsLeaf.getInputStream();
    if (is == null) {
        throw new AssertException("Could not open input stream for file::" + getFileDebuggingPath(this.baseContainer, this.fileRelPath));
    }
    this.charSet = SimpleHtmlParser.extractHTMLCharset(vfsLeaf);
    String leafData = FileUtils.load(is, charSet);
    if (leafData == null || leafData.length() == 0) {
        leafData = "";
    }
    int generatorPos = leafData.indexOf(GENERATOR);
    SimpleHtmlParser parser = new SimpleHtmlParser(leafData);
    StringBuilder sb = new StringBuilder();
    if (parser.getHtmlDocType() != null)
        sb.append(parser.getHtmlDocType());
    if (parser.getXhtmlNamespaces() != null) {
        sb.append(parser.getXhtmlNamespaces());
    } else {
        sb.append(CLOSE_HTML);
    }
    sb.append(OPEN_HEAD);
    // include generator so foreign editor warning only appears once
    if (generatorPos == -1)
        sb.append(GENERATOR_META);
    if (parser.getHtmlHead() != null)
        sb.append(parser.getHtmlHead());
    sb.append(CLOSE_HEAD);
    sb.append(parser.getBodyTag());
    preface = sb.toString();
    // warn if the file has no generator metadata and is not empty
    if (generatorPos == -1 && leafData.length() > 0) {
        if (editorCheckEnabled)
            showWarning("warn.foreigneditor");
    // else ignore warning but try to keep header anyway
    } else if (leafData.length() == 0) {
        // set new one when file created with this editor
        preface = null;
    }
    // now get the body part
    return parser.getHtmlContent();
}
Also used : AssertException(org.olat.core.logging.AssertException) InputStream(java.io.InputStream) SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 7 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project OpenOLAT by OpenOLAT.

the class HtmlStaticPageComponent method getFileContent.

/**
 * Retreives the file from the filesystem and parses the file.
 */
private void getFileContent(VFSLeaf sourceFile) {
    if (currentURI == null)
        return;
    String encoding = SimpleHtmlParser.extractHTMLCharset(sourceFile);
    String data = FileUtils.load(sourceFile.getInputStream(), encoding);
    SimpleHtmlParser parser = new SimpleHtmlParser(data);
    htmlHead = parser.getHtmlHead();
    jsOnLoad = parser.getJsOnLoad();
    if (parser.isValidHtml()) {
        htmlContent = parser.getHtmlContent();
    } else {
        // if not a full html file, just take the original
        htmlContent = data;
    }
}
Also used : SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 8 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project openolat by klemens.

the class TunnelComponent method fetchFirstResource.

private void fetchFirstResource(UserRequest ureq) {
    // config, ureq);
    TURequest tureq = new TURequest();
    // not used
    tureq.setContentType(null);
    tureq.setMethod("GET");
    tureq.setParameterMap(Collections.<String, String[]>emptyMap());
    tureq.setQueryString(query);
    if (startUri != null) {
        if (startUri.startsWith("/")) {
            tureq.setUri(startUri);
        } else {
            tureq.setUri("/" + startUri);
        }
    }
    fillTURequestWithUserInfo(tureq, ureq);
    HttpResponse response = fetch(tureq, httpClientInstance);
    if (response == null) {
        setFetchError();
    } else {
        Header responseHeader = response.getFirstHeader("Content-Type");
        String mimeType;
        if (responseHeader == null) {
            setFetchError();
            mimeType = null;
        } else {
            mimeType = responseHeader.getValue();
        }
        if (mimeType != null && mimeType.startsWith("text/html")) {
            // we have html content, let doDispatch handle it for
            // inline rendering, update hreq for next content request
            String body;
            try {
                body = EntityUtils.toString(response.getEntity());
            } catch (IOException e) {
                log.warn("Problems when tunneling URL::" + tureq.getUri(), e);
                htmlContent = "Error: cannot display inline :" + tureq.getUri() + ": Unknown transfer problem '";
                return;
            }
            SimpleHtmlParser parser = new SimpleHtmlParser(body);
            if (!parser.isValidHtml()) {
            // this is not valid HTML, deliver
            // asynchronuous
            }
            htmlHead = parser.getHtmlHead();
            jsOnLoad = parser.getJsOnLoad();
            htmlContent = parser.getHtmlContent();
        } else {
            htmlContent = "Error: cannot display inline :" + tureq.getUri() + ": mime type was '" + mimeType + "' but expected 'text/html'. Response header was '" + responseHeader + "'.";
        }
    }
}
Also used : TURequest(org.olat.course.nodes.tu.TURequest) Header(org.apache.http.Header) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 9 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project openolat by klemens.

the class HTMLEditorController method parsePage.

/**
 * Internal helper to parse the page content
 * @param vfsLeaf
 * @return String containing the page body
 */
private String parsePage(VFSLeaf vfsLeaf) {
    // Load data with given encoding
    InputStream is = vfsLeaf.getInputStream();
    if (is == null) {
        throw new AssertException("Could not open input stream for file::" + getFileDebuggingPath(this.baseContainer, this.fileRelPath));
    }
    this.charSet = SimpleHtmlParser.extractHTMLCharset(vfsLeaf);
    String leafData = FileUtils.load(is, charSet);
    if (leafData == null || leafData.length() == 0) {
        leafData = "";
    }
    int generatorPos = leafData.indexOf(GENERATOR);
    SimpleHtmlParser parser = new SimpleHtmlParser(leafData);
    StringBuilder sb = new StringBuilder();
    if (parser.getHtmlDocType() != null)
        sb.append(parser.getHtmlDocType());
    if (parser.getXhtmlNamespaces() != null) {
        sb.append(parser.getXhtmlNamespaces());
    } else {
        sb.append(CLOSE_HTML);
    }
    sb.append(OPEN_HEAD);
    // include generator so foreign editor warning only appears once
    if (generatorPos == -1)
        sb.append(GENERATOR_META);
    if (parser.getHtmlHead() != null)
        sb.append(parser.getHtmlHead());
    sb.append(CLOSE_HEAD);
    sb.append(parser.getBodyTag());
    preface = sb.toString();
    // warn if the file has no generator metadata and is not empty
    if (generatorPos == -1 && leafData.length() > 0) {
        if (editorCheckEnabled)
            showWarning("warn.foreigneditor");
    // else ignore warning but try to keep header anyway
    } else if (leafData.length() == 0) {
        // set new one when file created with this editor
        preface = null;
    }
    // now get the body part
    return parser.getHtmlContent();
}
Also used : AssertException(org.olat.core.logging.AssertException) InputStream(java.io.InputStream) SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Example 10 with SimpleHtmlParser

use of org.olat.core.util.SimpleHtmlParser in project openolat by klemens.

the class HtmlStaticPageComponent method getFileContent.

/**
 * Retreives the file from the filesystem and parses the file.
 */
private void getFileContent(VFSLeaf sourceFile) {
    if (currentURI == null)
        return;
    String encoding = SimpleHtmlParser.extractHTMLCharset(sourceFile);
    String data = FileUtils.load(sourceFile.getInputStream(), encoding);
    SimpleHtmlParser parser = new SimpleHtmlParser(data);
    htmlHead = parser.getHtmlHead();
    jsOnLoad = parser.getJsOnLoad();
    if (parser.isValidHtml()) {
        htmlContent = parser.getHtmlContent();
    } else {
        // if not a full html file, just take the original
        htmlContent = data;
    }
}
Also used : SimpleHtmlParser(org.olat.core.util.SimpleHtmlParser)

Aggregations

SimpleHtmlParser (org.olat.core.util.SimpleHtmlParser)10 IOException (java.io.IOException)4 Header (org.apache.http.Header)4 HttpResponse (org.apache.http.HttpResponse)4 TURequest (org.olat.course.nodes.tu.TURequest)4 InputStream (java.io.InputStream)2 HttpRequestMediaResource (org.olat.core.gui.media.HttpRequestMediaResource)2 AssertException (org.olat.core.logging.AssertException)2