Populate data/ hierarchy
[yangtools.git] / yang / yang-model-export / src / main / java / org / opendaylight / yangtools / yang / model / export / YinXMLEventReaderFactory.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.export;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import javax.xml.stream.Location;
15 import javax.xml.stream.XMLEventFactory;
16 import javax.xml.stream.XMLEventReader;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
21
22 /**
23  * Factory for creating {@link XMLEventReader} instances reporting events equivalent to reading a YIN document defining
24  * a specified {@link ModuleEffectiveStatement}. This class is thread-safe.
25  */
26 @Beta
27 public final class YinXMLEventReaderFactory {
28     private static final Location DUMMY_LOCATION = new Location() {
29
30         @Override
31         public int getLineNumber() {
32             return -1;
33         }
34
35         @Override
36         public int getColumnNumber() {
37             return -1;
38         }
39
40         @Override
41         public int getCharacterOffset() {
42             return -1;
43         }
44
45         @Override
46         public String getPublicId() {
47             return null;
48         }
49
50         @Override
51         public String getSystemId() {
52             return null;
53         }
54     };
55
56     private static final YinXMLEventReaderFactory DEFAULT;
57
58     static {
59         final XMLEventFactory eventFactory = XMLEventFactory.newFactory();
60         eventFactory.setLocation(DUMMY_LOCATION);
61         DEFAULT = new YinXMLEventReaderFactory(eventFactory);
62     }
63
64     private final XMLEventFactory eventFactory;
65
66     private YinXMLEventReaderFactory(final XMLEventFactory eventFactory) {
67         this.eventFactory = requireNonNull(eventFactory);
68     }
69
70     /**
71      * Get the system-wide default instance, backed by system-wide default XMLEventFactory.
72      *
73      * @return Default instance.
74      */
75     public static YinXMLEventReaderFactory defaultInstance() {
76         return DEFAULT;
77     }
78
79     public static YinXMLEventReaderFactory ofEventFactory(final XMLEventFactory factory) {
80         return new YinXMLEventReaderFactory(factory);
81     }
82
83     /**
84      * Create a new XMLEventReader iterating of the YIN document equivalent of an effective module.
85      *
86      * @param module Effective module
87      * @return A new XMLEventReader.
88      * @throws NullPointerException if module is null
89      * @throws IllegalArgumentException if the specified module does not expose declared model
90      */
91     public XMLEventReader createXMLEventReader(final ModuleEffectiveStatement module) {
92         final ModuleStatement declared = module.getDeclared();
93         checkArgument(declared != null, "Module %s does not expose declared model", module);
94
95         return new YinXMLEventReader(eventFactory, new ModuleNamespaceContext(module), declared);
96     }
97
98     /**
99      * Create a new XMLEventReader iterating of the YIN document equivalent of an effective submodule.
100      *
101      * @param module Effective module
102      * @param submodule Effective submodule
103      * @return A new XMLEventReader.
104      * @throws NullPointerException if any argument is null
105      * @throws IllegalArgumentException if the specified submodule does not expose declared model
106      */
107     public XMLEventReader createXMLEventReader(final ModuleEffectiveStatement module,
108             final SubmoduleEffectiveStatement submodule) {
109         final SubmoduleStatement declared = submodule.getDeclared();
110         checkArgument(declared != null, "Submodule %s does not expose declared model", submodule);
111         return new YinXMLEventReader(eventFactory, new ModuleNamespaceContext(module), declared);
112     }
113 }