From 9f9849c0c3981be5e9d62f7b0a401233ae49ee44 Mon Sep 17 00:00:00 2001 From: Zac Spitzer Date: Thu, 21 Nov 2024 16:43:45 +0100 Subject: [PATCH] experiment with sniffing --- .../transformer/cfml/tag/CFMLTransformer.java | 59 +++++++++++++++---- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/lucee/transformer/cfml/tag/CFMLTransformer.java b/core/src/main/java/lucee/transformer/cfml/tag/CFMLTransformer.java index a407d4ebe1..c60fbce46d 100755 --- a/core/src/main/java/lucee/transformer/cfml/tag/CFMLTransformer.java +++ b/core/src/main/java/lucee/transformer/cfml/tag/CFMLTransformer.java @@ -150,22 +150,46 @@ public Page transform(Factory factory, ConfigPro config, PageSource ps, TagLib[] Charset charset = config.getTemplateCharset(); boolean dotUpper = ((MappingImpl) ps.getMapping()).getDotNotationUpperCase(); + //sc = new PageSourceCode(ps, charset, writeLog); + TagLibTag scriptTag = CFMLTransformer.getTLT(Constants.CFML_SCRIPT_TAG_NAME, config.getIdentification()); + TagLibTag componentTag = CFMLTransformer.getTLT(Constants.CFML_COMPONENT_TAG_NAME, config.getIdentification()); + + String fileExt = ResourceUtil.getExtension(ps.getResource(), ""); + + // could it be a component? + boolean isCFMLCompExt = Constants.isCFMLComponentExtension(fileExt); // parse regular while (true) { try { - sc = new PageSourceCode(ps, charset, writeLog); + // sc = new PageSourceCode(ps, charset, writeLog); + String srcText = ""; + boolean addCfscript = false; + if (Constants.isCFMLScriptExtension(fileExt)) { + addCfscript = true; + srcText = PageSourceCode.toString(ps, charset); + } else if (isCFMLCompExt) { + srcText = PageSourceCode.toString(ps, charset); + String srcTextLower = srcText.toLowerCase(); + // sniff to see if the cfc is already tag based + if (!srcTextLower.contains("<" + scriptTag.getFullName()) + && !srcTextLower.contains("<" + componentTag.getFullName()) + && !srcTextLower.contains("" + original.getText() + "\n"; + String text = "<" + scriptTag.getFullName() + ">" + srcText + "\n"; sc = new PageSourceCode(ps, text, charset, writeLog); + } else { + sc = new PageSourceCode(ps, charset, writeLog); } p = transform(factory, config, sc, tlibs, flibs, ps.getResource().lastModified(), dotUpper, returnValue, ignoreScopes); @@ -178,8 +202,7 @@ public Page transform(Factory factory, ConfigPro config, PageSource ps, TagLib[] } } - // could it be a component? - boolean isCFMLCompExt = Constants.isCFMLComponentExtension(ResourceUtil.getExtension(ps.getResource(), "")); + boolean possibleUndetectedComponent = false; @@ -191,7 +214,7 @@ public Page transform(Factory factory, ConfigPro config, PageSource ps, TagLib[] if (possibleUndetectedComponent) { Page _p; - TagLibTag scriptTag = CFMLTransformer.getTLT(sc, Constants.CFML_SCRIPT_TAG_NAME, config.getIdentification()); + //TagLibTag scriptTag = CFMLTransformer.getTLT(sc, Constants.CFML_SCRIPT_TAG_NAME, config.getIdentification()); sc.setPos(0); SourceCode original = sc; @@ -203,8 +226,8 @@ public Page transform(Factory factory, ConfigPro config, PageSource ps, TagLib[] try { while (true) { if (sc == null) { - sc = new PageSourceCode(ps, charset, writeLog); - text = "<" + scriptTag.getFullName() + ">" + sc.getText() + "\n"; + //sc = new PageSourceCode(ps, charset, writeLog); + text = "<" + scriptTag.getFullName() + ">" + PageSourceCode.toString(ps, charset) + "\n"; sc = new PageSourceCode(ps, text, charset, writeLog); } try { @@ -247,6 +270,18 @@ public static TagLibTag getTLT(SourceCode cfml, String name, Identification id) } } + public static TagLibTag getTLT(String name, Identification id) throws TemplateException { + TagLib tl; + try { + // this is already loaded, oherwise we where not here + tl = TagLibFactory.loadFromSystem(id); + return tl.getTag(name); + } + catch (TagLibException e) { + throw new TemplateException(e.getMessage()); + } + } + /** * Startmethode zum transfomieren einer CFMLString.
* EBNF: