2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.util;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableMap;
15 import java.io.IOException;
16 import java.util.Objects;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter.MetadataExtension;
23 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
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;
32 * Utility abstract class for tracking parser state, as needed by StAX-like parser.
33 * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
36 public abstract sealed class AbstractNodeDataWithSchema<T extends DataSchemaNode>
37 permits SimpleNodeDataWithSchema, CompositeNodeDataWithSchema {
38 private final T schema;
39 private ImmutableMap<QName, Object> attributes;
41 AbstractNodeDataWithSchema(final T schema) {
42 this.schema = requireNonNull(schema);
45 public static @NonNull AbstractNodeDataWithSchema<?> of(final DataSchemaNode schema) {
46 if (schema instanceof ContainerLike containerLike) {
47 return new ContainerNodeDataWithSchema(containerLike);
48 } else if (schema instanceof ListSchemaNode list) {
49 return new ListNodeDataWithSchema(list);
50 } else if (schema instanceof AnyxmlSchemaNode anyxml) {
51 return new AnyXmlNodeDataWithSchema(anyxml);
52 } else if (schema instanceof LeafSchemaNode leaf) {
53 return new LeafNodeDataWithSchema(leaf);
54 } else if (schema instanceof LeafListSchemaNode leafList) {
55 return new LeafListNodeDataWithSchema(leafList);
56 } else if (schema instanceof AnydataSchemaNode anydata) {
57 return new AnydataNodeDataWithSchema(anydata);
59 throw new IllegalStateException("Unsupported schema " + schema);
64 * Return the associated schema node.
66 * @return Associated schema node.
68 public final @NonNull T getSchema() {
73 * Set the associated attributes.
75 * @param attributes parsed attributes
77 public final void setAttributes(final ImmutableMap<QName, Object> attributes) {
78 checkState(this.attributes == null, "Node '%s' has already set its attributes to %s.", getSchema().getQName(),
80 this.attributes = attributes;
84 * Return the associated attributes.
86 * @return associated attributes
88 public final ImmutableMap<QName, Object> getAttributes() {
93 * Emit this node's events into the specified writer.
95 * @param writer Target writer
96 * @throws IOException reported when thrown by the writer.
98 public final void write(final NormalizedNodeStreamWriter writer) throws IOException {
99 write(writer, writer.extension(MetadataExtension.class));
102 protected abstract void write(NormalizedNodeStreamWriter writer, @Nullable MetadataExtension metaWriter)
105 protected final NodeIdentifier provideNodeIdentifier() {
106 return NodeIdentifier.create(schema.getQName());
109 protected final void writeMetadata(final MetadataExtension metaWriter) throws IOException {
110 if (metaWriter != null && attributes != null && !attributes.isEmpty()) {
111 metaWriter.metadata(attributes);
116 public int hashCode() {
117 final int prime = 31;
119 result = prime * result + Objects.hashCode(schema);
124 public boolean equals(final Object obj) {
131 if (getClass() != obj.getClass()) {
134 final AbstractNodeDataWithSchema<?> other = (AbstractNodeDataWithSchema<?>) obj;
135 return schema.equals(other.schema);