Search in sources :

Example 6 with LagartoParser

use of jodd.lagarto.LagartoParser in project jodd by oblac.

the class Jspp method process.

// ---------------------------------------------------------------- process
/**
	 * Processes input JSP content and replace macros.
	 */
public String process(final String input) {
    LagartoParser lagartoParser = new LagartoParser(input, true);
    final MutableInteger lastPosition = new MutableInteger(0);
    final StringBuilder sb = new StringBuilder();
    lagartoParser.parse(new EmptyTagVisitor() {

        @Override
        public void tag(Tag tag) {
            if (tag.getType() == TagType.SELF_CLOSING) {
                if (tag.matchTagNamePrefix(tagPrefix)) {
                    int tagStart = tag.getTagPosition();
                    sb.append(input.substring(lastPosition.get(), tagStart));
                    String tagName = tag.getName().toString();
                    tagName = tagName.substring(tagPrefix.length);
                    String macroBody = loadMacro(tagName);
                    int attrCount = tag.getAttributeCount();
                    for (int i = 0; i < attrCount; i++) {
                        String key = macroPrefix + tag.getAttributeName(i) + macroSuffix;
                        macroBody = StringUtil.replace(macroBody, key, tag.getAttributeValue(i).toString());
                    }
                    sb.append(macroBody);
                    lastPosition.set(tagStart + tag.getTagLength());
                }
            }
        }
    });
    sb.append(input.substring(lastPosition.get()));
    return sb.toString();
}
Also used : EmptyTagVisitor(jodd.lagarto.EmptyTagVisitor) MutableInteger(jodd.mutable.MutableInteger) Tag(jodd.lagarto.Tag) LagartoParser(jodd.lagarto.LagartoParser)

Example 7 with LagartoParser

use of jodd.lagarto.LagartoParser in project jmeter by apache.

the class LagartoBasedHtmlParser method getEmbeddedResourceURLs.

@Override
public Iterator<URL> getEmbeddedResourceURLs(String userAgent, byte[] html, URL baseUrl, URLCollection coll, String encoding) throws HTMLParseException {
    try {
        Float ieVersion = extractIEVersion(userAgent);
        String contents = new String(html, encoding);
        // As per Jodd javadocs, emitStrings should be false for visitor for better performances
        LagartoParser lagartoParser = new LagartoParser(contents, false);
        LagartoParserConfig<LagartoParserConfig<?>> config = new LagartoParserConfig<>();
        config.setCaseSensitive(false);
        // Conditional comments only apply for IE < 10
        config.setEnableConditionalComments(isEnableConditionalComments(ieVersion));
        lagartoParser.setConfig(config);
        JMeterTagVisitor tagVisitor = new JMeterTagVisitor(new URLPointer(baseUrl), coll, ieVersion);
        lagartoParser.parse(tagVisitor);
        return coll.iterator();
    } catch (LagartoException e) {
        // TODO is it the best way ? https://bz.apache.org/bugzilla/show_bug.cgi?id=55634
        if (log.isDebugEnabled()) {
            log.debug("Error extracting embedded resource URLs from:'" + baseUrl + "', probably not text content, message:" + e.getMessage());
        }
        return Collections.<URL>emptyList().iterator();
    } catch (Exception e) {
        throw new HTMLParseException(e);
    }
}
Also used : LagartoParserConfig(jodd.lagarto.LagartoParserConfig) LagartoException(jodd.lagarto.LagartoException) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) LagartoException(jodd.lagarto.LagartoException) LagartoParser(jodd.lagarto.LagartoParser)

Aggregations

LagartoParser (jodd.lagarto.LagartoParser)7 TagWriter (jodd.lagarto.TagWriter)2 Test (org.junit.Test)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 EmptyTagVisitor (jodd.lagarto.EmptyTagVisitor)1 LagartoException (jodd.lagarto.LagartoException)1 LagartoParserConfig (jodd.lagarto.LagartoParserConfig)1 Tag (jodd.lagarto.Tag)1 MutableInteger (jodd.mutable.MutableInteger)1