Search in sources :

Example 1 with SourceFormatter

use of net.htmlparser.jericho.SourceFormatter in project jahia by Jahia.

the class JahiaContentManagementServiceImpl method drawPortletInstanceOutput.

@Override
public GWTJahiaPortletOutputBean drawPortletInstanceOutput(String windowID, String entryPointIDStr, String pathInfo, String queryString) {
    GWTJahiaPortletOutputBean result = new GWTJahiaPortletOutputBean();
    try {
        int fieldId = Integer.parseInt(windowID);
        String portletOutput = ServicesRegistry.getInstance().getApplicationsDispatchService().getAppOutput(fieldId, entryPointIDStr, getRemoteJahiaUser(), getRequest(), getResponse(), getServletContext(), getWorkspace());
        try {
            JCRNodeWrapper node = JCRSessionFactory.getInstance().getCurrentUserSession().getNodeByUUID(entryPointIDStr);
            String nodeTypeName = node.getPrimaryNodeTypeName();
            /**
             * todo cleanup the hardcoded value here
             */
            if ("jnt:htmlPortlet".equals(nodeTypeName)) {
                result.setInIFrame(false);
            }
            if ("jnt:contentPortlet".equals(nodeTypeName) || "jnt:rssPortlet".equals(nodeTypeName)) {
                result.setInContentPortlet(true);
            }
        } catch (RepositoryException e) {
            logger.error(e.getMessage(), e);
        }
        result.setHtmlOutput(portletOutput);
        // what we need to do now is to do special processing for <script> tags, and on the client side we will
        // create them dynamically.
        Source source = new Source(portletOutput);
        source = new Source((new SourceFormatter(source)).toString());
        List<StartTag> scriptTags = source.getAllStartTags(HTMLElementName.SCRIPT);
        for (StartTag curScriptTag : scriptTags) {
            if ((curScriptTag.getAttributeValue("src") != null) && (!curScriptTag.getAttributeValue("src").equals(""))) {
                result.getScriptsWithSrc().add(curScriptTag.getAttributeValue("src"));
            } else {
                result.getScriptsWithCode().add(curScriptTag.getElement().getContent().toString());
            }
        }
    } catch (JahiaException e) {
        logger.error(e.getMessage(), e);
    }
    return result;
}
Also used : JahiaException(org.jahia.exceptions.JahiaException) SourceFormatter(net.htmlparser.jericho.SourceFormatter) Source(net.htmlparser.jericho.Source) StartTag(net.htmlparser.jericho.StartTag)

Example 2 with SourceFormatter

use of net.htmlparser.jericho.SourceFormatter in project jahia by Jahia.

the class SourceFormatterFilter method execute.

@Override
public String execute(String previousOut, RenderContext renderContext, Resource resource, RenderChain chain) throws Exception {
    long timer = System.currentTimeMillis();
    final SourceFormatter sourceFormatter = new SourceFormatter(new Source(previousOut));
    sourceFormatter.setIndentString("  ");
    String out = sourceFormatter.toString();
    if (logger.isDebugEnabled()) {
        logger.debug("Formatting took {} ms", System.currentTimeMillis() - timer);
    }
    return out;
}
Also used : SourceFormatter(net.htmlparser.jericho.SourceFormatter) Source(net.htmlparser.jericho.Source)

Example 3 with SourceFormatter

use of net.htmlparser.jericho.SourceFormatter in project jahia by Jahia.

the class DiffHelper method getHighlighted.

