Package org.olat.core.util

Examples of org.olat.core.util.SimpleHtmlParser


   * when puttings js to the end or menachism like ext.onReady is needed
   */
  private String injectJavaScript(String page, boolean addCheckForInlineEvents) {
    StringOutput sb = new StringOutput();
    //do not use parser and just check for css and script stuff myself and append just before body and head
    SimpleHtmlParser parser = new SimpleHtmlParser(page);
    if (!parser.isValidHtml()) {
      return page;
    }

    String docType = parser.getHtmlDocType();   
    if (docType != null) sb.append(docType).append("\n");
    if (parser.getXhtmlNamespaces() == null) sb.append("<html><head>");
    else {
      sb.append(parser.getXhtmlNamespaces());
      sb.append("<head><meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\">");//neded to allow body onload attribute
    }
    //<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=");
    if (parser.getCharsetName() != null) sb.append(parser.getCharsetName());
    else sb.append(DEFAULT_ENCODING);
    sb.append("\"");
    if (docType != null && docType.indexOf("XHTML") > 0) sb.append("/"); // close tag only when xhtml to validate
    sb.append(">");
   
    if (!parser.hasOwnCss()) {
      // add olat content css as used in html editor
      sb.append("<link href=\"").append(themeBaseUri).append("all/content.css\" rel=\"stylesheet\" type=\"text/css\" ");
      if (docType != null && docType.indexOf("XHTML") > 0) sb.append("/"); // close tag only when xhtml to validate
      sb.append(">\n");
      if (customCssURL != null) {
        // add the custom  CSS, e.g. the course css that overrides the standard content css
        sb.append("<link href=\"").append(customCssURL).append("\" rel=\"stylesheet\" type=\"text/css\" ");
        if (docType != null && docType.indexOf("XHTML") > 0) sb.append("/"); // close tag only when xhtml to validate
        sb.append(">\n");       
      }
    }
   
    //TODO:gs:a do not include if it is a scorm packge!! may results in problems
    if (this.enableTextmarking) {
      if (log.isDebug()) log.debug("Textmarking is enabled, including tooltips js files into iframe source...");
      sb.append("\n<script type=\"text/javascript\" src=\"");
      StaticMediaDispatcher.renderStaticURI(sb, "js/prototype/prototype.js");
      sb.append("\"></script>");
      sb.append("\n<script type=\"text/javascript\" src=\"");
      StaticMediaDispatcher.renderStaticURI(sb, "js/ext/adapter/prototype/ext-prototype-adapter.js");
      sb.append("\"></script>");
      sb.append("\n<link rel=\"stylesheet\" type=\"text/css\" href=\"");
      StaticMediaDispatcher.renderStaticURI(sb, "js/ext/resources/css/ext-all.css");
      sb.append("\"");
      if (docType != null && docType.indexOf("XHTML") > 0) sb.append("/"); // close tag only when xhtml to validate
      // Loading ExtJS minimalisic, only what's needed for the quick tips
      sb.append(">\n<script type=\"text/javascript\" src=\"");
      StaticMediaDispatcher.renderStaticURI(sb, "js/ext/pkgs/ext-foundation.js");
      sb.append("\"></script>");
      sb.append("\n<script type=\"text/javascript\" src=\"");
      StaticMediaDispatcher.renderStaticURI(sb, "js/ext/pkgs/cmp-foundation.js");
      sb.append("\"></script>");
      sb.append("\n<script type=\"text/javascript\" src=\"");
      StaticMediaDispatcher.renderStaticURI(sb, "js/ext/pkgs/ext-dd.js");
      sb.append("\"></script>");
      sb.append("\n<script type=\"text/javascript\" src=\"");
      StaticMediaDispatcher.renderStaticURI(sb, "js/ext/pkgs/pkg-tips.js");
      sb.append("\"></script>");
      // Load glossary code now     
      sb.append("\n<script type=\"text/javascript\" id=\"textmarkerLib\" src=\"");
      sb.append( getWindowControl().getWindowBackOffice().getWindowManager().getMapPathFor(GlossaryMarkupItemController.class) ) ;
      sb.append("/js/glossaryhighlighter.js");
      sb.append("\"></script>");
     
      sb.append("\n<link rel=\"stylesheet\" type=\"text/css\" id=\"textmarkercss\" href=\"");
      sb.append( getWindowControl().getWindowBackOffice().getWindowManager().getMapPathFor(GlossaryMarkupItemController.class) ) ;
      sb.append("/css/textmarker.css");
      sb.append("\">\n");
    }
   
    // Load some iframe.js helper code
    sb.append("\n<script type=\"text/javascript\">\n/* <![CDATA[ */\n");
    // Set the iframe id, used by the resize function. Important to set before iframe.js is loaded
    sb.append("b_iframeid=\"").append(this.frameId).append("\";");
    sb.append("b_isInlineUri=").append(Boolean.valueOf(addCheckForInlineEvents).toString()).append(";");
    sb.append("\n/* ]]> */\n</script>");
    sb.append("<script type=\"text/javascript\" src=\"").append(staticFilesPath).append("/js/iframe.js\"></script>\n");

    // Resize frame to fit height of html page.
    // Do this only when there is some content available. This can be false when
    // the content is written all dynamically via javascript. In this cases, the
    // resizeing is meaningless anyway.
    if (parser.getHtmlContent().length() > 0) {
      sb.append("\n<script type=\"text/javascript\">\n/* <![CDATA[ */\n");
      // register the resize code to be executed on document load and click events
      if (adjusteightAutomatically) {
        sb.append("b_addOnloadEvent(b_sizeIframe);");   
        sb.append("b_addOnclickEvent(b_sizeIframe);");   
      }
      // register the tooltips enabling on document load event
      sb.append("b_addOnloadEvent(b_enableTooltips);");
      sb.append("b_addOnloadEvent(b_hideExtMessageBox);");
      if (addCheckForInlineEvents) {
        // Refresh dirty menu tree by triggering client side request to component which fires events
        // which is not possible by mappers. The method will first check if the page is loaded in our
        // iframe and ignore all other requests (files in framesets, sub-iframes, AJAX calls etc)
        if ((System.currentTimeMillis() - this.suppressEndlessReload) > 2000) sb.append("b_addOnloadEvent(b_sendNewUriEventToParent);");
        this.suppressEndlessReload = System.currentTimeMillis();
      }
      sb.append("b_addOnloadEvent(b_changeLinkTargets);");
     
      if (this.enableTextmarking){
        sb.append("b_addOnloadEvent(b_glossaryHighlight);");
      }
     
      sb.append("\n/* ]]> */\n</script>");
    }   
   
   
    String origHTMLHead = parser.getHtmlHead();
    // jsMath brute force approach to render latex formulas: add library if
    // a jsmath class is found in the code and the library is not already in
    // the header of the page
    if (BaseChiefController.isJsMathEnabled()) {
      if ((page.indexOf("class=\"math\"") != -1 || page.indexOf("class='math'") != -1) && (origHTMLHead == null || origHTMLHead.indexOf("jsMath/easy/load.js") == -1)) {
        sb.append("\n<script type=\"text/javascript\" src=\"");
        StaticMediaDispatcher.renderStaticURI(sb, "js/jsMath/easy/load.js");
        sb.append("\"></script>");     
        // don't show jsmath info box, aready visible in parent window
        sb.append("<style type='text/css'>#jsMath_button {display:none}</style>");     
      }     
    }

    // add some custom header things like js code or css
    if (customHeaderContent  != null) {
      sb.append(customHeaderContent);
    }

    // Add HTML header stuff from original page: css, javascript, title etc.
    if (origHTMLHead != null) sb.append(origHTMLHead);   
    sb.append("</head>");
    // use the original body tag, may include all kind of attributes (class, style, onload, on...)
    sb.append(parser.getBodyTag());
    // finally add content and finish page
    sb.append(parser.getHtmlContent());
    sb.append("</body></html>");
   
    return sb.toString();
  }
