CssTemplateProcessor implements ITemplateProcessor { public String handleToken(StringBuilder originalToken, List<IResource> dependencies, String cssPath) { if (originalToken == null || originalToken.length() == 0) { return ""; } StringBuilder token = originalToken; boolean isEl = originalToken.indexOf("${") == 0; String imageRelativePath = token.toString().trim(); if (isEl) { int endPos = imageRelativePath.lastIndexOf('}'); String suffix = null; String elPath = imageRelativePath; if (endPos != -1 && endPos != imageRelativePath.length() - 1) { suffix = imageRelativePath.substring(endPos + 1); elPath = imageRelativePath.substring(0, endPos + 1); } List<String> elKeys = ELHelper.getELKeys(elPath); if (elKeys.size() > 0 && elKeys.get(0).equals("res")) { IResource dependentResource = null; ResourceExpression expression = new ResBeanExpression(); boolean hasValue = false; for (int i = 1; i < elKeys.size(); i++) { String key = elKeys.get(i); Object value = expression.get(key); if (value instanceof ResourceExpression) { expression = (ResourceExpression) value; } else { if (value instanceof IResource) { dependentResource = (IResource) value; hasValue = true; } } } if (!hasValue) { dependentResource = (IResource) expression.evaluate(); } if (dependentResource != null) { if (!dependencies.contains(dependentResource)) { dependencies.add(dependentResource); } String url = dependentResource.getUrl(ResourceRuntimeContext.ctx().createResourceContext()); if (url != null) { return suffix != null ? url + suffix : url; } } } } else if (!imageRelativePath.startsWith("data:") && !imageRelativePath.startsWith("http")) { try { URI imgPath = (URI.create(cssPath).resolve(imageRelativePath)); IResource dependentResource = ResourceFactory.createResource(imgPath.getPath()); if (dependentResource != null) { if (!dependencies.contains(dependentResource)) { dependencies.add(dependentResource); } return dependentResource.getUrl(ResourceRuntimeContext.ctx().createResourceContext()); } } catch (Exception e) { s_logger.log( LogLevel.WARN, String.format("Failed to parse EL(%s) in css(%s), exception:%s", imageRelativePath, cssPath, e.toString())); } } return imageRelativePath; }  String handleToken(StringBuilder originalToken, List<IResource> dependencies, String cssPath); void parse(StringBuilder template, List<IResource> dependencies, String cssPath); @Override void process(StringBuilder template, IResource resource);  }
@Test public void testBackgroundEL() { CssTemplateProcessor processor = new CssTemplateProcessor(); StringBuilder originalToken = new StringBuilder( "${res.img.local.img.ebayLogo_gif}"); List<IResource> dependencies = new ArrayList<IResource>(); String actual = processor.handleToken(originalToken, dependencies, "/resoures"); Assert.assertEquals( "/BaseResourceTest/lrssvr/ry4sqfvidizw3iszva2fca1haul.gif", actual); Assert.assertEquals( "[Resource [m_urn=img.local:/img/ebayLogo.gif, m_libInfo=ResourceLibrary [m_id=webressdk, m_version=1.0.8, m_type=WAR]]]", dependencies.toString()); }
@Test public void testBackgroundELWithSpace() { CssTemplateProcessor processor = new CssTemplateProcessor(); StringBuilder originalToken = new StringBuilder( "${res.img.local.img.ebayLogo_gif }"); List<IResource> dependencies = new ArrayList<IResource>(); String actual = processor.handleToken(originalToken, dependencies, "/resoures"); Assert.assertEquals( "/BaseResourceTest/lrssvr/ry4sqfvidizw3iszva2fca1haul.gif", actual); Assert.assertEquals( "[Resource [m_urn=img.local:/img/ebayLogo.gif, m_libInfo=ResourceLibrary [m_id=webressdk, m_version=1.0.8, m_type=WAR]]]", dependencies.toString()); }
@Test public void testUnknownEL() { CssTemplateProcessor processor = new CssTemplateProcessor(); StringBuilder originalToken = new StringBuilder( "${res.img.local.img.ebayLogoxxxx_gif}"); List<IResource> dependencies = new ArrayList<IResource>(); String actual = processor.handleToken(originalToken, dependencies, "/resoures"); Assert.assertEquals("${res.img.local.img.ebayLogoxxxx_gif}", actual); Assert.assertEquals("[]", dependencies.toString()); originalToken.setLength(0); originalToken.append("${res.img.local.img.ebayLogoxxxx"); actual = processor.handleToken(originalToken, dependencies, "/resoures"); Assert.assertEquals("${res.img.local.img.ebayLogoxxxx", actual); }
@Test public void testBackgroundELExclude() { CssTemplateProcessor processor = new CssTemplateProcessor(); StringBuilder originalToken = new StringBuilder( "${res.img.local.img.ebayLogo_gif}?e"); List<IResource> dependencies = new ArrayList<IResource>(); String actual = processor.handleToken(originalToken, dependencies, "/resoures"); Assert.assertEquals( "/BaseResourceTest/lrssvr/ry4sqfvidizw3iszva2fca1haul.gif?e", actual); Assert.assertEquals( "[Resource [m_urn=img.local:/img/ebayLogo.gif, m_libInfo=ResourceLibrary [m_id=webressdk, m_version=1.0.8, m_type=WAR]]]", dependencies.toString()); }