1

首先,我想解释我在做什么,然后是我的问题。我需要扫描一个 css 文件并获取其所有内部链接(主要是图像),但我需要获取找到链接的行号。

现在我正在使用长笛库解析文件,它工作得很好我也使用 LineNumberReader 来获取找到链接的行号,但是这个类抛出了一个不正确的行号。

例如:链接 ../../image/bg.gif 位于第 350 行,但 LineNumberReader 类中的 getLineNumber 方法显示为 490。

因此,如果你们中的一些人能以正确的方式驱使我,并给我一个可能的解释 LineNumberReader 类为什么这样做,我将不胜感激。

pd:另一个解决方案将非常感激。

  • 对不起,可能的错别字,英语不是我的母语。
4

3 回答 3

1

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

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

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

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

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

于 2010-10-05T04:29:44.510 回答
0

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.

于 2010-10-07T20:56:21.367 回答
0

或者,您可以使用ph-css作为解析库。请参阅https://github.com/phax/ph-css#code-examples中的“访问 CSS 中包含的所有 URL”示例,了解如何提取 URL 并确定正确源位置的示例。

于 2013-05-16T13:34:05.013 回答