use of lucee.transformer.util.PageSourceCode in project Lucee by lucee.
the class CFMLTransformer method transform.
/**
* Startmethode zum transfomieren einer CFML Datei.
* <br />
* EBNF:<br />
* <code>{body}</code>
* @param config
* @param ps CFML File
* @param tlibs Tag Library Deskriptoren, nach denen innerhalb der CFML Datei geprueft werden soll.
* @param flibs Function Library Deskriptoren, nach denen innerhalb der Expressions der CFML Datei geprueft werden soll.
* @param returnValue if true the method returns the value of the last expression executed inside when you call the method "call"
* @return uebersetztes CFXD Dokument Element.
* @throws TemplateException
* @throws IOException
*/
public Page transform(Factory factory, ConfigImpl config, PageSource ps, TagLib[] tlibs, FunctionLib[] flibs, boolean returnValue, boolean ignoreScopes) throws TemplateException, IOException {
Page p;
SourceCode sc;
boolean writeLog = config.getExecutionLogEnabled();
Charset charset = config.getTemplateCharset();
boolean dotUpper = ps.getDialect() == CFMLEngine.DIALECT_CFML && ((MappingImpl) ps.getMapping()).getDotNotationUpperCase();
// parse regular
while (true) {
try {
sc = new PageSourceCode(ps, charset, writeLog);
p = transform(factory, config, sc, tlibs, flibs, ps.getResource().lastModified(), dotUpper, returnValue, ignoreScopes);
break;
} catch (ProcessingDirectiveException pde) {
if (pde.getWriteLog() != null)
writeLog = pde.getWriteLog().booleanValue();
if (pde.getDotNotationUpperCase() != null)
dotUpper = pde.getDotNotationUpperCase().booleanValue();
if (!StringUtil.isEmpty(pde.getCharset()))
charset = pde.getCharset();
}
}
// could it be a component?
boolean isCFML = ps.getDialect() == CFMLEngine.DIALECT_CFML;
boolean isCFMLCompExt = isCFML && Constants.isCFMLComponentExtension(ResourceUtil.getExtension(ps.getResource(), ""));
boolean possibleUndetectedComponent = false;
// we don't have a component or interface
if (p.isPage()) {
if (isCFML)
possibleUndetectedComponent = isCFMLCompExt;
else if (Constants.isLuceeComponentExtension(ResourceUtil.getExtension(ps.getResource(), ""))) {
Expression expr;
Statement stat;
PrintOut po;
LitString ls;
List<Statement> statements = p.getStatements();
// check the root statements for component
Iterator<Statement> it = statements.iterator();
String str;
while (it.hasNext()) {
stat = it.next();
if (stat instanceof PrintOut && (expr = ((PrintOut) stat).getExpr()) instanceof LitString) {
ls = (LitString) expr;
str = ls.getString();
if (str.indexOf(Constants.LUCEE_COMPONENT_TAG_NAME) != -1 || str.indexOf(Constants.LUCEE_INTERFACE_TAG_NAME) != -1 || // cfml name is supported as alias
str.indexOf(Constants.CFML_COMPONENT_TAG_NAME) != -1) {
possibleUndetectedComponent = true;
break;
}
}
}
}
}
/*if(p.isPage() && (isCFML?
Constants.isCFMLComponentExtension(ResourceUtil.getExtension(ps.getResource(),"")):
Constants.isLuceeComponentExtension(ResourceUtil.getExtension(ps.getResource(),""))) &&
isPossibleRawScript(sc,config.getIdentification())){*/
if (possibleUndetectedComponent) {
Page _p;
TagLibTag scriptTag = CFMLTransformer.getTLT(sc, isCFML ? Constants.CFML_SCRIPT_TAG_NAME : Constants.LUCEE_SCRIPT_TAG_NAME, config.getIdentification());
sc.setPos(0);
SourceCode original = sc;
// try inside a cfscript
String text = "<" + scriptTag.getFullName() + ">" + original.getText() + "\n</" + scriptTag.getFullName() + ">";
sc = new PageSourceCode(ps, text, charset, writeLog);
try {
while (true) {
if (sc == null) {
sc = new PageSourceCode(ps, charset, writeLog);
text = "<" + scriptTag.getFullName() + ">" + sc.getText() + "\n</" + scriptTag.getFullName() + ">";
sc = new PageSourceCode(ps, text, charset, writeLog);
}
try {
_p = transform(factory, config, sc, tlibs, flibs, ps.getResource().lastModified(), dotUpper, returnValue, ignoreScopes);
break;
} catch (ProcessingDirectiveException pde) {
if (pde.getWriteLog() != null)
writeLog = pde.getWriteLog().booleanValue();
if (pde.getDotNotationUpperCase() != null)
dotUpper = pde.getDotNotationUpperCase().booleanValue();
if (!StringUtil.isEmpty(pde.getCharset()))
charset = pde.getCharset();
sc = null;
}
}
} catch (ComponentTemplateException e) {
throw e.getTemplateException();
}
// we only use that result if it is a component now
if (_p != null && !_p.isPage())
return _p;
}
if (isCFMLCompExt && !p.isComponent() && !p.isInterface()) {
String msg = "template [" + ps.getDisplayPath() + "] must contain a component or an interface.";
if (sc != null)
throw new TemplateException(sc, msg);
throw new TemplateException(msg);
}
return p;
}
use of lucee.transformer.util.PageSourceCode in project Lucee by lucee.
the class Component method isInsideCITemplate.
/**
* is the template ending with a component extension?
* @param page
* @return return true if so false otherwse and null if the code is not depending on a template
*/
private Boolean isInsideCITemplate(Page page) {
SourceCode sc = page.getSourceCode();
if (!(sc instanceof PageSourceCode))
return null;
PageSource psc = ((PageSourceCode) sc).getPageSource();
String src = psc.getDisplayPath();
return Constants.isComponentExtension(ResourceUtil.getExtension(src, ""));
// int pos=src.lastIndexOf(".");
// return pos!=-1 && pos<src.length() && src.substring(pos+1).equals(Constants.COMPONENT_EXTENSION);
}
use of lucee.transformer.util.PageSourceCode in project Lucee by lucee.
the class PageEncoding method execute.
@Override
public TagLib execute(Config config, Tag tag, TagLibTag libTag, FunctionLib[] flibs, Data data) throws TemplateException {
// encoding
String str = ASMUtil.getAttributeString(tag, "charset", null);
if (str == null)
throw new TemplateException(data.srcCode, "attribute [pageencoding] of the tag [processingdirective] must be a constant value");
Charset cs = CharsetUtil.toCharset(str);
PageSourceCode psc = data.srcCode instanceof PageSourceCode ? (PageSourceCode) data.srcCode : null;
if (psc == null || cs.equals(psc.getCharset()) || CharsetUtil.UTF8.equals(psc.getCharset())) {
cs = null;
}
if (cs != null) {
throw new ProcessingDirectiveException(data.srcCode, cs, null, data.srcCode.getWriteLog());
}
return null;
}
use of lucee.transformer.util.PageSourceCode in project Lucee by lucee.
the class AbstrCFMLScriptTransformer method funcStatement.
/**
* Liest ein function Statement ein.
* <br />
* EBNF:<br />
* <code>identifier spaces "(" spaces identifier spaces {"," spaces identifier spaces} ")" spaces block;</code>
* @return function Statement
* @throws TemplateException
*/
private final Statement funcStatement(ExprData data, Body parent) throws TemplateException {
int pos = data.srcCode.getPos();
// read 5 tokens (returntype,access modifier,"abstract|final|static","function", function name)
String str = variableDec(data, false);
// if there is no token at all we have no function
if (str == null) {
data.srcCode.setPos(pos);
return null;
}
comments(data);
String[] tokens = new String[] { str, null, null, null, null };
tokens[1] = variableDec(data, false);
comments(data);
if (tokens[1] != null) {
tokens[2] = variableDec(data, false);
comments(data);
if (tokens[2] != null) {
tokens[3] = variableDec(data, false);
comments(data);
if (tokens[3] != null) {
tokens[4] = identifier(data, false);
comments(data);
}
}
}
// function name
String functionName = null;
for (int i = tokens.length - 1; i >= 0; i--) {
// first from right is the function name
if (tokens[i] != null) {
functionName = tokens[i];
tokens[i] = null;
break;
}
}
if (functionName == null || functionName.indexOf(',') != -1 || functionName.indexOf('[') != -1) {
data.srcCode.setPos(pos);
return null;
}
// throw new TemplateException(data.srcCode, "invalid syntax");
String returnType = null;
// search for "function"
boolean hasOthers = false, first = true;
for (int i = tokens.length - 1; i >= 0; i--) {
if ("function".equalsIgnoreCase(tokens[i])) {
// if it is the first "function" (from right) and we had already something else, the syntax is broken!
if (hasOthers && first)
throw new TemplateException(data.srcCode, "invalid syntax");
else // we already have a return type,so this is the 3th "function"!
if (returnType != null)
throw new TemplateException(data.srcCode, "invalid syntax");
else if (!first)
returnType = tokens[i];
first = false;
tokens[i] = null;
} else if (tokens[i] != null) {
hasOthers = true;
}
}
// no "function" found
if (first) {
data.srcCode.setPos(pos);
return null;
}
// access modifier
int _access, access = -1;
for (int i = 0; i < tokens.length; i++) {
if (tokens[i] != null && (_access = ComponentUtil.toIntAccess(tokens[i], -1)) != -1) {
// we already have an access modifier
if (access != -1) {
// we already have a return type
if (returnType != null)
throw new TemplateException(data.srcCode, "invalid syntax");
returnType = tokens[i];
} else
access = _access;
tokens[i] = null;
}
}
// no access defined
if (access == -1)
access = Component.ACCESS_PUBLIC;
// Non access modifier
int _modifier, modifier = Component.MODIFIER_NONE;
boolean isStatic = false;
for (int i = 0; i < tokens.length; i++) {
if (tokens[i] != null) {
_modifier = ComponentUtil.toModifier(tokens[i], Component.MODIFIER_NONE, Component.MODIFIER_NONE);
// abstract|final
if (_modifier != Component.MODIFIER_NONE) {
// we already have an Non access modifier
if (modifier != Component.MODIFIER_NONE || isStatic) {
// we already have a return type
if (returnType != null)
throw new TemplateException(data.srcCode, "invalid syntax");
returnType = tokens[i];
} else
modifier = _modifier;
tokens[i] = null;
} else // static
if (tokens[i].equalsIgnoreCase("static")) {
// we already have an Non access modifier
if (modifier != Component.MODIFIER_NONE || isStatic) {
// we already have a return type
if (returnType != null)
throw new TemplateException(data.srcCode, "invalid syntax");
returnType = tokens[i];
} else
isStatic = true;
tokens[i] = null;
}
}
}
// return type
for (int i = 0; i < tokens.length; i++) {
if (tokens[i] != null) {
if (returnType != null)
throw new TemplateException(data.srcCode, "invalid syntax");
returnType = tokens[i];
}
}
Position line = data.srcCode.getPosition();
// Name
if (!data.isCFC && !data.isInterface) {
FunctionLibFunction flf = getFLF(data, functionName);
try {
if (flf != null && flf.getFunctionClassDefinition().getClazz() != CFFunction.class) {
PageSource ps = null;
if (data.srcCode instanceof PageSourceCode) {
ps = ((PageSourceCode) data.srcCode).getPageSource();
}
String path = null;
if (ps != null) {
path = ps.getDisplayPath();
path = path.replace('\\', '/');
}
if (// TODO make better
path == null || path.indexOf("/library/function/") == -1)
throw new TemplateException(data.srcCode, "The name [" + functionName + "] is already used by a built in Function");
}
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
throw new PageRuntimeException(Caster.toPageException(t));
}
}
Function res = closurePart(data, functionName, access, modifier, returnType, line, false);
if (isStatic) {
if (data.context == CTX_INTERFACE)
throw new TemplateException(data.srcCode, "static functions are not allowed within the interface body");
TagOther tag = createStaticTag(data, res.getStart());
tag.getBody().addStatement(res);
return tag;
}
return res;
}
Aggregations