Skip to content

Commit

Permalink
Support to have configuration by STGroup (#108)
Browse files Browse the repository at this point in the history
* Refs #19960. Support to have config by STGroup

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #19960. Fix conding style.

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #19960. Apply suggestions

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

---------

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
  • Loading branch information
richiware authored Nov 22, 2023
1 parent 21a04f9 commit 2a59bd2
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 34 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/eprosima/idl/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,11 @@ public boolean isGenerateTypesC()
return generate_typesc_;
}

public TemplateManager get_template_manager()
{
return tmanager_;
}

// OS
String m_os = null;
String m_userdir = null;
Expand Down
56 changes: 33 additions & 23 deletions src/main/java/com/eprosima/idl/generator/manager/TemplateGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,44 @@

public class TemplateGroup
{
private Map<String, ST> m_templates = new HashMap<String, ST>();
private Map<String, TemplateST> m_templates = new HashMap<String, TemplateST>();
private Map<String, List<ST>> m_extensionstemplates = new HashMap<String, List<ST>>();
private TemplateErrorListener error_listener_ = null;
private TemplateManager manager_ = null;

public TemplateGroup(TemplateManager manager)
{
manager_ = manager;
error_listener_ = new TemplateErrorListener(manager);
}

public void addTemplate(String groupname, ST template)
public void addTemplate(String groupname, TemplateST template)
{
m_templates.put(groupname, template);
}

public void addTemplate(String groupname, ST template, List<ST> extensionstemplates)
public void addTemplate(String groupname, TemplateST template, List<ST> extensionstemplates)
{
addTemplate(groupname, template);
m_extensionstemplates.put(groupname + "_" + template.getName(), extensionstemplates);
m_extensionstemplates.put(groupname + "_" + template.get_st().getName(), extensionstemplates);
}

public ST getTemplate(String groupname)
public TemplateST getTemplate(String groupname)
{
ST template = m_templates.get(groupname);
TemplateST template = m_templates.get(groupname);

//If there is extensiones, add them before return the template.
if(m_extensionstemplates.containsKey(groupname + "_" + template.getName()))
if(m_extensionstemplates.containsKey(groupname + "_" + template.get_st().getName()))
{
List<ST> extemplates = new ArrayList<ST>();
List<ST> extensions = m_extensionstemplates.get(groupname + "_" + template.getName());
List<ST> extensions = m_extensionstemplates.get(groupname + "_" + template.get_st().getName());

for(ST extension : extensions)
{
extemplates.add(extension);
}

template.add("extensions", extemplates);
template.get_st().add("extensions", extemplates);
}

return template;
Expand All @@ -76,43 +78,51 @@ public void setAttribute(String attribute, TemplateGroup tg)
{
if(tg != null)
{
Set<Entry<String, ST>> set = m_templates.entrySet();
Iterator<Entry<String, ST>> it = set.iterator();
Set<Entry<String, TemplateST>> set = m_templates.entrySet();
Iterator<Entry<String, TemplateST>> it = set.iterator();

while(it.hasNext())
{
Map.Entry<String, ST> m = it.next();
Map.Entry<String, TemplateST> m = it.next();

// Call setAttribute
ST template = tg.getTemplate(m.getKey());
TemplateST template = tg.getTemplate(m.getKey());

if(template != null)
{
Log.printDebug("setting attribute (TemplateGroup) to template group " + m.getKey() + " from " + template.getName() + " to " + m.getValue().getName());
// Before render, set the current TemplateSTGroup in TemplateManager.
manager_.set_current_template_stgroup(m.getValue().get_template_stgroup());

Log.printDebug("setting attribute (TemplateGroup) to template group " + m.getKey() + " from " +
template.get_st().getName() + " to " + m.getValue().get_st().getName());
StringWriter out = new StringWriter();
STWriter wr = new AutoIndentWriter(out);
template.write(wr, error_listener_);
m.getValue().add(attribute, out.toString());
template.get_st().write(wr, error_listener_);
m.getValue().get_st().add(attribute, out.toString());

// Unset the current TemplateSTGroup in TemplateManager.
manager_.set_current_template_stgroup(null);
}
}
}
}

public void setAttribute(String attribute, Object obj1)
{
Set<Entry<String, ST>> set = m_templates.entrySet();
Iterator<Entry<String, ST>> it = set.iterator();
Set<Entry<String, TemplateST>> set = m_templates.entrySet();
Iterator<Entry<String, TemplateST>> it = set.iterator();

while(it.hasNext())
{
Map.Entry<String, ST> m = it.next();
Map.Entry<String, TemplateST> m = it.next();

// Call setAttribute
Log.printDebug("setting attribute (obj1) to template group " + m.getKey() + " to " + m.getValue().getName());
ST template = m.getValue();
template.add(attribute, obj1);
Log.printDebug("setting attribute (obj1) to template group " + m.getKey() + " to " +
m.getValue().get_st().getName());
TemplateST template = m.getValue();
template.get_st().add(attribute, obj1);
// Update extensions
List<ST> extensions = m_extensionstemplates.get(m.getKey() + "_" + template.getName());
List<ST> extensions = m_extensionstemplates.get(m.getKey() + "_" + template.get_st().getName());
if(extensions != null)
{
for(ST extension : extensions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,38 @@

public class TemplateManager
{
private Map<String, STGroup> m_groups = new HashMap<String, STGroup>();
private Map<String, TemplateSTGroup> m_groups = new HashMap<String, TemplateSTGroup>();

private boolean st_error_ = false;

public void addGroup(String groupname)
private TemplateSTGroup current_template_stgroup_ = null;

public TemplateSTGroup addGroup(String groupname)
{
STGroup group = new STGroupFile(groupname, '$', '$');
TemplateSTGroup group = new TemplateSTGroup(groupname);
m_groups.put(groupname, group);
return group;
}

public void addGroupFromString(String groupname, String text)
public TemplateSTGroup addGroupFromString(String groupname, String text)
{
STGroup group = new STGroupString(groupname, text, '$', '$');
TemplateSTGroup group = new TemplateSTGroup(groupname, text);
m_groups.put(groupname, group);
return group;
}

public TemplateGroup createTemplateGroup(String templatename)
{
TemplateGroup tg = new TemplateGroup(this);
Set<Entry<String, STGroup>> set = m_groups.entrySet();
Iterator<Entry<String, STGroup>> it = set.iterator();
Set<Entry<String, TemplateSTGroup>> set = m_groups.entrySet();
Iterator<Entry<String, TemplateSTGroup>> it = set.iterator();

while(it.hasNext())
{
Map.Entry<String, STGroup> m = it.next();
Map.Entry<String, TemplateSTGroup> m = it.next();

// Obtain instance
ST template = m.getValue().getInstanceOf(templatename);
TemplateST template = new TemplateST(m.getValue(), templatename);
tg.addTemplate(m.getKey(), template);
}

Expand All @@ -78,4 +82,14 @@ public boolean get_st_error()
{
return st_error_;
}

public void set_current_template_stgroup(TemplateSTGroup template_stgroup)
{
current_template_stgroup_ = template_stgroup;
}

public TemplateSTGroup get_current_template_stgroup()
{
return current_template_stgroup_;
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/eprosima/idl/generator/manager/TemplateST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.eprosima.idl.generator.manager;

import org.stringtemplate.v4.*;

public class TemplateST
{
public TemplateST(TemplateSTGroup parent_group, String template_name)
{
parent_group_ = parent_group;
st_ = parent_group_.get_stgroup().getInstanceOf(template_name);
}

public ST get_st()
{
return st_;
}

public TemplateSTGroup get_template_stgroup()
{
return parent_group_;
}

private ST st_ = null;
private TemplateSTGroup parent_group_ = null;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.eprosima.idl.generator.manager;

import org.stringtemplate.v4.*;

public class TemplateSTGroup
{
public TemplateSTGroup(String groupname)
{
st_group_ = new STGroupFile(groupname, '$', '$');
}

public TemplateSTGroup(String groupname, String text)
{
st_group_ = new STGroupString(groupname, text, '$', '$');
}

public STGroup get_stgroup()
{
return st_group_;
}

/*!
* @brief Returns if the current `Context` scope will be remove from the scoped name of types (returned by
* `getScopedname()`).
*/
public boolean is_enabled_using_explicitly_modules()
{
return using_explicitly_modules_;
}

/*!
* @brief Enable removing the current `Context` scope from the scoped name of types (returned by `getScopedname()`).
*/
public void enable_using_explicitly_modules()
{
using_explicitly_modules_ = true;
}

private STGroup st_group_ = null;

//! If set, the scoped name of types (returned by `getScopedname()`) will remove the current `Context` scope.
private boolean using_explicitly_modules_ = false;
};
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,29 @@ public String getName()

public String getScopedname()
{
if (m_scope.isEmpty())
String scoped_name = getFullScopedname();

if (!ctx.get_template_manager().get_current_template_stgroup().is_enabled_using_explicitly_modules())
{
return scoped_name;
}

String current_scope = ctx.getScope();

if(current_scope.isEmpty() || !scoped_name.startsWith(current_scope + "::"))
{
return scoped_name;
}

return scoped_name.replace(current_scope + "::", "");
}

/*!
* @brief Return the scoped name of the type.
*/
public String getFullScopedname()
{
if(m_scope.isEmpty())
{
return m_name;
}
Expand Down
Loading

0 comments on commit 2a59bd2

Please sign in to comment.