Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add save extra namespaces #18

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/org/apache/xmlbeans/XmlOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public enum XmlOptionsKeys {
SAVE_CDATA_LENGTH_THRESHOLD,
SAVE_CDATA_ENTITY_COUNT_THRESHOLD,
SAVE_SAX_NO_NSDECLS_IN_ATTRIBUTES,
SAVE_EXTRA_NAMESPACES,
LOAD_REPLACE_DOCUMENT_ELEMENT,
LOAD_STRIP_WHITESPACE,
LOAD_STRIP_COMMENTS,
Expand Down Expand Up @@ -448,6 +449,23 @@ public Map<String, String> getSaveSuggestedPrefixes() {
return (Map<String, String>) get(XmlOptionsKeys.SAVE_SUGGESTED_PREFIXES);
}

/**
* A map of hints to pass to the saver for which prefixes to use
* for which namespace URI.
*
* @param extraNamespaces a map from URIs to prefixes
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveExtraNamespaces(Map<String, String> extraNamespaces) {
return set(XmlOptionsKeys.SAVE_EXTRA_NAMESPACES, extraNamespaces);
}

@SuppressWarnings("unchecked")
public Map<String, String> getSaveExtraNamespaces() {
return (Map<String, String>) get(XmlOptionsKeys.SAVE_EXTRA_NAMESPACES);
}

/**
* This option causes the saver to filter a Processing Instruction
* with the given target
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/org/apache/xmlbeans/impl/store/Saver.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ abstract class Saver {

private SaveCur _cur;

protected boolean _isTopLevelElement = true;
protected final Map<String, String> _extraNamespaces;

private List<String> _ancestorNamespaces;
private final Map<String, String> _suggestedPrefixes;
protected XmlOptionCharEscapeMap _replaceChar;
Expand Down Expand Up @@ -135,6 +138,8 @@ protected void syntheticNamespace(String prefix, String uri, boolean considerDef
_suggestedPrefixes = options.getSaveSuggestedPrefixes();

_ancestorNamespaces = _cur.getAncestorNamespaces();

_extraNamespaces = options.getSaveExtraNamespaces();
}

private static SaveCur createSaveCur(Cur c, XmlOptions options) {
Expand Down Expand Up @@ -287,10 +292,12 @@ protected final boolean process() {
switch (_cur.kind()) {
case ROOT: {
processRoot();
_isTopLevelElement = true;
break;
}
case ELEM: {
processElement();
_isTopLevelElement = false;
break;
}
case -ELEM: {
Expand Down Expand Up @@ -899,6 +906,8 @@ protected boolean emitElement(SaveCur c, List<QName> attrNames, List<String> att
emitNamespacesHelper();
}

emitExtraNamespacesHelper();

for (int i = 0; i < attrNames.size(); i++) {
emitAttrHelper(attrNames.get(i), attrValues.get(i));
}
Expand Down Expand Up @@ -943,6 +952,17 @@ protected void emitXmlns(String prefix, String uri) {
emit('"');
}

private void emitExtraNamespacesHelper() {
if (!_isTopLevelElement || null == _extraNamespaces)
return;

for (Map.Entry<String, String> nsEntry : _extraNamespaces.entrySet()) {
emit(' ');
emitXmlns(nsEntry.getKey(), nsEntry.getValue());
}

}

private void emitNamespacesHelper() {
LinkedHashMap<String, String> nsMap = new LinkedHashMap<>();
for (iterateMappings(); hasMapping(); nextMapping()) {
Expand Down Expand Up @@ -1853,6 +1873,8 @@ protected boolean emitElement(SaveCur c, List<QName> attrNames, List<String> att
emit('<');
emitName(c.getName(), false);

emitExtraNamespacesHelper();

for (int i = 0; i < attrNames.size(); i++) {
emitAttrHelper(attrNames.get(i), attrValues.get(i));
}
Expand Down Expand Up @@ -1902,6 +1924,15 @@ private void emitNamespacesHelper() {
}
}

private void emitExtraNamespacesHelper() {
if (!_isTopLevelElement || null == _extraNamespaces)
return;
for (Map.Entry<String, String> nsEntry : _extraNamespaces.entrySet()) {
emit(' ');
emitXmlns(nsEntry.getKey(), nsEntry.getValue());
}
}

private void emitAttrHelper(QName attrName, String attrValue) {
emit(' ');
emitName(attrName, true);
Expand Down Expand Up @@ -2578,6 +2609,15 @@ private String getPrefixedName(QName name) {
return prefix + ":" + local;
}

private void emitExtraNamespacesHelper() {
if (!_isTopLevelElement || null == _extraNamespaces|| !_nsAsAttrs)
return;
for (Map.Entry<String, String> nsEntry : _extraNamespaces.entrySet()) {
String prefix = nsEntry.getKey();
_attributes.addAttribute("http://www.w3.org/2000/xmlns/", prefix, "xmlns:" + prefix , "CDATA", nsEntry.getValue());
}
}

private void emitNamespacesHelper() {
for (iterateMappings(); hasMapping(); nextMapping()) {
String prefix = mappingPrefix();
Expand Down Expand Up @@ -2607,6 +2647,8 @@ protected boolean emitElement(SaveCur c, List<QName> attrNames, List<String> att
emitNamespacesHelper();
}

emitExtraNamespacesHelper();

for (int i = 0; i < attrNames.size(); i++) {
QName name = attrNames.get(i);

Expand Down