http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltIn.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltIn.java b/src/main/java/org/apache/freemarker/core/ast/BuiltIn.java deleted file mode 100644 index 5d25b3f..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltIn.java +++ /dev/null @@ -1,486 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - -import org.apache.freemarker.core.Configuration; -import org.apache.freemarker.core.ast.BuiltInsForDates.iso_BI; -import org.apache.freemarker.core.ast.BuiltInsForDates.iso_utc_or_local_BI; -import org.apache.freemarker.core.ast.BuiltInsForMarkupOutputs.markup_stringBI; -import org.apache.freemarker.core.ast.BuiltInsForMultipleTypes.is_dateLikeBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.ancestorsBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.childrenBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.nextSiblingBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.node_nameBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.node_namespaceBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.node_typeBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.parentBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.previousSiblingBI; -import org.apache.freemarker.core.ast.BuiltInsForNodes.rootBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.absBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.byteBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.ceilingBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.doubleBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.floatBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.floorBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.intBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.is_infiniteBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.is_nanBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.longBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.number_to_dateBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.roundBI; -import org.apache.freemarker.core.ast.BuiltInsForNumbers.shortBI; -import org.apache.freemarker.core.ast.BuiltInsForOutputFormatRelated.escBI; -import org.apache.freemarker.core.ast.BuiltInsForOutputFormatRelated.no_escBI; -import org.apache.freemarker.core.ast.BuiltInsForSequences.chunkBI; -import org.apache.freemarker.core.ast.BuiltInsForSequences.firstBI; -import org.apache.freemarker.core.ast.BuiltInsForSequences.lastBI; -import org.apache.freemarker.core.ast.BuiltInsForSequences.reverseBI; -import org.apache.freemarker.core.ast.BuiltInsForSequences.seq_containsBI; -import org.apache.freemarker.core.ast.BuiltInsForSequences.seq_index_ofBI; -import org.apache.freemarker.core.ast.BuiltInsForSequences.sortBI; -import org.apache.freemarker.core.ast.BuiltInsForSequences.sort_byBI; -import org.apache.freemarker.core.ast.BuiltInsForStringsMisc.evalBI; -import org.apache.freemarker.core.model.TemplateDateModel; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; -import org.apache.freemarker.core.model.TemplateNumberModel; -import org.apache.freemarker.core.model.TemplateScalarModel; -import org.apache.freemarker.core.util._DateUtil; -import org.apache.freemarker.core.util._StringUtil; - -/** - * The {@code ?} operator used for things like {@code foo?upper_case}. - */ -abstract class BuiltIn extends Expression implements Cloneable { - - protected Expression target; - protected String key; - - static final Set<String> CAMEL_CASE_NAMES = new TreeSet<>(); - static final Set<String> SNAKE_CASE_NAMES = new TreeSet<>(); - static final int NUMBER_OF_BIS = 263; - static final HashMap<String, BuiltIn> BUILT_INS_BY_NAME = new HashMap(NUMBER_OF_BIS * 3 / 2 + 1, 1f); - - static { - // Note that you must update NUMBER_OF_BIS if you add new items here! - - putBI("abs", new absBI()); - putBI("ancestors", new ancestorsBI()); - putBI("api", new BuiltInsForMultipleTypes.apiBI()); - putBI("boolean", new BuiltInsForStringsMisc.booleanBI()); - putBI("byte", new byteBI()); - putBI("c", new BuiltInsForMultipleTypes.cBI()); - putBI("cap_first", "capFirst", new BuiltInsForStringsBasic.cap_firstBI()); - putBI("capitalize", new BuiltInsForStringsBasic.capitalizeBI()); - putBI("ceiling", new ceilingBI()); - putBI("children", new childrenBI()); - putBI("chop_linebreak", "chopLinebreak", new BuiltInsForStringsBasic.chop_linebreakBI()); - putBI("contains", new BuiltInsForStringsBasic.containsBI()); - putBI("date", new BuiltInsForMultipleTypes.dateBI(TemplateDateModel.DATE)); - putBI("date_if_unknown", "dateIfUnknown", new BuiltInsForDates.dateType_if_unknownBI(TemplateDateModel.DATE)); - putBI("datetime", new BuiltInsForMultipleTypes.dateBI(TemplateDateModel.DATETIME)); - putBI("datetime_if_unknown", "datetimeIfUnknown", new BuiltInsForDates.dateType_if_unknownBI(TemplateDateModel.DATETIME)); - putBI("default", new ExistenceBuiltins.defaultBI()); - putBI("double", new doubleBI()); - putBI("ends_with", "endsWith", new BuiltInsForStringsBasic.ends_withBI()); - putBI("ensure_ends_with", "ensureEndsWith", new BuiltInsForStringsBasic.ensure_ends_withBI()); - putBI("ensure_starts_with", "ensureStartsWith", new BuiltInsForStringsBasic.ensure_starts_withBI()); - putBI("esc", new escBI()); - putBI("eval", new evalBI()); - putBI("exists", new ExistenceBuiltins.existsBI()); - putBI("first", new firstBI()); - putBI("float", new floatBI()); - putBI("floor", new floorBI()); - putBI("chunk", new chunkBI()); - putBI("counter", new BuiltInsForLoopVariables.counterBI()); - putBI("item_cycle", "itemCycle", new BuiltInsForLoopVariables.item_cycleBI()); - putBI("has_api", "hasApi", new BuiltInsForMultipleTypes.has_apiBI()); - putBI("has_content", "hasContent", new ExistenceBuiltins.has_contentBI()); - putBI("has_next", "hasNext", new BuiltInsForLoopVariables.has_nextBI()); - putBI("html", new BuiltInsForStringsEncoding.htmlBI()); - putBI("if_exists", "ifExists", new ExistenceBuiltins.if_existsBI()); - putBI("index", new BuiltInsForLoopVariables.indexBI()); - putBI("index_of", "indexOf", new BuiltInsForStringsBasic.index_ofBI(false)); - putBI("int", new intBI()); - putBI("interpret", new Interpret()); - putBI("is_boolean", "isBoolean", new BuiltInsForMultipleTypes.is_booleanBI()); - putBI("is_collection", "isCollection", new BuiltInsForMultipleTypes.is_collectionBI()); - putBI("is_collection_ex", "isCollectionEx", new BuiltInsForMultipleTypes.is_collection_exBI()); - is_dateLikeBI bi = new BuiltInsForMultipleTypes.is_dateLikeBI(); - putBI("is_date", "isDate", bi); // misnomer - putBI("is_date_like", "isDateLike", bi); - putBI("is_date_only", "isDateOnly", new BuiltInsForMultipleTypes.is_dateOfTypeBI(TemplateDateModel.DATE)); - putBI("is_even_item", "isEvenItem", new BuiltInsForLoopVariables.is_even_itemBI()); - putBI("is_first", "isFirst", new BuiltInsForLoopVariables.is_firstBI()); - putBI("is_last", "isLast", new BuiltInsForLoopVariables.is_lastBI()); - putBI("is_unknown_date_like", "isUnknownDateLike", new BuiltInsForMultipleTypes.is_dateOfTypeBI(TemplateDateModel.UNKNOWN)); - putBI("is_datetime", "isDatetime", new BuiltInsForMultipleTypes.is_dateOfTypeBI(TemplateDateModel.DATETIME)); - putBI("is_directive", "isDirective", new BuiltInsForMultipleTypes.is_directiveBI()); - putBI("is_enumerable", "isEnumerable", new BuiltInsForMultipleTypes.is_enumerableBI()); - putBI("is_hash_ex", "isHashEx", new BuiltInsForMultipleTypes.is_hash_exBI()); - putBI("is_hash", "isHash", new BuiltInsForMultipleTypes.is_hashBI()); - putBI("is_infinite", "isInfinite", new is_infiniteBI()); - putBI("is_indexable", "isIndexable", new BuiltInsForMultipleTypes.is_indexableBI()); - putBI("is_macro", "isMacro", new BuiltInsForMultipleTypes.is_macroBI()); - putBI("is_markup_output", "isMarkupOutput", new BuiltInsForMultipleTypes.is_markup_outputBI()); - putBI("is_method", "isMethod", new BuiltInsForMultipleTypes.is_methodBI()); - putBI("is_nan", "isNan", new is_nanBI()); - putBI("is_node", "isNode", new BuiltInsForMultipleTypes.is_nodeBI()); - putBI("is_number", "isNumber", new BuiltInsForMultipleTypes.is_numberBI()); - putBI("is_odd_item", "isOddItem", new BuiltInsForLoopVariables.is_odd_itemBI()); - putBI("is_sequence", "isSequence", new BuiltInsForMultipleTypes.is_sequenceBI()); - putBI("is_string", "isString", new BuiltInsForMultipleTypes.is_stringBI()); - putBI("is_time", "isTime", new BuiltInsForMultipleTypes.is_dateOfTypeBI(TemplateDateModel.TIME)); - putBI("is_transform", "isTransform", new BuiltInsForMultipleTypes.is_transformBI()); - - putBI("iso_utc", "isoUtc", new iso_utc_or_local_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_SECONDS, /* useUTC = */ true)); - putBI("iso_utc_fz", "isoUtcFZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.TRUE, _DateUtil.ACCURACY_SECONDS, /* useUTC = */ true)); - putBI("iso_utc_nz", "isoUtcNZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_SECONDS, /* useUTC = */ true)); - - putBI("iso_utc_ms", "isoUtcMs", new iso_utc_or_local_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_MILLISECONDS, /* useUTC = */ true)); - putBI("iso_utc_ms_nz", "isoUtcMsNZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_MILLISECONDS, /* useUTC = */ true)); - - putBI("iso_utc_m", "isoUtcM", new iso_utc_or_local_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_MINUTES, /* useUTC = */ true)); - putBI("iso_utc_m_nz", "isoUtcMNZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_MINUTES, /* useUTC = */ true)); - - putBI("iso_utc_h", "isoUtcH", new iso_utc_or_local_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_HOURS, /* useUTC = */ true)); - putBI("iso_utc_h_nz", "isoUtcHNZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_HOURS, /* useUTC = */ true)); - - putBI("iso_local", "isoLocal", new iso_utc_or_local_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_SECONDS, /* useUTC = */ false)); - putBI("iso_local_nz", "isoLocalNZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_SECONDS, /* useUTC = */ false)); - - putBI("iso_local_ms", "isoLocalMs", new iso_utc_or_local_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_MILLISECONDS, /* useUTC = */ false)); - putBI("iso_local_ms_nz", "isoLocalMsNZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_MILLISECONDS, /* useUTC = */ false)); - - putBI("iso_local_m", "isoLocalM", new iso_utc_or_local_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_MINUTES, /* useUTC = */ false)); - putBI("iso_local_m_nz", "isoLocalMNZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_MINUTES, /* useUTC = */ false)); - - putBI("iso_local_h", "isoLocalH", new iso_utc_or_local_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_HOURS, /* useUTC = */ false)); - putBI("iso_local_h_nz", "isoLocalHNZ", new iso_utc_or_local_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_HOURS, /* useUTC = */ false)); - - putBI("iso", new iso_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_SECONDS)); - putBI("iso_nz", "isoNZ", new iso_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_SECONDS)); - - putBI("iso_ms", "isoMs", new iso_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_MILLISECONDS)); - putBI("iso_ms_nz", "isoMsNZ", new iso_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_MILLISECONDS)); - - putBI("iso_m", "isoM", new iso_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_MINUTES)); - putBI("iso_m_nz", "isoMNZ", new iso_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_MINUTES)); - - putBI("iso_h", "isoH", new iso_BI( - /* showOffset = */ null, _DateUtil.ACCURACY_HOURS)); - putBI("iso_h_nz", "isoHNZ", new iso_BI( - /* showOffset = */ Boolean.FALSE, _DateUtil.ACCURACY_HOURS)); - - putBI("j_string", "jString", new BuiltInsForStringsEncoding.j_stringBI()); - putBI("join", new BuiltInsForSequences.joinBI()); - putBI("js_string", "jsString", new BuiltInsForStringsEncoding.js_stringBI()); - putBI("json_string", "jsonString", new BuiltInsForStringsEncoding.json_stringBI()); - putBI("keep_after", "keepAfter", new BuiltInsForStringsBasic.keep_afterBI()); - putBI("keep_before", "keepBefore", new BuiltInsForStringsBasic.keep_beforeBI()); - putBI("keep_after_last", "keepAfterLast", new BuiltInsForStringsBasic.keep_after_lastBI()); - putBI("keep_before_last", "keepBeforeLast", new BuiltInsForStringsBasic.keep_before_lastBI()); - putBI("keys", new BuiltInsForHashes.keysBI()); - putBI("last_index_of", "lastIndexOf", new BuiltInsForStringsBasic.index_ofBI(true)); - putBI("last", new lastBI()); - putBI("left_pad", "leftPad", new BuiltInsForStringsBasic.padBI(true)); - putBI("length", new BuiltInsForStringsBasic.lengthBI()); - putBI("long", new longBI()); - putBI("lower_abc", "lowerAbc", new BuiltInsForNumbers.lower_abcBI()); - putBI("lower_case", "lowerCase", new BuiltInsForStringsBasic.lower_caseBI()); - putBI("namespace", new BuiltInsForMultipleTypes.namespaceBI()); - putBI("new", new NewBI()); - putBI("markup_string", "markupString", new markup_stringBI()); - putBI("node_name", "nodeName", new node_nameBI()); - putBI("node_namespace", "nodeNamespace", new node_namespaceBI()); - putBI("node_type", "nodeType", new node_typeBI()); - putBI("no_esc", "noEsc", new no_escBI()); - putBI("number", new BuiltInsForStringsMisc.numberBI()); - putBI("number_to_date", "numberToDate", new number_to_dateBI(TemplateDateModel.DATE)); - putBI("number_to_time", "numberToTime", new number_to_dateBI(TemplateDateModel.TIME)); - putBI("number_to_datetime", "numberToDatetime", new number_to_dateBI(TemplateDateModel.DATETIME)); - putBI("parent", new parentBI()); - putBI("previous_sibling", "previousSibling", new previousSiblingBI()); - putBI("next_sibling", "nextSibling", new nextSiblingBI()); - putBI("item_parity", "itemParity", new BuiltInsForLoopVariables.item_parityBI()); - putBI("item_parity_cap", "itemParityCap", new BuiltInsForLoopVariables.item_parity_capBI()); - putBI("reverse", new reverseBI()); - putBI("right_pad", "rightPad", new BuiltInsForStringsBasic.padBI(false)); - putBI("root", new rootBI()); - putBI("round", new roundBI()); - putBI("remove_ending", "removeEnding", new BuiltInsForStringsBasic.remove_endingBI()); - putBI("remove_beginning", "removeBeginning", new BuiltInsForStringsBasic.remove_beginningBI()); - putBI("rtf", new BuiltInsForStringsEncoding.rtfBI()); - putBI("seq_contains", "seqContains", new seq_containsBI()); - putBI("seq_index_of", "seqIndexOf", new seq_index_ofBI(1)); - putBI("seq_last_index_of", "seqLastIndexOf", new seq_index_ofBI(-1)); - putBI("short", new shortBI()); - putBI("size", new BuiltInsForMultipleTypes.sizeBI()); - putBI("sort_by", "sortBy", new sort_byBI()); - putBI("sort", new sortBI()); - putBI("split", new BuiltInsForStringsBasic.split_BI()); - putBI("switch", new BuiltInsWithParseTimeParameters.switch_BI()); - putBI("starts_with", "startsWith", new BuiltInsForStringsBasic.starts_withBI()); - putBI("string", new BuiltInsForMultipleTypes.stringBI()); - putBI("substring", new BuiltInsForStringsBasic.substringBI()); - putBI("then", new BuiltInsWithParseTimeParameters.then_BI()); - putBI("time", new BuiltInsForMultipleTypes.dateBI(TemplateDateModel.TIME)); - putBI("time_if_unknown", "timeIfUnknown", new BuiltInsForDates.dateType_if_unknownBI(TemplateDateModel.TIME)); - putBI("trim", new BuiltInsForStringsBasic.trimBI()); - putBI("uncap_first", "uncapFirst", new BuiltInsForStringsBasic.uncap_firstBI()); - putBI("upper_abc", "upperAbc", new BuiltInsForNumbers.upper_abcBI()); - putBI("upper_case", "upperCase", new BuiltInsForStringsBasic.upper_caseBI()); - putBI("url", new BuiltInsForStringsEncoding.urlBI()); - putBI("url_path", "urlPath", new BuiltInsForStringsEncoding.urlPathBI()); - putBI("values", new BuiltInsForHashes.valuesBI()); - putBI("web_safe", "webSafe", BUILT_INS_BY_NAME.get("html")); // deprecated; use ?html instead - putBI("word_list", "wordList", new BuiltInsForStringsBasic.word_listBI()); - putBI("xhtml", new BuiltInsForStringsEncoding.xhtmlBI()); - putBI("xml", new BuiltInsForStringsEncoding.xmlBI()); - putBI("matches", new BuiltInsForStringsRegexp.matchesBI()); - putBI("groups", new BuiltInsForStringsRegexp.groupsBI()); - putBI("replace", new BuiltInsForStringsRegexp.replace_reBI()); - - - if (NUMBER_OF_BIS < BUILT_INS_BY_NAME.size()) { - throw new AssertionError("Update NUMBER_OF_BIS! Should be: " + BUILT_INS_BY_NAME.size()); - } - } - - private static void putBI(String name, BuiltIn bi) { - BUILT_INS_BY_NAME.put(name, bi); - SNAKE_CASE_NAMES.add(name); - CAMEL_CASE_NAMES.add(name); - } - - private static void putBI(String nameSnakeCase, String nameCamelCase, BuiltIn bi) { - BUILT_INS_BY_NAME.put(nameSnakeCase, bi); - BUILT_INS_BY_NAME.put(nameCamelCase, bi); - SNAKE_CASE_NAMES.add(nameSnakeCase); - CAMEL_CASE_NAMES.add(nameCamelCase); - } - - /** - * @param target - * Left-hand-operand expression - * @param keyTk - * Built-in name token - */ - static BuiltIn newBuiltIn(int incompatibleImprovements, Expression target, Token keyTk, - FMParserTokenManager tokenManager) throws ParseException { - String key = keyTk.image; - BuiltIn bi = BUILT_INS_BY_NAME.get(key); - if (bi == null) { - StringBuilder buf = new StringBuilder("Unknown built-in: ").append(_StringUtil.jQuote(key)).append(". "); - - buf.append( - "Help (latest version): http://freemarker.org/docs/ref_builtins.html; " - + "you're using FreeMarker ").append(Configuration.getVersion()).append(".\n" - + "The alphabetical list of built-ins:"); - List<String> names = new ArrayList<>(BUILT_INS_BY_NAME.keySet().size()); - names.addAll(BUILT_INS_BY_NAME.keySet()); - Collections.sort(names); - char lastLetter = 0; - - int shownNamingConvention; - { - int namingConvention = tokenManager.namingConvention; - shownNamingConvention = namingConvention != Configuration.AUTO_DETECT_NAMING_CONVENTION - ? namingConvention : Configuration.LEGACY_NAMING_CONVENTION /* [2.4] CAMEL_CASE */; - } - - boolean first = true; - for (String correctName : names) { - int correctNameNamingConvetion = _StringUtil.getIdentifierNamingConvention(correctName); - if (shownNamingConvention == Configuration.CAMEL_CASE_NAMING_CONVENTION - ? correctNameNamingConvetion != Configuration.LEGACY_NAMING_CONVENTION - : correctNameNamingConvetion != Configuration.CAMEL_CASE_NAMING_CONVENTION) { - if (first) { - first = false; - } else { - buf.append(", "); - } - - char firstChar = correctName.charAt(0); - if (firstChar != lastLetter) { - lastLetter = firstChar; - buf.append('\n'); - } - buf.append(correctName); - } - } - - throw new ParseException(buf.toString(), null, keyTk); - } - - try { - bi = (BuiltIn) bi.clone(); - } catch (CloneNotSupportedException e) { - throw new InternalError(); - } - bi.key = key; - bi.target = target; - return bi; - } - - @Override - public String getCanonicalForm() { - return target.getCanonicalForm() + "?" + key; - } - - @Override - String getNodeTypeSymbol() { - return "?" + key; - } - - @Override - boolean isLiteral() { - return false; // be on the safe side. - } - - protected final void checkMethodArgCount(List args, int expectedCnt) throws TemplateModelException { - checkMethodArgCount(args.size(), expectedCnt); - } - - protected final void checkMethodArgCount(int argCnt, int expectedCnt) throws TemplateModelException { - if (argCnt != expectedCnt) { - throw MessageUtil.newArgCntError("?" + key, argCnt, expectedCnt); - } - } - - protected final void checkMethodArgCount(List args, int minCnt, int maxCnt) throws TemplateModelException { - checkMethodArgCount(args.size(), minCnt, maxCnt); - } - - protected final void checkMethodArgCount(int argCnt, int minCnt, int maxCnt) throws TemplateModelException { - if (argCnt < minCnt || argCnt > maxCnt) { - throw MessageUtil.newArgCntError("?" + key, argCnt, minCnt, maxCnt); - } - } - - /** - * Same as {@link #getStringMethodArg}, but checks if {@code args} is big enough, and returns {@code null} if it - * isn't. - */ - protected final String getOptStringMethodArg(List args, int argIdx) - throws TemplateModelException { - return args.size() > argIdx ? getStringMethodArg(args, argIdx) : null; - } - - /** - * Gets a method argument and checks if it's a string; it does NOT check if {@code args} is big enough. - */ - protected final String getStringMethodArg(List args, int argIdx) - throws TemplateModelException { - TemplateModel arg = (TemplateModel) args.get(argIdx); - if (!(arg instanceof TemplateScalarModel)) { - throw MessageUtil.newMethodArgMustBeStringException("?" + key, argIdx, arg); - } else { - return EvalUtil.modelToString((TemplateScalarModel) arg, null, null); - } - } - - /** - * Gets a method argument and checks if it's a number; it does NOT check if {@code args} is big enough. - */ - protected final Number getNumberMethodArg(List args, int argIdx) - throws TemplateModelException { - TemplateModel arg = (TemplateModel) args.get(argIdx); - if (!(arg instanceof TemplateNumberModel)) { - throw MessageUtil.newMethodArgMustBeNumberException("?" + key, argIdx, arg); - } else { - return EvalUtil.modelToNumber((TemplateNumberModel) arg, null); - } - } - - protected final TemplateModelException newMethodArgInvalidValueException(int argIdx, Object[] details) { - return MessageUtil.newMethodArgInvalidValueException("?" + key, argIdx, details); - } - - protected final TemplateModelException newMethodArgsInvalidValueException(Object[] details) { - return MessageUtil.newMethodArgsInvalidValueException("?" + key, details); - } - - @Override - protected Expression deepCloneWithIdentifierReplaced_inner( - String replacedIdentifier, Expression replacement, ReplacemenetState replacementState) { - try { - BuiltIn clone = (BuiltIn) clone(); - clone.target = target.deepCloneWithIdentifierReplaced(replacedIdentifier, replacement, replacementState); - return clone; - } catch (CloneNotSupportedException e) { - throw new RuntimeException("Internal error: " + e); - } - } - - @Override - int getParameterCount() { - return 2; - } - - @Override - Object getParameterValue(int idx) { - switch (idx) { - case 0: return target; - case 1: return key; - default: throw new IndexOutOfBoundsException(); - } - } - - @Override - ParameterRole getParameterRole(int idx) { - switch (idx) { - case 0: return ParameterRole.LEFT_HAND_OPERAND; - case 1: return ParameterRole.RIGHT_HAND_OPERAND; - default: throw new IndexOutOfBoundsException(); - } - } - -}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java deleted file mode 100644 index d6fd349..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.freemarker.core.ast; - -/** - * A string built-in whose usage is banned when auto-escaping with a markup-output format is active. - * This is just a marker; the actual checking is in {@code FTL.jj}. - */ -abstract class BuiltInBannedWhenAutoEscaping extends SpecialBuiltIn { - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForDate.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForDate.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForDate.java deleted file mode 100644 index 9dc8ee6..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForDate.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import java.util.Date; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateDateModel; -import org.apache.freemarker.core.model.TemplateModel; - -abstract class BuiltInForDate extends BuiltIn { - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - TemplateModel model = target.eval(env); - if (model instanceof TemplateDateModel) { - TemplateDateModel tdm = (TemplateDateModel) model; - return calculateResult(EvalUtil.modelToDate(tdm, target), tdm.getDateType(), env); - } else { - throw newNonDateException(env, model, target); - } - } - - /** Override this to implement the built-in. */ - protected abstract TemplateModel calculateResult( - Date date, int dateType, Environment env) - throws TemplateException; - - static TemplateException newNonDateException(Environment env, TemplateModel model, Expression target) - throws InvalidReferenceException { - TemplateException e; - if (model == null) { - e = InvalidReferenceException.getInstance(target, env); - } else { - e = new NonDateException(target, model, "date", env); - } - return e; - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForHashEx.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForHashEx.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForHashEx.java deleted file mode 100644 index f67925f..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForHashEx.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateHashModelEx; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; - -abstract class BuiltInForHashEx extends BuiltIn { - - @Override - TemplateModel _eval(Environment env) throws TemplateException { - TemplateModel model = target.eval(env); - if (model instanceof TemplateHashModelEx) { - return calculateResult((TemplateHashModelEx) model, env); - } - throw new NonExtendedHashException(target, model, env); - } - - abstract TemplateModel calculateResult(TemplateHashModelEx hashExModel, Environment env) - throws TemplateModelException, InvalidReferenceException; - - protected InvalidReferenceException newNullPropertyException( - String propertyName, TemplateModel tm, Environment env) { - if (env.getFastInvalidReferenceExceptions()) { - return InvalidReferenceException.FAST_INSTANCE; - } else { - return new InvalidReferenceException( - new _ErrorDescriptionBuilder( - "The exteneded hash (of class ", tm.getClass().getName(), ") has returned null for its \"", - propertyName, - "\" property. This is maybe a bug. The extended hash was returned by this expression:") - .blame(target), - env, this); - } - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java deleted file mode 100644 index 731f42b..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateModel; - -/** - * A string built-in whose usage is banned when auto-escaping with a markup-output format is active. - * This is just a marker; the actual checking is in {@code FTL.jj}. - */ -abstract class BuiltInForLegacyEscaping extends BuiltInBannedWhenAutoEscaping { - - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - TemplateModel tm = target.eval(env); - Object moOrStr = EvalUtil.coerceModelToStringOrMarkup(tm, target, null, env); - if (moOrStr instanceof String) { - return calculateResult((String) moOrStr, env); - } else { - TemplateMarkupOutputModel<?> mo = (TemplateMarkupOutputModel<?>) moOrStr; - if (mo.getOutputFormat().isLegacyBuiltInBypassed(key)) { - return mo; - } - throw new NonStringException(target, tm, env); - } - } - - abstract TemplateModel calculateResult(String s, Environment env) throws TemplateException; - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForLoopVariable.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForLoopVariable.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForLoopVariable.java deleted file mode 100644 index a55f82c..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForLoopVariable.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.ast.IteratorBlock.IterationContext; -import org.apache.freemarker.core.model.TemplateModel; - -abstract class BuiltInForLoopVariable extends SpecialBuiltIn { - - private String loopVarName; - - void bindToLoopVariable(String loopVarName) { - this.loopVarName = loopVarName; - } - - @Override - TemplateModel _eval(Environment env) throws TemplateException { - IterationContext iterCtx = IteratorBlock.findEnclosingIterationContext(env, loopVarName); - if (iterCtx == null) { - // The parser should prevent this situation - throw new _MiscTemplateException( - this, env, - "There's no iteration in context that uses loop variable ", new _DelayedJQuote(loopVarName), "."); - } - - return calculateResult(iterCtx, env); - } - - abstract TemplateModel calculateResult(IterationContext iterCtx, Environment env) throws TemplateException; - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForMarkupOutput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForMarkupOutput.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForMarkupOutput.java deleted file mode 100644 index 15f0f4b..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForMarkupOutput.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; - -abstract class BuiltInForMarkupOutput extends BuiltIn { - - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - TemplateModel model = target.eval(env); - if (!(model instanceof TemplateMarkupOutputModel)) { - throw new NonMarkupOutputException(target, model, env); - } - return calculateResult((TemplateMarkupOutputModel) model); - } - - protected abstract TemplateModel calculateResult(TemplateMarkupOutputModel model) throws TemplateModelException; - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForNode.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForNode.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForNode.java deleted file mode 100644 index 42f88a9..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForNode.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; -import org.apache.freemarker.core.model.TemplateNodeModel; - -abstract class BuiltInForNode extends BuiltIn { - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - TemplateModel model = target.eval(env); - if (model instanceof TemplateNodeModel) { - return calculateResult((TemplateNodeModel) model, env); - } else { - throw new NonNodeException(target, model, env); - } - } - abstract TemplateModel calculateResult(TemplateNodeModel nodeModel, Environment env) - throws TemplateModelException; -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForNodeEx.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForNodeEx.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForNodeEx.java deleted file mode 100644 index dffaeea..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForNodeEx.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; -import org.apache.freemarker.core.model.TemplateNodeModelEx; - -public abstract class BuiltInForNodeEx extends BuiltIn { - @Override - TemplateModel _eval(Environment env) throws TemplateException { - TemplateModel model = target.eval(env); - if (model instanceof TemplateNodeModelEx) { - return calculateResult((TemplateNodeModelEx) model, env); - } else { - throw new NonExtendedNodeException(target, model, env); - } - } - abstract TemplateModel calculateResult(TemplateNodeModelEx nodeModel, Environment env) - throws TemplateModelException; -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForNumber.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForNumber.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForNumber.java deleted file mode 100644 index 761bd79..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForNumber.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; - -abstract class BuiltInForNumber extends BuiltIn { - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - TemplateModel model = target.eval(env); - return calculateResult(target.modelToNumber(model, env), model); - } - - abstract TemplateModel calculateResult(Number num, TemplateModel model) - throws TemplateModelException; -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForSequence.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForSequence.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForSequence.java deleted file mode 100644 index 7f8d838..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForSequence.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; -import org.apache.freemarker.core.model.TemplateSequenceModel; - -abstract class BuiltInForSequence extends BuiltIn { - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - TemplateModel model = target.eval(env); - if (!(model instanceof TemplateSequenceModel)) { - throw new NonSequenceException(target, model, env); - } - return calculateResult((TemplateSequenceModel) model); - } - abstract TemplateModel calculateResult(TemplateSequenceModel tsm) - throws TemplateModelException; -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInForString.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForString.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForString.java deleted file mode 100644 index 478c920..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForString.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.TemplateModel; - -abstract class BuiltInForString extends BuiltIn { - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - return calculateResult(getTargetString(target, env), env); - } - abstract TemplateModel calculateResult(String s, Environment env) throws TemplateException; - - static String getTargetString(Expression target, Environment env) throws TemplateException { - return target.evalAndCoerceToStringOrUnsupportedMarkup(env); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInWithParseTimeParameters.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInWithParseTimeParameters.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInWithParseTimeParameters.java deleted file mode 100644 index 21fdc75..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInWithParseTimeParameters.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.freemarker.core.ast; - -import java.util.List; - -import org.apache.freemarker.core.ast.Token; - - -abstract class BuiltInWithParseTimeParameters extends SpecialBuiltIn { - - abstract void bindToParameters(List/*<Expression>*/ parameters, Token openParen, Token closeParen) - throws ParseException; - - @Override - public String getCanonicalForm() { - StringBuilder buf = new StringBuilder(); - - buf.append(super.getCanonicalForm()); - - buf.append("("); - List/*<Expression>*/args = getArgumentsAsList(); - int size = args.size(); - for (int i = 0; i < size; i++) { - if (i != 0) { - buf.append(", "); - } - Expression arg = (Expression) args.get(i); - buf.append(arg.getCanonicalForm()); - } - buf.append(")"); - - return buf.toString(); - } - - @Override - String getNodeTypeSymbol() { - return super.getNodeTypeSymbol() + "(...)"; - } - - @Override - int getParameterCount() { - return super.getParameterCount() + getArgumentsCount(); - } - - @Override - Object getParameterValue(int idx) { - final int superParamCnt = super.getParameterCount(); - if (idx < superParamCnt) { - return super.getParameterValue(idx); - } - - final int argIdx = idx - superParamCnt; - return getArgumentParameterValue(argIdx); - } - - @Override - ParameterRole getParameterRole(int idx) { - final int superParamCnt = super.getParameterCount(); - if (idx < superParamCnt) { - return super.getParameterRole(idx); - } - - if (idx - superParamCnt < getArgumentsCount()) { - return ParameterRole.ARGUMENT_VALUE; - } else { - throw new IndexOutOfBoundsException(); - } - } - - protected ParseException newArgumentCountException(String ordinalityDesc, Token openParen, Token closeParen) { - return new ParseException( - "?" + key + "(...) " + ordinalityDesc + " parameters", getTemplate(), - openParen.beginLine, openParen.beginColumn, - closeParen.endLine, closeParen.endColumn); - } - - @Override - protected Expression deepCloneWithIdentifierReplaced_inner( - String replacedIdentifier, Expression replacement, ReplacemenetState replacementState) { - final Expression clone = super.deepCloneWithIdentifierReplaced_inner(replacedIdentifier, replacement, replacementState); - cloneArguments(clone, replacedIdentifier, replacement, replacementState); - return clone; - } - - protected abstract List getArgumentsAsList(); - - protected abstract int getArgumentsCount(); - - protected abstract Expression getArgumentParameterValue(int argIdx); - - protected abstract void cloneArguments(Expression clone, String replacedIdentifier, - Expression replacement, ReplacemenetState replacementState); - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInsForDates.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInsForDates.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInsForDates.java deleted file mode 100644 index ee84a99..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInsForDates.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import java.util.Date; -import java.util.List; -import java.util.TimeZone; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.model.AdapterTemplateModel; -import org.apache.freemarker.core.model.TemplateDateModel; -import org.apache.freemarker.core.model.TemplateMethodModelEx; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; -import org.apache.freemarker.core.model.TemplateScalarModel; -import org.apache.freemarker.core.model.impl.SimpleDate; -import org.apache.freemarker.core.model.impl.SimpleScalar; -import org.apache.freemarker.core.util.UnrecognizedTimeZoneException; -import org.apache.freemarker.core.util._DateUtil; - -/** - * A holder for built-ins that operate exclusively on date left-hand values. - */ -class BuiltInsForDates { - - static class dateType_if_unknownBI extends BuiltIn { - - private final int dateType; - - dateType_if_unknownBI(int dateType) { - this.dateType = dateType; - } - - @Override - TemplateModel _eval(Environment env) - throws TemplateException { - TemplateModel model = target.eval(env); - if (model instanceof TemplateDateModel) { - TemplateDateModel tdm = (TemplateDateModel) model; - int tdmDateType = tdm.getDateType(); - if (tdmDateType != TemplateDateModel.UNKNOWN) { - return tdm; - } - return new SimpleDate(EvalUtil.modelToDate(tdm, target), dateType); - } else { - throw BuiltInForDate.newNonDateException(env, model, target); - } - } - - protected TemplateModel calculateResult(Date date, int dateType, Environment env) throws TemplateException { - // TODO Auto-generated method stub - return null; - } - - } - - /** - * Implements {@code ?iso(timeZone)}. - */ - static class iso_BI extends AbstractISOBI { - - class Result implements TemplateMethodModelEx { - private final Date date; - private final int dateType; - private final Environment env; - - Result(Date date, int dateType, Environment env) { - this.date = date; - this.dateType = dateType; - this.env = env; - } - - @Override - public Object exec(List args) throws TemplateModelException { - checkMethodArgCount(args, 1); - - TemplateModel tzArgTM = (TemplateModel) args.get(0); - TimeZone tzArg; - Object adaptedObj; - if (tzArgTM instanceof AdapterTemplateModel - && (adaptedObj = - ((AdapterTemplateModel) tzArgTM) - .getAdaptedObject(TimeZone.class)) - instanceof TimeZone) { - tzArg = (TimeZone) adaptedObj; - } else if (tzArgTM instanceof TemplateScalarModel) { - String tzName = EvalUtil.modelToString((TemplateScalarModel) tzArgTM, null, null); - try { - tzArg = _DateUtil.getTimeZone(tzName); - } catch (UnrecognizedTimeZoneException e) { - throw new _TemplateModelException( - "The time zone string specified for ?", key, - "(...) is not recognized as a valid time zone name: ", - new _DelayedJQuote(tzName)); - } - } else { - throw MessageUtil.newMethodArgUnexpectedTypeException( - "?" + key, 0, "string or java.util.TimeZone", tzArgTM); - } - - return new SimpleScalar(_DateUtil.dateToISO8601String( - date, - dateType != TemplateDateModel.TIME, - dateType != TemplateDateModel.DATE, - shouldShowOffset(date, dateType, env), - accuracy, - tzArg, - env.getISOBuiltInCalendarFactory())); - } - - } - - iso_BI(Boolean showOffset, int accuracy) { - super(showOffset, accuracy); - } - - @Override - protected TemplateModel calculateResult( - Date date, int dateType, Environment env) - throws TemplateException { - checkDateTypeNotUnknown(dateType); - return new Result(date, dateType, env); - } - - } - - /** - * Implements {@code ?iso_utc} and {@code ?iso_local} variants, but not - * {@code ?iso(timeZone)}. - */ - static class iso_utc_or_local_BI extends AbstractISOBI { - - private final boolean useUTC; - - iso_utc_or_local_BI(Boolean showOffset, int accuracy, boolean useUTC) { - super(showOffset, accuracy); - this.useUTC = useUTC; - } - - @Override - protected TemplateModel calculateResult( - Date date, int dateType, Environment env) - throws TemplateException { - checkDateTypeNotUnknown(dateType); - return new SimpleScalar(_DateUtil.dateToISO8601String( - date, - dateType != TemplateDateModel.TIME, - dateType != TemplateDateModel.DATE, - shouldShowOffset(date, dateType, env), - accuracy, - useUTC - ? _DateUtil.UTC - : env.shouldUseSQLDTTZ(date.getClass()) - ? env.getSQLDateAndTimeTimeZone() - : env.getTimeZone(), - env.getISOBuiltInCalendarFactory())); - } - - } - - // Can't be instantiated - private BuiltInsForDates() { } - - static abstract class AbstractISOBI extends BuiltInForDate { - protected final Boolean showOffset; - protected final int accuracy; - - protected AbstractISOBI(Boolean showOffset, int accuracy) { - this.showOffset = showOffset; - this.accuracy = accuracy; - } - - protected void checkDateTypeNotUnknown(int dateType) - throws TemplateException { - if (dateType == TemplateDateModel.UNKNOWN) { - throw new _MiscTemplateException(new _ErrorDescriptionBuilder( - "The value of the following has unknown date type, but ?", key, - " needs a value where it's known if it's a date (no time part), time, or date-time value:" - ).blame(target).tip(MessageUtil.UNKNOWN_DATE_TYPE_ERROR_TIP)); - } - } - - protected boolean shouldShowOffset(Date date, int dateType, Environment env) { - if (dateType == TemplateDateModel.DATE) { - return false; // ISO 8061 doesn't allow zone for date-only values - } else if (showOffset != null) { - return showOffset.booleanValue(); - } else { - // java.sql.Time values meant to carry calendar field values only, so we don't show offset for them. - return !(date instanceof java.sql.Time); - } - } - - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInsForHashes.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInsForHashes.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInsForHashes.java deleted file mode 100644 index 8e5b434..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInsForHashes.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.model.TemplateCollectionModel; -import org.apache.freemarker.core.model.TemplateHashModelEx; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; -import org.apache.freemarker.core.model.TemplateSequenceModel; - -/** - * A holder for builtins that operate exclusively on hash left-hand value. - */ -class BuiltInsForHashes { - - static class keysBI extends BuiltInForHashEx { - - @Override - TemplateModel calculateResult(TemplateHashModelEx hashExModel, Environment env) - throws TemplateModelException, InvalidReferenceException { - TemplateCollectionModel keys = hashExModel.keys(); - if (keys == null) throw newNullPropertyException("keys", hashExModel, env); - return keys instanceof TemplateSequenceModel ? keys : new CollectionAndSequence(keys); - } - - } - - static class valuesBI extends BuiltInForHashEx { - @Override - TemplateModel calculateResult(TemplateHashModelEx hashExModel, Environment env) - throws TemplateModelException, InvalidReferenceException { - TemplateCollectionModel values = hashExModel.values(); - if (values == null) throw newNullPropertyException("values", hashExModel, env); - return values instanceof TemplateSequenceModel ? values : new CollectionAndSequence(values); - } - } - - // Can't be instantiated - private BuiltInsForHashes() { } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInsForLoopVariables.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInsForLoopVariables.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInsForLoopVariables.java deleted file mode 100644 index 3cb8684..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInsForLoopVariables.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.freemarker.core.ast; - -import java.util.List; - -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.ast.IteratorBlock.IterationContext; -import org.apache.freemarker.core.model.TemplateBooleanModel; -import org.apache.freemarker.core.model.TemplateMethodModelEx; -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; -import org.apache.freemarker.core.model.impl.SimpleNumber; -import org.apache.freemarker.core.model.impl.SimpleScalar; - - -class BuiltInsForLoopVariables { - - static class indexBI extends BuiltInForLoopVariable { - - @Override - TemplateModel calculateResult(IterationContext iterCtx, Environment env) throws TemplateException { - return new SimpleNumber(iterCtx.getIndex()); - } - - } - - static class counterBI extends BuiltInForLoopVariable { - - @Override - TemplateModel calculateResult(IterationContext iterCtx, Environment env) throws TemplateException { - return new SimpleNumber(iterCtx.getIndex() + 1); - } - - } - - static abstract class BooleanBuiltInForLoopVariable extends BuiltInForLoopVariable { - - @Override - final TemplateModel calculateResult(IterationContext iterCtx, Environment env) throws TemplateException { - return calculateBooleanResult(iterCtx, env) ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; - } - - protected abstract boolean calculateBooleanResult(IterationContext iterCtx, Environment env); - - } - - static class has_nextBI extends BooleanBuiltInForLoopVariable { - - @Override - protected boolean calculateBooleanResult(IterationContext iterCtx, Environment env) { - return iterCtx.hasNext(); - } - - } - - static class is_lastBI extends BooleanBuiltInForLoopVariable { - - @Override - protected boolean calculateBooleanResult(IterationContext iterCtx, Environment env) { - return !iterCtx.hasNext(); - } - - } - - static class is_firstBI extends BooleanBuiltInForLoopVariable { - - @Override - protected boolean calculateBooleanResult(IterationContext iterCtx, Environment env) { - return iterCtx.getIndex() == 0; - } - - } - - static class is_odd_itemBI extends BooleanBuiltInForLoopVariable { - - @Override - protected boolean calculateBooleanResult(IterationContext iterCtx, Environment env) { - return iterCtx.getIndex() % 2 == 0; - } - - } - - static class is_even_itemBI extends BooleanBuiltInForLoopVariable { - - @Override - protected boolean calculateBooleanResult(IterationContext iterCtx, Environment env) { - return iterCtx.getIndex() % 2 != 0; - } - - } - - static class item_parityBI extends BuiltInForLoopVariable { - - private static final SimpleScalar ODD = new SimpleScalar("odd"); - private static final SimpleScalar EVEN = new SimpleScalar("even"); - - @Override - TemplateModel calculateResult(IterationContext iterCtx, Environment env) throws TemplateException { - return iterCtx.getIndex() % 2 == 0 ? ODD: EVEN; - } - - } - - static class item_parity_capBI extends BuiltInForLoopVariable { - - private static final SimpleScalar ODD = new SimpleScalar("Odd"); - private static final SimpleScalar EVEN = new SimpleScalar("Even"); - - @Override - TemplateModel calculateResult(IterationContext iterCtx, Environment env) throws TemplateException { - return iterCtx.getIndex() % 2 == 0 ? ODD: EVEN; - } - - } - - static class item_cycleBI extends BuiltInForLoopVariable { - - private class BIMethod implements TemplateMethodModelEx { - - private final IterationContext iterCtx; - - private BIMethod(IterationContext iterCtx) { - this.iterCtx = iterCtx; - } - - @Override - public Object exec(List args) throws TemplateModelException { - checkMethodArgCount(args, 1, Integer.MAX_VALUE); - return args.get(iterCtx.getIndex() % args.size()); - } - } - - @Override - TemplateModel calculateResult(IterationContext iterCtx, Environment env) throws TemplateException { - return new BIMethod(iterCtx); - } - - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d784b2b/src/main/java/org/apache/freemarker/core/ast/BuiltInsForMarkupOutputs.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInsForMarkupOutputs.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInsForMarkupOutputs.java deleted file mode 100644 index 6eac3ec..0000000 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInsForMarkupOutputs.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.ast; - -import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.model.TemplateModelException; -import org.apache.freemarker.core.model.impl.SimpleScalar; - -/** - * A holder for builtins that operate exclusively on markup output left-hand value. - */ -class BuiltInsForMarkupOutputs { - - static class markup_stringBI extends BuiltInForMarkupOutput { - - @Override - protected TemplateModel calculateResult(TemplateMarkupOutputModel model) throws TemplateModelException { - return new SimpleScalar(model.getOutputFormat().getMarkupString(model)); - } - - } - -}
