001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.templateparser;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.DocumentException;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.model.Company;
027    import com.liferay.portal.security.permission.PermissionThreadLocal;
028    import com.liferay.portal.service.CompanyLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    
031    import java.io.IOException;
032    
033    import java.util.ArrayList;
034    import java.util.HashMap;
035    import java.util.List;
036    import java.util.Locale;
037    import java.util.Map;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Marcellus Tavares
042     */
043    public abstract class BaseTemplateParser implements TemplateParser {
044    
045            public String getLanguageId() {
046                    return _languageId;
047            }
048    
049            public String getScript() {
050                    return _script;
051            }
052    
053            public ThemeDisplay getThemeDisplay() {
054                    return _themeDisplay;
055            }
056    
057            public Map<String, String> getTokens() {
058                    return _tokens;
059            }
060    
061            public String getViewMode() {
062                    return _viewMode;
063            }
064    
065            public String getXML() {
066                    return _xml;
067            }
068    
069            public void setLanguageId(String languageId) {
070                    _languageId = languageId;
071            }
072    
073            public void setScript(String script) {
074                    _script = script;
075            }
076    
077            public void setThemeDisplay(ThemeDisplay themeDisplay) {
078                    _themeDisplay = themeDisplay;
079            }
080    
081            public void setTokens(Map<String, String> tokens) {
082                    _tokens = tokens;
083            }
084    
085            public void setViewMode(String viewMode) {
086                    _viewMode = viewMode;
087            }
088    
089            public void setXML(String xml) {
090                    _xml = xml;
091            }
092    
093            public String transform() throws TransformException {
094                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
095    
096                    boolean load = false;
097    
098                    try {
099                            TemplateContext templateContext = getTemplateContext();
100    
101                            Document document = SAXReaderUtil.read(_xml);
102    
103                            Element rootElement = document.getRootElement();
104    
105                            List<TemplateNode> templateNodes = getTemplateNodes(rootElement);
106    
107                            if (templateNodes != null) {
108                                    for (TemplateNode templateNode : templateNodes) {
109                                            templateContext.put(templateNode.getName(), templateNode);
110                                    }
111                            }
112    
113                            Element requestElement = rootElement.element("request");
114    
115                            templateContext.put(
116                                    "request", insertRequestVariables(requestElement));
117    
118                            templateContext.put("xmlRequest", requestElement.asXML());
119    
120                            populateTemplateContext(templateContext);
121    
122                            load = mergeTemplate(templateContext, unsyncStringWriter);
123                    }
124                    catch (Exception e) {
125                            if (e instanceof DocumentException) {
126                                    throw new TransformException("Unable to read XML document", e);
127                            }
128                            else if (e instanceof IOException) {
129                                    throw new TransformException("Error reading template", e);
130                            }
131                            else if (e instanceof TransformException) {
132                                    throw (TransformException)e;
133                            }
134                            else {
135                                    throw new TransformException("Unhandled exception", e);
136                            }
137                    }
138    
139                    if (!load) {
140                            throw new TransformException(
141                                    "Unable to dynamically load transform script");
142                    }
143    
144                    return unsyncStringWriter.toString();
145            }
146    
147            protected Company getCompany() throws Exception {
148                    long companyId = getCompanyId();
149    
150                    return CompanyLocalServiceUtil.getCompany(companyId);
151            }
152    
153            protected long getCompanyId() {
154                    return GetterUtil.getLong(_tokens.get("company_id"));
155            }
156    
157            protected long getGroupId() {
158                    return GetterUtil.getLong(_tokens.get("group_id"));
159            }
160    
161            protected abstract TemplateContext getTemplateContext() throws Exception;
162    
163            protected String getTemplateId() {
164                    long companyGroupId = GetterUtil.getLong(
165                            _tokens.get("company_group_id"));
166                    String templateId = _tokens.get("template_id");
167    
168                    StringBundler sb = new StringBundler(5);
169    
170                    sb.append(getCompanyId());
171                    sb.append(StringPool.POUND);
172    
173                    if (companyGroupId > 0) {
174                            sb.append(companyGroupId);
175                    }
176                    else {
177                            sb.append(getGroupId());
178                    }
179    
180                    sb.append(StringPool.POUND);
181                    sb.append(templateId);
182    
183                    return sb.toString();
184            }
185    
186            protected abstract List<TemplateNode> getTemplateNodes(Element element)
187                    throws Exception;
188    
189            protected Map<String, Object> insertRequestVariables(Element element) {
190                    Map<String, Object> map = new HashMap<String, Object>();
191    
192                    if (element == null) {
193                            return map;
194                    }
195    
196                    for (Element childElement : element.elements()) {
197                            String name = childElement.getName();
198    
199                            if (name.equals("attribute")) {
200                                    Element nameElement = childElement.element("name");
201                                    Element valueElement = childElement.element("value");
202    
203                                    map.put(nameElement.getText(), valueElement.getText());
204                            }
205                            else if (name.equals("parameter")) {
206                                    Element nameElement = childElement.element("name");
207    
208                                    List<Element> valueElements = childElement.elements("value");
209    
210                                    if (valueElements.size() == 1) {
211                                            Element valueElement = valueElements.get(0);
212    
213                                            map.put(nameElement.getText(), valueElement.getText());
214                                    }
215                                    else {
216                                            List<String> values = new ArrayList<String>();
217    
218                                            for (Element valueElement : valueElements) {
219                                                    values.add(valueElement.getText());
220                                            }
221    
222                                            map.put(nameElement.getText(), values);
223                                    }
224                            }
225                            else if (childElement.elements().size() > 0) {
226                                    map.put(name, insertRequestVariables(childElement));
227                            }
228                            else {
229                                    map.put(name, childElement.getText());
230                            }
231                    }
232    
233                    return map;
234            }
235    
236            protected abstract boolean mergeTemplate(
237                            TemplateContext templateContext,
238                            UnsyncStringWriter unsyncStringWriter)
239                    throws Exception;
240    
241            protected void populateTemplateContext(TemplateContext templateContext)
242                    throws Exception {
243    
244                    templateContext.put("company", getCompany());
245                    templateContext.put("companyId", getCompanyId());
246                    templateContext.put("device", _themeDisplay.getDevice());
247                    templateContext.put("groupId", getGroupId());
248    
249                    Locale locale = LocaleUtil.fromLanguageId(_languageId);
250    
251                    templateContext.put("locale", locale);
252    
253                    templateContext.put(
254                            "permissionChecker", PermissionThreadLocal.getPermissionChecker());
255                    templateContext.put("viewMode", _viewMode);
256            }
257    
258            private String _languageId;
259            private String _script;
260            private ThemeDisplay _themeDisplay;
261            private Map<String, String> _tokens;
262            private String _viewMode;
263            private String _xml;
264    
265    }