用java解析css文件

发布于 2024-09-26 06:01:40 字数 386 浏览 7 评论 0原文

首先我想解释一下我在做什么,然后是我的问题。 我需要扫描 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身骄傲 2024-10-03 06:01:41

嗨@eakbas 和@Favonius,感谢您的回答。
我终于找到了一个解决方案,也许它不是最好的,但至少对我有用。

正如我之前提到的,我使用flute库来实现包org.w3c.sac包的DocumentHandler类,以便分析css文件。

所以我实现了“property”方法,这个方法有3个参数,属性名称,一个LexicalUnit对象和一个布尔值,指示该属性是否有重要的语句。

public void property(String property, LexicalUnit lexicalUnit, boolean important)

因为我需要找到特定属性的行号,所以我进行了搜索,我可以看到笛子用来实现 LexicalUnit 接口的类保存了行号(它是 LexicalUnitImp),所以我使用反射来进行转换从 LexicalUnit 接口到一个 LexicalUnitImp 对象。

Class<?> clazz = ClassUtils.getClass("org.w3c.flute.parser.LexicalUnitImpl");
Object lexicalObject = clazz.cast(lexicalUnit);
Integer line = (Integer)MethodUtils.invokeMethod(lexicalObject, "getLineNumber", null, null);

我这样做是因为类 LexicalUnitImpl 是“受保护的”,我无法以传统方式转换它。

class LexicalUnitImpl implements LexicalUnit

注意:类 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.

public void property(String property, LexicalUnit lexicalUnit, boolean important)

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);

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

Note: The class ClassUtils and MethodUtils are part of the commons-beanutils apache library.

白色秋天 2024-10-03 06:01:41

或者,您可以使用 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.

一花一树开 2024-10-03 06:01:40

另一个解决方案——
看看这些解析器生成工具...

  1. Antlr - http://www.antlr。 org/grammar/1240941192304/css21.g
  2. JavaCC - http://sourceforge.net/projects/cssparser /

JavaCC和Antlr提供了获取行号和列号的方法。

你的问题的可能原因...第一行...可能是因为解析器生成工具的工作方式...他们试图找出最好的可能匹配...有时他们必须回溯/倒带流....因此您的 LineNumberReader 实例不同步....

获取行号或列号的理想方法是使用工具本身提供的方法..

Another solution --
Have a look at these parser generating tools...

  1. Antlr - http://www.antlr.org/grammar/1240941192304/css21.g
  2. JavaCC - http://sourceforge.net/projects/cssparser/

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文