### Eclipse Workspace Patch 1.0 #P org.jboss.tools.jsf.vpe.richfaces.test Index: src/org/jboss/tools/jsf/vpe/richfaces/test/jbide/RichFacesJBIDE1169Test.java =================================================================== --- src/org/jboss/tools/jsf/vpe/richfaces/test/jbide/RichFacesJBIDE1169Test.java (revision 33408) +++ src/org/jboss/tools/jsf/vpe/richfaces/test/jbide/RichFacesJBIDE1169Test.java (working copy) @@ -16,7 +16,7 @@ import org.jboss.tools.jsf.vpe.richfaces.test.RichFacesAllTests; import org.jboss.tools.vpe.base.test.TestUtil; import org.jboss.tools.vpe.base.test.VpeTest; -import org.jboss.tools.vpe.editor.util.ElService; +import org.jboss.tools.vpe.editor.util.ElServiceUtil; /** * @author mareshkau @@ -48,7 +48,7 @@ } public void testJBIDE1169() { - String replacedValue = ElService.replaceEl(testFile,"#{"+RICH_FACES_SKIN_KEY+'}'); //$NON-NLS-1$ + String replacedValue = ElServiceUtil.replaceEl(testFile,"#{"+RICH_FACES_SKIN_KEY+'}'); //$NON-NLS-1$ assertEquals("Skin value should be equals",SKIN_VALUE, replacedValue); //$NON-NLS-1$ } /** #P org.jboss.tools.jsf.vpe.jsf Index: src/org/jboss/tools/jsf/vpe/jsf/template/JSF2CompositeAttributeTemplate.java =================================================================== --- src/org/jboss/tools/jsf/vpe/jsf/template/JSF2CompositeAttributeTemplate.java (revision 33408) +++ src/org/jboss/tools/jsf/vpe/jsf/template/JSF2CompositeAttributeTemplate.java (working copy) @@ -33,9 +33,9 @@ //we should register attributes only if we process this as custom component, but not when we open component definition page if(!pageContext.getVisualBuilder().isCurrentMainDocument()){ String compositionCustomElementAttributeKey = Jsf2CustomComponentTemplate.JSF2_CUSTOM_COMPONENT_PARAMETR_KEY +name; - if(pageContext.getCustomElementsAttributes().containsKey(compositionCustomElementAttributeKey)){ + if(pageContext.containsAttributeInCustomElementsMap(compositionCustomElementAttributeKey)){ pageContext.addAttributeInCustomElementsMap(JSF.CUSTOM_COMPONENT_ATTR_PREFIX+name, - pageContext.getCustomElementsAttributes().get(compositionCustomElementAttributeKey)); + pageContext.getAttributefromCustomElementsMapValue(compositionCustomElementAttributeKey)); }else if(sourceElement.hasAttribute(JSF.ATTR_DEFAULT)) { String defaultValue = sourceElement.getAttribute(JSF.ATTR_DEFAULT); pageContext.addAttributeInCustomElementsMap(JSF.CUSTOM_COMPONENT_ATTR_PREFIX+name, defaultValue); #P org.jboss.tools.vpe Index: src/org/jboss/tools/vpe/editor/util/ElService.java =================================================================== --- src/org/jboss/tools/vpe/editor/util/ElService.java (revision 33486) +++ src/org/jboss/tools/vpe/editor/util/ElService.java (working copy) @@ -11,20 +11,8 @@ package org.jboss.tools.vpe.editor.util; -import java.util.Arrays; -import java.util.Comparator; -import java.util.Map; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Platform; -import org.jboss.tools.common.el.core.ELReferenceList; -import org.jboss.tools.common.el.core.GlobalELReferenceList; import org.jboss.tools.common.resref.core.ResourceReference; -import org.jboss.tools.jst.jsp.bundle.BundleMapUtil; import org.jboss.tools.vpe.editor.context.VpePageContext; -import org.w3c.dom.Attr; -import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; /** @@ -32,318 +20,44 @@ * @author Eugeny Stherbin * @author Vitali Yemialyanchyk */ -public final class ElService { +public class ElService { - public static final String DOLLAR_PREFIX = "${"; //$NON-NLS-1$ - - private static final String SUFFIX = "}"; //$NON-NLS-1$ - - public static final String SHARP_PREFIX = "#{"; //$NON-NLS-1$ - - /** - * Replace el. - * - * @param resourceFile - * the resource file - * @param resourceString - * the resource string - * - * @return the string - * - * @see IELService#replaceEl(IFile, String) - */ - public static String replaceEl(IFile file, String resourceString) { - if (resourceString == null) { - return ""; //$NON-NLS-1$ - } - String rst = resourceString; - ResourceReference[] references = getAllResources(file); - if (references == null || references.length == 0) { - return rst; - } - references = sortReferencesByScope2(references); - rst = replace(resourceString, references); - return rst; - } - - public static String replaceEl(VpePageContext pageContext, String resourceString) { - if (resourceString == null) { - return ""; //$NON-NLS-1$ - } - String rst = resourceString; - final ResourceReference[] references = getResourceReferences(pageContext); - if (references == null || references.length == 0) { - return rst; - } - rst = replace(resourceString, references); - return rst; - } + protected ELResolver elResolver; - /** - * Replace. - * - * @param resourceString - * the resource string - * @param references - * the references - * - * @return the string - */ - private static String replace(String resourceString, ResourceReference[] sortedReferences) { - String result = resourceString; - // Values with higher precedence should be replaced in the first place. - // Expect sorted by scope references. - final int nPrefLen = getPrefLen(); // for small-simple optimization - for (ResourceReference rf : sortedReferences) { - final String tmp = rf.getLocation(); - if (tmp.length() + nPrefLen > resourceString.length()) { - continue; // no sense for sequence contains checks... - } - final String dollarEl = DOLLAR_PREFIX + tmp + SUFFIX; - final String sharpEl = SHARP_PREFIX + tmp + SUFFIX; - if (resourceString.contains(dollarEl)) { - result = result.replace(dollarEl, rf.getProperties()); - } - if (resourceString.contains(sharpEl)) { - result = result.replace(sharpEl, rf.getProperties()); - } - } - return result; + public ElService(VpePageContext pageContext) { + elResolver = new ELResolver(pageContext); } - private static String replaceCustomAttributes(VpePageContext pageContext, String value) { - String result = value; - final int nPrefLen = getPrefLen(); // for small-simple optimization - final Map tmp = pageContext.getCustomElementsAttributes(); - for (String el : tmp.keySet()) { - if (el.length() + nPrefLen > result.length()) { - continue; // no sense for sequence contains checks... - } - final String dollarEl = DOLLAR_PREFIX + el + SUFFIX; - final String sharpEl = SHARP_PREFIX + el + SUFFIX; - - if (result.contains(dollarEl)) { - result = result.replace(dollarEl, tmp.get(el)); - } - if (result.contains(sharpEl)) { - result = result.replace(sharpEl, tmp.get(el)); - } - } - return result; + public String replaceEl(String resourceString) { + return getElResolver().replaceEl(resourceString); } - private static int getPrefLen() { - int nPrefLen = DOLLAR_PREFIX.length(); - if (nPrefLen > SHARP_PREFIX.length()) { - nPrefLen = SHARP_PREFIX.length(); - } - nPrefLen += SUFFIX.length(); - return nPrefLen; - } - /** - * Creates a copy of {@code references} array and sorts its elements by scope value. - * - * References with the lowest scope value ({@link ResourceReference#FILE_SCOPE}) become the first in the array and - * so on. - * - * @param references - * array to be sorted - * @return sorted copy of {@code references} - * @author yradtsevich - */ - public static ResourceReference[] sortReferencesByScope(ResourceReference[] references) { - ResourceReference[] sortedReferences = references.clone(); - return sortReferencesByScope(sortedReferences); - } - - public static ResourceReference[] sortReferencesByScope2(ResourceReference[] references) { - Arrays.sort(references, new Comparator() { - public int compare(ResourceReference r1, ResourceReference r2) { - return r1.getScope() - r2.getScope(); - } - }); - return references; - } - - /** * Checks if is cloneable node. * * @param sourceNode * the source node - * @param pageContext - * the page context * * @return true, if is cloneable node */ - public static boolean isELNode(VpePageContext pageContext, Node sourceNode) { - boolean rst = false; - if (isInCustomElementsAttributes(pageContext, sourceNode)) { - return true; - } - if (isAvailableForNode(pageContext, sourceNode) - || BundleMapUtil.isInResourcesBundle(pageContext.getBundle(), sourceNode)) { - rst = true; - } else if (Jsf2ResourceUtil.isContainJSFContextPath(sourceNode)) { - rst = true; - } - if (Jsf2ResourceUtil.isContainJSF2ResourceAttributes(sourceNode)) { - // added by Maksim Areshkau, see JBIDE-4812 - rst = true; - } - return rst; + public boolean isELNode(Node sourceNode) { + return getElResolver().isELNode(sourceNode); } - /** - * Checks is node exist in source custom element attribute map and if so, then retrun true - * - * @param pageContext - * @param sourceNode - * @return - */ - private static boolean isInCustomElementsAttributes(VpePageContext pageContext, Node sourceNode) { - boolean res = false; - String textValue = null; - if (sourceNode.getNodeType() == Node.TEXT_NODE) { - textValue = sourceNode.getNodeValue(); - res = isInCustomElementsAttributes(pageContext, textValue); - } else if (sourceNode.getNodeType() == Node.ELEMENT_NODE) { - NamedNodeMap attributesMap = sourceNode.getAttributes(); - for (int i = 0; !res && i < attributesMap.getLength(); i++) { - Attr attr = (Attr) attributesMap.item(i); - textValue = attr.getValue(); - res = isInCustomElementsAttributes(pageContext, textValue); - } - } - return res; + public ResourceReference[] getResourceReferences() { + return getElResolver().getResourceReferences(); } - private static boolean isInCustomElementsAttributes(VpePageContext pageContext, String textValue) { - boolean res = false; - if (textValue != null) { - for (String key : pageContext.getCustomElementsAttributes().keySet()) { - if (equalsExppression(textValue, key)) { - res = true; - break; - } - } - } - return res; + public String replaceElAndResources(String value) { + return getElResolver().replaceElAndResources(value); } - - /** - * Checks if is available for node. - * - * @param resourceFile - * the resource file - * @param sourceNode - * the source node - * - * @return true, if is available for node - */ - private static boolean isAvailableForNode(VpePageContext pageContext, Node sourceNode) { - boolean rst = findForNode(pageContext, sourceNode); - return rst; - } - - /** - * @param sourceNode - * @param resourceFile - * @return - */ - private static boolean findForNode(VpePageContext pageContext, Node sourceNode) { - final ResourceReference[] references = getResourceReferences(pageContext); - if (references == null || references.length == 0) { - return false; - } - String textValue = null; - if (sourceNode.getNodeType() == Node.TEXT_NODE) { - textValue = sourceNode.getNodeValue(); - if (textValue != null && isInReferenceResourcesList(references, textValue)) { - return true; - } - } - final NamedNodeMap nodeMap = sourceNode.getAttributes(); - if (nodeMap != null) { - for (int i = 0; i < nodeMap.getLength(); i++) { - if (isInReferenceResourcesList(references, ((Attr) nodeMap.item(i)).getValue())) { - return true; - } - } - } - return false; - } - public static ResourceReference[] getResourceReferences(VpePageContext pageContext) { - final ResourceReference[] res = (ResourceReference[])pageContext.getValue(VpePageContext.RES_REFERENCES); - return res; + private ELResolver getElResolver() { + return elResolver; } - - /** - * Checks if is in reference resources list. - * - * @param value - * the value - * @param references - * the references - * - * @return true, if is in reference resources list - */ - private static boolean isInReferenceResourcesList(ResourceReference[] references, String value) { - for (ResourceReference ref : references) { - // FIXED FOR JBIDE-3149 by sdzmitrovich - if (equalsExppression(value, ref.getLocation())) { - return true; - } - } - return false; + public void setElResolver(ELResolver elResolver) { + this.elResolver = elResolver; } - public static ResourceReference[] getAllResources(IFile resourceFile) { - final IPath workspacePath = Platform.getLocation(); - final ResourceReference[] gResources = GlobalELReferenceList.getInstance().getAllResources(workspacePath); - final ResourceReference[] elResources = ELReferenceList.getInstance().getAllResources(resourceFile); - - int size = (gResources == null ? 0 : gResources.length); - size += (elResources == null ? 0 : elResources.length); - ResourceReference[] rst = new ResourceReference[size]; - - if ((gResources != null) && (gResources.length > 0)) { - System.arraycopy(gResources, 0, rst, 0, gResources.length); - } - if ((elResources != null) && (elResources.length > 0)) { - System.arraycopy(elResources, 0, rst, gResources == null ? 0 : gResources.length, elResources.length); - } - return rst; - - } - - public static String replaceElAndResources(VpePageContext pageContext, String value) { - String rst = value; - rst = ResourceUtil.getBundleValue(pageContext, value); - // replace custom attributes - rst = replaceCustomAttributes(pageContext, rst); - if (Jsf2ResourceUtil.isExternalContextPathString(value)) { - rst = Jsf2ResourceUtil.processExternalContextPath(value); - } - if (Jsf2ResourceUtil.isRequestContextPathString(value)) { - rst = Jsf2ResourceUtil.processRequestContextPath(value); - } - if (Jsf2ResourceUtil.isJSF2ResourceString(rst)) { - rst = Jsf2ResourceUtil.processCustomJSFAttributes(pageContext, rst); - } - rst = replaceEl(pageContext, rst); - return rst; - } - - private static boolean equalsExppression(String value, String expression) { - final String dollarEl = DOLLAR_PREFIX + expression + SUFFIX; - final String sharpEl = SHARP_PREFIX + expression + SUFFIX; - if (value.contains(dollarEl) || value.contains(sharpEl)) { - return true; - } - return false; - } - } Index: src/org/jboss/tools/vpe/editor/context/VpePageContext.java =================================================================== --- src/org/jboss/tools/vpe/editor/context/VpePageContext.java (revision 33486) +++ src/org/jboss/tools/vpe/editor/context/VpePageContext.java (working copy) @@ -12,8 +12,10 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.eclipse.core.resources.IFile; import org.eclipse.swt.widgets.Display; @@ -31,6 +33,7 @@ import org.jboss.tools.vpe.editor.VpeVisualDomBuilder; import org.jboss.tools.vpe.editor.mapping.VpeDomMapping; import org.jboss.tools.vpe.editor.util.ElService; +import org.jboss.tools.vpe.editor.util.ElServiceUtil; import org.jboss.tools.vpe.editor.util.XmlUtil; import org.jboss.tools.vpe.resref.core.AbsoluteFolderReferenceList; import org.jboss.tools.vpe.resref.core.RelativeFolderReferenceList; @@ -48,6 +51,7 @@ public static final String CUSTOM_ELEMENTS_ATTRS = "customElementsAttributes"; //$NON-NLS-1$ public static final String CURRENT_VISUAL_NODE = "currentVisualNode"; //$NON-NLS-1$ public static final String RES_REFERENCES = "resourceReferences"; //$NON-NLS-1$ + public static final String EL_EXPR_SERVICE = "elExprService"; //$NON-NLS-1$ private BundleMap bundle; private VpeSourceDomBuilder sourceBuilder; @@ -58,7 +62,13 @@ public VpePageContext(BundleMap bundle, VpeEditorPart editPart) { this.bundle = bundle; this.editPart = editPart; + resetElService(); } + + public void resetElService() { + // set default el expression service + setElService(new ElService(this)); + } public VpeSourceDomBuilder getSourceBuilder() { return sourceBuilder; @@ -81,6 +91,26 @@ getCustomElementsAttributes().put(key, value); } + public void removerAttributeFromCustomElementsMap(String key) { + getCustomElementsAttributes().remove(key); + } + + public boolean containsAttributeInCustomElementsMap(String key) { + return getCustomElementsAttributes().containsKey(key); + } + + public String getAttributefromCustomElementsMapValue(String key) { + return getCustomElementsAttributes().get(key); + } + + public Set getKeysCustomElementsAttributes() { + final Set res = new HashSet(); + for (String key : getCustomElementsAttributes().keySet()) { + res.add(key); + } + return res; + } + /** * Removes attribute from custom attribute map * @@ -112,8 +142,8 @@ final VpeIncludeInfo vii = visualBuilder.getCurrentIncludeInfo(); if (vii != null && (vii.getStorage() instanceof IFile)) { final IFile file = (IFile)vii.getStorage(); - ResourceReference[] rr = ElService.getAllResources(file); - rr = ElService.sortReferencesByScope2(rr); + ResourceReference[] rr = ElServiceUtil.getAllResources(file); + rr = ElServiceUtil.sortReferencesByScope2(rr); putValue(RES_REFERENCES, rr); } } @@ -254,6 +284,21 @@ } /** + * @return the elService + */ + public ElService getElService() { + return (ElService)getValue(EL_EXPR_SERVICE); + } + + /** + * @param elService + * the elService to set + */ + public void setElService(ElService elService) { + putValue(EL_EXPR_SERVICE, elService); + } + + /** * Processes display events to prevent eclipse froze */ public static void processDisplayEvents() { @@ -271,7 +316,7 @@ * @return the customElementsAttributes */ @SuppressWarnings("unchecked") - public Map getCustomElementsAttributes() { + protected Map getCustomElementsAttributes() { Map res = null; Object obj = values.get(CUSTOM_ELEMENTS_ATTRS); if (obj == null || !(obj instanceof HashMap)) { Index: src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java =================================================================== --- src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java (revision 33486) +++ src/org/jboss/tools/vpe/editor/VpeVisualDomBuilder.java (working copy) @@ -56,11 +56,9 @@ import org.jboss.tools.vpe.editor.template.VpeToggableTemplate; import org.jboss.tools.vpe.editor.template.expression.VpeExpressionException; import org.jboss.tools.vpe.editor.util.Docbook; -import org.jboss.tools.vpe.editor.util.ElService; import org.jboss.tools.vpe.editor.util.FaceletUtil; import org.jboss.tools.vpe.editor.util.HTML; import org.jboss.tools.vpe.editor.util.TextUtil; -import org.jboss.tools.vpe.editor.util.VisualDomUtil; import org.jboss.tools.vpe.editor.util.VpeStyleUtil; import org.jboss.tools.vpe.editor.util.XmlUtil; import org.jboss.tools.vpe.resref.core.CSSReferenceList; @@ -68,9 +66,6 @@ import org.mozilla.interfaces.nsIDOMAttr; import org.mozilla.interfaces.nsIDOMDocument; import org.mozilla.interfaces.nsIDOMElement; -import org.mozilla.interfaces.nsIDOMEvent; -import org.mozilla.interfaces.nsIDOMEventTarget; -import org.mozilla.interfaces.nsIDOMNSEvent; import org.mozilla.interfaces.nsIDOMNode; import org.mozilla.interfaces.nsIDOMNodeList; import org.mozilla.interfaces.nsIDOMText; @@ -226,6 +221,7 @@ super.dispose(); pageContext.clearAll(); + pageContext.resetElService(); // FIXED FOR JBIDE-3799 by sdzmitrovich, moved calling of this method to buid dom // refreshExternalLinks(); pageContext.getBundle().refreshRegisteredBundles(); @@ -365,7 +361,7 @@ Node sourceNodeProxy = null; // FIX FOR JBIDE-1568, added by Max Areshkau try { - if (ElService.isELNode(getPageContext(), sourceNode)) { + if (getPageContext().getElService().isELNode(sourceNode)) { sourceNodeProxy = VpeProxyUtil.createProxyForELExpressionNode( getPageContext(), sourceNode); try { Index: src/org/jboss/tools/vpe/editor/VpePreviewDomBuilder.java =================================================================== --- src/org/jboss/tools/vpe/editor/VpePreviewDomBuilder.java (revision 33486) +++ src/org/jboss/tools/vpe/editor/VpePreviewDomBuilder.java (working copy) @@ -25,7 +25,6 @@ import org.jboss.tools.vpe.editor.template.VpeCreationData; import org.jboss.tools.vpe.editor.template.VpeTemplate; import org.jboss.tools.vpe.editor.template.VpeTemplateManager; -import org.jboss.tools.vpe.editor.util.ElService; import org.mozilla.interfaces.nsIDOMElement; import org.mozilla.interfaces.nsIDOMNode; import org.mozilla.xpcom.XPCOMException; @@ -74,7 +73,7 @@ //FIX FOR JBIDE-1568, added by Max Areshkau try { - if (ElService.isELNode(getPageContext(), sourceNode)) { + if (getPageContext().getElService().isELNode(sourceNode)) { final Node sourceNodeProxy = VpeProxyUtil.createProxyForELExpressionNode(getPageContext(), sourceNode); try { Index: src/org/jboss/tools/vpe/editor/proxy/VpeNodeInvocationHandler.java =================================================================== --- src/org/jboss/tools/vpe/editor/proxy/VpeNodeInvocationHandler.java (revision 33486) +++ src/org/jboss/tools/vpe/editor/proxy/VpeNodeInvocationHandler.java (working copy) @@ -13,7 +13,6 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import org.jboss.tools.vpe.editor.context.VpePageContext; -import org.jboss.tools.vpe.editor.util.ElService; import org.w3c.dom.Attr; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; @@ -59,7 +58,7 @@ * @return */ private String replaceEL(String toReplace) { - return ElService.replaceElAndResources(pageContext, toReplace); + return pageContext.getElService().replaceElAndResources(toReplace); } } Index: src/org/jboss/tools/vpe/editor/util/SourceDomUtil.java =================================================================== --- src/org/jboss/tools/vpe/editor/util/SourceDomUtil.java (revision 33486) +++ src/org/jboss/tools/vpe/editor/util/SourceDomUtil.java (working copy) @@ -80,7 +80,7 @@ final String attrName = "rendered"; //$NON-NLS-1$ Element tempElement = sourceNode; if (sourceNode.hasAttribute(attrName)) { - if (ElService.isELNode(pageContext, sourceNode)) { + if (pageContext.getElService().isELNode(sourceNode)) { tempElement = (Element) VpeProxyUtil .createProxyForELExpressionNode(pageContext, sourceNode); } @@ -117,7 +117,7 @@ if (node instanceof Element) { String templateName = VpeTemplateManager.getInstance() .getTemplateName(pageContext, node); - return "f:facet".equals(templateName); + return "f:facet".equals(templateName); //$NON-NLS-1$ } else { return false; } Index: src/org/jboss/tools/vpe/editor/util/ELResolver.java =================================================================== --- src/org/jboss/tools/vpe/editor/util/ELResolver.java (revision 0) +++ src/org/jboss/tools/vpe/editor/util/ELResolver.java (revision 0) @@ -0,0 +1,201 @@ +/** + * + */ +package org.jboss.tools.vpe.editor.util; + +import org.eclipse.core.resources.IFile; +import org.jboss.tools.common.resref.core.ResourceReference; +import org.jboss.tools.jst.jsp.bundle.BundleMapUtil; +import org.jboss.tools.vpe.editor.context.VpePageContext; +import org.w3c.dom.Attr; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; + +/** + * Class which for resolve EL Expressions + * @author mareshkau + * + */ +public class ELResolver { + + protected VpePageContext pageContext; + + public ELResolver(VpePageContext pageContext) { + this.pageContext = pageContext; + } + + /** + * Replace el. + * + * @param pageContext + * @param resourceString + * the resource string + * + * @return the string + * + * @see IELService#replaceEl(IFile, String) + */ + public String replaceEl(String resourceString) { + if (resourceString == null) { + return ""; //$NON-NLS-1$ + } + String rst = resourceString; + final ResourceReference[] references = getResourceReferences(); + if (references == null || references.length == 0) { + return rst; + } + rst = ElServiceUtil.replace(resourceString, references); + return rst; + } + + protected String replaceCustomAttributes(String value) { + String result = value; + final int nPrefLen = ElServiceUtil.getPrefLen(); // for small-simple optimization + for (String el : pageContext.getKeysCustomElementsAttributes()) { + if (el.length() + nPrefLen > result.length()) { + continue; // no sense for sequence contains checks... + } + final String dollarEl = ElServiceUtil.envelopeInDollarEl(el); + final String sharpEl = ElServiceUtil.envelopeInSharpEl(el); + + if (result.contains(dollarEl)) { + result = result.replace(dollarEl, pageContext.getAttributefromCustomElementsMapValue(el)); + } + if (result.contains(sharpEl)) { + result = result.replace(sharpEl, pageContext.getAttributefromCustomElementsMapValue(el)); + } + } + return result; + } + + /** + * Checks if is cloneable node. + * + * @param sourceNode + * the source node + * @param pageContext + * the page context + * + * @return true, if is cloneable node + */ + public boolean isELNode(Node sourceNode) { + boolean rst = false; + if (isInCustomElementsAttributes(sourceNode)) { + return true; + } + if (isAvailableForNode(sourceNode) + || BundleMapUtil.isInResourcesBundle(pageContext.getBundle(), sourceNode)) { + rst = true; + } else if (Jsf2ResourceUtil.isContainJSFContextPath(sourceNode)) { + rst = true; + } + if (Jsf2ResourceUtil.isContainJSF2ResourceAttributes(sourceNode)) { + // added by Maksim Areshkau, see JBIDE-4812 + rst = true; + } + return rst; + } + + /** + * Checks is node exist in source custom element attribute map and if so, then retrun true + * + * @param pageContext + * @param sourceNode + * @return + */ + protected boolean isInCustomElementsAttributes(Node sourceNode) { + boolean res = false; + String textValue = null; + if (sourceNode.getNodeType() == Node.TEXT_NODE) { + textValue = sourceNode.getNodeValue(); + res = isInCustomElementsAttributes(textValue); + } else if (sourceNode.getNodeType() == Node.ELEMENT_NODE) { + NamedNodeMap attributesMap = sourceNode.getAttributes(); + for (int i = 0; !res && i < attributesMap.getLength(); i++) { + Attr attr = (Attr) attributesMap.item(i); + textValue = attr.getValue(); + res = isInCustomElementsAttributes(textValue); + } + } + return res; + } + + protected boolean isInCustomElementsAttributes(String textValue) { + boolean res = false; + if (textValue != null) { + for (String key : pageContext.getKeysCustomElementsAttributes()) { + if (ElServiceUtil.equalsExppression(textValue, key)) { + res = true; + break; + } + } + } + return res; + } + + /** + * Checks if is available for node. + * + * @param resourceFile + * the resource file + * @param sourceNode + * the source node + * + * @return true, if is available for node + */ + protected boolean isAvailableForNode(Node sourceNode) { + boolean rst = findForNode(sourceNode); + return rst; + } + + /** + * @param sourceNode + * @param resourceFile + * @return + */ + protected boolean findForNode(Node sourceNode) { + final ResourceReference[] references = getResourceReferences(); + if (references == null || references.length == 0) { + return false; + } + String textValue = null; + if (sourceNode.getNodeType() == Node.TEXT_NODE) { + textValue = sourceNode.getNodeValue(); + if (textValue != null && ElServiceUtil.isInReferenceResourcesList(references, textValue)) { + return true; + } + } + final NamedNodeMap nodeMap = sourceNode.getAttributes(); + if (nodeMap != null) { + for (int i = 0; i < nodeMap.getLength(); i++) { + if (ElServiceUtil.isInReferenceResourcesList(references, ((Attr) nodeMap.item(i)).getValue())) { + return true; + } + } + } + return false; + } + + public ResourceReference[] getResourceReferences() { + final ResourceReference[] res = (ResourceReference[])pageContext.getValue(VpePageContext.RES_REFERENCES); + return res; + } + + public String replaceElAndResources(String value) { + String rst = value; + rst = ResourceUtil.getBundleValue(pageContext, value); + // replace custom attributes + rst = replaceCustomAttributes(rst); + if (Jsf2ResourceUtil.isExternalContextPathString(value)) { + rst = Jsf2ResourceUtil.processExternalContextPath(value); + } + if (Jsf2ResourceUtil.isRequestContextPathString(value)) { + rst = Jsf2ResourceUtil.processRequestContextPath(value); + } + if (Jsf2ResourceUtil.isJSF2ResourceString(rst)) { + rst = Jsf2ResourceUtil.processCustomJSFAttributes(pageContext, rst); + } + rst = replaceEl(rst); + return rst; + } +} Index: src/org/jboss/tools/vpe/editor/util/VpeStyleUtil.java =================================================================== --- src/org/jboss/tools/vpe/editor/util/VpeStyleUtil.java (revision 33486) +++ src/org/jboss/tools/vpe/editor/util/VpeStyleUtil.java (working copy) @@ -777,7 +777,7 @@ String resolvedUrl = url.replaceFirst( "^\\s*(\\#|\\$)\\{facesContext.externalContext.requestContextPath\\}", Constants.EMPTY); //$NON-NLS-1$ - resolvedUrl = ElService.replaceEl(file, resolvedUrl); + resolvedUrl = ElServiceUtil.replaceEl(file, resolvedUrl); URI uri = null; try { Index: src/org/jboss/tools/vpe/editor/util/ElServiceUtil.java =================================================================== --- src/org/jboss/tools/vpe/editor/util/ElServiceUtil.java (revision 0) +++ src/org/jboss/tools/vpe/editor/util/ElServiceUtil.java (revision 0) @@ -0,0 +1,168 @@ +package org.jboss.tools.vpe.editor.util; + +import java.util.Arrays; +import java.util.Comparator; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Platform; +import org.jboss.tools.common.el.core.ELReferenceList; +import org.jboss.tools.common.el.core.GlobalELReferenceList; +import org.jboss.tools.common.resref.core.ResourceReference; + +public class ElServiceUtil { + + public static final String DOLLAR_PREFIX = "${"; //$NON-NLS-1$ + + public static final String SUFFIX = "}"; //$NON-NLS-1$ + + public static final String SHARP_PREFIX = "#{"; //$NON-NLS-1$ + + /** + * Replace el. + * + * @param resourceFile + * the resource file + * @param resourceString + * the resource string + * + * @return the string + * + * @see IELService#replaceEl(IFile, String) + */ + public static String replaceEl(IFile file, String resourceString) { + if (resourceString == null) { + return ""; //$NON-NLS-1$ + } + String rst = resourceString; + ResourceReference[] references = getAllResources(file); + if (references == null || references.length == 0) { + return rst; + } + references = sortReferencesByScope2(references); + rst = replace(resourceString, references); + return rst; + } + + /** + * Creates a copy of {@code references} array and sorts its elements by scope value. + * + * References with the lowest scope value ({@link ResourceReference#FILE_SCOPE}) become the first in the array and + * so on. + * + * @param references + * array to be sorted + * @return sorted copy of {@code references} + * @author yradtsevich + */ + public static ResourceReference[] sortReferencesByScope(ResourceReference[] references) { + ResourceReference[] sortedReferences = references.clone(); + return sortReferencesByScope(sortedReferences); + } + + public static ResourceReference[] sortReferencesByScope2(ResourceReference[] references) { + Arrays.sort(references, new Comparator() { + public int compare(ResourceReference r1, ResourceReference r2) { + return r1.getScope() - r2.getScope(); + } + }); + return references; + } + + public static ResourceReference[] getAllResources(IFile resourceFile) { + final IPath workspacePath = Platform.getLocation(); + final ResourceReference[] gResources = GlobalELReferenceList.getInstance().getAllResources(workspacePath); + final ResourceReference[] elResources = ELReferenceList.getInstance().getAllResources(resourceFile); + + int size = (gResources == null ? 0 : gResources.length); + size += (elResources == null ? 0 : elResources.length); + ResourceReference[] rst = new ResourceReference[size]; + + if ((gResources != null) && (gResources.length > 0)) { + System.arraycopy(gResources, 0, rst, 0, gResources.length); + } + if ((elResources != null) && (elResources.length > 0)) { + System.arraycopy(elResources, 0, rst, gResources == null ? 0 : gResources.length, elResources.length); + } + return rst; + + } + + /** + * Replace. + * + * @param resourceString + * the resource string + * @param references + * the references + * + * @return the string + */ + public static String replace(String resourceString, ResourceReference[] sortedReferences) { + String result = resourceString; + // Values with higher precedence should be replaced in the first place. + // Expect sorted by scope references. + final int nPrefLen = getPrefLen(); // for small-simple optimization + for (ResourceReference rf : sortedReferences) { + final String tmp = rf.getLocation(); + if (tmp.length() + nPrefLen > resourceString.length()) { + continue; // no sense for sequence contains checks... + } + final String dollarEl = envelopeInDollarEl(tmp); + final String sharpEl = envelopeInSharpEl(tmp); + if (resourceString.contains(dollarEl)) { + result = result.replace(dollarEl, rf.getProperties()); + } + if (resourceString.contains(sharpEl)) { + result = result.replace(sharpEl, rf.getProperties()); + } + } + return result; + } + + public static int getPrefLen() { + int nPrefLen = DOLLAR_PREFIX.length(); + if (nPrefLen > SHARP_PREFIX.length()) { + nPrefLen = SHARP_PREFIX.length(); + } + nPrefLen += SUFFIX.length(); + return nPrefLen; + } + + public static boolean equalsExppression(String value, String expression) { + final String dollarEl = envelopeInDollarEl(expression); + final String sharpEl = envelopeInSharpEl(expression); + if (value.contains(dollarEl) || value.contains(sharpEl)) { + return true; + } + return false; + } + + /** + * Checks if is in reference resources list. + * + * @param value + * the value + * @param references + * the references + * + * @return true, if is in reference resources list + */ + public static boolean isInReferenceResourcesList(ResourceReference[] references, String value) { + for (ResourceReference ref : references) { + // FIXED FOR JBIDE-3149 by sdzmitrovich + if (equalsExppression(value, ref.getLocation())) { + return true; + } + } + return false; + } + + public static String envelopeInDollarEl(String str) { + return DOLLAR_PREFIX + str + SUFFIX; + } + + public static String envelopeInSharpEl(String str) { + return SHARP_PREFIX + str + SUFFIX; + } +} #P org.jboss.tools.jsf.vpe.jsf.test Index: src/org/jboss/tools/jsf/vpe/jsf/test/ElPreferencesTestCase.java =================================================================== --- src/org/jboss/tools/jsf/vpe/jsf/test/ElPreferencesTestCase.java (revision 33408) +++ src/org/jboss/tools/jsf/vpe/jsf/test/ElPreferencesTestCase.java (working copy) @@ -13,7 +13,7 @@ import org.eclipse.core.runtime.CoreException; -import org.jboss.tools.vpe.editor.util.ElService; +import org.jboss.tools.vpe.editor.util.ElServiceUtil; /** *

