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 com.google.common.base.Preconditions;
11 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.AnyXmlSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
16 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.YangModeledAnyXmlSchemaNode;
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 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(final Object schema, final List<E> childNodes) {
46 Preconditions.checkArgument(!childNodes.isEmpty());
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 final ListSchemaNode listSchemaNode = (ListSchemaNode)schema;
56 if (listSchemaNode.getKeyDefinition().isEmpty()) {
57 return factory.getUnkeyedListNodeParser().parse(childNodes, listSchemaNode);
58 } else if (listSchemaNode.isUserOrdered()) {
59 return factory.getOrderedListNodeParser().parse(childNodes, listSchemaNode);
61 return factory.getMapNodeParser().parse(childNodes, listSchemaNode);
63 } else if (schema instanceof ChoiceSchemaNode) {
64 return factory.getChoiceNodeParser().parse(childNodes, (ChoiceSchemaNode) schema);
65 } else if (schema instanceof AugmentationSchema) {
66 return factory.getAugmentationNodeParser().parse(childNodes, (AugmentationSchema) schema);
67 } else if (schema instanceof YangModeledAnyXmlSchemaNode) {
68 return factory.getYangModeledAnyXmlNodeParser().parse(childNodes,(YangModeledAnyXmlSchemaNode)schema);
69 } else if (schema instanceof AnyXmlSchemaNode) {
70 return factory.getAnyXmlNodeParser().parse(childNodes,(AnyXmlSchemaNode)schema);
73 throw new IllegalArgumentException("Unable to parse node, unknown schema type: " + schema.getClass());