/**
*
*/
package com.vaadin.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";
private static final String JBOSS_OBJECT_FILE = "itmill-object.xml";
private static final String JBOSS_INSTANCE_FILE = "portlet-instances.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.vaadin.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 = "\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" + "";
private static final String JBOSS_INSTANCE_HEAD = "\r\n"
+ "\r\n"
+ "\r\n";
private static final String JBOSS_INSTANCE_SECTION = " \r\n \r\n"
+ " %PORTLETNAME%Instance\r\n"
+ " %PORTLETNAME%\r\n"
+ " \r\n \r\n";
private static final String JBOSS_INSTANCE_FOOT = "";
private static final String JBOSS_OBJECT_HEAD = "\r\n"
+ "\r\n"
+ "\r\n";
private static final String JBOSS_OBJECT_SECTION = " \r\n"
+ " default.default\r\n"
+ " overwrite"
+ " \r\n"
+ " %PORTLETNAME%Window\r\n"
+ " \r\n"
+ " portlet\r\n"
+ " %PORTLETNAME%Instance\r\n"
+ " \r\n"
+ " center\r\n"
+ " 1\r\n"
+ " \r\n \r\n";
private static final String JBOSS_OBJECT_FOOT = "";
/**
* @param args
* [default 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 = 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!");
}
// open jboss object.xml
File jbossObjectXmlFile = new File(args[0] + File.separatorChar
+ JBOSS_OBJECT_FILE);
OutputStreamWriter joout = null;
try {
joout = new OutputStreamWriter(new FileOutputStream(
jbossObjectXmlFile), Charset.forName("UTF-8"));
} catch (FileNotFoundException e) {
System.out.println(jbossObjectXmlFile + " not found!");
}
// open jboss instance.xml
File jbossInstanceXmlFile = new File(args[0] + File.separatorChar
+ JBOSS_INSTANCE_FILE);
OutputStreamWriter jiout = null;
try {
jiout = new OutputStreamWriter(new FileOutputStream(
jbossInstanceXmlFile), Charset.forName("UTF-8"));
} catch (FileNotFoundException e) {
System.out.println(jbossInstanceXmlFile + " not found!");
}
if (pout != null && lpout != null && ldout != null && joout != null
&& jiout != null) {
String pstring = PORTLET_XML_HEAD;
String lpstring = LIFERAY_PORTLET_XML_HEAD;
String ldstring = LIFERAY_DISPLAY_XML_HEAD;
String jostring = JBOSS_OBJECT_HEAD;
String jistring = JBOSS_INSTANCE_HEAD;
Pattern p1 = Pattern
.compile(".*?(.*?)<\\/servlet-name>.*?(.*?)<\\/url-pattern>(.*?)<\\/servlet-mapping>");
Pattern p2 = Pattern
.compile(".*?.*?");
Pattern findWidgetset = Pattern
.compile(".*?widgetset<\\/param-name>.*?(.*?)<\\/param-value>");
Matcher m = p1.matcher(webXml);
while (m.find()) {
if (m.groupCount() < 3) {
// don't include
continue;
}
String name = m.group(1);
// remove leading- and trailing whitespace
name = name.replaceAll("^\\s*", "");
name = name.replaceAll("\\s*$", "");
String comment = m.group(3);
Matcher m2 = p2.matcher(comment);
if (!m2.find()) {
// don't include
continue;
}
String style = "";
if (m2.groupCount() == 1 && m2.group(1) != null
&& !m2.group(1).equals("")) {
style = "style"
+ m2.group(1) + "";
}
// Find widgetset
Pattern findServlet = Pattern
.compile(".*?" + name
+ "<\\/servlet-name>(.*?)<\\/servlet>");
Matcher servletMatcher = findServlet.matcher(webXml);
if (servletMatcher.find()) {
String servletXml = servletMatcher.group(1);
Matcher widgetsetMatcher = findWidgetset
.matcher(servletXml);
if (widgetsetMatcher.find()) {
String definedWidgetSet = widgetsetMatcher.group(1);
if (!definedWidgetSet.equals(widgetset)) {
System.err
.println("WARNING: Widgetset in web.xml ("
+ definedWidgetSet
+ ") does not match used ("
+ widgetset + ")");
}
}
}
if (widgetset != null && !widgetset.equals("")) {
System.err.println("Using widgetset: " + widgetset);
style += "\n "
+ "widgetset"
+ widgetset + "";
}
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;
s = JBOSS_OBJECT_SECTION;
s = s.replaceAll("%NAME%", name);
s = s.replaceAll("%PORTLETNAME%", pname);
s = s.replaceAll("%URL%", url);
jostring += s;
s = JBOSS_INSTANCE_SECTION;
s = s.replaceAll("%NAME%", name);
s = s.replaceAll("%PORTLETNAME%", pname);
s = s.replaceAll("%URL%", url);
jistring += s;
}
pstring += PORTLET_XML_FOOT;
lpstring += LIFERAY_PORTLET_XML_FOOT;
ldstring += LIFERAY_DISPLAY_XML_FOOT;
jostring += JBOSS_OBJECT_FOOT;
jistring += JBOSS_INSTANCE_FOOT;
try {
pout.write(pstring);
lpout.write(lpstring);
ldout.write(ldstring);
joout.write(jostring);
jiout.write(jistring);
} 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();
}
if (joout != null) {
joout.close();
}
if (jiout != null) {
jiout.close();
}
} catch (IOException e) {
System.out.println("Close FAILED: " + e);
}
System.out.println("Done.");
}
}