@@ -42,7 +42,7 @@ */ public void testReplaceAttributeValue() throws CoreException { String string1 = "#{beanA.property1}/images/smalle.gif"; //$NON-NLS-1$ - String replacedValue = ElService.replaceEl(file, string1); + String replacedValue = ElServiceUtil.replaceEl(file, string1); assertEquals("Should be equals " + elValuesMap.get(KEY_1) + "/images/smalle.gif", replacedValue, elValuesMap.get(KEY_1) //$NON-NLS-1$ //$NON-NLS-2$ + "/images/smalle.gif"); //$NON-NLS-1$ @@ -57,7 +57,7 @@ public void testReplaceAttributeValue2() throws CoreException { String string1 = "#{beanA.property1}/images/#{beanA.property2}/path2/#{facesContext.requestPath}/smalle.gif"; //$NON-NLS-1$ - final String replacedValue = ElService.replaceEl(file, string1); + final String replacedValue = ElServiceUtil.replaceEl(file, string1); final String check = elValuesMap.get(KEY_1) + "/images/" + elValuesMap.get(KEY_2) + "/path2/" + elValuesMap.get(KEY_3) //$NON-NLS-1$ //$NON-NLS-2$ + "/smalle.gif"; //$NON-NLS-1$ assertEquals("Should be equals " + check, check, replacedValue); //$NON-NLS-1$ @@ -70,6 +70,6 @@ public void testReplaceNotInSet() { String string1 = "#{requestScope}/smalle.gif"; //$NON-NLS-1$ - assertEquals("Should be equals", string1, ElService.replaceEl(file, string1)); //$NON-NLS-1$ + assertEquals("Should be equals", string1, ElServiceUtil.replaceEl(file, string1)); //$NON-NLS-1$ } } Index: src/org/jboss/tools/jsf/vpe/jsf/test/jbide/JBIDE2594Test.java =================================================================== --- src/org/jboss/tools/jsf/vpe/jsf/test/jbide/JBIDE2594Test.java (revision 33408) +++ src/org/jboss/tools/jsf/vpe/jsf/test/jbide/JBIDE2594Test.java (working copy) @@ -21,7 +21,7 @@ import org.jboss.tools.common.el.core.GlobalELReferenceList; import org.jboss.tools.common.resref.core.ResourceReference; import org.jboss.tools.jsf.vpe.jsf.test.CommonJBIDE2010Test; -import org.jboss.tools.vpe.editor.util.ElService; +import org.jboss.tools.vpe.editor.util.ElServiceUtil; /** * Test case for testing global El expression substitution @@ -67,7 +67,7 @@ public void testReplaceGlobalElVariable(){ String replaceString= "#{"+KEY_6+"}"+"images/test.gif"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - String replacedString = ElService.replaceEl(file, replaceString); + String replacedString = ElServiceUtil.replaceEl(file, replaceString); assertEquals("Should be equals " + globalElMap.get(KEY_6) + "images/test.gif", replacedString, globalElMap.get(KEY_6) //$NON-NLS-1$ //$NON-NLS-2$ + "images/test.gif"); //$NON-NLS-1$ @@ -80,7 +80,7 @@ */ public void testOverrideLocalVariable() throws CoreException { String string1 = "${" + KEY_1 + "}" + KEY_1_POSTFIX; //$NON-NLS-1$ //$NON-NLS-2$ - String replacedValue = ElService.replaceEl(file, string1); + String replacedValue = ElServiceUtil.replaceEl(file, string1); assertEquals(elValuesMap.get(KEY_1) + KEY_1_POSTFIX, replacedValue); }