--- /dev/null
+/**\r
+ * \r
+ */\r
+package com.itmill.toolkit.buildhelpers;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.FileReader;\r
+import java.io.IOException;\r
+import java.io.OutputStreamWriter;\r
+import java.nio.charset.Charset;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
+\r
+/**\r
+ * Generates portlet.xml, liferay-portlet.xml, liferay-display.xml from web.xml.\r
+ * Currently uses regular expressions to avoid dependencies; does not strictly\r
+ * adhere to xml rules, but should work with a 'normal' web.xml.\r
+ * \r
+ * To be included, the servlet-mapping must include a special comment: <!--\r
+ * portlet --> If the portlet requires some special styles (i.e height): <!--\r
+ * portlet style=height:400px -->\r
+ * \r
+ * @author marc\r
+ */\r
+public class PortletConfigurationGenerator {\r
+ // can be changed for debugging:\r
+ private static final String WEB_XML_FILE = "web.xml";\r
+ private static final String PORTLET_XML_FILE = "portlet.xml";\r
+ private static final String LIFERAY_PORTLET_XML_FILE = "liferay-portlet.xml";\r
+ private static final String LIFERAY_DISPLAY_XML_FILE = "liferay-display.xml";\r
+\r
+ // "templates" follow;\r
+ private static final String PORTLET_XML_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"\r
+ + "<portlet-app\n"\r
+ + " xmlns=\"http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd\"\n"\r
+ + " version=\"1.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"\r
+ + " xsi:schemaLocation=\"http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd\">\n";\r
+ private static final String PORTLET_XML_SECTION = " <portlet>\n"\r
+ + " <portlet-name>%PORTLETNAME%</portlet-name>\n"\r
+ + " <display-name>IT Mill Toolkit %NAME%</display-name>\n"\r
+ + " <portlet-class>com.itmill.toolkit.terminal.gwt.server.ApplicationPortlet</portlet-class>\n"\r
+ + " <init-param>\n"\r
+ + " <name>application</name>\n"\r
+ + " <value>%URL%</value>\n"\r
+ + " </init-param>\n"\r
+ + " %EXTRAPARAMS%\n"\r
+ + " <supports>\n"\r
+ + " <mime-type>text/html</mime-type>\n"\r
+ + " <portlet-mode>view</portlet-mode>\n"\r
+ + " <portlet-mode>edit</portlet-mode>\n"\r
+ + " <portlet-mode>help</portlet-mode>\n"\r
+ + " </supports>\n"\r
+ + " <portlet-info>\n"\r
+ + " <title>%NAME%</title>\n"\r
+ + " <short-title>%NAME%</short-title>\n"\r
+ + " </portlet-info>\n" + " \n"\r
+ + " <security-role-ref>\n"\r
+ + " <role-name>administrator</role-name>\n"\r
+ + " </security-role-ref>\n"\r
+ + " <security-role-ref>\n"\r
+ + " <role-name>guest</role-name>\n"\r
+ + " </security-role-ref>\n"\r
+ + " <security-role-ref>\n"\r
+ + " <role-name>power-user</role-name>\n"\r
+ + " </security-role-ref>\n"\r
+ + " <security-role-ref>\n"\r
+ + " <role-name>user</role-name>\n"\r
+ + " </security-role-ref>\n" + " </portlet>\n";\r
+ private static final String PORTLET_XML_FOOT = " %CONTEXTPARAMS%\n"\r
+ + " <container-runtime-option>\n"\r
+ + " <name>javax.portlet.escapeXml</name>\n"\r
+ + " <value>false</value>\n"\r
+ + " </container-runtime-option>\n" + "</portlet-app>";\r
+\r
+ private static final String LIFERAY_PORTLET_XML_HEAD = "<?xml version=\"1.0\"?>\n"\r
+ + "<!DOCTYPE liferay-portlet-app PUBLIC \"-//Liferay//DTD Portlet Application 4.3.0//EN\" \"http://www.liferay.com/dtd/liferay-portlet-app_4_3_0.dtd\">\n"\r
+ + "\n" + "<liferay-portlet-app>\n" + "";\r
+ private static final String LIFERAY_PORTLET_XML_SECTION = " <portlet>\n"\r
+ + " <portlet-name>%PORTLETNAME%</portlet-name>\n"\r
+ + " <instanceable>true</instanceable> \n"\r
+ + " <ajaxable>false</ajaxable>\n"\r
+ + " </portlet>\n" + "";\r
+ private static final String LIFERAY_PORTLET_XML_FOOT = " \n"\r
+ + " <role-mapper>\n"\r
+ + " <role-name>administrator</role-name>\n"\r
+ + " <role-link>Administrator</role-link>\n"\r
+ + " </role-mapper>\n" + " <role-mapper>\n"\r
+ + " <role-name>guest</role-name>\n"\r
+ + " <role-link>Guest</role-link>\n"\r
+ + " </role-mapper>\n" + " <role-mapper>\n"\r
+ + " <role-name>power-user</role-name>\n"\r
+ + " <role-link>Power User</role-link>\n"\r
+ + " </role-mapper>\n" + " <role-mapper>\n"\r
+ + " <role-name>user</role-name>\n"\r
+ + " <role-link>User</role-link>\n"\r
+ + " </role-mapper>\n" + " \n"\r
+ + "</liferay-portlet-app>";\r
+ private static final String LIFERAY_DISPLAY_XML_HEAD = "<?xml version=\"1.0\"?>\n"\r
+ + "<!DOCTYPE display PUBLIC \"-//Liferay//DTD Display 4.0.0//EN\" \"http://www.liferay.com/dtd/liferay-display_4_0_0.dtd\">\n"\r
+ + "\n"\r
+ + "<display>\n"\r
+ + " <category name=\"IT Mill Toolkit\">\n" + "";\r
+ private static final String LIFERAY_DISPLAY_XML_SECTION = " <portlet id=\"%PORTLETNAME%\" />\n";\r
+ private static final String LIFERAY_DISPLAY_XML_FOOT = "\n"\r
+ + " </category>\n" + "</display>";\r
+\r
+ /**\r
+ * @param args\r
+ * <path to directory with web.xml> [widgetset to use]\r
+ */\r
+ public static void main(String[] args) {\r
+ if (args.length < 1 || !new File(args[0]).isDirectory()) {\r
+ System.err\r
+ .println("Usage: PortletConfigurationGenerator <directory> [widgetset]");\r
+ return;\r
+ }\r
+\r
+ String widgetset = "";\r
+ if (args.length > 1) {\r
+ widgetset = "<context-param><name>widgetset</name><value>"\r
+ + args[1] + "</value></context-param>";\r
+ }\r
+\r
+ /*\r
+ * Read web.xml\r
+ */\r
+ File dir = new File(args[0]);\r
+ File webxmlFile = new File(dir.getAbsolutePath() + File.separatorChar\r
+ + WEB_XML_FILE);\r
+ String webXml = "";\r
+ BufferedReader in = null;\r
+ try {\r
+ in = new BufferedReader(new FileReader(webxmlFile));\r
+ String line = in.readLine();\r
+ while (line != null) {\r
+ webXml += line;\r
+ line = in.readLine();\r
+ }\r
+ } catch (FileNotFoundException e1) {\r
+ System.out.println(webxmlFile + " not found!");\r
+ return;\r
+ } catch (IOException e2) {\r
+ System.out.println("IOException while reading " + webxmlFile);\r
+ webXml = null;\r
+ }\r
+ try {\r
+ if (in != null) {\r
+ in.close();\r
+ }\r
+ } catch (IOException e1) {\r
+ System.out.println("IOException while closing " + webxmlFile);\r
+ }\r
+ if (webXml == null) {\r
+ System.out.println("Could not read web.xml!");\r
+ return;\r
+ }\r
+\r
+ /*\r
+ * Open outputs\r
+ */\r
+\r
+ // Open portlet.xml\r
+ File portletXmlFile = new File(args[0] + File.separatorChar\r
+ + PORTLET_XML_FILE);\r
+ OutputStreamWriter pout = null;\r
+ try {\r
+ pout = new OutputStreamWriter(new FileOutputStream(portletXmlFile),\r
+ Charset.forName("UTF-8"));\r
+ } catch (FileNotFoundException e) {\r
+ System.out.println(portletXmlFile + " not found!");\r
+ }\r
+ // open liferay-portlet.xml\r
+ File liferayPortletXmlFile = new File(args[0] + File.separatorChar\r
+ + LIFERAY_PORTLET_XML_FILE);\r
+ OutputStreamWriter lpout = null;\r
+ try {\r
+ lpout = new OutputStreamWriter(new FileOutputStream(\r
+ liferayPortletXmlFile), Charset.forName("UTF-8"));\r
+ } catch (FileNotFoundException e) {\r
+ System.out.println(liferayPortletXmlFile + " not found!");\r
+ }\r
+ // open liferay-display.xml\r
+ File liferayDisplayXmlFile = new File(args[0] + File.separatorChar\r
+ + LIFERAY_DISPLAY_XML_FILE);\r
+ OutputStreamWriter ldout = null;\r
+ try {\r
+ ldout = new OutputStreamWriter(new FileOutputStream(\r
+ liferayDisplayXmlFile), Charset.forName("UTF-8"));\r
+ } catch (FileNotFoundException e) {\r
+ System.out.println(liferayDisplayXmlFile + " not found!");\r
+ }\r
+\r
+ if (pout != null && lpout != null && ldout != null) {\r
+\r
+ String pstring = PORTLET_XML_HEAD;\r
+ String lpstring = LIFERAY_PORTLET_XML_HEAD;\r
+ String ldstring = LIFERAY_DISPLAY_XML_HEAD;\r
+\r
+ Pattern p1 = Pattern\r
+ .compile("<servlet-mapping>.*?<servlet-name>(.*?)<\\/servlet-name>.*?<url-pattern>(.*?)<\\/url-pattern>(.*?)<\\/servlet-mapping>");\r
+ Pattern p2 = Pattern\r
+ .compile(".*?<!--\\s+portlet\\s?(style=\\S+)?\\s+-->.*?");\r
+ Matcher m = p1.matcher(webXml);\r
+ while (m.find()) {\r
+ if (m.groupCount() < 3) {\r
+ // don't include\r
+ continue;\r
+ }\r
+ Matcher m2 = p2.matcher(m.group(3));\r
+ if (!m2.find()) {\r
+ // don't include\r
+ continue;\r
+ }\r
+\r
+ String style = "";\r
+ if (m2.groupCount() == 1 && m2.group(1) != null) {\r
+ style = "<init-param><name>style</name><value>"\r
+ + m2.group(1) + "</value></init-param>";\r
+ }\r
+\r
+ String name = m.group(1);\r
+ // remove leading- and trailing whitespace\r
+ name = name.replaceAll("^\\s*", "");\r
+ name = name.replaceAll("\\s*$", "");\r
+ String pname = name + "Portlet";\r
+ String url = m.group(2);\r
+ // remove leading- and trailing whitespace\r
+ url = url.replaceAll("^\\s*", "");\r
+ url = url.replaceAll("\\s*$", "");\r
+ if (url.startsWith("/")) {\r
+ url = url.substring(1);\r
+ }\r
+ if (url.endsWith("*")) {\r
+ url = url.substring(0, url.length() - 1);\r
+ }\r
+ if (url.endsWith("/")) {\r
+ url = url.substring(0, url.length() - 1);\r
+ }\r
+\r
+ System.out.println("Mapping " + pname + " to " + url);\r
+\r
+ String s = PORTLET_XML_SECTION;\r
+ s = s.replaceAll("%NAME%", name);\r
+ s = s.replaceAll("%PORTLETNAME%", pname);\r
+ s = s.replaceAll("%URL%", url);\r
+ s = s.replaceAll("%EXTRAPARAMS%", style);\r
+\r
+ pstring += s;\r
+\r
+ s = LIFERAY_PORTLET_XML_SECTION;\r
+ s = s.replaceAll("%NAME%", name);\r
+ s = s.replaceAll("%PORTLETNAME%", pname);\r
+ s = s.replaceAll("%URL%", url);\r
+ lpstring += s;\r
+\r
+ s = LIFERAY_DISPLAY_XML_SECTION;\r
+ s = s.replaceAll("%NAME%", name);\r
+ s = s.replaceAll("%PORTLETNAME%", pname);\r
+ s = s.replaceAll("%URL%", url);\r
+ ldstring += s;\r
+\r
+ }\r
+\r
+ pstring += PORTLET_XML_FOOT\r
+ .replaceAll("%CONTEXTPARAMS%", widgetset);\r
+ lpstring += LIFERAY_PORTLET_XML_FOOT;\r
+ ldstring += LIFERAY_DISPLAY_XML_FOOT;\r
+\r
+ try {\r
+ pout.write(pstring);\r
+ lpout.write(lpstring);\r
+ ldout.write(ldstring);\r
+ } catch (IOException e) {\r
+ System.out.println("Write FAILED:" + e);\r
+ }\r
+\r
+ }\r
+\r
+ try {\r
+ if (pout != null) {\r
+ pout.close();\r
+ }\r
+ if (lpout != null) {\r
+ lpout.close();\r
+ }\r
+ if (ldout != null) {\r
+ ldout.close();\r
+ }\r
+ } catch (IOException e) {\r
+ System.out.println("Close FAILED: " + e);\r
+ }\r
+ System.out.println("Done.");\r
+ }\r
+}\r