使用java解析css文件 [英] Parsing a css file with java
问题描述
首先,我想解释我在做什么,然后我的问题。
我需要扫描一个css文件并获取它的所有内部链接(主要是图像),但我需要获取链接的行号。
First I want to explain what am I doing and then my problem. I need to scan a css file and obtain all its internal links(images mainly), but I need to get the line number where the links were found.
现在我使用flute库解析文件,它的工作非常好,我使用LineNumberReader为了获得发现链接的行号,但这个类抛出一个不正确的行号。
Right now I am parsing the files using flute library and it works very well also I am using LineNumberReader in order to obtain the line number where a link was found, but this class throws an incorrect line number.
例如:链接../../image/bg.gif在行号350,但LineNumberReader类中的getLineNumber方法说490。
For example: the link ../../image/bg.gif is in the line number 350 but the method getLineNumber in the class LineNumberReader says 490.
所以我会感激,如果你能用正确的方式驱动我,并给我一个可能的解释为什么LineNumberReader类。
So I will appreciate if some of you can drive me by the correct way and give me a possible explanation why the LineNumberReader class does it.
pd
- 对不起错误,英语不是我的母语。
推荐答案
Hi @eakbas和@Favonius感谢您的回答。
我终于得到了一个解决方案,也许这不是最好的,但至少对我有用。
正如我之前提到的,我使用flute库来实现包org.w3c.sac包的DocumentHandler类,以便分析css文件。
所以我实现了'property'方法,这个方法有3个参数,属性名,一个LexicalUnit对象和一个布尔值表示属性有重要的语句。
Hi @eakbas and @Favonius Thanks for your answer.
I finally got a solution, maybe it is not the best but at least works for me.
As I mentioned before I used the flute library to implement the DocumentHandler class of the package org.w3c.sac package in order to analyze the css file.
So I implemented the 'property' method, this method has 3 parameter, the property name, an LexicalUnit object and a boolean indicating that the property has the important statement or not.
public void property(String property, LexicalUnit lexicalUnit, boolean important)
由于我需要找到特定属性的行号,所以我进行搜索,我可以看到长笛用来实现LexicalUnit界面的类保存行号(它是LexicalUnitImp ),所以我使用反射从一个LexicalUnitImp对象转换到一个LexicalUnitImp对象。
As I need the line number where a specific property is found, I made a search and I could see that the class that flute uses to implement the LexicalUnit interface holds the line number(it is LexicalUnitImp), so I used reflexion to make a casting from LexicalUnit interface to one LexicalUnitImp object.
Class<?> clazz = ClassUtils.getClass("org.w3c.flute.parser.LexicalUnitImpl");
Object lexicalObject = clazz.cast(lexicalUnit);
Integer line = (Integer)MethodUtils.invokeMethod(lexicalObject, "getLineNumber", null, null);
我这样做是因为类LexicalUnitImpl是'protected',我不能把它传统方式。
I did it in that way because the class LexicalUnitImpl is 'protected' and I cannot cast it in a traditional way.
class LexicalUnitImpl implements LexicalUnit
注意:ClassUtils和MethodUtils类是commons-beanutils apache库的一部分。
Note: The class ClassUtils and MethodUtils are part of the commons-beanutils apache library.
这篇关于使用java解析css文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!