http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NativeCollectionEx.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NativeCollectionEx.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeCollectionEx.java new file mode 100644 index 0000000..5afa98a --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeCollectionEx.java @@ -0,0 +1,73 @@ +/* + * 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; + +import java.util.Collection; +import java.util.Iterator; + +import org.apache.freemarker.core.model.ObjectWrapper; +import org.apache.freemarker.core.model.TemplateCollectionModelEx; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateModelException; +import org.apache.freemarker.core.model.TemplateModelIterator; + +/** + * A collection where each items is already a {@link TemplateModel}, so no {@link ObjectWrapper} need to be specified. + */ +class NativeCollectionEx implements TemplateCollectionModelEx { + + private final Collection<TemplateModel> collection; + + public NativeCollectionEx(Collection<TemplateModel> collection) { + this.collection = collection; + } + + @Override + public int size() { + return collection.size(); + } + + @Override + public boolean isEmpty() { + return collection.isEmpty(); + } + + @Override + public TemplateModelIterator iterator() throws TemplateModelException { + return new TemplateModelIterator() { + + private final Iterator<TemplateModel> iterator = collection.iterator(); + + @Override + public TemplateModel next() throws TemplateModelException { + if (!iterator.hasNext()) { + throw new TemplateModelException("The collection has no more items."); + } + + return iterator.next(); + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + }; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NativeHashEx2.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NativeHashEx2.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeHashEx2.java new file mode 100644 index 0000000..2850255 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeHashEx2.java @@ -0,0 +1,106 @@ +/* + * 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; + +import java.io.Serializable; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.freemarker.core.model.ObjectWrapper; +import org.apache.freemarker.core.model.TemplateCollectionModel; +import org.apache.freemarker.core.model.TemplateHashModelEx2; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateModelException; +import org.apache.freemarker.core.model.impl.SimpleScalar; + +/** + * A hash where each value is already a {@link TemplateModel}, so no {@link ObjectWrapper} need to be specified. + * + * <p>While this class allows adding items, doing so is not thread-safe, and thus only meant to be done during the + * initialization of the sequence. + */ +class NativeHashEx2 implements TemplateHashModelEx2, Serializable { + + private final LinkedHashMap<String, TemplateModel> map; + + public NativeHashEx2() { + this.map = new LinkedHashMap<>(); + } + + @Override + public int size() throws TemplateModelException { + return map.size(); + } + + @Override + public TemplateModel get(String key) throws TemplateModelException { + return map.get(key); + } + + @Override + public boolean isEmpty() throws TemplateModelException { + return map.isEmpty(); + } + + @Override + public KeyValuePairIterator keyValuePairIterator() throws TemplateModelException { + return new KeyValuePairIterator() { + private final Iterator<Map.Entry<String, TemplateModel>> entrySetIterator = map.entrySet().iterator(); + + @Override + public boolean hasNext() throws TemplateModelException { + return entrySetIterator.hasNext(); + } + + @Override + public KeyValuePair next() throws TemplateModelException { + return new KeyValuePair() { + private final Map.Entry<String, TemplateModel> entry = entrySetIterator.next(); + + @Override + public TemplateModel getKey() throws TemplateModelException { + return new SimpleScalar(entry.getKey()); + } + + @Override + public TemplateModel getValue() throws TemplateModelException { + return entry.getValue(); + } + }; + } + }; + } + + @Override + public TemplateCollectionModel keys() throws TemplateModelException { + return new NativeStringCollectionCollectionEx(map.keySet()); + } + + @Override + public TemplateCollectionModel values() throws TemplateModelException { + return new NativeCollectionEx(map.values()); + } + + public TemplateModel put(String key, TemplateModel value) { + return map.put(key, value); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NativeSequence.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NativeSequence.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeSequence.java new file mode 100644 index 0000000..d1b6886 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeSequence.java @@ -0,0 +1,74 @@ +/* + * 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; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; + +import org.apache.freemarker.core.model.ObjectWrapper; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateModelException; +import org.apache.freemarker.core.model.TemplateSequenceModel; + +/** + * A sequence where each items is already a {@link TemplateModel}, so no {@link ObjectWrapper} need to be specified. + * + * <p>While this class allows adding items, doing so is not thread-safe, and thus only meant to be done during the + * initialization of the sequence. + */ +class NativeSequence implements TemplateSequenceModel, Serializable { + + private final ArrayList<TemplateModel> items; + + public NativeSequence(int capacity) { + items = new ArrayList<>(capacity); + } + + /** + * Copies the collection + */ + public NativeSequence(Collection<TemplateModel> items) { + this.items = new ArrayList<>(items.size()); + this.items.addAll(items); + } + + public void add(TemplateModel tm) { + items.add(tm); + } + + public void addAll(Collection<TemplateModel> items) { + this.items.addAll(items); + } + + public void clear() { + items.clear(); + } + + @Override + public TemplateModel get(int index) throws TemplateModelException { + return items.get(index); + } + + @Override + public int size() throws TemplateModelException { + return items.size(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringArraySequence.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringArraySequence.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringArraySequence.java new file mode 100644 index 0000000..96e9899 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringArraySequence.java @@ -0,0 +1,53 @@ +/* + * 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; + +import org.apache.freemarker.core.model.ObjectWrapper; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateModelException; +import org.apache.freemarker.core.model.TemplateSequenceModel; +import org.apache.freemarker.core.model.impl.DefaultArrayAdapter; +import org.apache.freemarker.core.model.impl.SimpleScalar; + +/** + * Adapts (not copies) a {@link String} array with on-the-fly wrapping of the items to {@link SimpleScalar}-s. The + * important difference to {@link DefaultArrayAdapter} is that it doesn't depend on an {@link ObjectWrapper}, which is + * needed to guarantee the behavior of some template language constructs. The important difference to + * {@link NativeSequence} is that it doesn't need upfront conversion to {@link TemplateModel}-s (performance). + */ +class NativeStringArraySequence implements TemplateSequenceModel { + + private final String[] items; + + public NativeStringArraySequence(String[] items) { + this.items = items; + } + + @Override + public TemplateModel get(int index) throws TemplateModelException { + return index < items.length ? new SimpleScalar(items[index]) : null; + } + + @Override + public int size() throws TemplateModelException { + return items.length; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringCollectionCollectionEx.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringCollectionCollectionEx.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringCollectionCollectionEx.java new file mode 100644 index 0000000..b2437e7 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringCollectionCollectionEx.java @@ -0,0 +1,79 @@ +/* + * 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; + +import java.util.Collection; +import java.util.Iterator; + +import org.apache.freemarker.core.model.ObjectWrapper; +import org.apache.freemarker.core.model.TemplateCollectionModelEx; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateModelException; +import org.apache.freemarker.core.model.TemplateModelIterator; +import org.apache.freemarker.core.model.impl.DefaultNonListCollectionAdapter; +import org.apache.freemarker.core.model.impl.SimpleScalar; + +/** + * Adapts (not copies) a {@link Collection} of {@link String}-s with on-the-fly wrapping of the items to {@link + * SimpleScalar}-s. The important difference to {@link DefaultNonListCollectionAdapter} is that it doesn't depend on an + * {@link ObjectWrapper}, which is needed to guarantee the behavior of some template language constructs. The important + * difference to {@link NativeCollectionEx} is that it doesn't need upfront conversion to {@link TemplateModel}-s + * (performance). + */ +class NativeStringCollectionCollectionEx implements TemplateCollectionModelEx { + + private final Collection<String> collection; + + public NativeStringCollectionCollectionEx(Collection<String> collection) { + this.collection = collection; + } + + @Override + public int size() { + return collection.size(); + } + + @Override + public boolean isEmpty() { + return collection.isEmpty(); + } + + @Override + public TemplateModelIterator iterator() throws TemplateModelException { + return new TemplateModelIterator() { + + private final Iterator<String> iterator = collection.iterator(); + + @Override + public TemplateModel next() throws TemplateModelException { + if (!iterator.hasNext()) { + throw new TemplateModelException("The collection has no more items."); + } + + return new SimpleScalar(iterator.next()); + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringListSequence.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringListSequence.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringListSequence.java new file mode 100644 index 0000000..7846fd3 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NativeStringListSequence.java @@ -0,0 +1,56 @@ +/* + * 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; + +import java.util.List; + +import org.apache.freemarker.core.model.ObjectWrapper; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateModelException; +import org.apache.freemarker.core.model.TemplateSequenceModel; +import org.apache.freemarker.core.model.impl.DefaultListAdapter; +import org.apache.freemarker.core.model.impl.SimpleScalar; + +/** + * Adapts (not copies) a {@link List} of {@link String}-s with on-the-fly wrapping of the items to {@link + * SimpleScalar}-s. The important difference to {@link DefaultListAdapter} is that it doesn't depend on an {@link + * ObjectWrapper}, which is needed to guarantee the behavior of some template language constructs. The important + * difference to {@link NativeSequence} is that it doesn't need upfront conversion to {@link TemplateModel}-s + * (performance). + */ +class NativeStringListSequence implements TemplateSequenceModel { + + private final List<String> items; + + public NativeStringListSequence(List<String> items) { + this.items = items; + } + + @Override + public TemplateModel get(int index) throws TemplateModelException { + return index < items.size() ? new SimpleScalar(items.get(index)) : null; + } + + @Override + public int size() throws TemplateModelException { + return items.size(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NestedContentNotSupportedException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NestedContentNotSupportedException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NestedContentNotSupportedException.java new file mode 100644 index 0000000..cca60ad --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NestedContentNotSupportedException.java @@ -0,0 +1,67 @@ +/* + * 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; + +import org.apache.freemarker.core.Environment.NestedElementTemplateDirectiveBody; +import org.apache.freemarker.core.ThreadInterruptionSupportTemplatePostProcessor.ASTThreadInterruptionCheck; +import org.apache.freemarker.core.model.TemplateDirectiveBody; +import org.apache.freemarker.core.util._StringUtil; + +/** + * Used in custom {@link org.apache.freemarker.core.model.TemplateDirectiveModel}-s to check if the directive invocation + * has no body. This is more intelligent than a {@code null} check; for example, when the body + * only contains a thread interruption check node, it treats it as valid. + */ +public class NestedContentNotSupportedException extends TemplateException { + + public static void check(TemplateDirectiveBody body) throws NestedContentNotSupportedException { + if (body == null) { + return; + } + if (body instanceof NestedElementTemplateDirectiveBody) { + ASTElement[] tes = ((NestedElementTemplateDirectiveBody) body).getChildrenBuffer(); + if (tes == null || tes.length == 0 + || tes[0] instanceof ASTThreadInterruptionCheck && (tes.length == 1 || tes[1] == null)) { + return; + } + } + throw new NestedContentNotSupportedException(Environment.getCurrentEnvironment()); + } + + + private NestedContentNotSupportedException(Environment env) { + this(null, null, env); + } + + private NestedContentNotSupportedException(Exception cause, Environment env) { + this(null, cause, env); + } + + private NestedContentNotSupportedException(String description, Environment env) { + this(description, null, env); + } + + private NestedContentNotSupportedException(String description, Exception cause, Environment env) { + super( "Nested content (body) not supported." + + (description != null ? " " + _StringUtil.jQuote(description) : ""), + cause, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonBooleanException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonBooleanException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonBooleanException.java new file mode 100644 index 0000000..3844fd0 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonBooleanException.java @@ -0,0 +1,62 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateBooleanModel; +import org.apache.freemarker.core.model.TemplateModel; + +/** + * Indicates that a {@link TemplateBooleanModel} value was expected, but the value had a different type. + */ +public class NonBooleanException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateBooleanModel.class }; + + public NonBooleanException(Environment env) { + super(env, "Expecting boolean value here"); + } + + public NonBooleanException(String description, Environment env) { + super(env, description); + } + + NonBooleanException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonBooleanException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "boolean", EXPECTED_TYPES, env); + } + + NonBooleanException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "boolean", EXPECTED_TYPES, tip, env); + } + + NonBooleanException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "boolean", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonDateException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonDateException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonDateException.java new file mode 100644 index 0000000..2e63e48 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonDateException.java @@ -0,0 +1,58 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateDateModel; +import org.apache.freemarker.core.model.TemplateModel; + +/** + * Indicates that a {@link TemplateDateModel} value was expected, but the value had a different type. + */ +public class NonDateException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateDateModel.class }; + + public NonDateException(Environment env) { + super(env, "Expecting date/time value here"); + } + + public NonDateException(String description, Environment env) { + super(env, description); + } + + NonDateException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "date/time", EXPECTED_TYPES, env); + } + + NonDateException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "date/time", EXPECTED_TYPES, tip, env); + } + + NonDateException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "date/time", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonExtendedHashException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonExtendedHashException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonExtendedHashException.java new file mode 100644 index 0000000..5614b23 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonExtendedHashException.java @@ -0,0 +1,62 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateHashModelEx; +import org.apache.freemarker.core.model.TemplateModel; + +/** + * Indicates that a {@link TemplateHashModelEx} value was expected, but the value had a different type. + */ +public class NonExtendedHashException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateHashModelEx.class }; + + public NonExtendedHashException(Environment env) { + super(env, "Expecting extended hash value here"); + } + + public NonExtendedHashException(String description, Environment env) { + super(env, description); + } + + NonExtendedHashException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonExtendedHashException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "extended hash", EXPECTED_TYPES, env); + } + + NonExtendedHashException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "extended hash", EXPECTED_TYPES, tip, env); + } + + NonExtendedHashException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "extended hash", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonExtendedNodeException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonExtendedNodeException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonExtendedNodeException.java new file mode 100644 index 0000000..b95cf77 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonExtendedNodeException.java @@ -0,0 +1,64 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateNodeModelEx; + +/** + * Indicates that a {@link TemplateNodeModelEx} value was expected, but the value had a different type. + * + * @since 2.3.26 + */ +public class NonExtendedNodeException extends UnexpectedTypeException { + + private static final Class<?>[] EXPECTED_TYPES = new Class[] { TemplateNodeModelEx.class }; + + public NonExtendedNodeException(Environment env) { + super(env, "Expecting extended node value here"); + } + + public NonExtendedNodeException(String description, Environment env) { + super(env, description); + } + + NonExtendedNodeException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonExtendedNodeException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "extended node", EXPECTED_TYPES, env); + } + + NonExtendedNodeException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "extended node", EXPECTED_TYPES, tip, env); + } + + NonExtendedNodeException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "extended node", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonHashException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonHashException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonHashException.java new file mode 100644 index 0000000..7c26bf2 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonHashException.java @@ -0,0 +1,64 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateHashModel; +import org.apache.freemarker.core.model.TemplateModel; + +/** + * Indicates that a {@link TemplateHashModel} value was expected, but the value had a different type. + * + * @since 2.3.21 + */ +public class NonHashException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateHashModel.class }; + + public NonHashException(Environment env) { + super(env, "Expecting hash value here"); + } + + public NonHashException(String description, Environment env) { + super(env, description); + } + + NonHashException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonHashException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "hash", EXPECTED_TYPES, env); + } + + NonHashException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "hash", EXPECTED_TYPES, tip, env); + } + + NonHashException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "hash", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonMarkupOutputException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonMarkupOutputException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonMarkupOutputException.java new file mode 100644 index 0000000..9a2d5c4 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonMarkupOutputException.java @@ -0,0 +1,64 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateMarkupOutputModel; +import org.apache.freemarker.core.model.TemplateModel; + +/** + * Indicates that a {@link TemplateMarkupOutputModel} value was expected, but the value had a different type. + * + * @since 2.3.24 + */ +public class NonMarkupOutputException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateMarkupOutputModel.class }; + + public NonMarkupOutputException(Environment env) { + super(env, "Expecting markup output value here"); + } + + public NonMarkupOutputException(String description, Environment env) { + super(env, description); + } + + NonMarkupOutputException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonMarkupOutputException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "markup output", EXPECTED_TYPES, env); + } + + NonMarkupOutputException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "markup output", EXPECTED_TYPES, tip, env); + } + + NonMarkupOutputException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "markup output", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonMethodException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonMethodException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonMethodException.java new file mode 100644 index 0000000..b6c461e --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonMethodException.java @@ -0,0 +1,64 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateMethodModel; +import org.apache.freemarker.core.model.TemplateModel; + +/** + * Indicates that a {@link TemplateMethodModel} value was expected, but the value had a different type. + * + * @since 2.3.21 + */ +public class NonMethodException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateMethodModel.class }; + + public NonMethodException(Environment env) { + super(env, "Expecting method value here"); + } + + public NonMethodException(String description, Environment env) { + super(env, description); + } + + NonMethodException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonMethodException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "method", EXPECTED_TYPES, env); + } + + NonMethodException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "method", EXPECTED_TYPES, tip, env); + } + + NonMethodException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "method", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonNamespaceException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonNamespaceException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonNamespaceException.java new file mode 100644 index 0000000..bf66312 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonNamespaceException.java @@ -0,0 +1,63 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateModel; + +/** + * Indicates that a {@link Environment.Namespace} value was expected, but the value had a different type. + * + * @since 2.3.21 + */ +class NonNamespaceException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { Environment.Namespace.class }; + + public NonNamespaceException(Environment env) { + super(env, "Expecting namespace value here"); + } + + public NonNamespaceException(String description, Environment env) { + super(env, description); + } + + NonNamespaceException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonNamespaceException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "namespace", EXPECTED_TYPES, env); + } + + NonNamespaceException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "namespace", EXPECTED_TYPES, tip, env); + } + + NonNamespaceException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "namespace", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonNodeException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonNodeException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonNodeException.java new file mode 100644 index 0000000..9c9e566 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonNodeException.java @@ -0,0 +1,64 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateNodeModel; + +/** + * Indicates that a {@link TemplateNodeModel} value was expected, but the value had a different type. + * + * @since 2.3.21 + */ +public class NonNodeException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateNodeModel.class }; + + public NonNodeException(Environment env) { + super(env, "Expecting node value here"); + } + + public NonNodeException(String description, Environment env) { + super(env, description); + } + + NonNodeException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonNodeException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "node", EXPECTED_TYPES, env); + } + + NonNodeException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "node", EXPECTED_TYPES, tip, env); + } + + NonNodeException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "node", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonNumericalException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonNumericalException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonNumericalException.java new file mode 100644 index 0000000..f70bd83 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonNumericalException.java @@ -0,0 +1,74 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateNumberModel; + +/** + * Indicates that a {@link TemplateNumberModel} value was expected, but the value had a different type. + */ +public class NonNumericalException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateNumberModel.class }; + + public NonNumericalException(Environment env) { + super(env, "Expecting numerical value here"); + } + + public NonNumericalException(String description, Environment env) { + super(env, description); + } + + NonNumericalException(_ErrorDescriptionBuilder description, Environment env) { + super(env, description); + } + + NonNumericalException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "number", EXPECTED_TYPES, env); + } + + NonNumericalException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "number", EXPECTED_TYPES, tip, env); + } + + NonNumericalException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "number", EXPECTED_TYPES, tips, env); + } + + NonNumericalException( + String assignmentTargetVarName, TemplateModel model, String[] tips, Environment env) + throws InvalidReferenceException { + super(assignmentTargetVarName, model, "number", EXPECTED_TYPES, tips, env); + } + static NonNumericalException newMalformedNumberException(ASTExpression blamed, String text, Environment env) { + return new NonNumericalException( + new _ErrorDescriptionBuilder("Can't convert this string to number: ", new _DelayedJQuote(text)) + .blame(blamed), + env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonSequenceException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonSequenceException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonSequenceException.java new file mode 100644 index 0000000..5018dc8 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonSequenceException.java @@ -0,0 +1,64 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateSequenceModel; + +/** + * Indicates that a {@link TemplateSequenceModel} value was expected, but the value had a different type. + * + * @since 2.3.21 + */ +public class NonSequenceException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { TemplateSequenceModel.class }; + + public NonSequenceException(Environment env) { + super(env, "Expecting sequence value here"); + } + + public NonSequenceException(String description, Environment env) { + super(env, description); + } + + NonSequenceException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonSequenceException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "sequence", EXPECTED_TYPES, env); + } + + NonSequenceException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "sequence", EXPECTED_TYPES, tip, env); + } + + NonSequenceException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "sequence", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonSequenceOrCollectionException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonSequenceOrCollectionException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonSequenceOrCollectionException.java new file mode 100644 index 0000000..0baa5c5 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonSequenceOrCollectionException.java @@ -0,0 +1,92 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateCollectionModel; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateSequenceModel; +import org.apache.freemarker.core.model.WrapperTemplateModel; +import org.apache.freemarker.core.util._CollectionUtil; + +/** + * Indicates that a {@link TemplateSequenceModel} or {@link TemplateCollectionModel} value was expected, but the value + * had a different type. + * + * @since 2.3.21 + */ +public class NonSequenceOrCollectionException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { + TemplateSequenceModel.class, TemplateCollectionModel.class + }; + private static final String ITERABLE_SUPPORT_HINT = "The problematic value is a java.lang.Iterable. Using " + + "DefaultObjectWrapper(..., iterableSupport=true) as the object_wrapper setting of the FreeMarker " + + "configuration should solve this."; + + public NonSequenceOrCollectionException(Environment env) { + super(env, "Expecting sequence or collection value here"); + } + + public NonSequenceOrCollectionException(String description, Environment env) { + super(env, description); + } + + NonSequenceOrCollectionException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonSequenceOrCollectionException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + this(blamed, model, _CollectionUtil.EMPTY_OBJECT_ARRAY, env); + } + + NonSequenceOrCollectionException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + this(blamed, model, new Object[] { tip }, env); + } + + NonSequenceOrCollectionException( + ASTExpression blamed, TemplateModel model, Object[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "sequence or collection", EXPECTED_TYPES, extendTipsIfIterable(model, tips), env); + } + + private static Object[] extendTipsIfIterable(TemplateModel model, Object[] tips) { + if (isWrappedIterable(model)) { + final int tipsLen = tips != null ? tips.length : 0; + Object[] extendedTips = new Object[tipsLen + 1]; + for (int i = 0; i < tipsLen; i++) { + extendedTips[i] = tips[i]; + } + extendedTips[tipsLen] = ITERABLE_SUPPORT_HINT; + return extendedTips; + } else { + return tips; + } + } + + public static boolean isWrappedIterable(TemplateModel model) { + return model instanceof WrapperTemplateModel + && ((WrapperTemplateModel) model).getWrappedObject() instanceof Iterable; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonStringException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonStringException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonStringException.java new file mode 100644 index 0000000..c8cbc6d --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonStringException.java @@ -0,0 +1,74 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateBooleanModel; +import org.apache.freemarker.core.model.TemplateDateModel; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateNumberModel; +import org.apache.freemarker.core.model.TemplateScalarModel; + +/** + * Indicates that a {@link TemplateScalarModel} value was expected (or maybe something that can be automatically coerced + * to that), but the value had a different type. + */ +public class NonStringException extends UnexpectedTypeException { + + static final String STRING_COERCABLE_TYPES_DESC + = "string or something automatically convertible to string (number, date or boolean)"; + + static final Class[] STRING_COERCABLE_TYPES = new Class[] { + TemplateScalarModel.class, TemplateNumberModel.class, TemplateDateModel.class, TemplateBooleanModel.class + }; + + private static final String DEFAULT_DESCRIPTION + = "Expecting " + NonStringException.STRING_COERCABLE_TYPES_DESC + " value here"; + + public NonStringException(Environment env) { + super(env, DEFAULT_DESCRIPTION); + } + + public NonStringException(String description, Environment env) { + super(env, description); + } + + NonStringException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonStringException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, NonStringException.STRING_COERCABLE_TYPES_DESC, STRING_COERCABLE_TYPES, env); + } + + NonStringException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, NonStringException.STRING_COERCABLE_TYPES_DESC, STRING_COERCABLE_TYPES, tip, env); + } + + NonStringException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, NonStringException.STRING_COERCABLE_TYPES_DESC, STRING_COERCABLE_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonStringOrTemplateOutputException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonStringOrTemplateOutputException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonStringOrTemplateOutputException.java new file mode 100644 index 0000000..ddeb811 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonStringOrTemplateOutputException.java @@ -0,0 +1,78 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateMarkupOutputModel; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateScalarModel; + +/** + * Indicates that a {@link TemplateScalarModel} (or maybe something that can be automatically coerced + * to that) or {@link TemplateMarkupOutputModel} value was expected, but the value had a different type. + */ +public class NonStringOrTemplateOutputException extends UnexpectedTypeException { + + static final String STRING_COERCABLE_TYPES_OR_TOM_DESC + = NonStringException.STRING_COERCABLE_TYPES_DESC + ", or \"template output\""; + + static final Class[] STRING_COERCABLE_TYPES_AND_TOM; + static { + STRING_COERCABLE_TYPES_AND_TOM = new Class[NonStringException.STRING_COERCABLE_TYPES.length + 1]; + int i; + for (i = 0; i < NonStringException.STRING_COERCABLE_TYPES.length; i++) { + STRING_COERCABLE_TYPES_AND_TOM[i] = NonStringException.STRING_COERCABLE_TYPES[i]; + } + STRING_COERCABLE_TYPES_AND_TOM[i] = TemplateMarkupOutputModel.class; + } + + private static final String DEFAULT_DESCRIPTION + = "Expecting " + NonStringOrTemplateOutputException.STRING_COERCABLE_TYPES_OR_TOM_DESC + " value here"; + + public NonStringOrTemplateOutputException(Environment env) { + super(env, DEFAULT_DESCRIPTION); + } + + public NonStringOrTemplateOutputException(String description, Environment env) { + super(env, description); + } + + NonStringOrTemplateOutputException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonStringOrTemplateOutputException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, NonStringOrTemplateOutputException.STRING_COERCABLE_TYPES_OR_TOM_DESC, STRING_COERCABLE_TYPES_AND_TOM, env); + } + + NonStringOrTemplateOutputException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, NonStringOrTemplateOutputException.STRING_COERCABLE_TYPES_OR_TOM_DESC, STRING_COERCABLE_TYPES_AND_TOM, tip, env); + } + + NonStringOrTemplateOutputException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, NonStringOrTemplateOutputException.STRING_COERCABLE_TYPES_OR_TOM_DESC, STRING_COERCABLE_TYPES_AND_TOM, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/NonUserDefinedDirectiveLikeException.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/NonUserDefinedDirectiveLikeException.java b/freemarker-core/src/main/java/org/apache/freemarker/core/NonUserDefinedDirectiveLikeException.java new file mode 100644 index 0000000..918c720 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/NonUserDefinedDirectiveLikeException.java @@ -0,0 +1,67 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateDirectiveModel; +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.model.TemplateTransformModel; + +/** + * Indicates that a {@link TemplateDirectiveModel} or {@link TemplateTransformModel} or {@link ASTDirMacro} value was + * expected, but the value had a different type. + * + * @since 2.3.21 + */ +class NonUserDefinedDirectiveLikeException extends UnexpectedTypeException { + + private static final Class[] EXPECTED_TYPES = new Class[] { + TemplateDirectiveModel.class, TemplateTransformModel.class, ASTDirMacro.class }; + + public NonUserDefinedDirectiveLikeException(Environment env) { + super(env, "Expecting user-defined directive, transform or macro value here"); + } + + public NonUserDefinedDirectiveLikeException(String description, Environment env) { + super(env, description); + } + + NonUserDefinedDirectiveLikeException(Environment env, _ErrorDescriptionBuilder description) { + super(env, description); + } + + NonUserDefinedDirectiveLikeException( + ASTExpression blamed, TemplateModel model, Environment env) + throws InvalidReferenceException { + super(blamed, model, "user-defined directive, transform or macro", EXPECTED_TYPES, env); + } + + NonUserDefinedDirectiveLikeException( + ASTExpression blamed, TemplateModel model, String tip, + Environment env) + throws InvalidReferenceException { + super(blamed, model, "user-defined directive, transform or macro", EXPECTED_TYPES, tip, env); + } + + NonUserDefinedDirectiveLikeException( + ASTExpression blamed, TemplateModel model, String[] tips, Environment env) throws InvalidReferenceException { + super(blamed, model, "user-defined directive, transform or macro", EXPECTED_TYPES, tips, env); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/OutputFormatBoundBuiltIn.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/OutputFormatBoundBuiltIn.java b/freemarker-core/src/main/java/org/apache/freemarker/core/OutputFormatBoundBuiltIn.java new file mode 100644 index 0000000..c67f2c0 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/OutputFormatBoundBuiltIn.java @@ -0,0 +1,48 @@ +/* + * 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; + +import org.apache.freemarker.core.model.TemplateModel; +import org.apache.freemarker.core.outputformat.OutputFormat; +import org.apache.freemarker.core.util._NullArgumentException; + +abstract class OutputFormatBoundBuiltIn extends SpecialBuiltIn { + + protected OutputFormat outputFormat; + protected int autoEscapingPolicy; + + void bindToOutputFormat(OutputFormat outputFormat, int autoEscapingPolicy) { + _NullArgumentException.check(outputFormat); + this.outputFormat = outputFormat; + this.autoEscapingPolicy = autoEscapingPolicy; + } + + @Override + TemplateModel _eval(Environment env) throws TemplateException { + if (outputFormat == null) { + // The parser should prevent this situation + throw new NullPointerException("outputFormat was null"); + } + return calculateResult(env); + } + + protected abstract TemplateModel calculateResult(Environment env) + throws TemplateException; + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/3fd56062/freemarker-core/src/main/java/org/apache/freemarker/core/ParameterRole.java ---------------------------------------------------------------------- diff --git a/freemarker-core/src/main/java/org/apache/freemarker/core/ParameterRole.java b/freemarker-core/src/main/java/org/apache/freemarker/core/ParameterRole.java new file mode 100644 index 0000000..146f0b8 --- /dev/null +++ b/freemarker-core/src/main/java/org/apache/freemarker/core/ParameterRole.java @@ -0,0 +1,91 @@ +/* + * 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; + +// Change this to an Enum in Java 5 +/** + * @see ASTNode#getParameterRole(int) + */ +final class ParameterRole { + + private final String name; + + static final ParameterRole UNKNOWN = new ParameterRole("[unknown role]"); + + // When figuring out the names of these, always read them after the possible getName() values. It should sound OK. + // Like "`+` left hand operand", or "`#if` parameter". That is, the roles (only) have to make sense in the + // context of the possible ASTNode classes. + static final ParameterRole LEFT_HAND_OPERAND = new ParameterRole("left-hand operand"); + static final ParameterRole RIGHT_HAND_OPERAND = new ParameterRole("right-hand operand"); + static final ParameterRole ENCLOSED_OPERAND = new ParameterRole("enclosed operand"); + static final ParameterRole ITEM_VALUE = new ParameterRole("item value"); + static final ParameterRole ITEM_KEY = new ParameterRole("item key"); + static final ParameterRole ASSIGNMENT_TARGET = new ParameterRole("assignment target"); + static final ParameterRole ASSIGNMENT_OPERATOR = new ParameterRole("assignment operator"); + static final ParameterRole ASSIGNMENT_SOURCE = new ParameterRole("assignment source"); + static final ParameterRole VARIABLE_SCOPE = new ParameterRole("variable scope"); + static final ParameterRole NAMESPACE = new ParameterRole("namespace"); + static final ParameterRole ERROR_HANDLER = new ParameterRole("error handler"); + static final ParameterRole PASSED_VALUE = new ParameterRole("passed value"); + static final ParameterRole CONDITION = new ParameterRole("condition"); + static final ParameterRole VALUE = new ParameterRole("value"); + static final ParameterRole AST_NODE_SUBTYPE = new ParameterRole("AST-node subtype"); + static final ParameterRole PLACEHOLDER_VARIABLE = new ParameterRole("placeholder variable"); + static final ParameterRole EXPRESSION_TEMPLATE = new ParameterRole("expression template"); + static final ParameterRole LIST_SOURCE = new ParameterRole("list source"); + static final ParameterRole TARGET_LOOP_VARIABLE = new ParameterRole("target loop variable"); + static final ParameterRole TEMPLATE_NAME = new ParameterRole("template name"); + static final ParameterRole IGNORE_MISSING_PARAMETER = new ParameterRole("\"ignore_missing\" parameter"); + static final ParameterRole PARAMETER_NAME = new ParameterRole("parameter name"); + static final ParameterRole PARAMETER_DEFAULT = new ParameterRole("parameter default"); + static final ParameterRole CATCH_ALL_PARAMETER_NAME = new ParameterRole("catch-all parameter name"); + static final ParameterRole ARGUMENT_NAME = new ParameterRole("argument name"); + static final ParameterRole ARGUMENT_VALUE = new ParameterRole("argument value"); + static final ParameterRole CONTENT = new ParameterRole("content"); + static final ParameterRole EMBEDDED_TEMPLATE = new ParameterRole("embedded template"); + static final ParameterRole VALUE_PART = new ParameterRole("value part"); + static final ParameterRole MINIMUM_DECIMALS = new ParameterRole("minimum decimals"); + static final ParameterRole MAXIMUM_DECIMALS = new ParameterRole("maximum decimals"); + static final ParameterRole NODE = new ParameterRole("node"); + static final ParameterRole CALLEE = new ParameterRole("callee"); + static final ParameterRole MESSAGE = new ParameterRole("message"); + + private ParameterRole(String name) { + this.name = name; + } + + static ParameterRole forBinaryOperatorOperand(int paramIndex) { + switch (paramIndex) { + case 0: return LEFT_HAND_OPERAND; + case 1: return RIGHT_HAND_OPERAND; + default: throw new IndexOutOfBoundsException(); + } + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return name; + } + +}
