All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.st.p2012.mind.idl.IDLTemplateProcessor Maven / Gradle / Ivy
/**
\file com/st/p2012/mind/idl/IDLTemplateProcessor.java
\brief The class encapsulating the template component for the IDL documentation generator.
\n
This file is part of the Platform 2012 program,
a cooperation between STMicroelectronics and CEA.\n
Redistribution of this file to outside parties is
strictly prohibited without the written consent
of the module owner indicated below.\n
\par Module owner: [email protected]
\par Copyright (C) 2009 STMicroelectronics
\par Authors: [email protected]
\par Id: $Id$
\par Date: $Date$
\par Revision: $Rev$
*/
package com.st.p2012.mind.idl;
import static org.objectweb.fractal.cecilia.adl.file.SourceFileWriter.writeToFile;
import static org.objectweb.fractal.mind.BindingControllerImplHelper.checkItfName;
import static org.objectweb.fractal.mind.BindingControllerImplHelper.listFcHelper;
import static org.objectweb.fractal.mind.PathHelper.fullyQualifiedNameToPath;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.antlr.stringtemplate.StringTemplate;
import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.language.DefaultTemplateLexer;
import org.objectweb.fractal.adl.ADLException;
import org.objectweb.fractal.adl.CompilerError;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.mind.InputResourceLocator;
import org.objectweb.fractal.mind.idl.IDLVisitor;
import org.objectweb.fractal.mind.idl.ast.EnumDefinition;
import org.objectweb.fractal.mind.idl.ast.IDL;
import org.objectweb.fractal.mind.idl.ast.InterfaceDefinition;
import org.objectweb.fractal.mind.idl.ast.Method;
import org.objectweb.fractal.mind.idl.ast.StructDefinition;
import org.objectweb.fractal.mind.idl.ast.Type;
import org.objectweb.fractal.mind.idl.ast.TypeCollectionContainer;
import org.objectweb.fractal.mind.idl.ast.TypeDefinition;
import org.objectweb.fractal.mind.idl.ast.UnionDefinition;
import org.objectweb.fractal.mind.idl.jtb.visitor.TreeFormatter;
import org.objectweb.fractal.mind.io.IOErrors;
import org.objectweb.fractal.mind.io.OutputFileLocator;
import org.objectweb.fractal.mind.st.AbstractStringTemplateProcessor;
import com.st.p2012.mind.HTMLDocumentationHelper;
import com.st.p2012.mind.HTMLRenderer;
import com.st.p2012.mind.comments.CommentProcessor;
import static com.st.p2012.mind.HTMLDocumentationHelper.addAnnotationDecoration;
import static com.st.p2012.mind.HTMLDocumentationHelper.getMethodAnchor;
import static com.st.p2012.mind.HTMLDocumentationHelper.getPathToRoot;
public class IDLTemplateProcessor extends AbstractStringTemplateProcessor
implements
IDLVisitor,
BindingController {
// ---------------------------------------------------------------------------
// Client interfaces
// ---------------------------------------------------------------------------
/** Client interface used to locate output files. */
public OutputFileLocator outputFileLocatorItf;
/** client interface used to checks timestamps of input resources. */
public InputResourceLocator inputResourceLocatorItf;
// ---------------------------------------------------------------------------
// Implementation of the Visitor interface
// ---------------------------------------------------------------------------
public IDLTemplateProcessor() {
super("st.definitions.documentation.Interface");
}
public void visit(final IDL idl, final Map context)
throws ADLException {
final StringTemplate st = getInstanceOf("InterfaceDocumentation");
final File headerFile = outputFileLocatorItf.getCSourceOutputFile(
getOutputFileName(idl), context);
CommentProcessor.process(idl);
st.setAttribute("idl", idl);
st.setAttribute("pathToRoot", getPathToRoot(idl.getName()));
st.setAttribute("anchors", getAnchorMap(idl));
st.setAttribute("sectionAnchors", getSectionAnchorMap(idl));
addDecorations(idl);
try {
writeToFile(headerFile, st.toString());
} catch (final IOException e) {
throw new CompilerError(IOErrors.WRITE_ERROR, e, headerFile
.getAbsolutePath());
}
}
private Map getSectionAnchorMap(final IDL idl) {
final Map map= new HashMap();
if (idl instanceof InterfaceDefinition) {
final InterfaceDefinition def = (InterfaceDefinition)idl;
if(def.getMethods().length != 0) {
map.put("methods", "METHODS");
}
}
if (idl instanceof TypeCollectionContainer) {
final TypeCollectionContainer container = (TypeCollectionContainer)idl;
if (container.getTypes().length != 0) {
map.put("types", "TYPES");
}
}
return map;
}
private Map getAnchorMap(final IDL idl) {
final Map superMap = new HashMap();
if (idl instanceof InterfaceDefinition) {
final InterfaceDefinition def = (InterfaceDefinition)idl;
final Map map = new HashMap();
superMap.put("methods", map);
for (final Method method : def.getMethods()) {
map.put(method.getName(), getMethodAnchor(method.getName()));
}
}
if (idl instanceof TypeCollectionContainer) {
final TypeCollectionContainer container = (TypeCollectionContainer)idl;
final Map> typeMap = new HashMap>();
superMap.put("types", typeMap);
final Map typedefMap = new HashMap();
typeMap.put("typedef", typedefMap);
final Map structMap = new HashMap();
typeMap.put("struct", structMap);
final Map unionMap = new HashMap();
typeMap.put("union", unionMap);
final Map enumMap = new HashMap();
typeMap.put("enum", enumMap);
for (final Type type : container.getTypes()) {
if (type instanceof TypeDefinition) {
final String name = ((TypeDefinition) type).getName();
typedefMap.put(name, HTMLDocumentationHelper.getTypedefAnchor(name));
} else if (type instanceof StructDefinition) {
final String name = ((StructDefinition)type).getName();
structMap.put(name, HTMLDocumentationHelper.getStructAnchor(name));
} else if (type instanceof UnionDefinition) {
final String name = ((UnionDefinition)type).getName();
unionMap.put(name, HTMLDocumentationHelper.getUnionAnchor(name));
} else if (type instanceof EnumDefinition) {
final String name = ((EnumDefinition)type).getName();
enumMap.put(name, HTMLDocumentationHelper.getEnumAnchor(name));
}
}
}
return superMap;
}
private void addDecorations(final IDL idl) {
final List types = getTypeList(idl);
idl.astSetDecoration("types", types);
addAnnotationDecoration(idl);
if (idl instanceof InterfaceDefinition) {
final InterfaceDefinition def = (InterfaceDefinition)idl;
for (final Method method : def.getMethods()) {
addAnnotationDecoration(method);
}
}
}
private List getTypeList(final IDL idl) {
final List result = new LinkedList();
if (idl instanceof TypeCollectionContainer) {
final TypeCollectionContainer container = (TypeCollectionContainer)idl;
for (final Type type : container.getTypes()) {
if (type instanceof TypeDefinition) {
result.add(type);
setPrettyPrintSource(type);
} else if (type instanceof StructDefinition) {
final StructDefinition struct = (StructDefinition)type;
if(!(struct.getName().length() == 0)) {
result.add(type);
setPrettyPrintSource(type);
}
} else if (type instanceof UnionDefinition) {
final UnionDefinition struct = (UnionDefinition)type;
if(!(struct.getName().length() == 0)) {
result.add(type);
setPrettyPrintSource(type);
}
} else if (type instanceof EnumDefinition) {
final EnumDefinition struct = (EnumDefinition)type;
if(!(struct.getName().length() == 0)) {
result.add(type);
setPrettyPrintSource(type);
}
}
}
}
return result;
}
private void setPrettyPrintSource(final Type type) {
final StringWriter sw = new StringWriter();
final TreeFormatter formatter = new IDLTreeFormatter();
final IDLTreeDumper dumper = new IDLTreeDumper(sw);
final org.objectweb.fractal.mind.idl.jtb.syntaxtree.TypeDefinition astNode = (org.objectweb.fractal.mind.idl.jtb.syntaxtree.TypeDefinition) type.astGetDecoration("syntax-tree");
if(astNode != null) {
formatter.visit(astNode);
dumper.visit(astNode);
type.astSetDecoration("source", sw.toString());
}
}
protected String getOutputFileName(final IDL interfaceDefinition) {
return fullyQualifiedNameToPath(interfaceDefinition.getName(), HTMLDocumentationHelper.ITF_DOC_EXT);
}
// ---------------------------------------------------------------------------
// Overridden BindingController methods
// ---------------------------------------------------------------------------
@Override
public void bindFc(final String itfName, final Object value)
throws NoSuchInterfaceException, IllegalBindingException {
checkItfName(itfName);
if (itfName.equals(OutputFileLocator.ITF_NAME)) {
outputFileLocatorItf = (OutputFileLocator) value;
} else if (itfName.equals(InputResourceLocator.ITF_NAME)) {
inputResourceLocatorItf = (InputResourceLocator) value;
} else {
super.bindFc(itfName, value);
}
}
@Override
public String[] listFc() {
return listFcHelper(super.listFc(), OutputFileLocator.ITF_NAME,
InputResourceLocator.ITF_NAME);
}
@Override
public Object lookupFc(final String itfName) throws NoSuchInterfaceException {
checkItfName(itfName);
if (itfName.equals(OutputFileLocator.ITF_NAME)) {
return outputFileLocatorItf;
} else if (itfName.equals(InputResourceLocator.ITF_NAME)) {
return inputResourceLocatorItf;
} else {
return super.lookupFc(itfName);
}
}
@Override
public void unbindFc(final String itfName) throws NoSuchInterfaceException,
IllegalBindingException {
checkItfName(itfName);
if (itfName.equals(OutputFileLocator.ITF_NAME)) {
outputFileLocatorItf = null;
} else if (itfName.equals(InputResourceLocator.ITF_NAME)) {
inputResourceLocatorItf = null;
} else {
super.unbindFc(itfName);
}
}
@SuppressWarnings("unchecked")
@Override
protected Class getTemplateLexer() {
return DefaultTemplateLexer.class;
}
@Override
protected void registerCustomRenderer(final StringTemplateGroup templateGroup) {
templateGroup.registerRenderer(String.class, new HTMLRenderer());
}
}