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;
24 * Dispatches the parsing process of elements according to schema and returns the parsed Node.
26 * @param <E> type of elements parsed
28 public interface NodeParserDispatcher<E> {
30 DataContainerChild<?, ?> dispatchChildElement(Object schema, List<E> childNodes);
33 * Abstract implementation that implements the dispatch conditions. Only requires parsers to be provided.
34 * The same instance of parser can be provided in case it is immutable.
36 public abstract static class BaseNodeParserDispatcher<E> implements NodeParserDispatcher<E> {
37 private final ToNormalizedNodeParserFactory<E> factory;
39 protected BaseNodeParserDispatcher(final ToNormalizedNodeParserFactory<E> factory) {
40 this.factory = Preconditions.checkNotNull(factory);
44 public final DataContainerChild<?, ?> dispatchChildElement(final Object schema, final List<E> childNodes) {
45 Preconditions.checkArgument(!childNodes.isEmpty());
47 if (schema instanceof ContainerSchemaNode) {
48 return factory.getContainerNodeParser().parse(childNodes, (ContainerSchemaNode) schema);
49 } else if (schema instanceof LeafSchemaNode) {
50 return factory.getLeafNodeParser().parse(childNodes, (LeafSchemaNode) schema);
51 } else if (schema instanceof LeafListSchemaNode) {
52 return factory.getLeafSetNodeParser().parse(childNodes, (LeafListSchemaNode) schema);
53 } else if (schema instanceof ListSchemaNode) {
54 final ListSchemaNode listSchemaNode = (ListSchemaNode)schema;
55 if (listSchemaNode.getKeyDefinition().isEmpty()) {
56 return factory.getUnkeyedListNodeParser().parse(childNodes, listSchemaNode);
57 } else if (listSchemaNode.isUserOrdered()) {
58 return factory.getOrderedListNodeParser().parse(childNodes, listSchemaNode);
60 return factory.getMapNodeParser().parse(childNodes, listSchemaNode);
62 } else if (schema instanceof ChoiceSchemaNode) {
63 return factory.getChoiceNodeParser().parse(childNodes, (ChoiceSchemaNode) schema);
64 } else if (schema instanceof AugmentationSchema) {
65 return factory.getAugmentationNodeParser().parse(childNodes, (AugmentationSchema) schema);
66 } else if (schema instanceof AnyXmlSchemaNode) {
67 return factory.getAnyXmlNodeParser().parse(childNodes,(AnyXmlSchemaNode)schema);
70 throw new IllegalArgumentException("Unable to parse node, unknown schema type: " + schema.getClass());