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 com.google.common.collect.Iterables;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
19 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 * Abstract(base) parser for LeafSetEntryNodes, parses elements of type E.
24 * @param <E> type of elements to be parsed
26 public abstract class LeafSetEntryNodeBaseParser<E> implements ExtensibleParser<NodeWithValue, E, LeafSetEntryNode<?>, LeafListSchemaNode> {
28 private final BuildingStrategy<NodeWithValue, LeafSetEntryNode<?>> buildingStrategy;
30 public LeafSetEntryNodeBaseParser() {
31 buildingStrategy = new SimpleLeafSetEntryBuildingStrategy();
34 public LeafSetEntryNodeBaseParser(final BuildingStrategy<NodeWithValue, LeafSetEntryNode<?>> buildingStrategy) {
35 this.buildingStrategy = buildingStrategy;
38 @SuppressWarnings("unchecked")
40 public final LeafSetEntryNode<?> parse(final Iterable<E> elements, final LeafListSchemaNode schema) {
41 final int size = Iterables.size(elements);
42 Preconditions.checkArgument(size == 1, "Xml elements mapped to leaf node illegal count: %s", size);
44 final E e = elements.iterator().next();
45 Object value = parseLeafListEntry(e,schema);
47 NormalizedNodeAttrBuilder<NodeWithValue, Object, LeafSetEntryNode<Object>> leafEntryBuilder = Builders
48 .leafSetEntryBuilder(schema);
49 leafEntryBuilder.withAttributes(getAttributes(e));
50 leafEntryBuilder.withValue(value);
52 final BuildingStrategy rawBuildingStrat = buildingStrategy;
53 return (LeafSetEntryNode<?>) rawBuildingStrat.build(leafEntryBuilder);
57 public BuildingStrategy<NodeWithValue, LeafSetEntryNode<?>> getBuildingStrategy() {
58 return buildingStrategy;
63 * Parse the inner value of a LeafSetEntryNode from element of type E.
65 * @param element to be parsed
66 * @param schema schema for leaf-list
67 * @return parsed element as an Object
69 protected abstract Object parseLeafListEntry(E element, LeafListSchemaNode schema);
73 * @param e element to be parsed
74 * @return attributes mapped to QNames
76 protected abstract Map<QName, String> getAttributes(E e);
78 public static class SimpleLeafSetEntryBuildingStrategy implements BuildingStrategy<NodeWithValue, LeafSetEntryNode<?>> {
81 public LeafSetEntryNode<?> build(final NormalizedNodeBuilder<NodeWithValue, ?, LeafSetEntryNode<?>> builder) {
82 return builder.build();
86 public void prepareAttributes(final Map<QName, String> attributes, final NormalizedNodeBuilder<NodeWithValue, ?, LeafSetEntryNode<?>> containerBuilder) {