protected void runScript( PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { String language = ParamUtil.getString(actionRequest, "language"); String script = ParamUtil.getString(actionRequest, "script"); PortletContext portletContext = portletConfig.getPortletContext(); Map<String, Object> portletObjects = ScriptingUtil.getPortletObjects( portletConfig, portletContext, actionRequest, actionResponse); UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); UnsyncPrintWriter unsyncPrintWriter = UnsyncPrintWriterPool.borrow(unsyncByteArrayOutputStream); portletObjects.put("out", unsyncPrintWriter); try { SessionMessages.add(actionRequest, "language", language); SessionMessages.add(actionRequest, "script", script); ScriptingUtil.exec(null, portletObjects, language, script); unsyncPrintWriter.flush(); SessionMessages.add(actionRequest, "scriptOutput", unsyncByteArrayOutputStream.toString()); } catch (ScriptingException se) { SessionErrors.add(actionRequest, ScriptingException.class.getName(), se); _log.error(se.getMessage()); } }
public static String list(Properties properties) { UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); PrintStream printStream = new PrintStream(unsyncByteArrayOutputStream); properties.list(printStream); return unsyncByteArrayOutputStream.toString(); }
@Override public String compactString() throws IOException { UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); OutputFormat outputFormat = OutputFormat.createCompactFormat(); XMLWriter xmlWriter = new XMLWriter(unsyncByteArrayOutputStream, outputFormat); xmlWriter.write(_node); return unsyncByteArrayOutputStream.toString(StringPool.UTF8); }
public static String transform(URL xmlURL, URL xslURL) throws IOException, TransformerException { String xml = HttpUtil.URLtoString(xmlURL); String xsl = HttpUtil.URLtoString(xslURL); StreamSource xmlSource = new StreamSource(new UnsyncStringReader(xml)); StreamSource xslSource = new StreamSource(new UnsyncStringReader(xsl)); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(xslSource); UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); transformer.transform(xmlSource, new StreamResult(unsyncByteArrayOutputStream)); return unsyncByteArrayOutputStream.toString(); }
protected String doTransform( ThemeDisplay themeDisplay, Map<String, String> tokens, String viewMode, String languageId, String xml, String script) throws Exception { UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); long companyId = GetterUtil.getLong(tokens.get("company_id")); Company company = CompanyLocalServiceUtil.getCompanyById(companyId); long groupId = GetterUtil.getLong(tokens.get("group_id")); String journalTemplatesPath = VelocityResourceListener.JOURNAL_SEPARATOR + StringPool.SLASH + companyId + StringPool.SLASH + groupId; String randomNamespace = PwdGenerator.getPassword(PwdGenerator.KEY3, 4) + StringPool.UNDERLINE; Locale locale = LocaleUtil.fromLanguageId(languageId); XSLErrorListener xslErrorListener = new XSLErrorListener(locale); StreamSource xmlSource = new StreamSource(new UnsyncStringReader(xml)); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setURIResolver(new URIResolver(tokens, languageId)); transformerFactory.setErrorListener(xslErrorListener); try { StreamSource scriptSource = new StreamSource(new UnsyncStringReader(script)); Transformer transformer = transformerFactory.newTransformer(scriptSource); transformer.setParameter("company", company); transformer.setParameter("companyId", new Long(companyId)); transformer.setParameter("groupId", String.valueOf(groupId)); transformer.setParameter("journalTemplatesPath", journalTemplatesPath); transformer.setParameter("viewMode", viewMode); transformer.setParameter("locale", locale); transformer.setParameter("permissionChecker", PermissionThreadLocal.getPermissionChecker()); transformer.setParameter("randomNamespace", randomNamespace); transformer.transform(xmlSource, new StreamResult(unsyncByteArrayOutputStream)); } catch (Exception e1) { String errorTemplate = ContentUtil.get(PropsValues.JOURNAL_ERROR_TEMPLATE_XSL); StreamSource scriptSource = new StreamSource(new UnsyncStringReader(errorTemplate)); Transformer transformer = transformerFactory.newTransformer(scriptSource); transformer.setParameter("company", company); transformer.setParameter("companyId", new Long(companyId)); transformer.setParameter("groupId", String.valueOf(groupId)); transformer.setParameter("journalTemplatesPath", journalTemplatesPath); transformer.setParameter("locale", locale); transformer.setParameter("randomNamespace", randomNamespace); transformer.setParameter("exception", xslErrorListener.getMessageAndLocation()); transformer.setParameter("script", script); if (xslErrorListener.getLocation() != null) { transformer.setParameter("column", new Integer(xslErrorListener.getColumnNumber())); transformer.setParameter("line", new Integer(xslErrorListener.getLineNumber())); } transformer.transform(xmlSource, new StreamResult(unsyncByteArrayOutputStream)); } return unsyncByteArrayOutputStream.toString(StringPool.UTF8); }