public String getHighlighted(String original, String amendment) {
    final StringWriter sw = new StringWriter();
    try {
        final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) JahiaTransformerFactory.newInstance();
        final TransformerHandler result = transformerFactory.newTransformerHandler();
        result.setResult(new StreamResult(sw));
        final XslFilter filter = new XslFilter();
        // replace /live/ by /default/ in href and src attributes as it represents same image
        if (original.contains("/files/" + Constants.EDIT_WORKSPACE + "/") || amendment.contains("/files/" + Constants.EDIT_WORKSPACE + "/")) {
            original = LIVE_WS_PATTERN.matcher(original).replaceAll("/" + Constants.EDIT_WORKSPACE + "/");
            amendment = LIVE_WS_PATTERN.matcher(amendment).replaceAll("/" + Constants.EDIT_WORKSPACE + "/");
        }
        original = new SourceFormatter(new Source(original)).toString();
        amendment = new SourceFormatter(new Source(amendment)).toString();
        final ContentHandler postProcess = filter.xsl(result, "jahiahtmlheader.xsl");
        final Locale locale = Locale.ENGLISH;
        final String prefix = "diff";
        final HtmlCleaner cleaner = new HtmlCleaner();
        final InputSource oldSource = new InputSource(new StringReader(original));
        final InputSource newSource = new InputSource(new StringReader(amendment));
        final DomTreeBuilder oldHandler = new DomTreeBuilder();
        cleaner.cleanAndParse(oldSource, oldHandler);
        final TextNodeComparator leftComparator = new TextNodeComparator(oldHandler, locale);
        final DomTreeBuilder newHandler = new DomTreeBuilder();
        cleaner.cleanAndParse(newSource, newHandler);
        final TextNodeComparator rightComparator = new TextNodeComparator(newHandler, locale);
        postProcess.startDocument();
        postProcess.startElement("", "diffreport", "diffreport", new AttributesImpl());
        addDiffCss(postProcess);
        postProcess.startElement("", "diff", "diff", new AttributesImpl());
        final HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(postProcess, prefix);
        final HTMLDiffer differ = new HTMLDiffer(output);
        differ.diff(rightComparator, leftComparator);
        postProcess.endElement("", "diff", "diff");
        postProcess.endElement("", "diffreport", "diffreport");
        postProcess.endDocument();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return sw.toString();
}
Also used : Locale(java.util.Locale) TransformerHandler(javax.xml.transform.sax.TransformerHandler) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) SourceFormatter(net.htmlparser.jericho.SourceFormatter) HTMLDiffer(org.outerj.daisy.diff.html.HTMLDiffer) InputSource(org.xml.sax.InputSource) Source(net.htmlparser.jericho.Source) ContentHandler(org.xml.sax.ContentHandler) HtmlCleaner(org.outerj.daisy.diff.HtmlCleaner) SAXException(org.xml.sax.SAXException) TextNodeComparator(org.outerj.daisy.diff.html.TextNodeComparator) HtmlSaxDiffOutput(org.outerj.daisy.diff.html.HtmlSaxDiffOutput) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) XslFilter(org.outerj.daisy.diff.XslFilter) DomTreeBuilder(org.outerj.daisy.diff.html.dom.DomTreeBuilder) StringReader(java.io.StringReader)

Example 4 with SourceFormatter

use of net.htmlparser.jericho.SourceFormatter in project ramus by Vitaliy-Yakovchuk.

the class HTMLView method setHTMLText.

public void setHTMLText(String htmlText) {
    try {
        this.text = htmlText;
        if (formatt) {
            Source segment = new Source(text);
            segment.fullSequentialParse();
            SourceFormatter formatter = new SourceFormatter(segment);
            htmlText = formatter.toString();
        }
        editorPane.read(new StringReader(htmlText), null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : StringReader(java.io.StringReader) IOException(java.io.IOException) SourceFormatter(net.htmlparser.jericho.SourceFormatter) Source(net.htmlparser.jericho.Source)

Aggregations

Source (net.htmlparser.jericho.Source)4 SourceFormatter (net.htmlparser.jericho.SourceFormatter)4 StringReader (java.io.StringReader)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Locale (java.util.Locale)1 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)1 TransformerHandler (javax.xml.transform.sax.TransformerHandler)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StartTag (net.htmlparser.jericho.StartTag)1 JahiaException (org.jahia.exceptions.JahiaException)1 HtmlCleaner (org.outerj.daisy.diff.HtmlCleaner)1 XslFilter (org.outerj.daisy.diff.XslFilter)1 HTMLDiffer (org.outerj.daisy.diff.html.HTMLDiffer)1 HtmlSaxDiffOutput (org.outerj.daisy.diff.html.HtmlSaxDiffOutput)1 TextNodeComparator (org.outerj.daisy.diff.html.TextNodeComparator)1 DomTreeBuilder (org.outerj.daisy.diff.html.dom.DomTreeBuilder)1 ContentHandler (org.xml.sax.ContentHandler)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1