Skip to content

Commit bac1716

Browse files
author
s_zhangziang
committed
Added the saveExtraNamespaces
Added the saveExtraNamespaces input parameter and modified the top-level element to write to disk to ensure that the top-level element can write extraNamespaces
1 parent ab782ee commit bac1716

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/main/java/org/apache/xmlbeans/XmlOptions.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public enum XmlOptionsKeys {
107107
SAVE_CDATA_LENGTH_THRESHOLD,
108108
SAVE_CDATA_ENTITY_COUNT_THRESHOLD,
109109
SAVE_SAX_NO_NSDECLS_IN_ATTRIBUTES,
110+
SAVE_EXTRA_NAMESPACES,
110111
LOAD_REPLACE_DOCUMENT_ELEMENT,
111112
LOAD_STRIP_WHITESPACE,
112113
LOAD_STRIP_COMMENTS,
@@ -448,6 +449,23 @@ public Map<String, String> getSaveSuggestedPrefixes() {
448449
return (Map<String, String>) get(XmlOptionsKeys.SAVE_SUGGESTED_PREFIXES);
449450
}
450451

452+
/**
453+
* A map of hints to pass to the saver for which prefixes to use
454+
* for which namespace URI.
455+
*
456+
* @param extraNamespaces a map from URIs to prefixes
457+
* @see XmlTokenSource#save(java.io.File, XmlOptions)
458+
* @see XmlTokenSource#xmlText(XmlOptions)
459+
*/
460+
public XmlOptions setSaveExtraNamespaces(Map<String, String> extraNamespaces) {
461+
return set(XmlOptionsKeys.SAVE_EXTRA_NAMESPACES, extraNamespaces);
462+
}
463+
464+
@SuppressWarnings("unchecked")
465+
public Map<String, String> getSaveExtraNamespaces() {
466+
return (Map<String, String>) get(XmlOptionsKeys.SAVE_EXTRA_NAMESPACES);
467+
}
468+
451469
/**
452470
* This option causes the saver to filter a Processing Instruction
453471
* with the given target

src/main/java/org/apache/xmlbeans/impl/store/Saver.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ abstract class Saver {
4646

4747
private SaveCur _cur;
4848

49+
protected boolean _isTopLevelElement = true;
50+
protected final Map<String, String> _extraNamespaces;
51+
4952
private List<String> _ancestorNamespaces;
5053
private final Map<String, String> _suggestedPrefixes;
5154
protected XmlOptionCharEscapeMap _replaceChar;
@@ -135,6 +138,8 @@ protected void syntheticNamespace(String prefix, String uri, boolean considerDef
135138
_suggestedPrefixes = options.getSaveSuggestedPrefixes();
136139

137140
_ancestorNamespaces = _cur.getAncestorNamespaces();
141+
142+
_extraNamespaces = options.getSaveExtraNamespaces();
138143
}
139144

140145
private static SaveCur createSaveCur(Cur c, XmlOptions options) {
@@ -287,10 +292,12 @@ protected final boolean process() {
287292
switch (_cur.kind()) {
288293
case ROOT: {
289294
processRoot();
295+
_isTopLevelElement = true;
290296
break;
291297
}
292298
case ELEM: {
293299
processElement();
300+
_isTopLevelElement = false;
294301
break;
295302
}
296303
case -ELEM: {
@@ -899,6 +906,8 @@ protected boolean emitElement(SaveCur c, List<QName> attrNames, List<String> att
899906
emitNamespacesHelper();
900907
}
901908

909+
emitExtraNamespacesHelper();
910+
902911
for (int i = 0; i < attrNames.size(); i++) {
903912
emitAttrHelper(attrNames.get(i), attrValues.get(i));
904913
}
@@ -943,6 +952,17 @@ protected void emitXmlns(String prefix, String uri) {
943952
emit('"');
944953
}
945954

955+
private void emitExtraNamespacesHelper() {
956+
if (!_isTopLevelElement || null == _extraNamespaces)
957+
return;
958+
959+
for (Map.Entry<String, String> nsEntry : _extraNamespaces.entrySet()) {
960+
emit(' ');
961+
emitXmlns(nsEntry.getKey(), nsEntry.getValue());
962+
}
963+
964+
}
965+
946966
private void emitNamespacesHelper() {
947967
LinkedHashMap<String, String> nsMap = new LinkedHashMap<>();
948968
for (iterateMappings(); hasMapping(); nextMapping()) {
@@ -1853,6 +1873,8 @@ protected boolean emitElement(SaveCur c, List<QName> attrNames, List<String> att
18531873
emit('<');
18541874
emitName(c.getName(), false);
18551875

1876+
emitExtraNamespacesHelper();
1877+
18561878
for (int i = 0; i < attrNames.size(); i++) {
18571879
emitAttrHelper(attrNames.get(i), attrValues.get(i));
18581880
}
@@ -1902,6 +1924,15 @@ private void emitNamespacesHelper() {
19021924
}
19031925
}
19041926

1927+
private void emitExtraNamespacesHelper() {
1928+
if (!_isTopLevelElement || null == _extraNamespaces)
1929+
return;
1930+
for (Map.Entry<String, String> nsEntry : _extraNamespaces.entrySet()) {
1931+
emit(' ');
1932+
emitXmlns(nsEntry.getKey(), nsEntry.getValue());
1933+
}
1934+
}
1935+
19051936
private void emitAttrHelper(QName attrName, String attrValue) {
19061937
emit(' ');
19071938
emitName(attrName, true);
@@ -2578,6 +2609,15 @@ private String getPrefixedName(QName name) {
25782609
return prefix + ":" + local;
25792610
}
25802611

2612+
private void emitExtraNamespacesHelper() {
2613+
if (!_isTopLevelElement || null == _extraNamespaces|| !_nsAsAttrs)
2614+
return;
2615+
for (Map.Entry<String, String> nsEntry : _extraNamespaces.entrySet()) {
2616+
String prefix = nsEntry.getKey();
2617+
_attributes.addAttribute("http://www.w3.org/2000/xmlns/", prefix, "xmlns:" + prefix , "CDATA", nsEntry.getValue());
2618+
}
2619+
}
2620+
25812621
private void emitNamespacesHelper() {
25822622
for (iterateMappings(); hasMapping(); nextMapping()) {
25832623
String prefix = mappingPrefix();
@@ -2607,6 +2647,8 @@ protected boolean emitElement(SaveCur c, List<QName> attrNames, List<String> att
26072647
emitNamespacesHelper();
26082648
}
26092649

2650+
emitExtraNamespacesHelper();
2651+
26102652
for (int i = 0; i < attrNames.size(); i++) {
26112653
QName name = attrNames.get(i);
26122654

0 commit comments

Comments
 (0)