this.schemaFactory = Preconditions.checkNotNull(schemaFactory);
this.className = Preconditions.checkNotNull(className);
}
public ReparentedTree apply(IfExpandedTree parseTree) {
AlertSetBuilder alertSetBuilder = new AlertSetBuilder(parseTree.getAlerts());
// make sure root element is a template
List<ParsedElement> children = parseTree.getChildren();
if (!children.isEmpty()) {
ParsedElement first = children.get(0);
if (!first.canBeRoot()) {
alertSetBuilder.add(new InvalidRootError(first));
} else {
// determine the schema of this gxp
String contentType = DEFAULT_CONTENT_TYPE;
for (ParsedAttribute attr : first.getAttributes()) {
if (attr.getName().equals("content-type")
&& attr.getNamespace() == NullNamespace.INSTANCE) {
contentType = attr.getValue();
}
}
rootSchema = schemaFactory.fromContentTypeName(contentType);
}
}
Iterable<ParsedAttribute> noAttrs = Collections.emptyList();
Parts parts = groupParts(alertSetBuilder, parseTree, noAttrs, children);
// Throw out all but the first Root. The parse phase should have
// already complained about there being too many roots.
List<Root> roots = parts.getRoots();
Root root = roots.isEmpty()
? new NullRoot(parseTree, TemplateName.parseFullyQualifiedDottedName(className))
: roots.get(0);
return new ReparentedTree(parseTree.getSourcePosition(),
alertSetBuilder.buildAndClear(),
root);
}