org.jfxcore.compiler.ast.DocumentNode Maven / Gradle / Ivy
// Copyright (c) 2022, 2023, JFXcore. All rights reserved.
// Use of this source code is governed by the BSD-3-Clause license that can be found in the LICENSE file.
package org.jfxcore.compiler.ast;
import org.jfxcore.compiler.VersionInfo;
import org.jfxcore.compiler.diagnostic.SourceInfo;
import org.jfxcore.compiler.ast.codebehind.ClassNode;
import org.jfxcore.compiler.ast.codebehind.JavaEmitContext;
import org.jfxcore.compiler.ast.codebehind.JavaEmitterNode;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class DocumentNode extends AbstractNode implements JavaEmitterNode {
private final Path sourceFile;
private final List imports;
private Node root;
public DocumentNode(Path sourceFile, Collection imports, Node root) {
super(SourceInfo.none());
this.sourceFile = checkNotNull(sourceFile);
this.imports = new ArrayList<>(checkNotNull(imports));
this.root = checkNotNull(root);
}
public Path getSourceFile() {
return sourceFile;
}
public List getImports() {
return imports;
}
public Node getRoot() {
return root;
}
@Override
public void emit(JavaEmitContext context) {
context.getOutput()
.append("//\r\n")
.append(String.format("// This file was generated by %s:%s:%s\r\n",
VersionInfo.getGroup(), VersionInfo.getName(), VersionInfo.getVersion()))
.append("// Changes in this file will be lost if the code is regenerated.\r\n")
.append("//\r\n");
String packageName = ((ClassNode)root).getPackageName();
if (packageName != null && !packageName.isEmpty()) {
context.getOutput().append(String.format("package %s;\r\n\r\n", packageName));
}
for (String importDecl : imports) {
context.getOutput().append(String.format("import %s;\r\n", importDecl));
}
if (!imports.isEmpty()) {
context.getOutput().append("\r\n");
}
context.emit(root);
}
@Override
public void acceptChildren(Visitor visitor) {
super.acceptChildren(visitor);
root = root.accept(visitor);
}
@Override
public DocumentNode deepClone() {
return new DocumentNode(sourceFile, imports, root.deepClone());
}
}