Author: phuong_vu
Date: 2010-08-31 06:12:53 -0400 (Tue, 31 Aug 2010)
New Revision: 3976
Modified:
portal/branches/branched-r3845/component/web/resources/src/main/java/org/exoplatform/portal/resource/SkinService.java
Log:
GTNPORTAL-1199 Wrong parsing css file when this css is already compressed
Modified:
portal/branches/branched-r3845/component/web/resources/src/main/java/org/exoplatform/portal/resource/SkinService.java
===================================================================
---
portal/branches/branched-r3845/component/web/resources/src/main/java/org/exoplatform/portal/resource/SkinService.java 2010-08-31
09:25:26 UTC (rev 3975)
+++
portal/branches/branched-r3845/component/web/resources/src/main/java/org/exoplatform/portal/resource/SkinService.java 2010-08-31
10:12:53 UTC (rev 3976)
@@ -33,7 +33,6 @@
import org.exoplatform.management.jmx.annotations.Property;
import org.exoplatform.management.rest.annotations.RESTEndpoint;
import org.exoplatform.portal.resource.compressor.ResourceCompressor;
-import org.exoplatform.portal.resource.compressor.ResourceCompressorException;
import org.exoplatform.portal.resource.compressor.ResourceType;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
@@ -88,17 +87,17 @@
/** Immutable and therefore thread safe. */
private static final Pattern IMPORT_PATTERN =
- Pattern.compile("(@import\\s+" + "url" + LEFT_P +
"['\"]?" + ")([^'\"]+.css)(" +
"['\"]?" + RIGHT_P + "\\s*;)");
+ Pattern.compile("(@import\\s+" + "url" + LEFT_P +
"['\"]?" + ")([^'\";]+.css)(" +
"['\"]?" + RIGHT_P + "\\s*;)");
/** Immutable and therefore thread safe. */
private static final Pattern BACKGROUND_PATTERN =
- Pattern.compile("(background.*:.*url" + LEFT_P +
"['\"]?" + ")([^'\"]+)(" +
"['\"]?" + RIGHT_P + ".*;)");
+ Pattern.compile("(background[^;]+url" + LEFT_P +
"['\"]?" + ")([^'\";]+)(" +
"['\"]?" + RIGHT_P + "[^;]*;)");
/** Immutable and therefore thread safe. */
- private static final Pattern LT =
Pattern.compile("/\\*\\s*orientation=lt\\s*\\*/");
+ private static final Pattern LT =
Pattern.compile("[^{;]*;\\s*/\\*\\s*orientation=lt\\s*\\*/");
/** Immutable and therefore thread safe. */
- private static final Pattern RT =
Pattern.compile("/\\*\\s*orientation=rt\\s*\\*/");
+ private static final Pattern RT =
Pattern.compile("[^{;]*;\\s*/\\*\\s*orientation=rt\\s*\\*/");
/** One month caching. */
private static final int ONE_MONTH = 2592000;
@@ -455,7 +454,7 @@
CachedStylesheet cachedCss = cache.get(path);
if (cachedCss == null)
{
- StringBuilder sb = new StringBuilder();
+ StringBuffer sb = new StringBuffer();
processCSS(sb, path, orientation, true);
String css;
try
@@ -650,7 +649,7 @@
}
return resource;
}
-
+
/**
* Apply CSS of skin
* @param appendable
@@ -663,7 +662,7 @@
private void processCSS(Appendable appendable, String cssPath, Orientation
orientation, boolean merge)
throws RenderingException, IOException
{
- Resource skin = getCSSResource(cssPath, cssPath);
+ Resource skin = getCSSResource(cssPath, cssPath);
processCSSRecursively(appendable, merge, skin, orientation);
}
@@ -680,7 +679,6 @@
private void processCSSRecursively(Appendable appendable, boolean merge, Resource
skin, Orientation orientation)
throws RenderingException, IOException
{
-
if(skin == null)
{
return;
@@ -695,55 +693,43 @@
{
throw new RenderingException("No skin resolved for path " +
skin.getResourcePath());
}
- BufferedReader reader = new BufferedReader(tmp);
+ BufferedReader reader = new BufferedReader(tmp);
try
{
while ((line = reader.readLine()) != null)
- {
+ {
+ line = proccessOrientation(line, orientation);
+ line = proccessBackgroundUrl(line, basePath);
+
Matcher matcher = IMPORT_PATTERN.matcher(line);
- if (matcher.find())
+ while (matcher.find())
{
- String includedPath = matcher.group(2);
- if (includedPath.startsWith("/"))
+ String includedPath = matcher.group(2);
+ if (!includedPath.startsWith("/"))
{
- if (merge)
- {
- Resource ssskin = getCSSResource(includedPath, basePath +
skin.getFileName());
- processCSSRecursively(appendable, merge, ssskin, orientation);
- }
- else
- {
- appendable.append(matcher.group(1)).append(
- includedPath.substring(0, includedPath.length() -
".css".length())).append(
-
getSuffix(orientation)).append(matcher.group(3)).append("\n");
- }
+ includedPath = basePath + includedPath;
}
- else
- {
- if (merge)
- {
- String path = skin.getContextPath() + skin.getParentPath() +
includedPath;
- Resource ssskin = getCSSResource(path, basePath +
skin.getFileName());
- processCSSRecursively(appendable, merge, ssskin, orientation);
- }
- else
- {
- appendable.append(matcher.group(1));
- appendable.append(basePath);
- appendable.append(includedPath.substring(0, includedPath.length() -
".css".length()));
- appendable.append(getSuffix(orientation));
- appendable.append(matcher.group(3));
- }
- }
+
+ String embeddedPath = includedPath.substring(0, includedPath.length() -
".css".length());
+ StringBuffer strReplace = new StringBuffer();
+ if (merge)
+ {
+ Resource ssskin = getCSSResource(includedPath, basePath +
skin.getFileName());
+ processCSSRecursively(strReplace, merge, ssskin, orientation);
+ }
+ else
+ {
+ strReplace.append(matcher.group(1));
+ strReplace.append(embeddedPath);
+ strReplace.append(getSuffix(orientation));
+ strReplace.append(matcher.group(3));
+ }
+ String str = strReplace.toString().replaceAll("\\$",
"\\\\\\$");
+ matcher.appendReplacement((StringBuffer)appendable, str);
}
- else
- {
- if (orientation == null || wantInclude(line, orientation))
- {
- append(line, basePath, appendable);
- }
- }
- }
+ matcher.appendTail((StringBuffer)appendable);
+ appendable.append("\n");
+ }
}
finally
{
@@ -751,39 +737,43 @@
}
}
- /**
- * Filter what if it's annotated with the alternative orientation.
- *
- * @param line
- * the line to include
- * @param orientation
- * the orientation
- * @return true if the line is included
- */
- private boolean wantInclude(String line, Orientation orientation)
+ private String proccessBackgroundUrl(String line, String basePath)
{
- Pattern orientationPattern = orientation == Orientation.LT ? RT : LT;
- Matcher matcher2 = orientationPattern.matcher(line);
- return !matcher2.find();
- }
-
- /**
- * Rewrite background url pattern
- */
- private void append(String line, String basePath, Appendable appendable) throws
IOException
- {
// Rewrite background url pattern
Matcher matcher = BACKGROUND_PATTERN.matcher(line);
- if (matcher.find() && !matcher.group(2).startsWith("\"/")
&& !matcher.group(2).startsWith("'/")
- && !matcher.group(2).startsWith("/"))
+
+ StringBuffer tmpBuilder = new StringBuffer();
+ while (matcher.find())
{
-
appendable.append(matcher.group(1)).append(basePath).append(matcher.group(2)).append(matcher.group(3)).append(
- '\n');
+ if (!matcher.group(2).startsWith("\"/")) {
+ if (!matcher.group(2).startsWith("'/")) {
+ if (!matcher.group(2).startsWith("/")) {
+ StringBuilder strReplace = new StringBuilder();
+ strReplace.append(matcher.group(1));
+ strReplace.append(basePath);
+ strReplace.append(matcher.group(2));
+ strReplace.append(matcher.group(3));
+
+ matcher.appendReplacement(tmpBuilder, strReplace.toString());
+ }
+ }
+ }
}
- else
+ matcher.appendTail(tmpBuilder);
+ return tmpBuilder.toString();
+ }
+
+ private String proccessOrientation(String line, Orientation orientation)
+ {
+ Pattern orientationPattern = orientation == Orientation.LT ? RT : LT;
+ Matcher matcher = orientationPattern.matcher(line);
+ StringBuffer tmpBuilder = new StringBuffer();
+ while (matcher.find())
{
- appendable.append(line).append('\n');
+ matcher.appendReplacement(tmpBuilder, "");
}
+ matcher.appendTail(tmpBuilder);
+ return tmpBuilder.toString();
}
/**