Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
9aafa1c6
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
9aafa1c6
编写于
8月 27, 2010
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Various StAX improvements.
上级
b72cca54
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
139 addition
and
42 deletion
+139
-42
org.springframework.core/src/main/java/org/springframework/util/xml/AbstractStaxXMLReader.java
...a/org/springframework/util/xml/AbstractStaxXMLReader.java
+1
-13
org.springframework.core/src/main/java/org/springframework/util/xml/StaxEventXMLReader.java
...java/org/springframework/util/xml/StaxEventXMLReader.java
+60
-19
org.springframework.core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java
...ava/org/springframework/util/xml/StaxStreamXMLReader.java
+32
-2
org.springframework.core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java
...va/org/springframework/util/xml/XMLEventStreamReader.java
+2
-2
org.springframework.core/src/test/java/org/springframework/util/xml/StaxSourceTests.java
...st/java/org/springframework/util/xml/StaxSourceTests.java
+40
-4
org.springframework.core/src/test/java/org/springframework/util/xml/StaxStreamXMLReaderTests.java
...rg/springframework/util/xml/StaxStreamXMLReaderTests.java
+4
-2
未找到文件。
org.springframework.core/src/main/java/org/springframework/util/xml/AbstractStaxXMLReader.java
浏览文件 @
9aafa1c6
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
10
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -20,7 +20,6 @@ import javax.xml.namespace.QName;
import
javax.xml.stream.Location
;
import
javax.xml.stream.XMLStreamException
;
import
org.xml.sax.ContentHandler
;
import
org.xml.sax.InputSource
;
import
org.xml.sax.Locator
;
import
org.xml.sax.SAXException
;
...
...
@@ -108,17 +107,6 @@ abstract class AbstractStaxXMLReader extends AbstractXMLReader {
return
this
.
namespacePrefixesFeature
;
}
/**
* Sett the SAX <code>Locator</code> based on the given StAX <code>Location</code>.
* @param location the location
* @see ContentHandler#setDocumentLocator(org.xml.sax.Locator)
*/
protected
void
setLocator
(
Location
location
)
{
if
(
getContentHandler
()
!=
null
)
{
getContentHandler
().
setDocumentLocator
(
new
StaxLocator
(
location
));
}
}
/**
* Convert a <code>QName</code> to a qualified name, as used by DOM and SAX.
* The returned string has a format of <code>prefix:localName</code> if the
...
...
org.springframework.core/src/main/java/org/springframework/util/xml/StaxEventXMLReader.java
浏览文件 @
9aafa1c6
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
10
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -18,6 +18,7 @@ package org.springframework.util.xml;
import
java.util.Iterator
;
import
javax.xml.namespace.QName
;
import
javax.xml.stream.Location
;
import
javax.xml.stream.XMLEventReader
;
import
javax.xml.stream.XMLStreamConstants
;
import
javax.xml.stream.XMLStreamException
;
...
...
@@ -31,11 +32,13 @@ import javax.xml.stream.events.EntityReference;
import
javax.xml.stream.events.Namespace
;
import
javax.xml.stream.events.NotationDeclaration
;
import
javax.xml.stream.events.ProcessingInstruction
;
import
javax.xml.stream.events.StartDocument
;
import
javax.xml.stream.events.StartElement
;
import
javax.xml.stream.events.XMLEvent
;
import
org.xml.sax.Attributes
;
import
org.xml.sax.SAXException
;
import
org.xml.sax.ext.Locator2
;
import
org.xml.sax.helpers.AttributesImpl
;
import
org.springframework.util.StringUtils
;
...
...
@@ -86,10 +89,14 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
while
(
reader
.
hasNext
()
&&
elementDepth
>=
0
)
{
XMLEvent
event
=
reader
.
nextEvent
();
if
(!
event
.
isStartDocument
()
&&
!
event
.
isEndDocument
()
&&
!
documentStarted
)
{
handleStartDocument
();
handleStartDocument
(
event
);
documentStarted
=
true
;
}
switch
(
event
.
getEventType
())
{
case
XMLStreamConstants
.
START_DOCUMENT
:
handleStartDocument
(
event
);
documentStarted
=
true
;
break
;
case
XMLStreamConstants
.
START_ELEMENT
:
elementDepth
++;
handleStartElement
(
event
.
asStartElement
());
...
...
@@ -108,11 +115,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
case
XMLStreamConstants
.
CDATA
:
handleCharacters
(
event
.
asCharacters
());
break
;
case
XMLStreamConstants
.
START_DOCUMENT
:
setLocator
(
event
.
getLocation
());
handleStartDocument
();
documentStarted
=
true
;
break
;
case
XMLStreamConstants
.
END_DOCUMENT
:
handleEndDocument
();
documentEnded
=
true
;
...
...
@@ -140,6 +142,51 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
}
private
void
handleStartDocument
(
final
XMLEvent
event
)
throws
SAXException
{
if
(
getContentHandler
()
!=
null
)
{
final
Location
location
=
event
.
getLocation
();
getContentHandler
().
setDocumentLocator
(
new
Locator2
()
{
public
int
getColumnNumber
()
{
return
location
.
getColumnNumber
();
}
public
int
getLineNumber
()
{
return
location
.
getLineNumber
();
}
public
String
getPublicId
()
{
return
location
.
getPublicId
();
}
public
String
getSystemId
()
{
return
location
.
getSystemId
();
}
public
String
getXMLVersion
()
{
if
(
event
.
isStartDocument
())
{
StartDocument
startDocument
=
(
StartDocument
)
event
;
String
version
=
startDocument
.
getVersion
();
return
StringUtils
.
hasLength
(
version
)
?
version
:
"1.0"
;
}
return
null
;
}
public
String
getEncoding
()
{
if
(
event
.
isStartDocument
())
{
StartDocument
startDocument
=
(
StartDocument
)
event
;
if
(
startDocument
.
encodingSet
())
{
return
startDocument
.
getCharacterEncodingScheme
();
}
}
return
null
;
}
});
getContentHandler
().
startDocument
();
}
}
private
void
handleStartElement
(
StartElement
startElement
)
throws
SAXException
{
if
(
getContentHandler
()
!=
null
)
{
QName
qName
=
startElement
.
getName
();
...
...
@@ -174,12 +221,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
}
}
private
void
handleEndDocument
()
throws
SAXException
{
if
(
getContentHandler
()
!=
null
)
{
getContentHandler
().
endDocument
();
}
}
private
void
handleEndElement
(
EndElement
endElement
)
throws
SAXException
{
if
(
getContentHandler
()
!=
null
)
{
QName
qName
=
endElement
.
getName
();
...
...
@@ -197,6 +238,12 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
}
}
private
void
handleEndDocument
()
throws
SAXException
{
if
(
getContentHandler
()
!=
null
)
{
getContentHandler
().
endDocument
();
}
}
private
void
handleNotationDeclaration
(
NotationDeclaration
declaration
)
throws
SAXException
{
if
(
getDTDHandler
()
!=
null
)
{
getDTDHandler
().
notationDecl
(
declaration
.
getName
(),
declaration
.
getPublicId
(),
declaration
.
getSystemId
());
...
...
@@ -216,12 +263,6 @@ class StaxEventXMLReader extends AbstractStaxXMLReader {
}
}
private
void
handleStartDocument
()
throws
SAXException
{
if
(
getContentHandler
()
!=
null
)
{
getContentHandler
().
startDocument
();
}
}
private
void
handleComment
(
Comment
comment
)
throws
SAXException
{
if
(
getLexicalHandler
()
!=
null
)
{
char
[]
ch
=
comment
.
getText
().
toCharArray
();
...
...
org.springframework.core/src/main/java/org/springframework/util/xml/StaxStreamXMLReader.java
浏览文件 @
9aafa1c6
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
10
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -17,12 +17,14 @@
package
org.springframework.util.xml
;
import
javax.xml.namespace.QName
;
import
javax.xml.stream.Location
;
import
javax.xml.stream.XMLStreamConstants
;
import
javax.xml.stream.XMLStreamException
;
import
javax.xml.stream.XMLStreamReader
;
import
org.xml.sax.Attributes
;
import
org.xml.sax.SAXException
;
import
org.xml.sax.ext.Locator2
;
import
org.xml.sax.helpers.AttributesImpl
;
import
org.springframework.util.StringUtils
;
...
...
@@ -91,7 +93,6 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
handleCharacters
();
break
;
case
XMLStreamConstants
.
START_DOCUMENT
:
setLocator
(
reader
.
getLocation
());
handleStartDocument
();
documentStarted
=
true
;
break
;
...
...
@@ -123,6 +124,35 @@ class StaxStreamXMLReader extends AbstractStaxXMLReader {
private
void
handleStartDocument
()
throws
SAXException
{
if
(
getContentHandler
()
!=
null
)
{
final
Location
location
=
reader
.
getLocation
();
getContentHandler
().
setDocumentLocator
(
new
Locator2
()
{
public
int
getColumnNumber
()
{
return
location
.
getColumnNumber
();
}
public
int
getLineNumber
()
{
return
location
.
getLineNumber
();
}
public
String
getPublicId
()
{
return
location
.
getPublicId
();
}
public
String
getSystemId
()
{
return
location
.
getSystemId
();
}
public
String
getXMLVersion
()
{
String
version
=
reader
.
getVersion
();
return
StringUtils
.
hasLength
(
version
)
?
version
:
"1.0"
;
}
public
String
getEncoding
()
{
return
reader
.
getEncoding
();
}
});
getContentHandler
().
startDocument
();
if
(
reader
.
standaloneSet
())
{
setStandalone
(
reader
.
isStandalone
());
...
...
org.springframework.core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java
浏览文件 @
9aafa1c6
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
10
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -61,7 +61,7 @@ class XMLEventStreamReader extends AbstractXMLStreamReader {
return
((
StartDocument
)
event
).
getVersion
();
}
else
{
throw
new
IllegalStateException
()
;
return
null
;
}
}
...
...
org.springframework.core/src/test/java/org/springframework/util/xml/StaxSourceTests.java
浏览文件 @
9aafa1c6
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
10
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -18,16 +18,21 @@ package org.springframework.util.xml;
import
java.io.StringReader
;
import
java.io.StringWriter
;
import
javax.xml.parsers.DocumentBuilder
;
import
javax.xml.parsers.DocumentBuilderFactory
;
import
javax.xml.stream.XMLEventReader
;
import
javax.xml.stream.XMLInputFactory
;
import
javax.xml.stream.XMLStreamReader
;
import
javax.xml.transform.Transformer
;
import
javax.xml.transform.TransformerFactory
;
import
javax.xml.transform.dom.DOMResult
;
import
javax.xml.transform.stream.StreamResult
;
import
static
org
.
custommonkey
.
xmlunit
.
XMLAssert
.*;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.w3c.dom.Document
;
import
org.xml.sax.InputSource
;
public
class
StaxSourceTests
{
...
...
@@ -37,15 +42,20 @@ public class StaxSourceTests {
private
XMLInputFactory
inputFactory
;
private
DocumentBuilder
documentBuilder
;
@Before
public
void
create
setUp
()
throws
Exception
{
public
void
setUp
()
throws
Exception
{
TransformerFactory
transformerFactory
=
TransformerFactory
.
newInstance
();
transformer
=
transformerFactory
.
newTransformer
();
inputFactory
=
XMLInputFactory
.
newInstance
();
DocumentBuilderFactory
documentBuilderFactory
=
DocumentBuilderFactory
.
newInstance
();
documentBuilderFactory
.
setNamespaceAware
(
true
);
documentBuilder
=
documentBuilderFactory
.
newDocumentBuilder
();
}
@Test
public
void
streamReaderSource
()
throws
Exception
{
public
void
streamReaderSource
ToStreamResult
()
throws
Exception
{
XMLStreamReader
streamReader
=
inputFactory
.
createXMLStreamReader
(
new
StringReader
(
XML
));
StaxSource
source
=
new
StaxSource
(
streamReader
);
assertEquals
(
"Invalid streamReader returned"
,
streamReader
,
source
.
getXMLStreamReader
());
...
...
@@ -56,7 +66,20 @@ public class StaxSourceTests {
}
@Test
public
void
eventReaderSource
()
throws
Exception
{
public
void
streamReaderSourceToDOMResult
()
throws
Exception
{
XMLStreamReader
streamReader
=
inputFactory
.
createXMLStreamReader
(
new
StringReader
(
XML
));
StaxSource
source
=
new
StaxSource
(
streamReader
);
assertEquals
(
"Invalid streamReader returned"
,
streamReader
,
source
.
getXMLStreamReader
());
assertNull
(
"EventReader returned"
,
source
.
getXMLEventReader
());
Document
expected
=
documentBuilder
.
parse
(
new
InputSource
(
new
StringReader
(
XML
)));
Document
result
=
documentBuilder
.
newDocument
();
transformer
.
transform
(
source
,
new
DOMResult
(
result
));
assertXMLEqual
(
"Invalid result"
,
expected
,
result
);
}
@Test
public
void
eventReaderSourceToStreamResult
()
throws
Exception
{
XMLEventReader
eventReader
=
inputFactory
.
createXMLEventReader
(
new
StringReader
(
XML
));
StaxSource
source
=
new
StaxSource
(
eventReader
);
assertEquals
(
"Invalid eventReader returned"
,
eventReader
,
source
.
getXMLEventReader
());
...
...
@@ -65,4 +88,17 @@ public class StaxSourceTests {
transformer
.
transform
(
source
,
new
StreamResult
(
writer
));
assertXMLEqual
(
"Invalid result"
,
XML
,
writer
.
toString
());
}
@Test
public
void
eventReaderSourceToDOMResult
()
throws
Exception
{
XMLEventReader
eventReader
=
inputFactory
.
createXMLEventReader
(
new
StringReader
(
XML
));
StaxSource
source
=
new
StaxSource
(
eventReader
);
assertEquals
(
"Invalid eventReader returned"
,
eventReader
,
source
.
getXMLEventReader
());
assertNull
(
"StreamReader returned"
,
source
.
getXMLStreamReader
());
Document
expected
=
documentBuilder
.
parse
(
new
InputSource
(
new
StringReader
(
XML
)));
Document
result
=
documentBuilder
.
newDocument
();
transformer
.
transform
(
source
,
new
DOMResult
(
result
));
assertXMLEqual
(
"Invalid result"
,
expected
,
result
);
}
}
\ No newline at end of file
org.springframework.core/src/test/java/org/springframework/util/xml/StaxStreamXMLReaderTests.java
浏览文件 @
9aafa1c6
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
10
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -24,7 +24,7 @@ import javax.xml.stream.XMLStreamException;
import
javax.xml.stream.XMLStreamReader
;
import
org.easymock.MockControl
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
*
;
import
org.junit.Test
;
import
org.xml.sax.ContentHandler
;
import
org.xml.sax.InputSource
;
...
...
@@ -55,6 +55,8 @@ public class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase {
mockControl
.
setDefaultMatcher
(
new
SaxArgumentMatcher
());
ContentHandler
contentHandlerMock
=
(
ContentHandler
)
mockControl
.
getMock
();
contentHandlerMock
.
setDocumentLocator
(
null
);
mockControl
.
setMatcher
(
MockControl
.
ALWAYS_MATCHER
);
contentHandlerMock
.
startDocument
();
contentHandlerMock
.
startElement
(
"http://springframework.org/spring-ws"
,
"child"
,
"child"
,
new
AttributesImpl
());
contentHandlerMock
.
endElement
(
"http://springframework.org/spring-ws"
,
"child"
,
"child"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录