com.google.gxp.compiler.base..svn.text-base.Template.svn-base Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of google-gxp Show documentation
Show all versions of google-gxp Show documentation
Google XML Pages (GXP) is a templating system used to generate XML/SGML markup (most often HTML).
The newest version!
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed 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 com.google.gxp.compiler.base;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.gxp.compiler.alerts.SourcePosition;
import com.google.gxp.compiler.schema.Schema;
import java.util.List;
/**
* A GXP template. Corresponds to a {@code } element.
*/
public final class Template extends AbstractRoot {
private final Constructor constructor;
private final ImmutableList allParameters;
private final ImmutableList implementsDeclarations;
private final Expression content;
private final Callable callable;
private final InstanceCallable instanceCallable;
public Template(SourcePosition sourcePosition,
String displayName,
TemplateName.FullyQualified name,
Schema schema,
List javaAnnotations,
Constructor constructor,
List imports,
List implementsDeclarations,
List throwsDeclarations,
List parameters,
List formalTypeParameters,
Expression content) {
super(sourcePosition, displayName, name, schema, javaAnnotations,
imports, throwsDeclarations, parameters, formalTypeParameters);
this.constructor = Preconditions.checkNotNull(constructor);
this.allParameters = ImmutableList.copyOf(
Iterables.concat(constructor.getParameters(), getParameters()));
this.implementsDeclarations = ImmutableList.copyOf(implementsDeclarations);
this.content = Preconditions.checkNotNull(content);
this.callable = new TemplateCallable(name, schema, getAllParameters());
List params = Lists.newArrayList();
params.addAll(getParameters());
params.add(new Parameter(new FormalParameter(sourcePosition, Implementable.INSTANCE_PARAM_NAME,
Implementable.INSTANCE_PARAM_NAME,
new InstanceType(this, name))));
this.instanceCallable = new TemplateInstanceCallable(name, schema, params);
}
public Template(Node fromNode,
TemplateName.FullyQualified name,
Schema schema,
List javaAnnotations,
Constructor constructor,
List imports,
List implementsDeclarations,
List throwsDeclarations,
List parameters,
List formalTypeParameters,
Expression content) {
this(fromNode.getSourcePosition(), fromNode.getDisplayName(),
name, schema, javaAnnotations, constructor, imports,
implementsDeclarations, throwsDeclarations, parameters,
formalTypeParameters, content);
}
protected Template self() {
return this;
}
protected Template withParameters(List newParameters) {
return new Template(this, getName(), getSchema(), getJavaAnnotations(),
constructor, getImports(), getImplementsDeclarations(),
getThrowsDeclarations(), newParameters,
getFormalTypeParameters(), content);
}
public Template withContent(Expression newContent) {
return newContent.equals(content)
? this
: new Template(this, getName(), getSchema(), getJavaAnnotations(),
constructor, getImports(), getImplementsDeclarations(),
getThrowsDeclarations(), getParameters(),
getFormalTypeParameters(), newContent);
}
public Template withConstructor(Constructor newConstructor) {
return constructor.equals(newConstructor)
? this
: new Template(this, getName(), getSchema(), getJavaAnnotations(),
newConstructor, getImports(), getImplementsDeclarations(),
getThrowsDeclarations(), getParameters(),
getFormalTypeParameters(), content);
}
public Template withImplementsDeclarations(List newImplDec) {
return implementsDeclarations.equals(newImplDec)
? this
: new Template(this, getName(), getSchema(), getJavaAnnotations(),
getConstructor(), getImports(), newImplDec,
getThrowsDeclarations(), getParameters(),
getFormalTypeParameters(), content);
}
public T acceptVisitor(RootVisitor visitor) {
return visitor.visitTemplate(this);
}
public Constructor getConstructor() {
return constructor;
}
public List getAllParameters() {
return allParameters;
}
public List getImplementsDeclarations() {
return implementsDeclarations;
}
public Expression getContent() {
return content;
}
public Callable getCallable() {
return callable;
}
public InstanceCallable getInstanceCallable() {
return instanceCallable;
}
public Implementable getImplementable() {
return null;
}
/**
* The {@code Callable} exposed by a {@code Template}.
*/
@SuppressWarnings("serial") // let java pick the serialVersionUID
private static class TemplateCallable extends AbstractCallable {
public TemplateCallable(TemplateName.FullyQualified name, Schema schema,
List parameters) {
super(name, schema, parameters);
}
public T acceptCallableVisitor(CallableVisitor visitor) {
return visitor.visitCallable(this);
}
@Override
public boolean equals(Object that) {
return this == that
|| (that instanceof TemplateCallable && equals((TemplateCallable) that));
}
public boolean equals(TemplateCallable that) {
return equalsAbstractCallable(that);
}
@Override
public int hashCode() {
return abstractCallableHashCode();
}
}
/**
* The {@code InstanceCallable} exposed by a {@code Template}.
*/
@SuppressWarnings("serial") // let java pick the serialVersionUID
private static class TemplateInstanceCallable
extends AbstractCallable implements InstanceCallable {
public TemplateInstanceCallable(TemplateName.FullyQualified name, Schema schema,
List parameters) {
super(name, schema, parameters);
}
public Type getInstanceType() {
return getParameter(Implementable.INSTANCE_PARAM_NAME).getType();
}
public T acceptCallableVisitor(CallableVisitor visitor) {
return visitor.visitInstanceCallable(this);
}
@Override
public boolean equals(Object that) {
return this == that
|| (that instanceof TemplateInstanceCallable && equals((TemplateInstanceCallable) that));
}
public boolean equals(TemplateInstanceCallable that) {
return equalsAbstractCallable(that);
}
@Override
public int hashCode() {
return abstractCallableHashCode();
}
}
}