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