2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.model.export;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
24 * Factory for creating {@link XMLEventReader} instances reporting events equivalent to reading a YIN document defining
25 * a specified {@link ModuleEffectiveStatement}.
29 public final class YinXMLEventReaderFactory {
30 private static final Location DUMMY_LOCATION = new Location() {
33 public int getLineNumber() {
38 public int getColumnNumber() {
43 public int getCharacterOffset() {
48 public String getPublicId() {
53 public String getSystemId() {
58 private static final YinXMLEventReaderFactory DEFAULT;
61 final XMLEventFactory eventFactory = XMLEventFactory.newFactory();
62 eventFactory.setLocation(DUMMY_LOCATION);
63 DEFAULT = new YinXMLEventReaderFactory(eventFactory);
66 private final XMLEventFactory eventFactory;
68 private YinXMLEventReaderFactory(final XMLEventFactory eventFactory) {
69 this.eventFactory = requireNonNull(eventFactory);
73 * Get the system-wide default instance, backed by system-wide default XMLEventFactory.
75 * @return Default instance.
77 public static YinXMLEventReaderFactory defaultInstance() {
81 public static YinXMLEventReaderFactory ofEventFactory(final XMLEventFactory factory) {
82 return new YinXMLEventReaderFactory(factory);
86 * Create a new XMLEventReader iterating of the YIN document equivalent of an effective module.
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
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);
97 return new YinXMLEventReader(eventFactory, new ModuleNamespaceContext(module), declared);
101 * Create a new XMLEventReader iterating of the YIN document equivalent of an effective submodule.
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
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);