| 1 |
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package com.eprosima.idl.generator.manager; |
| 16 |
|
| 17 |
import java.io.StringWriter; |
| 18 |
import java.util.Iterator; |
| 19 |
import java.util.Map; |
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.Set; |
| 22 |
import java.util.Map.Entry; |
| 23 |
import java.util.List; |
| 24 |
import java.util.ArrayList; |
| 25 |
|
| 26 |
import org.stringtemplate.v4.AutoIndentWriter; |
| 27 |
import org.stringtemplate.v4.ST; |
| 28 |
import org.stringtemplate.v4.STWriter; |
| 29 |
|
| 30 |
import com.eprosima.log.Log; |
| 31 |
|
| 32 |
public class TemplateGroup |
| 33 |
{ |
| 34 |
private Map<String, TemplateST> m_templates = new HashMap<String, TemplateST>(); |
| 35 |
private Map<String, List<ST>> m_extensionstemplates = new HashMap<String, List<ST>>(); |
| 36 |
private TemplateErrorListener error_listener_ = null; |
| 37 |
private TemplateManager manager_ = null; |
| 38 |
|
| 39 |
public TemplateGroup(TemplateManager manager) |
| 40 |
{ |
| 41 |
manager_ = manager; |
| 42 |
error_listener_ = new TemplateErrorListener(manager); |
| 43 |
} |
| 44 |
|
| 45 |
public void addTemplate(String groupname, TemplateST template) |
| 46 |
{ |
| 47 |
m_templates.put(groupname, template); |
| 48 |
} |
| 49 |
|
| 50 |
public void addTemplate(String groupname, TemplateST template, List<ST> extensionstemplates) |
| 51 |
{ |
| 52 |
addTemplate(groupname, template); |
| 53 |
m_extensionstemplates.put(groupname + "_" + template.get_st().getName(), extensionstemplates); |
| 54 |
} |
| 55 |
|
| 56 |
public TemplateST getTemplate(String groupname) |
| 57 |
{ |
| 58 |
TemplateST template = m_templates.get(groupname); |
| 59 |
|
| 60 |
//If there is extensiones, add them before return the template. |
| 61 |
if(m_extensionstemplates.containsKey(groupname + "_" + template.get_st().getName())) |
| 62 |
{ |
| 63 |
List<ST> extemplates = new ArrayList<ST>(); |
| 64 |
List<ST> extensions = m_extensionstemplates.get(groupname + "_" + template.get_st().getName()); |
| 65 |
|
| 66 |
for(ST extension : extensions) |
| 67 |
{ |
| 68 |
extemplates.add(extension); |
| 69 |
} |
| 70 |
|
| 71 |
template.get_st().add("extensions", extemplates); |
| 72 |
} |
| 73 |
|
| 74 |
return template; |
| 75 |
} |
| 76 |
|
| 77 |
public void setAttribute(String attribute, TemplateGroup tg) |
| 78 |
{ |
| 79 |
if(tg != null) |
| 80 |
{ |
| 81 |
Set<Entry<String, TemplateST>> set = m_templates.entrySet(); |
| 82 |
Iterator<Entry<String, TemplateST>> it = set.iterator(); |
| 83 |
|
| 84 |
while(it.hasNext()) |
| 85 |
{ |
| 86 |
Map.Entry<String, TemplateST> m = it.next(); |
| 87 |
|
| 88 |
// Call setAttribute |
| 89 |
TemplateST template = tg.getTemplate(m.getKey()); |
| 90 |
|
| 91 |
if(template != null) |
| 92 |
{ |
| 93 |
// Before render, set the current TemplateSTGroup in TemplateManager. |
| 94 |
manager_.set_current_template_stgroup(m.getValue().get_template_stgroup()); |
| 95 |
|
| 96 |
Log.printDebug("setting attribute (TemplateGroup) to template group " + m.getKey() + " from " + |
| 97 |
template.get_st().getName() + " to " + m.getValue().get_st().getName()); |
| 98 |
StringWriter out = new StringWriter(); |
| 99 |
STWriter wr = new AutoIndentWriter(out); |
| 100 |
template.get_st().write(wr, error_listener_); |
| 101 |
String out_string = out.toString(); |
| 102 |
|
| 103 |
if (!out_string.isBlank()) |
| 104 |
{ |
| 105 |
m.getValue().get_st().add(attribute, out.toString()); |
| 106 |
} |
| 107 |
|
| 108 |
// Unset the current TemplateSTGroup in TemplateManager. |
| 109 |
manager_.set_current_template_stgroup(null); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public void setAttribute(String attribute, Object obj1) |
| 116 |
{ |
| 117 |
Set<Entry<String, TemplateST>> set = m_templates.entrySet(); |
| 118 |
Iterator<Entry<String, TemplateST>> it = set.iterator(); |
| 119 |
|
| 120 |
while(it.hasNext()) |
| 121 |
{ |
| 122 |
Map.Entry<String, TemplateST> m = it.next(); |
| 123 |
|
| 124 |
// Call setAttribute |
| 125 |
Log.printDebug("setting attribute (obj1) to template group " + m.getKey() + " to " + |
| 126 |
m.getValue().get_st().getName()); |
| 127 |
TemplateST template = m.getValue(); |
| 128 |
template.get_st().add(attribute, obj1); |
| 129 |
// Update extensions |
| 130 |
List<ST> extensions = m_extensionstemplates.get(m.getKey() + "_" + template.get_st().getName()); |
| 131 |
if(extensions != null) |
| 132 |
{ |
| 133 |
for(ST extension : extensions) |
| 134 |
{ |
| 135 |
extension.add(attribute, obj1); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |