Author: dmaliarevich
Date: 2011-12-15 06:01:44 -0500 (Thu, 15 Dec 2011)
New Revision: 37347
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeStyleCreator.java
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/VpeStyleUtil.java
Log:
https://issues.jboss.org/browse/JBIDE-5861 - CSS @import support was added.
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java 2011-12-15
08:04:30 UTC (rev 37346)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java 2011-12-15
11:01:44 UTC (rev 37347)
@@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Matcher;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IStorage;
@@ -57,6 +58,7 @@
import org.jboss.tools.vpe.editor.template.VpeTemplateSafeWrapper;
import org.jboss.tools.vpe.editor.template.VpeToggableTemplate;
import org.jboss.tools.vpe.editor.template.expression.VpeExpressionException;
+import org.jboss.tools.vpe.editor.util.Constants;
import org.jboss.tools.vpe.editor.util.Docbook;
import org.jboss.tools.vpe.editor.util.FaceletUtil;
import org.jboss.tools.vpe.editor.util.HTML;
@@ -1080,7 +1082,7 @@
*/
public nsIDOMNode addLinkNodeToHead(String href_val, String ext_val,
boolean firstElement) {
- nsIDOMElement newNode = createLinkNode(href_val,
+ nsIDOMElement newNode = createInlineStyleNode(href_val,
ATTR_REL_STYLESHEET_VALUE, ext_val);
// TODO Dzmitry Sakovich
@@ -1096,7 +1098,7 @@
public nsIDOMNode replaceLinkNodeToHead(nsIDOMNode oldNode,
String href_val, String ext_val) {
- nsIDOMNode newNode = createLinkNode(href_val,
+ nsIDOMNode newNode = createInlineStyleNode(href_val,
ATTR_REL_STYLESHEET_VALUE, ext_val);
getHeadNode().replaceChild(newNode, oldNode);
return newNode;
@@ -1115,10 +1117,9 @@
getHeadNode().removeChild(node);
}
- private nsIDOMElement createLinkNode(String href_val, String rel_val,
- String ext_val) {
- nsIDOMElement linkNode = null;
- if ((ATTR_REL_STYLESHEET_VALUE.equalsIgnoreCase(rel_val))
+ private nsIDOMElement createInlineStyleNode(String href_val, String rel_val, String
ext_val) {
+ nsIDOMElement inlineStyle = null;
+ if (ATTR_REL_STYLESHEET_VALUE.equalsIgnoreCase(rel_val)
&& href_val.startsWith("file:")) { //$NON-NLS-1$
/*
* Because of the Mozilla caches the linked css files we replace tag
@@ -1126,33 +1127,43 @@
* vpe="ATTR_VPE_INLINE_LINK_VALUE">file content</style> It is
* LinkReplacer
*/
- linkNode = getVisualDocument().createElement(HTML.TAG_STYLE);
- linkNode.setAttribute(ATTR_VPE, ATTR_VPE_INLINE_LINK_VALUE);
+ inlineStyle = getVisualDocument().createElement(HTML.TAG_STYLE);
+ inlineStyle.setAttribute(ATTR_VPE, ATTR_VPE_INLINE_LINK_VALUE);
/* Copy links attributes into our <style> */
- linkNode.setAttribute(VpeTemplateManager.ATTR_LINK_HREF, href_val);
- linkNode.setAttribute(VpeTemplateManager.ATTR_LINK_EXT, ext_val);
+ inlineStyle.setAttribute(VpeTemplateManager.ATTR_LINK_HREF, href_val);
+ inlineStyle.setAttribute(VpeTemplateManager.ATTR_LINK_EXT, ext_val);
BufferedReader in = null;
try {
StringBuilder styleText = new StringBuilder(EMPTY_STRING);
URL url = new URL((new Path(href_val)).toOSString());
String fileName = url.getFile();
- in = new BufferedReader(new FileReader(
- (fileName)));
+ in = new BufferedReader(new FileReader((fileName)));
String str = EMPTY_STRING;
while ((str = in.readLine()) != null) {
styleText.append(str);
}
+ in.close();
String styleForParse = styleText.toString();
- styleForParse = VpeStyleUtil.addFullPathIntoURLValue(
- styleForParse, href_val);
-
- in.close();
- nsIDOMText textNode = getVisualDocument()
- .createTextNode(styleForParse);
- linkNode.appendChild(textNode);
- return linkNode;
+ /*
+ *
https://issues.jboss.org/browse/JBIDE-5861
+ * Add nested @import constructions
+ */
+ List<String> imports = VpeStyleUtil.findCssImportConstruction(styleForParse,
pageContext);
+ if (imports.size() > 0) {
+ for (String key : imports) {
+ addLinkNodeToHead(key, "css_nested_import_construction", false);
//$NON-NLS-1$
+ }
+ /*
+ * Replace @import constructions
+ */
+ Matcher m = VpeStyleUtil.CSS_IMPORT_PATTERN.matcher(styleForParse);
+ styleForParse = m.replaceAll(Constants.EMPTY);
+ }
+ styleForParse = VpeStyleUtil.addFullPathIntoURLValue(styleForParse, href_val);
+ inlineStyle.appendChild(getVisualDocument().createTextNode(styleForParse));
+ return inlineStyle;
} catch (FileNotFoundException fnfe) {
/* File which was pointed by user is not exists. Do nothing. */
} catch (IOException ioe) {
@@ -1168,12 +1179,12 @@
}
}
- linkNode = getVisualDocument().createElement(HTML.TAG_LINK);
- linkNode.setAttribute(VpeTemplateManager.ATTR_LINK_REL, rel_val);
- linkNode.setAttribute(VpeTemplateManager.ATTR_LINK_HREF, href_val);
- linkNode.setAttribute(VpeTemplateManager.ATTR_LINK_EXT, ext_val);
+ inlineStyle = getVisualDocument().createElement(HTML.TAG_LINK);
+ inlineStyle.setAttribute(VpeTemplateManager.ATTR_LINK_REL, rel_val);
+ inlineStyle.setAttribute(VpeTemplateManager.ATTR_LINK_HREF, href_val);
+ inlineStyle.setAttribute(VpeTemplateManager.ATTR_LINK_EXT, ext_val);
- return linkNode;
+ return inlineStyle;
}
private boolean isLinkReplacer(nsIDOMNode node) {
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeStyleCreator.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeStyleCreator.java 2011-12-15
08:04:30 UTC (rev 37346)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeStyleCreator.java 2011-12-15
11:01:44 UTC (rev 37347)
@@ -10,9 +10,13 @@
******************************************************************************/
package org.jboss.tools.vpe.editor.template;
+import java.util.List;
import java.util.Map;
+import java.util.regex.Matcher;
+import org.jboss.tools.vpe.editor.VpeVisualDomBuilder;
import org.jboss.tools.vpe.editor.context.VpePageContext;
+import org.jboss.tools.vpe.editor.util.Constants;
import org.jboss.tools.vpe.editor.util.VpeStyleUtil;
import org.mozilla.interfaces.nsIDOMDocument;
import org.mozilla.interfaces.nsIDOMElement;
@@ -35,9 +39,24 @@
String text = null;
if (textNode != null) {
text = textNode.getNodeValue();
+ /*
+ *
https://issues.jboss.org/browse/JBIDE-5861
+ * Add inline <style> element for each found css @import
+ */
+ VpeVisualDomBuilder vvdb = pageContext.getVisualBuilder();
+ List<String> imports = VpeStyleUtil.findCssImportConstruction(text,
pageContext);
+ if (imports.size() > 0) {
+ for (String key : imports) {
+ vvdb.addLinkNodeToHead(key, "css_import_construction", false);
//$NON-NLS-1$
+ }
+ /*
+ * Replace @import constructions
+ */
+ Matcher m = VpeStyleUtil.CSS_IMPORT_PATTERN.matcher(text);
+ text = m.replaceAll(Constants.EMPTY);
+ }
text = VpeStyleUtil.addFullPathIntoURLValue(text, pageContext);
}
-
nsIDOMNode newStyle = pageContext.getVisualBuilder()
.addStyleNodeToHead(text);
visualNodeMap.put(this, newStyle);
@@ -47,14 +66,11 @@
@Override
public void removeElement(VpePageContext pageContext,
Element sourceElement, Map visualNodeMap) {
-
nsIDOMNode styleNode = (nsIDOMNode) visualNodeMap.get(this);
-
if (styleNode != null) {
pageContext.getVisualBuilder().removeStyleNodeFromHead(styleNode);
visualNodeMap.remove(this);
}
-
}
@Override
@@ -69,8 +85,8 @@
}
nsIDOMNode newStyleNode;
if (oldStyleNode == null) {
- newStyleNode = pageContext.getVisualBuilder().addStyleNodeToHead(
- text);
+ newStyleNode = pageContext.getVisualBuilder().
+ addStyleNodeToHead(text);
visualNodeMap.put(this, newStyleNode);
} else {
newStyleNode = pageContext.getVisualBuilder()
@@ -79,6 +95,5 @@
visualNodeMap.remove(this);
visualNodeMap.put(this, newStyleNode);
}
-
}
-}
+}
\ No newline at end of file
Modified:
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/VpeStyleUtil.java
===================================================================
---
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/VpeStyleUtil.java 2011-12-15
08:04:30 UTC (rev 37346)
+++
trunk/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/util/VpeStyleUtil.java 2011-12-15
11:01:44 UTC (rev 37347)
@@ -19,6 +19,8 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -81,6 +83,8 @@
* (.*) should be replaced with ([^;]*)
*/
public static final Pattern CSS_URL_PATTERN =
Pattern.compile("(?<=\\burl\\b)(?:[\\p{Space}]*\\()[\\p{Space}]*([^;]*)[\\p{Space}]*(?:\\)[\\p{Space}]*)(?=(?>[^\\)]*;|[^\\)]*))");
//$NON-NLS-1$
+ public static final Pattern CSS_IMPORT_PATTERN =
Pattern.compile("@import[\\p{Space}]+(?:\\burl\\b[\\p{Space}]*\\()*[\\p{Space}]*([^;]*)[\\p{Space}]*(?:\\)[\\p{Space}]*(?=(?>[^\\)]*;|[^\\)]*)))*");
//$NON-NLS-1$
+ public static final Pattern CSS_URI_PATTERN =
Pattern.compile("(?:\\\"{1}(.*)\\\"{1})|(?:\\'{1}(.*)\\'{1})");
//$NON-NLS-1$
public static String ATTR_URL = "url"; //$NON-NLS-1$
public static String OPEN_BRACKET = "("; //$NON-NLS-1$
@@ -438,11 +442,7 @@
if (urls.length == 1) {
return value;
}
- IFile file = null;
- final VpeIncludeInfo vii = pageContext.getVisualBuilder().getCurrentIncludeInfo();
- if (vii != null && (vii.getStorage() instanceof IFile)) {
- file = (IFile) vii.getStorage();
- }
+ IFile file = getSourceFileFromPageContext(pageContext);
for (int i = 1; i < urls.length; i++) {
urls[i] = removeQuotesUpdate(urls[i]);
String[] urlParts = splitURL(urls[i]);
@@ -450,7 +450,7 @@
continue;
}
if (file != null) {
- urlParts[1] = processUrl(urlParts[1], file);
+ urlParts[1] = processUrl(urlParts[1], file, true);
}
urls[i] = collectArrayInto1Str(urlParts);
}
@@ -484,7 +484,7 @@
// ignore
}
if (sourceFile != null) {
- urlParts[1] = processUrl(urlParts[1], sourceFile);
+ urlParts[1] = processUrl(urlParts[1], sourceFile, true);
} else {
urlParts[1] = updateURLFilePath(urlParts[1], href_val);
if (urlParts[1] == null) {
@@ -685,20 +685,26 @@
public static IPath getRootPath(IEditorInput input) {
IPath rootPath = null;
if (input instanceof IFileEditorInput) {
- IProject project = ((IFileEditorInput) input).getFile().getProject();
- if (project != null && project.isOpen()) {
- IModelNature modelNature = EclipseResourceUtil.getModelNature(project);
- if (modelNature != null) {
- XModel model = modelNature.getModel();
- String rootPathStr = WebProject.getInstance(model).getWebRootLocation();
- if (rootPathStr != null) {
- rootPath = new Path(rootPathStr);
- } else {
- rootPath = project.getLocation();
- }
+ rootPath = getRootPath(((IFileEditorInput) input).getFile());
+ }
+ return rootPath;
+ }
+
+ public static IPath getRootPath(IFile inputFile) {
+ IPath rootPath = null;
+ IProject project = inputFile.getProject();
+ if (project != null && project.isOpen()) {
+ IModelNature modelNature = EclipseResourceUtil.getModelNature(project);
+ if (modelNature != null) {
+ XModel model = modelNature.getModel();
+ String rootPathStr = WebProject.getInstance(model).getWebRootLocation();
+ if (rootPathStr != null) {
+ rootPath = new Path(rootPathStr);
} else {
rootPath = project.getLocation();
}
+ } else {
+ rootPath = project.getLocation();
}
}
return rootPath;
@@ -783,7 +789,7 @@
return Integer.toString(position) + PX_STRING;
}
- public static String processUrl(String url, IFile baseFile) {
+ public static String processUrl(String url, IFile baseFile, boolean putIntoQuotes) {
String resolvedUrl = url.replaceFirst(
"^\\s*(\\#|\\$)\\{facesContext.externalContext.requestContextPath\\}",
Constants.EMPTY); //$NON-NLS-1$
resolvedUrl = ElServiceUtil.replaceEl(baseFile, resolvedUrl);
@@ -799,22 +805,25 @@
}
if (uri == null || !uri.isAbsolute()) {
String decodedUrl = decodeUrl(resolvedUrl);
- Path path = new Path(decodedUrl);
- if (decodedUrl.startsWith("/") && (null !=
path.segment(0))//$NON-NLS-1$
- && path.segment(0).equals(baseFile.getProject().getName())) {
- decodedUrl = "/" + path.removeFirstSegments(1).toPortableString();
//$NON-NLS-1$
- }
+ Path decodedPath = new Path(decodedUrl);
+ if (decodedUrl.startsWith("/") && (null !=
decodedPath.segment(0))//$NON-NLS-1$
+ && decodedPath.segment(0).equals(baseFile.getProject().getName())) {
+ decodedUrl = "/" + decodedPath.removeFirstSegments(1).toPortableString();
//$NON-NLS-1$
+ }
IFile file = FileUtil.getFile(decodedUrl, baseFile);
if (file != null && file.getLocation() != null) {
resolvedUrl = pathToUrl(file.getLocation());
- }
+ }
}
/*
*
https://issues.jboss.org/browse/JBIDE-9975
* Put the URL into quotes.
* It's default xulrunner behavior.
*/
- return QUOTE_STRING + resolvedUrl + QUOTE_STRING;
+ if (putIntoQuotes) {
+ resolvedUrl = QUOTE_STRING + resolvedUrl + QUOTE_STRING;
+ }
+ return resolvedUrl;
}
private static String pathToUrl(IPath location) {
@@ -858,4 +867,71 @@
: HTML.STYLE_VALUE_NONE, HTML.STYLE_PRIORITY_IMPORTANT);
}
+
+ /**
+ * Finds CSS @import url(".."); construction
+ *
+ * @param cssText the css text
+ * @param pageContext VPE page context
+ * @return the map with the import statement as a key and the css file path as a value
+ */
+ public static List<String> findCssImportConstruction(String cssText,
VpePageContext pageContext) {
+ ArrayList<String> list = new ArrayList<String>();
+ IFile sourceFile = getSourceFileFromPageContext(pageContext);
+ Matcher m = CSS_IMPORT_PATTERN.matcher(cssText);
+ while (m.find()) {
+ /*
+ * Path should be a well formed URI
+ */
+ list.add(processUrl(getCorrectURI(m.group(1)), sourceFile, false));
+ }
+ return list;
+ }
+
+ /**
+ * Gets the source file from pageContext
+ *
+ * @param pageContext
+ * @return the opened file
+ */
+ public static IFile getSourceFileFromPageContext(VpePageContext pageContext) {
+ IFile file = null;
+ final VpeIncludeInfo vii = pageContext.getVisualBuilder().getCurrentIncludeInfo();
+ if ((vii != null) && (vii.getStorage() instanceof IFile)) {
+ file = (IFile) vii.getStorage();
+ }
+ return file;
+ }
+
+ /**
+ * Determine correct uri in the input path:
+ * Remove quotes and brackets
+ *
+ * @param path input path
+ * @return correct URI string
+ */
+ private static String getCorrectURI(String path) {
+ String uri = path;
+ /*
+ * Closing bracket appears due to regex pattern.
+ * Should be removed.
+ */
+ if (path.endsWith(CLOSE_BRACKET)) {
+ uri = uri.substring(0, uri.length() - 1);
+ }
+ Matcher m = CSS_URI_PATTERN.matcher(uri);
+ /*
+ * Find uri in " or ' quotes
+ */
+ if (m.find()) {
+ if ((m.group(1) != null)
+ && !EMPTY_STRING.equalsIgnoreCase(m.group(1))) {
+ uri = m.group(1);
+ } else if ((m.group(2) != null)
+ && !EMPTY_STRING.equalsIgnoreCase(m.group(2))) {
+ uri = m.group(2);
+ }
+ }
+ return uri;
+ }
}