2 * Copyright (c) 2013 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.impl.schema.transform.base.parser;
10 import java.util.List;
12 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
13 import org.opendaylight.yangtools.yang.data.impl.schema.transform.ToNormalizedNodeParserFactory;
14 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
15 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import com.google.common.base.Preconditions;
25 * Dispatches the parsing process of elements according to schema and returns the parsed Node.
27 * @param <E> type of elements parsed
29 public interface NodeParserDispatcher<E> {
31 DataContainerChild<?, ?> dispatchChildElement(Object schema, List<E> childNodes);
34 * Abstract implementation that implements the dispatch conditions. Only requires parsers to be provided.
35 * The same instance of parser can be provided in case it is immutable.
37 public static abstract class BaseNodeParserDispatcher<E> implements NodeParserDispatcher<E> {
38 private final ToNormalizedNodeParserFactory<E> factory;
40 protected BaseNodeParserDispatcher(final ToNormalizedNodeParserFactory<E> factory) {
41 this.factory = Preconditions.checkNotNull(factory);
45 public final DataContainerChild<?, ?> dispatchChildElement(Object schema, List<E> childNodes) {
46 Preconditions.checkArgument(childNodes.isEmpty() == false);
48 if (schema instanceof ContainerSchemaNode) {
49 return factory.getContainerNodeParser().parse(childNodes, (ContainerSchemaNode) schema);
50 } else if (schema instanceof LeafSchemaNode) {
51 return factory.getLeafNodeParser().parse(childNodes, (LeafSchemaNode) schema);
52 } else if (schema instanceof LeafListSchemaNode) {
53 return factory.getLeafSetNodeParser().parse(childNodes, (LeafListSchemaNode) schema);
54 } else if (schema instanceof ListSchemaNode) {
55 return factory.getMapNodeParser().parse(childNodes, (ListSchemaNode) schema);
56 } else if (schema instanceof ChoiceNode) {
57 return factory.getChoiceNodeParser().parse(childNodes, (ChoiceNode) schema);
58 } else if (schema instanceof AugmentationSchema) {
59 return factory.getAugmentationNodeParser().parse(childNodes, (AugmentationSchema) schema);
62 throw new IllegalArgumentException("Unable to parse node, unknown schema type: " + schema.getClass());