View Full Code Here


    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 {
      htmlContent = data; // if not a full html file, just take the original
    }
  }
View Full Code Here

        } catch (IOException e) {
          Tracing.logWarn("Problems when tunneling URL::" + tureq.getUri(), e, TunnelComponent.class);
          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
        }
        meth.releaseConnection();
        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 + "'.";
      }
    }
View Full Code Here

        body = meth.getResponseBodyAsString();
      } catch (IOException e) {
        Tracing.logWarn("Problems when tunneling URL::" + tureq.getUri(), e, TunnelComponent.class);
        return null;
      }
      SimpleHtmlParser parser = new SimpleHtmlParser(body);
      if (!parser.isValidHtml()) { // this is not valid HTML, deliver
        // asynchronuous
        return new HttpRequestMediaResource(meth);
      }
      meth.releaseConnection();
      htmlHead = parser.getHtmlHead();
      jsOnLoad = parser.getJsOnLoad();
      htmlContent = parser.getHtmlContent();
      setDirty(true);
    } else return new HttpRequestMediaResource(meth); // this is a async browser
    // refetch
    return null;
  }
View Full Code Here

 
    if (htmlContent == null || htmlContent.length() == 0) {
      htmlContent = "";
    }
    // TODO:gs:a create method in parser that delivers the full header untouched
    SimpleHtmlParser parser = new SimpleHtmlParser(htmlContent);
 
    // now get the body part
    return parser.getHtmlContent();
  }
View Full Code Here

    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();
  }
View Full Code Here

TOP

Related Classes of org.olat.core.util.SimpleHtmlParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.