java解析css文件路径,使用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.

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.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值