Skip to content

Commit

Permalink
MID-6320: XML/DOM: Use already defined prefixes in ItemPath serializa…
Browse files Browse the repository at this point in the history
…tion

XML ItemPath serialization now uses already defined prefixes in serialization,
instead of writing declaration / redeclaring same prefixes for namespaces
reuses definitions already present in XML.

Signed-off-by: Tony Tkacik <tonydamage@gmail.com>
  • Loading branch information
tonydamage committed Feb 1, 2021
1 parent e97933b commit 5ce285d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 16 deletions.
Expand Up @@ -449,4 +449,15 @@ public PrismNamespaceContext childDefaultNamespace(String namespace) {
return childContext(ImmutableMap.of(DEFAULT_PREFIX, namespace));
}

public final PrismNamespaceContext rebasedOn(PrismNamespaceContext current) {
ImmutableMap.Builder<String,String> prefixesToRebase = ImmutableMap.builder();
for(Entry<String, String> prefix : allPrefixes().entrySet()) {
Optional<String> other = current.namespaceFor(prefix.getKey());
if(other.isEmpty() || !other.get().equals(prefix.getValue()) ) {
prefixesToRebase.put(prefix);
}
}
return current.childContext(prefixesToRebase.build());
}

}
Expand Up @@ -6,10 +6,11 @@
*/
package com.evolveum.midpoint.prism.impl.lex.dom;

import com.evolveum.midpoint.prism.PrismNamespaceContext;
import com.evolveum.midpoint.prism.SerializationContext;
import com.evolveum.midpoint.prism.SerializationOptions;
import com.evolveum.midpoint.prism.impl.PrismContextImpl;
import com.evolveum.midpoint.prism.impl.marshaller.ItemPathHolder;
import com.evolveum.midpoint.prism.impl.marshaller.ItemPathSerialization;
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.prism.xml.DynamicNamespacePrefixMapper;
import com.evolveum.midpoint.prism.xml.XsdTypeMapper;
Expand All @@ -19,6 +20,7 @@
import com.evolveum.midpoint.prism.impl.xnode.RootXNodeImpl;
import com.evolveum.midpoint.prism.impl.xnode.SchemaXNodeImpl;
import com.evolveum.midpoint.prism.impl.xnode.XNodeImpl;
import com.evolveum.midpoint.prism.path.UniformItemPath;
import com.evolveum.midpoint.prism.xnode.IncompleteMarkerXNode;
import com.evolveum.midpoint.prism.xnode.MapXNode;
import com.evolveum.midpoint.prism.xnode.RootXNode;
Expand All @@ -35,6 +37,7 @@

import javax.xml.namespace.QName;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -314,8 +317,14 @@ private Element writeItemPath(PrimitiveXNodeImpl<?> xprim, Element parent, QName
}

if (itemPathType != null) {
Element element = ItemPathHolder.serializeToElement(itemPathType.getItemPath(), elementName, document);
parent.appendChild(element);
Map<String, String> availableNamespaces = DOMUtil.getAllNonDefaultNamespaceDeclarations(parent);
PrismNamespaceContext localNs = PrismNamespaceContext.from(availableNamespaces);

ItemPathSerialization ctx = ItemPathSerialization.serialize(UniformItemPath.from(itemPathType.getItemPath()), localNs);

Element element = createAndAppendChild(elementName, parent);
DOMUtil.setNamespaceDeclarations(element,ctx.undeclaredPrefixes());
element.setTextContent(ctx.getXPathWithoutDeclarations());
return element;
} else {
return null;
Expand Down
Expand Up @@ -233,7 +233,7 @@ private PrismNamespaceContext determineSerializationNamespaceContext(MapXNodeImp
return current.inherited();
}
// Use node local if non empty
return nodeLocal;
return nodeLocal.rebasedOn(current);
}

/**
Expand Down
Expand Up @@ -8,7 +8,6 @@

import java.io.IOException;

import com.evolveum.midpoint.prism.impl.marshaller.ItemPathSerializerTemp;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
Expand All @@ -19,7 +18,7 @@ public class ItemPathSerializer extends JsonSerializer<ItemPath> {

@Override
public void serialize(ItemPath value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeObject(ItemPathSerializerTemp.serializeWithForcedDeclarations(value));
throw new IllegalStateException("Should not be used anymore");
}

@Override
Expand Down
Expand Up @@ -7,7 +7,6 @@

package com.evolveum.midpoint.prism.impl.lex.json.writer;

import com.evolveum.midpoint.prism.impl.marshaller.ItemPathSerializerTemp;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
Expand All @@ -21,7 +20,7 @@ public class ItemPathTypeSerializer extends JsonSerializer<ItemPathType> {

@Override
public void serialize(@NotNull ItemPathType value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeObject(ItemPathSerializerTemp.serializeWithForcedDeclarations(value));
throw new IllegalStateException("Should not be used anymore");

}

Expand Down
Expand Up @@ -8,7 +8,6 @@
package com.evolveum.midpoint.prism.impl.marshaller;

import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.prism.xml.ns._public.types_3.ItemPathType;

/**
* TEMPORARY. WILL BE RESOLVED SOMEHOW.
Expand All @@ -20,11 +19,4 @@ public static String serializeWithDeclarations(ItemPath path) {
return ItemPathHolder.serializeWithDeclarations(path);
}

public static String serializeWithForcedDeclarations(ItemPath path) {
return ItemPathHolder.serializeWithForcedDeclarations(path);
}

public static String serializeWithForcedDeclarations(ItemPathType value) {
return ItemPathHolder.serializeWithForcedDeclarations(value.getItemPath());
}
}
Expand Up @@ -795,6 +795,12 @@ public static Map<String, String> getAllVisibleNamespaceDeclarations(Node node)
return retval;
}

public static Map<String, String> getAllNonDefaultNamespaceDeclarations(Node node) {
Map<String, String> retval = getAllVisibleNamespaceDeclarations(node);
retval.remove(null);
return retval;
}

// returns owner node - works also for attributes
private static Node getParentNode(Node node) {
if (node instanceof Attr) {
Expand Down

0 comments on commit 5ce285d

Please sign in to comment.