Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
af7203b8
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,发现更多精彩内容 >>
提交
af7203b8
编写于
9月 08, 2010
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added XMLEventStreamWriter
上级
a251d6a6
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
289 addition
and
3 deletion
+289
-3
org.springframework.core/src/main/java/org/springframework/util/xml/StaxUtils.java
...src/main/java/org/springframework/util/xml/StaxUtils.java
+9
-0
org.springframework.core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java
...va/org/springframework/util/xml/XMLEventStreamReader.java
+4
-3
org.springframework.core/src/main/java/org/springframework/util/xml/XMLEventStreamWriter.java
...va/org/springframework/util/xml/XMLEventStreamWriter.java
+213
-0
org.springframework.core/src/test/java/org/springframework/util/xml/XMLEventStreamWriterTest.java
...rg/springframework/util/xml/XMLEventStreamWriterTest.java
+63
-0
未找到文件。
org.springframework.core/src/main/java/org/springframework/util/xml/StaxUtils.java
浏览文件 @
af7203b8
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
org.springframework.util.xml
;
package
org.springframework.util.xml
;
import
javax.xml.stream.XMLEventFactory
;
import
javax.xml.stream.XMLEventReader
;
import
javax.xml.stream.XMLEventReader
;
import
javax.xml.stream.XMLEventWriter
;
import
javax.xml.stream.XMLEventWriter
;
import
javax.xml.stream.XMLStreamException
;
import
javax.xml.stream.XMLStreamException
;
...
@@ -308,6 +309,14 @@ public abstract class StaxUtils {
...
@@ -308,6 +309,14 @@ public abstract class StaxUtils {
return
new
XMLEventStreamReader
(
eventReader
);
return
new
XMLEventStreamReader
(
eventReader
);
}
}
/**
* Return a {@link XMLStreamWriter} that writes to a {@link XMLEventWriter}.
* @return a stream writer that writes to an event writer
* @since 3.0.5
*/
public
static
XMLStreamWriter
createEventStreamWriter
(
XMLEventWriter
eventWriter
,
XMLEventFactory
eventFactory
)
{
return
new
XMLEventStreamWriter
(
eventWriter
,
eventFactory
);
}
/**
/**
* Inner class to avoid a static JAXP 1.4 dependency.
* Inner class to avoid a static JAXP 1.4 dependency.
...
...
org.springframework.core/src/main/java/org/springframework/util/xml/XMLEventStreamReader.java
浏览文件 @
af7203b8
...
@@ -29,12 +29,13 @@ import javax.xml.stream.events.StartDocument;
...
@@ -29,12 +29,13 @@ import javax.xml.stream.events.StartDocument;
import
javax.xml.stream.events.XMLEvent
;
import
javax.xml.stream.events.XMLEvent
;
/**
/**
* Implementation of the
<code>XMLStreamReader</code> interface that wraps a <code>XMLEventReader</code>
. Useful,
* Implementation of the
{@link javax.xml.stream.XMLStreamReader} interface that wraps a {@link XMLEventReader}
. Useful,
* because the StAX
<code>XMLInputFactory</code> allows one to create a event reader from a stream reader, but not
* because the StAX
{@link javax.xml.stream.XMLInputFactory} allows one to create a event reader from a stream reader,
* vice-versa.
*
but not
vice-versa.
*
*
* @author Arjen Poutsma
* @author Arjen Poutsma
* @since 3.0
* @since 3.0
* @see StaxUtils#createEventStreamReader(javax.xml.stream.XMLEventReader)
*/
*/
class
XMLEventStreamReader
extends
AbstractXMLStreamReader
{
class
XMLEventStreamReader
extends
AbstractXMLStreamReader
{
...
...
org.springframework.core/src/main/java/org/springframework/util/xml/XMLEventStreamWriter.java
0 → 100644
浏览文件 @
af7203b8
/*
* Copyright 2002-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.util.xml
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
import
javax.xml.namespace.NamespaceContext
;
import
javax.xml.namespace.QName
;
import
javax.xml.stream.XMLEventFactory
;
import
javax.xml.stream.XMLEventWriter
;
import
javax.xml.stream.XMLStreamException
;
import
javax.xml.stream.XMLStreamWriter
;
import
javax.xml.stream.events.EndElement
;
import
javax.xml.stream.events.Namespace
;
import
javax.xml.stream.events.StartElement
;
import
org.springframework.util.Assert
;
/**
* Implementation of the {@link javax.xml.stream.XMLStreamWriter} interface that wraps a {@link XMLEventWriter}.
*
* @author Arjen Poutsma
* @since 3.0.5
* @see StaxUtils#createEventStreamWriter(javax.xml.stream.XMLEventWriter, javax.xml.stream.XMLEventFactory)
*/
class
XMLEventStreamWriter
implements
XMLStreamWriter
{
private
static
final
String
DEFAULT_ENCODING
=
"UTF-8"
;
private
final
XMLEventWriter
eventWriter
;
private
final
XMLEventFactory
eventFactory
;
private
List
<
EndElement
>
endElements
=
new
ArrayList
<
EndElement
>();
public
XMLEventStreamWriter
(
XMLEventWriter
eventWriter
,
XMLEventFactory
eventFactory
)
{
Assert
.
notNull
(
eventWriter
,
"'eventWriter' must not be null"
);
Assert
.
notNull
(
eventFactory
,
"'eventFactory' must not be null"
);
this
.
eventWriter
=
eventWriter
;
this
.
eventFactory
=
eventFactory
;
}
public
NamespaceContext
getNamespaceContext
()
{
return
eventWriter
.
getNamespaceContext
();
}
public
String
getPrefix
(
String
uri
)
throws
XMLStreamException
{
return
eventWriter
.
getPrefix
(
uri
);
}
public
void
setPrefix
(
String
prefix
,
String
uri
)
throws
XMLStreamException
{
eventWriter
.
setPrefix
(
prefix
,
uri
);
}
public
void
setDefaultNamespace
(
String
uri
)
throws
XMLStreamException
{
eventWriter
.
setDefaultNamespace
(
uri
);
}
public
void
setNamespaceContext
(
NamespaceContext
context
)
throws
XMLStreamException
{
eventWriter
.
setNamespaceContext
(
context
);
}
public
void
writeStartDocument
()
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createStartDocument
());
}
public
void
writeStartDocument
(
String
version
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createStartDocument
(
DEFAULT_ENCODING
,
version
));
}
public
void
writeStartDocument
(
String
encoding
,
String
version
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createStartDocument
(
encoding
,
version
));
}
public
void
writeStartElement
(
String
localName
)
throws
XMLStreamException
{
writeStartElement
(
eventFactory
.
createStartElement
(
new
QName
(
localName
),
null
,
null
));
}
public
void
writeStartElement
(
String
namespaceURI
,
String
localName
)
throws
XMLStreamException
{
writeStartElement
(
eventFactory
.
createStartElement
(
new
QName
(
namespaceURI
,
localName
),
null
,
null
));
}
public
void
writeStartElement
(
String
prefix
,
String
localName
,
String
namespaceURI
)
throws
XMLStreamException
{
writeStartElement
(
eventFactory
.
createStartElement
(
new
QName
(
namespaceURI
,
localName
,
prefix
),
null
,
null
));
}
public
void
writeEmptyElement
(
String
localName
)
throws
XMLStreamException
{
writeStartElement
(
localName
);
writeEndElement
();
}
public
void
writeEmptyElement
(
String
namespaceURI
,
String
localName
)
throws
XMLStreamException
{
writeStartElement
(
namespaceURI
,
localName
);
writeEndElement
();
}
public
void
writeEmptyElement
(
String
prefix
,
String
localName
,
String
namespaceURI
)
throws
XMLStreamException
{
writeStartElement
(
prefix
,
localName
,
namespaceURI
);
writeEndElement
();
}
public
void
writeEndElement
()
throws
XMLStreamException
{
int
last
=
endElements
.
size
()
-
1
;
EndElement
lastEndElement
=
endElements
.
get
(
last
);
eventWriter
.
add
(
lastEndElement
);
endElements
.
remove
(
last
);
}
public
void
writeAttribute
(
String
localName
,
String
value
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createAttribute
(
localName
,
value
));
}
public
void
writeAttribute
(
String
namespaceURI
,
String
localName
,
String
value
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createAttribute
(
new
QName
(
namespaceURI
,
localName
),
value
));
}
public
void
writeAttribute
(
String
prefix
,
String
namespaceURI
,
String
localName
,
String
value
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createAttribute
(
prefix
,
namespaceURI
,
localName
,
value
));
}
public
void
writeNamespace
(
String
prefix
,
String
namespaceURI
)
throws
XMLStreamException
{
writeNamespace
(
eventFactory
.
createNamespace
(
prefix
,
namespaceURI
));
}
public
void
writeDefaultNamespace
(
String
namespaceURI
)
throws
XMLStreamException
{
writeNamespace
(
eventFactory
.
createNamespace
(
namespaceURI
));
}
public
void
writeCharacters
(
String
text
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createCharacters
(
text
));
}
public
void
writeCharacters
(
char
[]
text
,
int
start
,
int
len
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createCharacters
(
new
String
(
text
,
start
,
len
)));
}
public
void
writeCData
(
String
data
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createCData
(
data
));
}
public
void
writeComment
(
String
data
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createComment
(
data
));
}
public
void
writeProcessingInstruction
(
String
target
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createProcessingInstruction
(
target
,
""
));
}
public
void
writeProcessingInstruction
(
String
target
,
String
data
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createProcessingInstruction
(
target
,
data
));
}
public
void
writeDTD
(
String
dtd
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createDTD
(
dtd
));
}
public
void
writeEntityRef
(
String
name
)
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createEntityReference
(
name
,
null
));
}
public
void
writeEndDocument
()
throws
XMLStreamException
{
eventWriter
.
add
(
eventFactory
.
createEndDocument
());
}
public
Object
getProperty
(
String
name
)
throws
IllegalArgumentException
{
throw
new
IllegalArgumentException
();
}
public
void
flush
()
throws
XMLStreamException
{
eventWriter
.
flush
();
}
public
void
close
()
throws
XMLStreamException
{
eventWriter
.
close
();
}
private
void
writeStartElement
(
StartElement
startElement
)
throws
XMLStreamException
{
eventWriter
.
add
(
startElement
);
endElements
.
add
(
eventFactory
.
createEndElement
(
startElement
.
getName
(),
null
));
}
private
void
writeNamespace
(
Namespace
namespace
)
throws
XMLStreamException
{
int
last
=
endElements
.
size
()
-
1
;
EndElement
oldEndElement
=
endElements
.
get
(
last
);
Iterator
oldNamespaces
=
oldEndElement
.
getNamespaces
();
List
<
Namespace
>
newNamespaces
=
new
ArrayList
<
Namespace
>();
while
(
oldNamespaces
.
hasNext
())
{
Namespace
oldNamespace
=
(
Namespace
)
oldNamespaces
.
next
();
newNamespaces
.
add
(
oldNamespace
);
}
newNamespaces
.
add
(
namespace
);
EndElement
newEndElement
=
eventFactory
.
createEndElement
(
oldEndElement
.
getName
(),
newNamespaces
.
iterator
());
eventWriter
.
add
(
namespace
);
endElements
.
set
(
last
,
newEndElement
);
}
}
org.springframework.core/src/test/java/org/springframework/util/xml/XMLEventStreamWriterTest.java
0 → 100644
浏览文件 @
af7203b8
/*
* Copyright 2002-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.util.xml
;
import
java.io.StringWriter
;
import
javax.xml.stream.XMLEventFactory
;
import
javax.xml.stream.XMLEventWriter
;
import
javax.xml.stream.XMLOutputFactory
;
import
static
org
.
custommonkey
.
xmlunit
.
XMLAssert
.*;
import
org.junit.Before
;
import
org.junit.Test
;
public
class
XMLEventStreamWriterTest
{
private
static
final
String
XML
=
"<?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2'>content</prefix:child></root>"
;
private
XMLEventStreamWriter
streamWriter
;
private
StringWriter
stringWriter
;
@Before
public
void
createStreamReader
()
throws
Exception
{
stringWriter
=
new
StringWriter
();
XMLOutputFactory
outputFactory
=
XMLOutputFactory
.
newInstance
();
XMLEventWriter
eventWriter
=
outputFactory
.
createXMLEventWriter
(
stringWriter
);
streamWriter
=
new
XMLEventStreamWriter
(
eventWriter
,
XMLEventFactory
.
newInstance
());
}
@Test
public
void
write
()
throws
Exception
{
streamWriter
.
writeStartDocument
();
streamWriter
.
writeProcessingInstruction
(
"pi"
,
"content"
);
streamWriter
.
writeStartElement
(
"namespace"
,
"root"
);
streamWriter
.
writeDefaultNamespace
(
"namespace"
);
streamWriter
.
writeStartElement
(
"prefix"
,
"child"
,
"namespace2"
);
streamWriter
.
writeNamespace
(
"prefix"
,
"namespace2"
);
streamWriter
.
writeCharacters
(
"content"
);
streamWriter
.
writeEndElement
();
streamWriter
.
writeEndElement
();
streamWriter
.
writeEndDocument
();
assertXMLEqual
(
XML
,
stringWriter
.
toString
());
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录