Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
9d1c3fa9
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,发现更多精彩内容 >>
提交
9d1c3fa9
编写于
3月 02, 2010
作者:
A
Arjen Poutsma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
SPR-6907 - JibxMarshaller - provide access to jibx's writeDocType
上级
d207c229
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
111 addition
and
26 deletion
+111
-26
org.springframework.oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java
...ain/java/org/springframework/oxm/jibx/JibxMarshaller.java
+101
-26
org.springframework.oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTests.java
...ava/org/springframework/oxm/jibx/JibxMarshallerTests.java
+10
-0
未找到文件。
org.springframework.oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java
浏览文件 @
9d1c3fa9
...
...
@@ -31,6 +31,8 @@ import javax.xml.stream.XMLStreamWriter;
import
javax.xml.transform.Transformer
;
import
javax.xml.transform.TransformerException
;
import
javax.xml.transform.TransformerFactory
;
import
javax.xml.transform.Result
;
import
javax.xml.transform.Source
;
import
javax.xml.transform.dom.DOMResult
;
import
javax.xml.transform.dom.DOMSource
;
import
javax.xml.transform.sax.SAXResult
;
...
...
@@ -64,6 +66,7 @@ import org.springframework.oxm.XmlMappingException;
import
org.springframework.oxm.support.AbstractMarshaller
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.xml.StaxUtils
;
/**
...
...
@@ -79,7 +82,7 @@ import org.springframework.util.xml.StaxUtils;
*/
public
class
JibxMarshaller
extends
AbstractMarshaller
implements
InitializingBean
{
private
Class
targetClass
;
private
Class
<?>
targetClass
;
private
String
bindingName
;
...
...
@@ -89,6 +92,14 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
private
Boolean
standalone
;
private
String
docTypeRootElementName
;
private
String
docTypeSystemId
;
private
String
docTypePublicId
;
private
String
docTypeInternalSubset
;
private
IBindingFactory
bindingFactory
;
private
TransformerFactory
transformerFactory
=
TransformerFactory
.
newInstance
();
...
...
@@ -97,7 +108,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
/**
* Set the target class for this instance. This property is required.
*/
public
void
setTargetClass
(
Class
targetClass
)
{
public
void
setTargetClass
(
Class
<?>
targetClass
)
{
this
.
targetClass
=
targetClass
;
}
...
...
@@ -129,9 +140,54 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
this
.
standalone
=
standalone
;
}
/**
* Sets the root element name for the DTD declaration written when marshalling. By default, this is
* {@code null} (i.e. no DTD declaration is written). If set to a value, the system ID or public ID also need to
* be set.
*
* @see #setDocTypeSystemId(String)
* @see #setDocTypePublicId(String)
*/
public
void
setDocTypeRootElementName
(
String
docTypeRootElementName
)
{
this
.
docTypeRootElementName
=
docTypeRootElementName
;
}
/**
* Sets the system Id for the DTD declaration written when marshalling. By default, this is
* {@code null}. Only used when the root element also has been set. Set either this property or
* {@code docTypePublicId}, not both.
*
* @see #setDocTypeRootElementName(String)
*/
public
void
setDocTypeSystemId
(
String
docTypeSystemId
)
{
this
.
docTypeSystemId
=
docTypeSystemId
;
}
/**
* Sets the public Id for the DTD declaration written when marshalling. By default, this is
* {@code null}. Only used when the root element also has been set. Set either this property or
* {@code docTypeSystemId}, not both.
*
* @see #setDocTypeRootElementName(String)
*/
public
void
setDocTypePublicId
(
String
docTypePublicId
)
{
this
.
docTypePublicId
=
docTypePublicId
;
}
/**
* Sets the internal subset Id for the DTD declaration written when marshalling. By default, this is
* {@code null}. Only used when the root element also has been set.
*
* @see #setDocTypeRootElementName(String)
*/
public
void
setDocTypeInternalSubset
(
String
docTypeInternalSubset
)
{
this
.
docTypeInternalSubset
=
docTypeInternalSubset
;
}
public
void
afterPropertiesSet
()
throws
JiBXException
{
Assert
.
notNull
(
this
.
targetClass
,
"targetClass is required"
);
Assert
.
notNull
(
this
.
targetClass
,
"'targetClass' is required"
);
Assert
.
isTrue
(!(
StringUtils
.
hasLength
(
docTypePublicId
)
&&
StringUtils
.
hasLength
(
docTypeSystemId
)),
"Set either 'docTypePublicId' or 'docTypeSystemId'; not both"
);
if
(
StringUtils
.
hasLength
(
this
.
bindingName
))
{
if
(
logger
.
isInfoEnabled
())
{
logger
.
info
(
"Configured for target class ["
+
this
.
targetClass
+
"] using binding ["
+
this
.
bindingName
+
"]"
);
...
...
@@ -167,7 +223,8 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
throws
XmlMappingException
,
IOException
{
try
{
IMarshallingContext
marshallingContext
=
createMarshallingContext
();
marshallingContext
.
marshalDocument
(
graph
,
this
.
encoding
,
this
.
standalone
,
outputStream
);
marshallingContext
.
startDocument
(
this
.
encoding
,
this
.
standalone
,
outputStream
);
marshalDocument
(
marshallingContext
,
graph
);
}
catch
(
JiBXException
ex
)
{
throw
convertJibxException
(
ex
,
true
);
...
...
@@ -178,13 +235,23 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
protected
void
marshalWriter
(
Object
graph
,
Writer
writer
)
throws
XmlMappingException
,
IOException
{
try
{
IMarshallingContext
marshallingContext
=
createMarshallingContext
();
marshallingContext
.
marshalDocument
(
graph
,
this
.
encoding
,
this
.
standalone
,
writer
);
marshallingContext
.
startDocument
(
this
.
encoding
,
this
.
standalone
,
writer
);
marshalDocument
(
marshallingContext
,
graph
);
}
catch
(
JiBXException
ex
)
{
throw
convertJibxException
(
ex
,
true
);
}
}
private
void
marshalDocument
(
IMarshallingContext
marshallingContext
,
Object
graph
)
throws
IOException
,
JiBXException
{
if
(
StringUtils
.
hasLength
(
docTypeRootElementName
))
{
IXMLWriter
xmlWriter
=
marshallingContext
.
getXmlWriter
();
xmlWriter
.
writeDocType
(
docTypeRootElementName
,
docTypeSystemId
,
docTypePublicId
,
docTypeInternalSubset
);
}
marshallingContext
.
marshalDocument
(
graph
);
}
@Override
protected
void
marshalXmlStreamWriter
(
Object
graph
,
XMLStreamWriter
streamWriter
)
throws
XmlMappingException
{
try
{
...
...
@@ -198,20 +265,16 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
}
}
// Unsupported Marshalling
@Override
protected
void
marshalDomNode
(
Object
graph
,
Node
node
)
throws
XmlMappingException
{
try
{
// JiBX does not support DOM natively, so we write to a buffer first, and transform that to the Node
ByteArrayOutputStream
os
=
new
ByteArrayOutputStream
();
marshalOutputStream
(
graph
,
os
);
ByteArrayInputStream
is
=
new
ByteArrayInputStream
(
os
.
toByteArray
());
Transformer
transformer
=
this
.
transformerFactory
.
newTransformer
();
transformer
.
transform
(
new
StreamSource
(
is
),
new
DOMResult
(
node
));
Result
result
=
new
DOMResult
(
node
);
transformAndMarshal
(
graph
,
result
);
}
catch
(
Exception
ex
)
{
catch
(
IO
Exception
ex
)
{
throw
new
MarshallingFailureException
(
"JiBX marshalling exception"
,
ex
);
}
}
...
...
@@ -221,17 +284,28 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
throws
XmlMappingException
{
try
{
// JiBX does not support SAX natively, so we write to a buffer first, and transform that to the handlers
SAXResult
saxResult
=
new
SAXResult
(
contentHandler
);
saxResult
.
setLexicalHandler
(
lexicalHandler
);
transformAndMarshal
(
graph
,
saxResult
);
}
catch
(
IOException
ex
)
{
throw
new
MarshallingFailureException
(
"JiBX marshalling exception"
,
ex
);
}
}
private
void
transformAndMarshal
(
Object
graph
,
Result
result
)
throws
IOException
{
try
{
ByteArrayOutputStream
os
=
new
ByteArrayOutputStream
();
marshalOutputStream
(
graph
,
os
);
ByteArrayInputStream
is
=
new
ByteArrayInputStream
(
os
.
toByteArray
());
Transformer
transformer
=
this
.
transformerFactory
.
newTransformer
();
SAXResult
saxResult
=
new
SAXResult
(
contentHandler
);
saxResult
.
setLexicalHandler
(
lexicalHandler
);
transformer
.
transform
(
new
StreamSource
(
is
),
saxResult
);
transformer
.
transform
(
new
StreamSource
(
is
),
result
);
}
catch
(
Exception
ex
)
{
throw
new
MarshallingFailureException
(
"JiBX marshalling exception"
,
ex
);
catch
(
TransformerException
ex
)
{
throw
new
MarshallingFailureException
(
"Could not transform to ["
+
ClassUtils
.
getShortName
(
result
.
getClass
())
+
"]"
);
}
}
@Override
...
...
@@ -295,13 +369,9 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
@Override
protected
Object
unmarshalDomNode
(
Node
node
)
throws
XmlMappingException
{
try
{
Transformer
transformer
=
transformerFactory
.
newTransformer
();
ByteArrayOutputStream
os
=
new
ByteArrayOutputStream
();
transformer
.
transform
(
new
DOMSource
(
node
),
new
StreamResult
(
os
));
ByteArrayInputStream
is
=
new
ByteArrayInputStream
(
os
.
toByteArray
());
return
unmarshalInputStream
(
is
);
return
transformAndUnmarshal
(
new
DOMSource
(
node
));
}
catch
(
Exception
ex
)
{
catch
(
IO
Exception
ex
)
{
throw
new
UnmarshallingFailureException
(
"JiBX unmarshalling exception"
,
ex
);
}
}
...
...
@@ -309,15 +379,20 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
@Override
protected
Object
unmarshalSaxReader
(
XMLReader
xmlReader
,
InputSource
inputSource
)
throws
XmlMappingException
,
IOException
{
return
transformAndUnmarshal
(
new
SAXSource
(
xmlReader
,
inputSource
));
}
private
Object
transformAndUnmarshal
(
Source
source
)
throws
IOException
{
try
{
Transformer
transformer
=
t
his
.
t
ransformerFactory
.
newTransformer
();
Transformer
transformer
=
transformerFactory
.
newTransformer
();
ByteArrayOutputStream
os
=
new
ByteArrayOutputStream
();
transformer
.
transform
(
new
SAXSource
(
xmlReader
,
inputSource
)
,
new
StreamResult
(
os
));
transformer
.
transform
(
source
,
new
StreamResult
(
os
));
ByteArrayInputStream
is
=
new
ByteArrayInputStream
(
os
.
toByteArray
());
return
unmarshalInputStream
(
is
);
}
catch
(
TransformerException
ex
)
{
throw
new
UnmarshallingFailureException
(
"JiBX unmarshalling exception"
,
ex
);
throw
new
MarshallingFailureException
(
"Could not transform from ["
+
ClassUtils
.
getShortName
(
source
.
getClass
())
+
"]"
);
}
}
...
...
org.springframework.oxm/src/test/java/org/springframework/oxm/jibx/JibxMarshallerTests.java
浏览文件 @
9d1c3fa9
...
...
@@ -75,6 +75,16 @@ public class JibxMarshallerTests extends AbstractMarshallerTests {
writer
.
toString
().
startsWith
(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>"
));
}
@Test
public
void
dtd
()
throws
Exception
{
((
JibxMarshaller
)
marshaller
).
setDocTypeRootElementName
(
"flights"
);
((
JibxMarshaller
)
marshaller
).
setDocTypeSystemId
(
"flights.dtd"
);
StringWriter
writer
=
new
StringWriter
();
marshaller
.
marshal
(
flights
,
new
StreamResult
(
writer
));
assertTrue
(
"doc type not written"
,
writer
.
toString
().
contains
(
"<!DOCTYPE flights SYSTEM \"flights.dtd\">"
));
}
@Test
public
void
testSupports
()
throws
Exception
{
assertTrue
(
"JibxMarshaller does not support Flights"
,
marshaller
.
supports
(
Flights
.
class
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录