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.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;
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.
27 public final class YinXMLEventReaderFactory {
28 private static final Location DUMMY_LOCATION = new Location() {
31 public int getLineNumber() {
36 public int getColumnNumber() {
41 public int getCharacterOffset() {
46 public String getPublicId() {
51 public String getSystemId() {
56 private static final YinXMLEventReaderFactory DEFAULT;
59 final XMLEventFactory eventFactory = XMLEventFactory.newFactory();
60 eventFactory.setLocation(DUMMY_LOCATION);
61 DEFAULT = new YinXMLEventReaderFactory(eventFactory);
64 private final XMLEventFactory eventFactory;
66 private YinXMLEventReaderFactory(final XMLEventFactory eventFactory) {
67 this.eventFactory = requireNonNull(eventFactory);
71 * Get the system-wide default instance, backed by system-wide default XMLEventFactory.
73 * @return Default instance.
75 public static YinXMLEventReaderFactory defaultInstance() {
79 public static YinXMLEventReaderFactory ofEventFactory(final XMLEventFactory factory) {
80 return new YinXMLEventReaderFactory(factory);
84 * Create a new XMLEventReader iterating of the YIN document equivalent of an effective module.
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
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);
95 return new YinXMLEventReader(eventFactory, new ModuleNamespaceContext(module), declared);
99 * Create a new XMLEventReader iterating of the YIN document equivalent of an effective submodule.
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
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);