java中使用tika,如何使用Apache Tika 1.16 SourceCodeParser?

社区注意:请不要将其关闭为重复,因为我正在研究的特定问题已表现为空指针异常 . 从堆栈跟踪中可以看出,NPE在Tika库中埋藏了4层深 . 这意味着在NPE上现有的StackExchange帖子中给出的所有好建议,没有一个Tika开发人员认为适合在四个模块中应用该建议(检查空指针) . 它不是学习Tika并使用补丁来改进他们的代码来完成这项工作,而是认为询问是否有人实现了使用SourcCodeParser的常见用例会更有效 .

我正在寻找有关Tika图书馆已发布示例的帮助 . 我没有编写示例代码 . 我见过许多与Tika库有关的类似问题,它有20个贡献者和数千行代码 . 请不要关闭这个问题,因为我相信任何以前使用过这个Parser的人都可以很快回答这个问题 . 我已经阅读了有关NullPointerException的帖子,并且正在遵循该问题的建议:

我仍然无法找到问题如果您尝试调试问题并且仍然没有解决方案,您可以发布问题以获得更多帮助,但请确保包含您迄今为止尝试过的内容 . 至少要在问题中包含堆栈跟踪,并在代码中标记重要的行号 .

由于我花了很多时间编写这篇文章,检索并包含相关的堆栈跟踪和源代码,如果你允许这种情况花费一点时间在一个未封闭的状态,我会非常感激,以便熟悉Tika的人可能会采取看一下看似相当普遍的问题 . 正如您所知道的Java专家一样,许多空指针异常问题可能并非易事,尤其是在使用大型不熟悉的框架时 . 我非常感谢你的帮助 .

我编写了一个简单的程序来测试Tika SourceCodeParser ,在the Tika Examples page的XHTML解析示例中将其替换为 AutoDetectParser . 在第137行执行parse命令时,存在NullPointerException . 看来,Parser代码的第180行上的 in 可能缺少一个委托 .

AutoDetectParser 有效,但未将源代码标识为java .

当我使用Tika桌面应用程序时,它工作正常并将代码识别为Java .

如何在运行时初始化 SourceCodeParser 以避免 NullPointerException ?

Example using Tika "Example" Package LocalFile.toTikaXhtmlString()

123 /** Parses as Tika using source code parser.

124 *

125 * @param filePathParam path to file to parse

126 */

127 public static String toTikaXhtmlString(final String filePathParam)

128 throws IOException, SAXException, TikaException

129 {

130 SourceCodeParser parser = new SourceCodeParser();

131 ContentHandler handler = new ToXMLContentHandler();

132 Metadata metadata = new Metadata();

133 File file = new File(filePathParam);

134 try (InputStream stream

135 = ContentHandlerExample.class

136 .getResourceAsStream(filePathParam)) {

137 parser.parse(stream, handler, metadata);

138 return handler.toString();

139 } catch (Exception e) {

140 System.out.println("Caught exception.");

141 System.out.println(e.toString());

142 e.printStackTrace();

143 throw e;

144 }

145

146 }

我还尝试使用与InputStreamReader的直接调用来避免使用Tika'ContentHandlerExample'类,结果相同:

public static String toTikaXhtmlString(final String filePathParam)

throws IOException, SAXException, TikaException

{

SourceCodeParser parser = new SourceCodeParser();

ContentHandler handler = new ToXMLContentHandler();

Metadata metadata = new Metadata();

File file = new File(filePathParam);

try (InputStream stream = new FileInputStream(file)) {

parser.parse(stream, handler, metadata);

return handler.toString();

} catch (Exception e) {

throw new RuntimeException(e.getMessage());

}

}

JUNIT Test

108 @Test

109 public void parseFile() {

110 String fileName, verifyInput, resultContent;

111

112 //arrange

113 fileName = "/Users/johnmeyer/Projects/code-proc/FileParseTest-run.txt";

114

115 String fileContent = "/** Test */ public MyTestClass {"

116 + "public static void main(String[] args) {"

117 + "System.out.println(\"This is a test.\"); }";

118

119

120 LocalFile.putText(fileName, fileContent);

121

122 verifyInput = LocalFile.getContent(fileName);

123

124 assertEquals(fileContent, verifyInput);

125 //act (and clean up)

126

127 try {

128

129 resultContent = LocalFile.toTikaXhtmlString(fileName);

130 } catch (Exception e) {

131 throw new RuntimeException(e.getMessage());

132 }

133

134 LocalFile.delete(fileName);

135

136 //assert

137 assertEquals(fileContent, resultContent);

138 }

Stack Trace

[INFO]运行us.johnmeyer.test.tools.FileParseTest捕获异常 . java.lang.NullPointerException java.lang.NullPointerException位于org.apache.com上的org.apache.commons.io.input.ProxyInputStream.markSupported(ProxyInputStream.java:181).AutoDetectReader.getBuffered(AutoDetectReader.java:137) org.apache.tika.detect.AutoDetectReader . (AutoDetectReader.java:114)位于org.apache.tika.parser.AbstractParser的org.apache.tika.parser.code.SourceCodeParser.parse(SourceCodeParser.java:93) . 解析(AbstractParser.java:53)at us.johnmeyer.utilities.LocalFile.toTikaXhtmlString(LocalFile.java:137)at us.johnmeyer.test.tools.FileParseTest.parseFile(FileParseTest.java:129)at sun.reflect.NativeMethodAccessorImpl .invoke0(本机方法)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java) :498)在org.junit.internal.r的org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47) unners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod . java:17)org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)atg.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner .java:50)org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner . runGildren(ParentRunner.java:236)org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)org.junit.runners .parentRunner.run(ParentRunner.java:309)位于org.apache.maven.surefire.junit4.JUnit4Provider的org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369) . 在org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)的org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)中的executeWithRerun(JUnit4Provider.java:275)在Org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)位于org.apache.maven.surefire的org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334) . 在org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)中的booter.ForkedBooter.execute(ForkedBooter.java:119)

Tika Source Code

17 package org.apache.tika.io;

18

19 import java.io.FilterInputStream;

20 import java.io.IOException;

21 import java.io.InputStream;

22

23 /**

24 * A Proxy stream which acts as expected, that is it passes the method

25 * calls on to the proxied stream and doesn't change which methods are

26 * being called.

27 *

28 * It is an alternative base class to FilterInputStream

29 * to increase reusability, because FilterInputStream changes the

30 * methods being called, such as read(byte[]) to read(byte[], int, int).

31 *

32 * See the protected methods for ways in which a subclass can easily decorate

33 * a stream with custom pre-, post- or error processing functionality.

34 *

35 * @author Stephen Colebourne

36 * @version $Id$

37 */

38 public abstract class ProxyInputStream extends FilterInputStream {

40 /**

41 * Constructs a new ProxyInputStream.

42 *

43 * @param proxy the InputStream to delegate to

44 */

45 public ProxyInputStream(InputStream proxy) {

46 super(proxy);

47 // the proxy is stored in a protected superclass variable named 'in'

48 }

...

174 /**

175 * Invokes the delegate's markSupported() method.

176 * @return true if mark is supported, otherwise false

177 */

178 @Override

179 public boolean markSupported() {

180 return in.markSupported();

181 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值