2008-10-14 17:16:44 UTC
Hey,
I've had some issues with liferay and imported stylesheets. Styles are mangled for some tags and classes especially, BODY,IMG,FORM,INPUT,P,SELECT,TABLE,TD,TH,TT,.logo,.portlet,*.
HIH,
Eric
PortletBridgeServlet.java
String cssRemoveCSV = this.getServletConfig().getInitParameter( "cssRemoveCSV" );
ContentRewriter cssRewriter = new RegexContentRemover( cssRegex, cssRemoveCSV );
PortletBridgePortlet.java
String cssRemoveCSV = config.getInitParameter( "cssRemoveCSV" );
ContentRewriter cssRewriter = new RegexContentRemover( cssRegex, cssRemoveCSV );
web.xml
<init-param>
<param-name>cssRemoveCSV</param-name>
<param-value>BODY,IMG,FORM,INPUT,P,SELECT,TABLE,TD,TH,TT,.logo,.portlet,*</param-value>
</init-param>
portlet-custom.xml
<init-param>
<name>cssRemoveCSV</name>
<value>BODY,IMG,FORM,INPUT,P,SELECT,TABLE,TD,TH,TT,.logo,.portlet,*</value>
</init-param>
package org.portletbridge.portlet;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexContentRemover extends RegexContentRewriter {
public static final String myPattern = "(@import\\s*url\\s*\\(.*?\\)\\s*;\\s*)|(([ \\S]*)(\\s*\\{\\s*)(.*?)(\\s*\\}\\s*))";
private Pattern rmPattern;
private HashSet suppressedTags;
public RegexContentRemover( String pattern, String suppressedTagsCsv ) {
super( pattern );
rmPattern = Pattern.compile( myPattern, Pattern.MULTILINE | Pattern.DOTALL );
suppressedTags = new HashSet();
String[] tags = suppressedTagsCsv.split( "[,]" );
if ( tags != null ) {
for( int tagCounter = 0; tagCounter < tags.length; tagCounter++ ) {
suppressedTags.add( tags[ tagCounter ] );
}
}
}
public String rewrite(String baseUrl, String content, LinkRewriter linkRewriter) {
return remove( super.rewrite( baseUrl, content, linkRewriter ) );
}
public String remove( String content ) {
if ( suppressedTags.size() == 0 ) {
return content;
} else {
StringBuffer sb = new StringBuffer();
Matcher matcher = rmPattern.matcher( content );
while( matcher.find() ) {
if ( matcher.groupCount() == 6 ) {
String tag = null;
if ( matcher.start( 3 ) != -1 ) {
tag = matcher.group( 3 ).trim();
String style = null;
if ( matcher.start( 5 ) != -1 ) {
style = matcher.group( 5 ).trim();
String[] tags = tag.split( "[,]" );
if ( tags !=null ) {
int sbLength = sb.length();
int tagsAdded = 0;
for( int tagCounter = 0; tagCounter < tags.length; tagCounter++ ) {
tag = tags[ tagCounter ].trim();
if ( suppressedTags.contains( tag ) )
continue;
if ( tagsAdded++ != 0 ) {
sb.append( ", " );
}
sb.append( tag );
}
if ( tagsAdded == 0 )
continue;
sb.append( " { " );
int stylesAdded = 0;
String[] styles = style.split( "[;\\r\\n]" );
if ( styles != null ) {
for( int styleCounter = 0; styleCounter < styles.length; styleCounter++ ) {
String localStyle = styles[ styleCounter ].trim();
if ( !localStyle.equals( "" ) ) {
stylesAdded++;
sb.append( localStyle );
sb.append( ";" );
}
}
}
if ( stylesAdded == 0 ) {
sb.setLength( sbLength );
} else {
sb.append( " }" );
sb.append( System.getProperty( "line.separator" ) );
}
}
}
} else if ( matcher.start( 1 ) != -1 ) {
sb.append( matcher.group( 1 ).trim() );
sb.append( System.getProperty( "line.separator" ) );
}
}
}
return sb.toString();
}
}
public static void main( String args[] ) {
for( int argCounter = 0; argCounter < args.length; argCounter++ ) {
try {
char buf[] = new char[ 1024 ];
int amountRead = 0;
StringBuffer sb = new StringBuffer();
FileReader fr = new FileReader( args[ argCounter ] );
while( ( amountRead = fr.read( buf, 0, buf.length ) ) != -1 ) {
sb.append( buf, 0, amountRead );
}
RegexContentRemover rcr = new RegexContentRemover( "(?:url\\((?:'|\")?(.*?)(?:'|\")?\\))|(?:@import\\s+[^url](?:'|\")?(.*?)(?:'|\")|;|\\s+|$)",
"BODY,IMG,FORM,TH,TABLE,*" );
System.out.print( rcr.remove( sb.toString() ) );
} catch( FileNotFoundException fnfe ) {
System.err.print( fnfe.getMessage() );
} catch( IOException ioe ) {
System.err.print( ioe.getMessage() );
}
}
}
}