7

I'm trying to get the URLs for images (all MIME types) in a remote CSS file using Java.

I am using jsoup to get the URL of the css.

After countless hours of looking at CSS Parser I couldn't figure it out due to the lack of documentation.

I also looked at some other treads, but have just confused me even more:

I've also seen some examples using regex, but I am not too familiar how to implement it in java.

Would anyone have some suggestions on how to go at this problem?

Community
  • 1
  • 1
pbojinov
  • 907
  • 9
  • 17
  • Try [CSS Parser](http://cssparser.sourceforge.net/) – Michael Mior Nov 21 '11 at 06:12
  • Do you need to follow recursive references to other CSS files? You could use a regular expression to find all url() occurrences. – Sam Barnum Nov 21 '11 at 06:16
  • Yes, I eventually need to get references to other CSS files. What regex would find all url() occurrences? – pbojinov Nov 21 '11 at 06:20
  • I've actually managed to get the contents of a CSS file using simple java URL code, so what would be the next step in matching all .jpg, .gif, .png, and other possible MIME inside the CSS file – pbojinov Nov 21 '11 at 06:54
  • ([^\s]+(\.(?i)(jpg|png|gif|bmp))$) works, now just need java implementation to pass it the css file as a String and find all URLS of images – pbojinov Nov 21 '11 at 07:32

2 Answers2

6

In Java, you have to use a Pattern and a Matcher from the java.util.regex package.

You compile your pattern, then you instantiate your matcher with your string and then you look for everything that matches your pattern.

Pattern p = Pattern.compile("...");
Matcher m = p.matcher("your CSS file as a String");
while (m.find()) {
  // Here use m.group(), m.group(1), ...
}

The CSS 2.1 spec states:

The format of a URI value is 'url(' followed by optional white space followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional white space followed by ')'. The two quote characters must be the same.

Thus you could use a regex like this one:

url\(\s*(['"]?+)(.*?)\1\s*\)

The .*? is non-greedy allowing you to take as few characters as necessary. The possessive quantifier avoids any backtrack in ['"]?+.

Ludovic Kuty
  • 4,868
  • 3
  • 28
  • 42
  • very nice, you nailed it right on. the code I wrote is almost the same except for the regex, which i'm about to test right now. Just wanted to clarify that it will match everything between the '' inside the parentheses correct? `url('domain/link/images/graphic.png');` would return `domain.../graphic.png` – pbojinov Nov 24 '11 at 20:20
  • Yes, it will return it in the second matching group. – Ludovic Kuty Nov 25 '11 at 08:58
0

You may also use ph-css for this. See the example "Visit all URLs contained in a CSS" located at https://github.com/phax/ph-css#code-examples. Can't do it much easier :)

Philip Helger
  • 1,814
  • 18
  • 28
  • Hi, how can I visit only the URLs of images and not all the URLs? – Idan Aug 17 '13 at 18:53
  • This is not easily possible because for the parser a URL is a URL - maybe you can decide upon the suffix of the URL. If it ends with ".jpg" or ".gif" than it is an image... – Philip Helger Aug 19 '13 at 06:50
  • Alternatively you can check if `declaration.getProperty ().equals ("background-image")` etc. (declaration is the second parameter of `onUrlDeclaration`) – Philip Helger Aug 19 '13 at 08:54