用java解析css文件
首先我想解释一下我在做什么,然后是我的问题。 我需要扫描 css 文件并获取其所有内部链接(主要是图像),但我需要获取找到链接的行号。
现在我正在使用长笛库解析文件,它工作得很好,我也使用 LineNumberReader 来获取找到链接的行号,但是这个类抛出了错误的行号。
例如:链接 ../../image/bg.gif 位于行号 350 中,但 LineNumberReader 类中的 getLineNumber 方法显示为 490。
因此,如果你们中的一些人能够以正确的方式引导我并给我,我将不胜感激我可能解释了为什么 LineNumberReader 类会这样做。
pd:另一个解决方案将非常感激。
- 抱歉可能有拼写错误,英语不是我的母语。
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.
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.
For example: the link ../../image/bg.gif is in the line number 350 but the method getLineNumber in the class LineNumberReader says 490.
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: another solution will be very appreciate.
- sorry the possibles typos, English is not my mother tongue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗨@eakbas 和@Favonius,感谢您的回答。
我终于找到了一个解决方案,也许它不是最好的,但至少对我有用。
正如我之前提到的,我使用flute库来实现包org.w3c.sac包的DocumentHandler类,以便分析css文件。
所以我实现了“property”方法,这个方法有3个参数,属性名称,一个LexicalUnit对象和一个布尔值,指示该属性是否有重要的语句。
因为我需要找到特定属性的行号,所以我进行了搜索,我可以看到笛子用来实现 LexicalUnit 接口的类保存了行号(它是 LexicalUnitImp),所以我使用反射来进行转换从 LexicalUnit 接口到一个 LexicalUnitImp 对象。
我这样做是因为类 LexicalUnitImpl 是“受保护的”,我无法以传统方式转换它。
注意:类 ClassUtils 和 MethodUtils 是 commons-beanutils apache 库的一部分。
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.
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.
I did it in that way because the class LexicalUnitImpl is 'protected' and I cannot cast it in a traditional way.
Note: The class ClassUtils and MethodUtils are part of the commons-beanutils apache library.
或者,您可以使用 ph-css 作为解析库。
请参阅示例“访问 CSS 中包含的所有 URL”,网址为 https://github.com /phax/ph-css#code-examples 有关如何提取 URL 并确定正确源位置的示例。
Alternatively you may use ph-css as a parsing library.
Please see the example "Visit all URLs contained in a CSS" at https://github.com/phax/ph-css#code-examples for an example of how to extract URLs and determine the correct source position.
另一个解决方案——
看看这些解析器生成工具...
JavaCC和Antlr提供了获取行号和列号的方法。
你的问题的可能原因...第一行...可能是因为解析器生成工具的工作方式...他们试图找出最好的可能匹配...有时他们必须回溯/倒带流....因此您的 LineNumberReader 实例不同步....
获取行号或列号的理想方法是使用工具本身提供的方法..
Another solution --
Have a look at these parser generating tools...
The JavaCC and Antlr provide a way to get the line number and the column number.
The possible reason for the your problem... the line number one... could be because of the way parser generating tools work... They try to find out the best possible match... for that sometime they have to trackback/rewind the stream.... and due to this your LineNumberReader instance is going out of sync....
The ideal way to get line or column number is to use the methods provided by the tool itself..