From bd279bfe39c5ed71f9581d005b0134e7e3b517c5 Mon Sep 17 00:00:00 2001 From: Marc Englund Date: Fri, 2 May 2008 08:42:58 +0000 Subject: [PATCH] PortletConfigurationGenerator moved -> buildhelpers svn changeset:4312/svn branch:trunk --- .../PortletConfigurationGenerator.java | 297 ++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 build/buildhelpers/com/itmill/toolkit/buildhelpers/PortletConfigurationGenerator.java diff --git a/build/buildhelpers/com/itmill/toolkit/buildhelpers/PortletConfigurationGenerator.java b/build/buildhelpers/com/itmill/toolkit/buildhelpers/PortletConfigurationGenerator.java new file mode 100644 index 0000000000..01a1c47798 --- /dev/null +++ b/build/buildhelpers/com/itmill/toolkit/buildhelpers/PortletConfigurationGenerator.java @@ -0,0 +1,297 @@ +/** + * + */ +package com.itmill.toolkit.buildhelpers; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Generates portlet.xml, liferay-portlet.xml, liferay-display.xml from web.xml. + * Currently uses regular expressions to avoid dependencies; does not strictly + * adhere to xml rules, but should work with a 'normal' web.xml. + * + * To be included, the servlet-mapping must include a special comment: If the portlet requires some special styles (i.e height): + * + * @author marc + */ +public class PortletConfigurationGenerator { + // can be changed for debugging: + private static final String WEB_XML_FILE = "web.xml"; + private static final String PORTLET_XML_FILE = "portlet.xml"; + private static final String LIFERAY_PORTLET_XML_FILE = "liferay-portlet.xml"; + private static final String LIFERAY_DISPLAY_XML_FILE = "liferay-display.xml"; + + // "templates" follow; + private static final String PORTLET_XML_HEAD = "\n" + + "\n"; + private static final String PORTLET_XML_SECTION = " \n" + + " %PORTLETNAME%\n" + + " IT Mill Toolkit %NAME%\n" + + " com.itmill.toolkit.terminal.gwt.server.ApplicationPortlet\n" + + " \n" + + " application\n" + + " %URL%\n" + + " \n" + + " %EXTRAPARAMS%\n" + + " \n" + + " text/html\n" + + " view\n" + + " edit\n" + + " help\n" + + " \n" + + " \n" + + " %NAME%\n" + + " %NAME%\n" + + " \n" + " \n" + + " \n" + + " administrator\n" + + " \n" + + " \n" + + " guest\n" + + " \n" + + " \n" + + " power-user\n" + + " \n" + + " \n" + + " user\n" + + " \n" + " \n"; + private static final String PORTLET_XML_FOOT = " %CONTEXTPARAMS%\n" + + " \n" + + " javax.portlet.escapeXml\n" + + " false\n" + + " \n" + ""; + + private static final String LIFERAY_PORTLET_XML_HEAD = "\n" + + "\n" + + "\n" + "\n" + ""; + private static final String LIFERAY_PORTLET_XML_SECTION = " \n" + + " %PORTLETNAME%\n" + + " true \n" + + " false\n" + + " \n" + ""; + private static final String LIFERAY_PORTLET_XML_FOOT = " \n" + + " \n" + + " administrator\n" + + " Administrator\n" + + " \n" + " \n" + + " guest\n" + + " Guest\n" + + " \n" + " \n" + + " power-user\n" + + " Power User\n" + + " \n" + " \n" + + " user\n" + + " User\n" + + " \n" + " \n" + + ""; + private static final String LIFERAY_DISPLAY_XML_HEAD = "\n" + + "\n" + + "\n" + + "\n" + + " \n" + ""; + private static final String LIFERAY_DISPLAY_XML_SECTION = " \n"; + private static final String LIFERAY_DISPLAY_XML_FOOT = "\n" + + " \n" + ""; + + /** + * @param args + * [widgetset to use] + */ + public static void main(String[] args) { + if (args.length < 1 || !new File(args[0]).isDirectory()) { + System.err + .println("Usage: PortletConfigurationGenerator [widgetset]"); + return; + } + + String widgetset = ""; + if (args.length > 1) { + widgetset = "widgetset" + + args[1] + ""; + } + + /* + * Read web.xml + */ + File dir = new File(args[0]); + File webxmlFile = new File(dir.getAbsolutePath() + File.separatorChar + + WEB_XML_FILE); + String webXml = ""; + BufferedReader in = null; + try { + in = new BufferedReader(new FileReader(webxmlFile)); + String line = in.readLine(); + while (line != null) { + webXml += line; + line = in.readLine(); + } + } catch (FileNotFoundException e1) { + System.out.println(webxmlFile + " not found!"); + return; + } catch (IOException e2) { + System.out.println("IOException while reading " + webxmlFile); + webXml = null; + } + try { + if (in != null) { + in.close(); + } + } catch (IOException e1) { + System.out.println("IOException while closing " + webxmlFile); + } + if (webXml == null) { + System.out.println("Could not read web.xml!"); + return; + } + + /* + * Open outputs + */ + + // Open portlet.xml + File portletXmlFile = new File(args[0] + File.separatorChar + + PORTLET_XML_FILE); + OutputStreamWriter pout = null; + try { + pout = new OutputStreamWriter(new FileOutputStream(portletXmlFile), + Charset.forName("UTF-8")); + } catch (FileNotFoundException e) { + System.out.println(portletXmlFile + " not found!"); + } + // open liferay-portlet.xml + File liferayPortletXmlFile = new File(args[0] + File.separatorChar + + LIFERAY_PORTLET_XML_FILE); + OutputStreamWriter lpout = null; + try { + lpout = new OutputStreamWriter(new FileOutputStream( + liferayPortletXmlFile), Charset.forName("UTF-8")); + } catch (FileNotFoundException e) { + System.out.println(liferayPortletXmlFile + " not found!"); + } + // open liferay-display.xml + File liferayDisplayXmlFile = new File(args[0] + File.separatorChar + + LIFERAY_DISPLAY_XML_FILE); + OutputStreamWriter ldout = null; + try { + ldout = new OutputStreamWriter(new FileOutputStream( + liferayDisplayXmlFile), Charset.forName("UTF-8")); + } catch (FileNotFoundException e) { + System.out.println(liferayDisplayXmlFile + " not found!"); + } + + if (pout != null && lpout != null && ldout != null) { + + String pstring = PORTLET_XML_HEAD; + String lpstring = LIFERAY_PORTLET_XML_HEAD; + String ldstring = LIFERAY_DISPLAY_XML_HEAD; + + Pattern p1 = Pattern + .compile(".*?(.*?)<\\/servlet-name>.*?(.*?)<\\/url-pattern>(.*?)<\\/servlet-mapping>"); + Pattern p2 = Pattern + .compile(".*?.*?"); + Matcher m = p1.matcher(webXml); + while (m.find()) { + if (m.groupCount() < 3) { + // don't include + continue; + } + Matcher m2 = p2.matcher(m.group(3)); + if (!m2.find()) { + // don't include + continue; + } + + String style = ""; + if (m2.groupCount() == 1 && m2.group(1) != null) { + style = "style" + + m2.group(1) + ""; + } + + String name = m.group(1); + // remove leading- and trailing whitespace + name = name.replaceAll("^\\s*", ""); + name = name.replaceAll("\\s*$", ""); + String pname = name + "Portlet"; + String url = m.group(2); + // remove leading- and trailing whitespace + url = url.replaceAll("^\\s*", ""); + url = url.replaceAll("\\s*$", ""); + if (url.startsWith("/")) { + url = url.substring(1); + } + if (url.endsWith("*")) { + url = url.substring(0, url.length() - 1); + } + if (url.endsWith("/")) { + url = url.substring(0, url.length() - 1); + } + + System.out.println("Mapping " + pname + " to " + url); + + String s = PORTLET_XML_SECTION; + s = s.replaceAll("%NAME%", name); + s = s.replaceAll("%PORTLETNAME%", pname); + s = s.replaceAll("%URL%", url); + s = s.replaceAll("%EXTRAPARAMS%", style); + + pstring += s; + + s = LIFERAY_PORTLET_XML_SECTION; + s = s.replaceAll("%NAME%", name); + s = s.replaceAll("%PORTLETNAME%", pname); + s = s.replaceAll("%URL%", url); + lpstring += s; + + s = LIFERAY_DISPLAY_XML_SECTION; + s = s.replaceAll("%NAME%", name); + s = s.replaceAll("%PORTLETNAME%", pname); + s = s.replaceAll("%URL%", url); + ldstring += s; + + } + + pstring += PORTLET_XML_FOOT + .replaceAll("%CONTEXTPARAMS%", widgetset); + lpstring += LIFERAY_PORTLET_XML_FOOT; + ldstring += LIFERAY_DISPLAY_XML_FOOT; + + try { + pout.write(pstring); + lpout.write(lpstring); + ldout.write(ldstring); + } catch (IOException e) { + System.out.println("Write FAILED:" + e); + } + + } + + try { + if (pout != null) { + pout.close(); + } + if (lpout != null) { + lpout.close(); + } + if (ldout != null) { + ldout.close(); + } + } catch (IOException e) { + System.out.println("Close FAILED: " + e); + } + System.out.println("Done."); + } +} -- 2.39.5