Comments require reader.markSupported() to return true beanio

huangapple go评论78阅读模式
英文:

Comments require reader.markSupported() to return true beanio

问题

以下是翻译好的内容:

Xml文件

<beanio xmlns="http://www.beanio.org/2012/03"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
    
            <stream name="stream" format="delimited" ignoreUnidentifiedRecords="true">
                <parser>
                    <property name="delimiter" value="|"/>
                    <property name="comments" value="Response Codes:,|File Trailer|,File Header|"/>
                </parser>
    
    			<group name="HeaderRecord">
    				<record>
    				.
    				.
    				.
    				</record>
    			</group>
    			<group name="GroupHeader">
    			    <record>
    				.
    				.
    				.
    				.
    				</record>
    			</group>
    		</stream>
    </beanio>

包含记录的文件如下所示

    1   File Header|
    .   HeaderRecord文本.....
    .   GroupHeader文本......
    19  Response Codes:
    20                 |A - StackOverflow           |                
    21                 |B - Facebook                |               
    22                 |C - Google                  |                
    23  |File Trailer|


错误
java.lang.IllegalArgumentException: 评论需要reader.markSupported()返回true
    	at org.beanio.stream.util.CommentReader.<init>(CommentReader.java:61)
    	at org.beanio.stream.delimited.DelimitedReader.<init>(DelimitedReader.java:149)
    	at org.beanio.stream.delimited.DelimitedRecordParserFactory.createReader(DelimitedRecordParserFactory.java:56)
    	at org.beanio.internal.parser.StreamFormatSupport.createRecordReader(StreamFormatSupport.java:61)
    	at org.beanio.internal.parser.Stream.createBeanReader(Stream.java:88)

我想忽略文件头,尾部和响应代码。响应代码从下一行开始。各种博客建议在属性中使用评论。有人能解释为什么不起作用吗?
英文:

Xml File

<beanio xmlns="http://www.beanio.org/2012/03"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

        <stream name="stream" format="delimited" ignoreUnidentifiedRecords="true">
            <parser>
                <property name="delimiter" value="|"/>
                <property name="comments" value="Response Codes:,|File Trailer|,File Header|"/>
            </parser>

			<group name="HeaderRecord">
				<record>
				.
				.
				.
				</record>
			</group>
			<group name="GroupHeader">
			    <record>
				.
				.
				.
				.
				</record>
			</group>
		</stream>
</beanio>

File that contains records looks like this

1   File Header|
.   HeaderRecord text.....
.   GroupHeader text......
19  Response Codes:
20                 |A - StackOverflow           |                
21                 |B - Facebook                |               
22                 |C - Google                  |                
23  |File Trailer|



Error 
java.lang.IllegalArgumentException: Comments require reader.markSupported() to return true
	at org.beanio.stream.util.CommentReader.<init>(CommentReader.java:61)
	at org.beanio.stream.delimited.DelimitedReader.<init>(DelimitedReader.java:149)
	at org.beanio.stream.delimited.DelimitedRecordParserFactory.createReader(DelimitedRecordParserFactory.java:56)
	at org.beanio.internal.parser.StreamFormatSupport.createRecordReader(StreamFormatSupport.java:61)
	at org.beanio.internal.parser.Stream.createBeanReader(Stream.java:88)

I want to ignore File Header, Trailer & Response Codes. Response codes starts in next line. Various blogs suggested for comments in property. Can anyone explain why it is not working?

答案1

得分: 2

问题出在你使用的Reader实现上,用于从中读取数据,正如错误消息所指示的:

> java.lang.IllegalArgumentException: Comments require
> reader.markSupported() to return true

你需要使用支持缓冲的Reader,也就是实现了mark()reset()方法的Reader。请参考文档的4.1.2节以及关于comments属性名称的描述和解释,其中指出:

> 用于识别注释行的一组以逗号分隔的值。如果从输入流中读取的行以配置的任何值开头,那么该行将被忽略。反斜杠可以用于转义逗号和它本身。所有的空白字符都会被保留。
>
> 要启用注释,需要确保传递给StreamFactory的输入读取器支持标记(mark)。Java的BufferedReader和StringReader等读取器支持标记。

为了使注释正常工作,你需要使用BufferedReaderStringReader来读取数据:

InputStream inputStream = ... // 获取到数据源的某个输入流 
Reader reader = new BufferedReader(new InputStreamReader(inputStream));
BeanReader beanReader = factory.createReader("streamName", reader);
英文:

The problem is with the Reader implementation that you used to read the data from as indicated by the error message:

> java.lang.IllegalArgumentException: Comments require
> reader.markSupported() to return true

You need to use a Reader that supports buffering, i.e. a Reader that implements the mark() and reset() methods. See section 4.1.2 of the documentation and the description and explanation for the comments property name, which states:

> A comma separated list of values for identifying commented lines. If a
> line read from an input stream begins with any of the configured
> values, the line is ignored. A backslash may be used to escape a comma
> and itself. All whitespace is preserved.
>
> Enabling comments require the input reader passed to StreamFactory to
> support marking. Among others, Java's BufferedReader and StringReader
> support marking.

For comments to work you need to read the data with either a BufferedReader or a StringReader

InputStream inputStream = ... // obtain some input stream to the source of your data 
Reader reader = new BufferedReader(new InputStreamReader(inputStream));
BeanReader beanReader = factory.createReader("streamName", reader);

huangapple
  • 本文由 发表于 2020年8月10日 19:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63338987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定