Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
野花太放肆
jenkins
提交
35e1602e
J
jenkins
项目概览
野花太放肆
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
35e1602e
编写于
1月 03, 2013
作者:
K
Kohsuke Kawaguchi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improved XStreamDOM by adding more methods
上级
227413be
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
65 addition
and
3 deletion
+65
-3
core/src/main/java/jenkins/util/xstream/XStreamDOM.java
core/src/main/java/jenkins/util/xstream/XStreamDOM.java
+46
-2
core/src/test/java/jenkins/util/xstream/XStreamDOMTest.java
core/src/test/java/jenkins/util/xstream/XStreamDOMTest.java
+15
-1
core/src/test/resources/jenkins/util/xstream/XStreamDOMTest.data2.xml
...t/resources/jenkins/util/xstream/XStreamDOMTest.data2.xml
+4
-0
未找到文件。
core/src/main/java/jenkins/util/xstream/XStreamDOM.java
浏览文件 @
35e1602e
...
...
@@ -35,11 +35,18 @@ import com.thoughtworks.xstream.io.xml.AbstractXmlReader;
import
com.thoughtworks.xstream.io.xml.AbstractXmlWriter
;
import
com.thoughtworks.xstream.io.xml.DocumentReader
;
import
com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer
;
import
com.thoughtworks.xstream.io.xml.XppDriver
;
import
hudson.Util
;
import
hudson.util.VariableResolver
;
import
hudson.util.XStream2
;
import
org.apache.commons.io.IOUtils
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.Reader
;
import
java.io.StringReader
;
import
java.io.StringWriter
;
import
java.io.Writer
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
...
...
@@ -63,6 +70,16 @@ import java.util.Stack;
* The reverse operation is {@link #from(XStream, Object)} method, which marshals an object
* into {@link XStreamDOM}.
*
* <p>
* You can also use this class to parse an entire XML document into a DOM like tree with
* {@link #from(HierarchicalStreamReader)} and {@link #writeTo(HierarchicalStreamWriter)}.
* These two methods support variants that accept other forms.
* <p>
* Whereas the above methods read from and write to {@link HierarchicalStreamReader} and,
* {@link HierarchicalStreamWriter}, we can also create {@link HierarchicalStreamReader}
* that read from DOM and {@link HierarchicalStreamWriter} that writes to DOM. See
* {@link #newReader()} and {@link #newWriter()} for those operations.
*
* @author Kohsuke Kawaguchi
* @since 1.473
*/
...
...
@@ -190,7 +207,15 @@ public class XStreamDOM {
* Writes this {@link XStreamDOM} into {@link OutputStream}.
*/
public
void
writeTo
(
OutputStream
os
)
{
new
XStream2
().
toXML
(
this
,
os
);
writeTo
(
new
XppDriver
().
createWriter
(
os
));
}
public
void
writeTo
(
Writer
w
)
{
writeTo
(
new
XppDriver
().
createWriter
(
w
));
}
public
void
writeTo
(
HierarchicalStreamWriter
w
)
{
new
ConverterImpl
().
marshal
(
this
,
w
,
null
);
}
/**
...
...
@@ -202,6 +227,18 @@ public class XStreamDOM {
return
w
.
getOutput
();
}
public
static
XStreamDOM
from
(
InputStream
in
)
{
return
from
(
new
XppDriver
().
createReader
(
in
));
}
public
static
XStreamDOM
from
(
Reader
in
)
{
return
from
(
new
XppDriver
().
createReader
(
in
));
}
public
static
XStreamDOM
from
(
HierarchicalStreamReader
in
)
{
return
new
ConverterImpl
().
unmarshalElement
(
in
,
null
);
}
private
static
class
ReaderImpl
extends
AbstractXmlReader
implements
DocumentReader
{
private
static
class
Pointer
{
final
XStreamDOM
node
;
...
...
@@ -405,9 +442,17 @@ public class XStreamDOM {
w
.
endNode
();
}
/**
* Unmarshals a single child element.
*/
public
XStreamDOM
unmarshal
(
HierarchicalStreamReader
r
,
UnmarshallingContext
context
)
{
r
.
moveDown
();
XStreamDOM
dom
=
unmarshalElement
(
r
,
context
);
r
.
moveUp
();
return
dom
;
}
public
XStreamDOM
unmarshalElement
(
HierarchicalStreamReader
r
,
UnmarshallingContext
context
)
{
String
name
=
r
.
getNodeName
();
int
c
=
r
.
getAttributeCount
();
...
...
@@ -427,7 +472,6 @@ public class XStreamDOM {
}
else
{
value
=
r
.
getValue
();
}
r
.
moveUp
();
return
new
XStreamDOM
(
name
,
attributes
,
children
,
value
);
}
...
...
core/src/test/java/jenkins/util/xstream/XStreamDOMTest.java
浏览文件 @
35e1602e
...
...
@@ -23,7 +23,9 @@
*/
package
jenkins.util.xstream
;
import
com.thoughtworks.xstream.io.xml.XppDriver
;
import
hudson.util.XStream2
;
import
jenkins.util.xstream.XStreamDOM.ConverterImpl
;
import
org.apache.commons.io.IOUtils
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -32,6 +34,8 @@ import static org.junit.Assert.*;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.StringReader
;
import
java.io.StringWriter
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -151,6 +155,16 @@ public class XStreamDOMTest {
assertXStreamDOMEquals
(
expected
.
getChildren
().
get
(
i
),
actual
.
getChildren
().
get
(
i
));
}
}
}
@Test
public
void
readFromInputStream
()
throws
Exception
{
for
(
String
name
:
new
String
[]{
"XStreamDOMTest.data1.xml"
,
"XStreamDOMTest.data2.xml"
})
{
String
input
=
IOUtils
.
toString
(
getClass
().
getResourceAsStream
(
name
));
XStreamDOM
dom
=
XStreamDOM
.
from
(
new
StringReader
(
input
));
StringWriter
sw
=
new
StringWriter
();
dom
.
writeTo
(
sw
);
assertEquals
(
input
.
trim
(),
sw
.
toString
().
trim
());
}
}
}
core/src/test/resources/jenkins/util/xstream/XStreamDOMTest.data2.xml
0 → 100644
浏览文件 @
35e1602e
<foo
abc=
"def"
>
<bar></bar>
<zot>
abc
</zot>
</foo>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录