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.rfc7952.data.api.StreamWriterMetadataExtension;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 * Utility abstract class for tracking parser state, as needed by StAX-like parser.
27 * This class is to be used only by respective XML and JSON parsers in yang-data-codec-xml and yang-data-codec-gson.
30 public abstract class AbstractNodeDataWithSchema<T extends DataSchemaNode> {
31 private final T schema;
32 private ImmutableMap<QName, Object> attributes;
34 public AbstractNodeDataWithSchema(final T schema) {
35 this.schema = requireNonNull(schema);
39 * Return the associated schema node.
41 * @return Associated schema node.
43 public final @NonNull T getSchema() {
48 * Set the associated attributes.
50 * @param attributes parsed attributes
52 public final void setAttributes(final ImmutableMap<QName, Object> attributes) {
53 checkState(this.attributes == null, "Node '%s' has already set its attributes to %s.", getSchema().getQName(),
55 this.attributes = attributes;
59 * Return the associated attributes.
61 * @return associated attributes
63 public final ImmutableMap<QName, Object> getAttributes() {
68 * Emit this node's events into the specified writer.
70 * @param writer Target writer
71 * @throws IOException reported when thrown by the writer.
73 public final void write(final NormalizedNodeStreamWriter writer) throws IOException {
74 write(writer, writer.getExtensions().getInstance(StreamWriterMetadataExtension.class));
77 protected abstract void write(NormalizedNodeStreamWriter writer,
78 @Nullable StreamWriterMetadataExtension metaWriter) throws IOException;
80 protected final NodeIdentifier provideNodeIdentifier() {
81 return NodeIdentifier.create(schema.getQName());
84 protected final void writeMetadata(final StreamWriterMetadataExtension metaWriter) throws IOException {
85 if (metaWriter != null && attributes != null && !attributes.isEmpty()) {
86 metaWriter.metadata(attributes);
91 public int hashCode() {
94 result = prime * result + Objects.hashCode(schema);
99 public boolean equals(final Object obj) {
106 if (getClass() != obj.getClass()) {
109 final AbstractNodeDataWithSchema<?> other = (AbstractNodeDataWithSchema<?>) obj;
110 return schema.equals(other.schema);