-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
s_zhangziang
committed
Jun 25, 2024
1 parent
16af8f6
commit 55b6a1d
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* Copyright 2004 The Apache Software Foundation | ||
* | ||
* 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 xmlobject.detailed; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.xmlbeans.XmlObject; | ||
import org.apache.xmlbeans.XmlOptions; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ExtraNameSpaceTest { | ||
// Test whether xmlobject can write extraNamespaces | ||
@Test | ||
public void testSaveExtraNamespaces() throws Exception { | ||
// Creating and setting up a namespace | ||
Map<String, String> extraNamespaces = new HashMap<>(); | ||
extraNamespaces.put("uof", "http://www.nits.org.cn/uof3/zh-cn/2021/uof"); | ||
extraNamespaces.put("图", "http://www.nits.org.cn/uof3/zh-cn/2021/graph"); | ||
extraNamespaces.put("字", "http://www.nits.org.cn/uof3/zh-cn/2021/wordproc"); | ||
extraNamespaces.put("表", "http://www.nits.org.cn/uof3/zh-cn/2021/spreadsheet"); | ||
extraNamespaces.put("演", "http://www.nits.org.cn/uof3/zh-cn/2021/presentation"); | ||
|
||
XmlOptions xmlOptions = new XmlOptions(); | ||
xmlOptions.setSaveExtraNamespaces(extraNamespaces); | ||
|
||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/uof", extraNamespaces.get("uof")); | ||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/graph", extraNamespaces.get("图")); | ||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/wordproc", extraNamespaces.get("字")); | ||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/spreadsheet", extraNamespaces.get("表")); | ||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/presentation", extraNamespaces.get("演")); | ||
|
||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
XmlObject doc = XmlObject.Factory.parse("<foo><a/></foo>"); | ||
|
||
doc.save(outputStream, xmlOptions); | ||
String outputXml = outputStream.toString(StandardCharsets.UTF_8); | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
Document document = builder.parse(new java.io.ByteArrayInputStream(outputXml.getBytes(StandardCharsets.UTF_8))); | ||
Element root = document.getDocumentElement(); | ||
|
||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/uof", root.getAttribute("xmlns:uof")); | ||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/graph", root.getAttribute("xmlns:图")); | ||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/wordproc", root.getAttribute("xmlns:字")); | ||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/spreadsheet", root.getAttribute("xmlns:表")); | ||
assertEquals("http://www.nits.org.cn/uof3/zh-cn/2021/presentation", root.getAttribute("xmlns:演")); | ||
} | ||
} |