Getting an EL in a Javascript file, loaded by #ResourceDependency - java

I'm using #ResourceDependency annotation in a JSF component to add Javascript and CSS files into my JSF page.
In my Javascript file, I need to reference another resource file (a .swf file, which located in META-INF/resources, as JSF requires). I tried to put a #{resource['swf:file.swf']} EL expression in my javascript code, but it won't get resolved.
For exapmle, for the following JS file in the server:
var instance = new JSClass();
instance.setResourceUrl("#{resource['swf:file.swf']}");
The browser gets:
var instance = new JSClass();
instance.setResourceUrl("#{resource['swf:file.swf']}");
which is wrong.
While when I put the same EL in the CSS file, it get resolved properly. For the following CSS file in the server:
.instance-css-class {
background: url("#{resource['swf:file.swf']}")
}
The browser gets:
.instance-css-class {
background: url("/webapp/javax.faces.resource/file.swf.jsf?ln=swf")
}
Which is exactly what I need, but in the JS file.
Obviously, I can use the CSS as a workaround for the issue (Create a DOM element, attach the CSS class to it, and then read and parse the required style property). But, is there a more elegant way to achive it? Is it a bug in the JSF library (I'm using Mojarra 2.0.3, with Jboss 6.1), or there is a reason for that behavior?
Please mind that the code above is part of a tag library, so workarounds such as those can't be used.
Edit - Seems that the CSS workaround is not feasible, since I can't see a way to get CSS attribute from a CSS file. So any (working) workaround would be gladly accepted as well.

BalusC has answered a similar question in detail here. Though the first solution is the easiest to implement, I would recommend you to go with the third, which in my opinion, is the correct way.

Related

Need to get all Stylesheets from a website by URL - JAVA

Hi guys I need for my project a CSS parser. I just look around and I find always the same libraries. But they don't have the implementation for obtaining the CSS file or code from a website. All of them need local files that I don't have because I want to analyze for example Facebook or Twitter etc.
Is there a library for this purpose?
I already found libraries like -> CSSPARSER, SAC
EDIT:
Found a solution for my problem. First of all, I use JSOUP to find out which link belongs to the CSS stylesheet. Then I filter it and save the URL to the CSS file into a string variable. After that, I download this file by the given name and put this file into the CSS parser and now its works.

Make modified JS and CSS file be fetched by browsers

I want to make the js and css files which are modified are to be downloaded at the client end when a page is accessed. I have these approaches
Manually add the modified timestamp the URL in each page.
I was thinking of writing a scriptlet code in all the jsp pages which will read all the js and css files modified timestamp and append it to the url in the page.
Add the modified timestamp while building the war file using ANT.
I have following questions.
Can any one let me know which would be a better solution of the above approaches? I am open to any other solutions also.
I went through this answer on SO and using it I can get the modified date but how to change the jsp file?
Is there anything similar to this in java?
In this situation better or best solution is took shape according to your exact requirements. I might derive simple questions like; Will your static resources in same server or included in your app in same server etc.. May be some other better ways..
I don't have ant experience so I can't talk about it now , but you can go with java way already.I want to share just idea/s. A filter(looks the .css or .js requests , gets resources and look resource lastmodified date or checksum return as version on response) or custom jsp tag will provide your requirements. Write a custom jsp tag <resource:static path="app.js"/> like that example. So it may look specific file's last modified date, assumed under the same document root, and it can produce <script type="text/javascript" src="app.js?version=8637"> like this result, so this result will bust the cache.

Does find element in selenium checkes if the tag is closed proeprly?

I am using selenium method find element to check if the tag exists or not. For ex-
driver.findElements(By.xpath("//meta[#name='keywords']");
I am not sure if the tag is not opened or closed properly will Selenium still find the element on the web page?
If your goal is to validate the HTML of the page, you can use the W3C Markup Validation Service. It is a SOAP-based service. Just add a reference to your project and build a request as the sample in the documentation- https://validator.w3.org/docs/api.html
If you want to manually check it, just paste the desired URL here: https://validator.w3.org/
The Selenium only runs element selector above DOM provided by the browser. And generally, the browser will create DOM (tree of elements) above almost any HTML - it is intentionally build to handle such cases, as not closed pair elements, wrongly formatted HTML etc. But the behavior of each browser could differ a bit.
So the answer is simply no. Selenium does not check for this, it will most probably find the element, because the browsers (which provide the data for the Selenium) have their heuristics how to parse HTML with even missing closing elements. So from Selenium point of view it could look like totally "valid" and unproblematic HTML.
To check HTML validity I suggest using different tools, like W3 validator or the HTML tidy project.
In short: maybe, but not guaranteed. Depends on the browser.
In long: selenium's findElements method and suchlink don't actually check the html - they check the browser's DOM. Now browsers are pretty good at figuring out badly constructed html (missing end tags and so on), so there's a chance that it will have figured out what is wrong with the tag and included it in the DOM. If so, then selenium will find it (in the DOM); but if the browser couldn't work out what the tag was trying to say and hasn't included it, then selenium won't find it.

Issue loading style sheet in JavaFX

I am developing an application on Linux using OpenJDK 8u20 and OpenJFX 8u5. I am basically trying to set a default style sheet for all scenes. There apparently isn't a a sanctioned way to do that, so the work around is to set the style sheet for each individual scene. The way to do this is "scene.getStylesheets().add(css)" where css is a String representing the location of the style sheet. That representation can be in three flavors: file, URL, or resource.
An example of the URL approach is:
String css = "http://localhost/file.css";
An example of the file approach is:
String css = "file://" + new File("file.css").getAbsolutePath().replace("\\", "/");
An example of the resource is:
String css = this.getClass().getResource("file.css").toString();
Of the three, only the URL approach appears to work as advertised.
The file approach appears to work initally, but subsequent compiles appears to break it. I can see that "scene.getStylesheets().add(css)" is being called with the correct value, but the application runs as if it never was. It only works after the compile that I edit the file that I am adding the stylesheet. If I edit any other file, compile, and run it does not work.
The resource approach just throws a runtime exception, namely "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)".
I have a work around for now by using the URL approach, but long term I would really like to be able to package that CSS file in the jar and use it from the jar. Does anybody have any ideas or suggestions?
OK, apparently I didn't understand how the resource approach is supposed to work sufficiently. I compile the class files in a temporary directory, and I was not also copying the resource files to that directory. Once I did that getResource starting working. So now both the URL and resource approaches are working for me.
If anybody wants to comment on the odd behavior of the file approach, it would be nice to know what's up with that.
There is a way of setting a default CSS style sheet for all of your scenes and you can accomplish that by invoking the static setUserAgentStyleSheet() method on the JavaFx Application.

Determine which CSS Framework is used with Selenium

I am writing a Programm which crawls through various Webpages and does some tests using Selenium.
Now I want to find out which CSS Frameworks are used on these Websites to get some statistics.
Right now I just check with the FireFox Webdriver if on there are .css Files linked in the page which have the name of a specific Framework:
Iterator<WebElement> divWebElementIteratorCSS = webDriver.findElements(By.xpath("//link[#rel='stylesheet']")).iterator();
and then I check if the name of the found .css Files contains the name of one of the CSS Frameworks I would like to check:
if ( src.contains( frameWorkName ) && cssFrameWorks.get( frameWorkName ) == false ) {
result.addAttribute("Framework", "STRING", frameWorkName);
result.setPercent( 100 );
result.setSuccessful( true );
cssFrameWorks.put( frameWorkName, true );
}
The Hashmap frameWorkName contains all the names of the Frameworks I am interested in.
Now my problem: If the administrator of the site renamed the .css file of the framework, my test does not work! Is there a safe way to check this, which works even if the .css has a different name?
I think #AaronDigulla's answer is pretty clear.
An alternative that I can think of though, is when you iterate through those, do a GET on that css file, then do a quick scan through the documentation at the beginning. For example, a CSS file might contain...
/* CSS Framework vX.X
* Author: Some Author
* License:
* Some ridiculously long license
*/
This would alleviate your file name changes issue.
I know no reliable way to determine which CSS (or JavaScript) frameworks are being used in a web site.
If you're lucky, then the admins will use a global URL (like the CDN links provided by jQuery).
When people start to rename files, you can try to download the CSS file and fingerprint it (create a checksum).
That will fail, of course, when people change those files. This can happen automatically; wro4j is a framework that will compile all JavaScript and CSS resources into one big file each automatically.
I'm also a bit worried why you would need this information. Instead of trying to figure out which framework is being used (and which version) look for the actual CSS styles that are being applied which might influence your tests.

Categories