1 package org.opendaylight.yangtools.yang.data.impl.codec.xml;
3 import static com.google.common.base.Preconditions.checkState;
9 import javax.activation.UnsupportedDataTypeException;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
16 import org.opendaylight.yangtools.yang.data.api.Node;
17 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
18 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
19 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
20 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
21 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SimpleValueSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.NodeList;
40 import com.google.common.base.Function;
41 import com.google.common.base.Objects;
42 import com.google.common.base.Optional;
43 import com.google.common.base.Preconditions;
44 import com.google.common.base.Strings;
45 import com.google.common.collect.ImmutableList;
47 public class XmlDocumentUtils {
49 private static final XmlCodecProvider DEFAULT_XML_VALUE_CODEC_PROVIDER = new XmlCodecProvider() {
52 public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(TypeDefinition<?> baseType) {
53 return TypeDefinitionAwareCodec.from(baseType);
57 private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
59 public static Document toDocument(CompositeNode data, DataNodeContainer schema, XmlCodecProvider codecProvider)
60 throws UnsupportedDataTypeException {
61 Preconditions.checkNotNull(data);
62 Preconditions.checkNotNull(schema);
64 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
67 DocumentBuilder bob = dbf.newDocumentBuilder();
68 doc = bob.newDocument();
69 } catch (ParserConfigurationException e) {
73 if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) {
74 doc.appendChild(createXmlRootElement(doc, data, (SchemaNode) schema, codecProvider));
77 throw new UnsupportedDataTypeException(
78 "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
82 public static Node<?> toDomNode(Element xmlElement, Optional<DataSchemaNode> schema,
83 Optional<XmlCodecProvider> codecProvider) {
84 if (schema.isPresent()) {
85 return toNodeWithSchema(xmlElement, schema.get(), codecProvider.or(DEFAULT_XML_VALUE_CODEC_PROVIDER));
87 return toDomNode(xmlElement);
90 public static CompositeNode fromElement(Element xmlElement) {
91 CompositeNodeBuilder<ImmutableCompositeNode> node = ImmutableCompositeNode.builder();
92 node.setQName(qNameFromElement(xmlElement));
94 return node.toInstance();
97 private static QName qNameFromElement(Element xmlElement) {
98 String namespace = xmlElement.getNamespaceURI();
99 String localName = xmlElement.getLocalName();
100 return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
103 private static Node<?> toNodeWithSchema(Element xmlElement, DataSchemaNode schema, XmlCodecProvider codecProvider) {
104 checkQName(xmlElement, schema.getQName());
105 if (schema instanceof DataNodeContainer) {
106 return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, codecProvider);
107 } else if (schema instanceof SimpleValueSchemaNode) {
108 return toSimpleNodeWithType(xmlElement, (SimpleValueSchemaNode) schema, codecProvider);
113 private static Node<?> toSimpleNodeWithType(Element xmlElement, SimpleValueSchemaNode schema,
114 XmlCodecProvider codecProvider) {
115 TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
116 String text = xmlElement.getTextContent();
117 Object value = codec.deserialize(text);
118 return new SimpleNodeTOImpl<Object>(schema.getQName(), null, value);
121 private static Node<?> toCompositeNodeWithSchema(Element xmlElement, QName qName, DataNodeContainer schema,
122 XmlCodecProvider codecProvider) {
123 List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()));
124 return new ImmutableCompositeNode(qName, values );
127 private static void checkQName(Element xmlElement, QName qName) {
128 checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
129 checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
132 private static Element createXmlRootElement(Document doc, Node<?> data, SchemaNode schema,
133 XmlCodecProvider codecProvider) throws UnsupportedDataTypeException {
134 QName dataType = data.getNodeType();
135 Element itemEl = createElementFor(doc, dataType);
136 if (data instanceof SimpleNode<?>) {
137 if (schema instanceof LeafListSchemaNode) {
138 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafListSchemaNode) schema).getType(),
139 (DataSchemaNode) schema, codecProvider);
140 } else if (schema instanceof LeafSchemaNode) {
141 writeValueByType(itemEl, (SimpleNode<?>) data, ((LeafSchemaNode) schema).getType(),
142 (DataSchemaNode) schema, codecProvider);
144 Object value = data.getValue();
146 itemEl.setTextContent(String.valueOf(value));
149 } else { // CompositeNode
150 for (Node<?> child : ((CompositeNode) data).getChildren()) {
151 DataSchemaNode childSchema = null;
152 if (schema != null) {
153 childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes());
154 if (logger.isDebugEnabled()) {
155 if (childSchema == null) {
156 logger.debug("Probably the data node \""
157 + ((child == null) ? "" : child.getNodeType().getLocalName())
158 + "\" is not conform to schema");
162 itemEl.appendChild(createXmlRootElement(doc, child, childSchema, codecProvider));
168 private static Element createElementFor(Document doc, QName dataType) {
169 if (dataType.getNamespace() != null) {
170 return doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName());
172 return doc.createElementNS(null, dataType.getLocalName());
176 public static void writeValueByType(Element element, SimpleNode<?> node, TypeDefinition<?> type,
177 DataSchemaNode schema, XmlCodecProvider codecProvider) {
179 TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
181 if (baseType instanceof IdentityrefTypeDefinition) {
182 if (node.getValue() instanceof QName) {
183 QName value = (QName) node.getValue();
185 if (value.getPrefix() != null && !value.getPrefix().isEmpty()) {
186 prefix = value.getPrefix();
188 element.setAttribute("xmlns:" + prefix, value.getNamespace().toString());
189 element.setTextContent(prefix + ":" + value.getLocalName());
191 logger.debug("Value of {}:{} is not instance of QName but is {}", baseType.getQName().getNamespace(), //
192 baseType.getQName().getLocalName(), //
193 node.getValue().getClass());
194 element.setTextContent(String.valueOf(node.getValue()));
197 if (node.getValue() != null) {
199 String value = codecProvider.codecFor(baseType).serialize(node.getValue());
200 element.setTextContent(value);
201 } catch (ClassCastException e) {
202 element.setTextContent(String.valueOf(node.getValue()));
203 logger.error("Provided node did not have type required by mapping. Using stream instead. {}", e);
209 public final static TypeDefinition<?> resolveBaseTypeFrom(TypeDefinition<?> type) {
210 TypeDefinition<?> superType = type;
211 while (superType.getBaseType() != null) {
212 superType = superType.getBaseType();
217 private static final DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
218 if (dataSchemaNode != null && node != null) {
219 for (DataSchemaNode dsn : dataSchemaNode) {
220 if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
222 } else if (dsn instanceof ChoiceNode) {
223 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
224 DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
225 if (foundDsn != null) {
235 private static final Optional<DataSchemaNode> findFirstSchema(QName qname, Set<DataSchemaNode> dataSchemaNode) {
236 if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
237 for (DataSchemaNode dsn : dataSchemaNode) {
238 if (qname.isEqualWithoutRevision(dsn.getQName())) {
239 return Optional.<DataSchemaNode> of(dsn);
240 } else if (dsn instanceof ChoiceNode) {
241 for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
242 Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
243 if (foundDsn != null) {
250 return Optional.absent();
253 public static final XmlCodecProvider defaultValueCodecProvider() {
254 return DEFAULT_XML_VALUE_CODEC_PROVIDER;
257 public static Node<?> toDomNode(Document doc) {
258 return toDomNode(doc.getDocumentElement());
261 private static Node<?> toDomNode(Element element) {
262 QName qname = qNameFromElement(element);
264 ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
265 NodeList nodes = element.getChildNodes();
266 boolean isSimpleObject = true;
268 for (int i = 0; i < nodes.getLength(); i++) {
269 org.w3c.dom.Node child = nodes.item(i);
270 if (child instanceof Element) {
271 isSimpleObject = false;
272 values.add(toDomNode((Element) child));
274 if (isSimpleObject && child instanceof org.w3c.dom.Text) {
275 value = element.getTextContent();
276 if (!Strings.isNullOrEmpty(value)) {
277 isSimpleObject = true;
281 if (isSimpleObject) {
282 return new SimpleNodeTOImpl<>(qname, null, value);
284 return new ImmutableCompositeNode(qname, values.build());
287 public static List<Node<?>> toDomNodes(final Element element, final Optional<Set<DataSchemaNode>> context) {
288 return forEachChild(element.getChildNodes(), new Function<Element, Optional<Node<?>>>() {
291 public Optional<Node<?>> apply(Element input) {
292 if (context.isPresent()) {
293 QName partialQName = qNameFromElement(input);
294 Optional<DataSchemaNode> schemaNode = findFirstSchema(partialQName, context.get());
295 if (schemaNode.isPresent()) {
296 return Optional.<Node<?>> fromNullable(//
297 toNodeWithSchema(input, schemaNode.get(), DEFAULT_XML_VALUE_CODEC_PROVIDER));
300 return Optional.<Node<?>> fromNullable(toDomNode(element));
307 private static final <T> List<T> forEachChild(NodeList nodes, Function<Element, Optional<T>> forBody) {
308 ImmutableList.Builder<T> ret = ImmutableList.<T> builder();
309 for (int i = 0; i < nodes.getLength(); i++) {
310 org.w3c.dom.Node child = nodes.item(i);
311 if(child instanceof Element) {
312 Optional<T> result = forBody.apply((Element) child);
313 if(result.isPresent()) {
314 ret.add(result.get());