001
014
015 package com.liferay.portlet.journal.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018 import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
019 import com.liferay.portal.kernel.templateparser.TemplateContext;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.util.PropsValues;
022 import com.liferay.util.ContentUtil;
023
024 import java.util.Locale;
025
026 import javax.xml.transform.Transformer;
027 import javax.xml.transform.TransformerFactory;
028 import javax.xml.transform.stream.StreamResult;
029 import javax.xml.transform.stream.StreamSource;
030
031
035 public class XSLTemplateParser extends VelocityTemplateParser {
036
037 @Override
038 protected String getErrorTemplateContent() {
039 return ContentUtil.get(PropsValues.JOURNAL_ERROR_TEMPLATE_XSL);
040 }
041
042 @Override
043 protected TemplateContext getTemplateContext() throws Exception {
044 return _getTemplateContext(getScript());
045 }
046
047 @Override
048 protected boolean mergeTemplate(
049 TemplateContext templateContext,
050 UnsyncStringWriter unsyncStringWriter)
051 throws Exception {
052
053 StreamSource xmlSource = new StreamSource(
054 new UnsyncStringReader(getXML()));
055
056 XSLContext xslContext = (XSLContext)templateContext;
057
058 Transformer transformer = xslContext.getTransformer();
059
060 try {
061 transformer.transform(
062 xmlSource, new StreamResult(unsyncStringWriter));
063 }
064 catch (Exception e) {
065 String errorTemplateContent = getErrorTemplateContent();
066
067 XSLContext errorXSLContext = (XSLContext)_getTemplateContext(
068 errorTemplateContent);
069
070 populateTemplateContext(errorXSLContext);
071
072 Transformer errorTransformer = errorXSLContext.getTransformer();
073
074 XSLErrorListener xslErrorListener = _getXSLErrorListener();
075
076 errorTransformer.setParameter(
077 "exception", xslErrorListener.getMessageAndLocation());
078 errorTransformer.setParameter("script", getScript());
079
080 if (xslErrorListener.getLocation() != null) {
081 errorTransformer.setParameter(
082 "column", new Integer(xslErrorListener.getColumnNumber()));
083 errorTransformer.setParameter(
084 "line", new Integer(xslErrorListener.getLineNumber()));
085 }
086
087 unsyncStringWriter.reset();
088
089 errorTransformer.transform(
090 xmlSource, new StreamResult(unsyncStringWriter));
091 }
092
093 return true;
094 }
095
096 private TemplateContext _getTemplateContext(String script)
097 throws Exception {
098
099 TransformerFactory transformerFactory = _getTransformerFactory();
100
101 StreamSource scriptSource = new StreamSource(
102 new UnsyncStringReader(script));
103
104 return new XSLContext(transformerFactory.newTransformer(scriptSource));
105 }
106
107 private TransformerFactory _getTransformerFactory() {
108 if (_transformerFactory == null) {
109 _transformerFactory = TransformerFactory.newInstance();
110
111 _transformerFactory.setErrorListener(_getXSLErrorListener());
112 _transformerFactory.setURIResolver(
113 new URIResolver(getTokens(), getLanguageId()));
114 }
115
116 return _transformerFactory;
117 }
118
119 private XSLErrorListener _getXSLErrorListener() {
120 if (_xslErrorListener == null) {
121 Locale locale = LocaleUtil.fromLanguageId(getLanguageId());
122
123 _xslErrorListener = new XSLErrorListener(locale);
124 }
125
126 return _xslErrorListener;
127 }
128
129 private TransformerFactory _transformerFactory;
130 private XSLErrorListener _xslErrorListener;
131
132 }