import static java.util.Objects.requireNonNull;
-import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.SetMultimap;
import java.io.IOException;
-import java.util.Collection;
import java.util.List;
-import java.util.Set;
import javax.xml.transform.TransformerException;
import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.api.YinDomSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource;
+import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.api.source.YinTextSource;
+import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
+import org.opendaylight.yangtools.yang.model.spi.source.YangIRSource;
+import org.opendaylight.yangtools.yang.model.spi.source.YinDomSource;
+import org.opendaylight.yangtools.yang.model.spi.source.YinXmlSource;
import org.opendaylight.yangtools.yang.parser.api.YangParser;
import org.opendaylight.yangtools.yang.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
import org.xml.sax.SAXException;
final class DefaultYangParser implements YangParser {
- static final @NonNull Collection<Class<? extends SchemaSourceRepresentation>> REPRESENTATIONS =
- ImmutableList.of(IRSchemaSource.class, YangTextSchemaSource.class, YinDomSchemaSource.class,
- YinXmlSchemaSource.class, YinTextSchemaSource.class);
+ static final @NonNull ImmutableSet<Class<? extends SourceRepresentation>> REPRESENTATIONS = ImmutableSet.of(
+ // In order of preference
+ YangIRSource.class,
+ YangTextSource.class,
+ YinDomSource.class,
+ YinXmlSource.class,
+ YinTextSource.class);
private final BuildAction buildAction;
}
@Override
- public @NonNull Collection<Class<? extends SchemaSourceRepresentation>> supportedSourceRepresentations() {
+ public ImmutableSet<Class<? extends SourceRepresentation>> supportedSourceRepresentations() {
return REPRESENTATIONS;
}
@Override
- public @NonNull YangParser addSource(final SchemaSourceRepresentation source) throws IOException,
- YangSyntaxErrorException {
+ public YangParser addSource(final SourceRepresentation source) throws IOException, YangSyntaxErrorException {
buildAction.addSource(sourceToStatementStream(source));
return this;
}
@Override
- public @NonNull YangParser addLibSource(final SchemaSourceRepresentation source) throws IOException,
- YangSyntaxErrorException {
+ public YangParser addLibSource(final SourceRepresentation source) throws IOException, YangSyntaxErrorException {
buildAction.addLibSource(sourceToStatementStream(source));
return this;
}
@Override
- public @NonNull YangParser setSupportedFeatures(final Set<QName> supportedFeatures) {
+ public YangParser setSupportedFeatures(final FeatureSet supportedFeatures) {
buildAction.setSupportedFeatures(supportedFeatures);
return this;
}
@Override
- public @NonNull YangParser setModulesWithSupportedDeviations(
+ public YangParser setModulesWithSupportedDeviations(
final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
buildAction.setModulesWithSupportedDeviations(modulesDeviatedByModules);
return this;
}
@Override
- public @NonNull List<DeclaredStatement<?>> buildDeclaredModel() throws YangParserException {
+ public List<DeclaredStatement<?>> buildDeclaredModel() throws YangParserException {
try {
return buildAction.build().getRootStatements();
} catch (ReactorException e) {
}
@Override
- public @NonNull EffectiveModelContext buildEffectiveModel() throws YangParserException {
+ public EffectiveModelContext buildEffectiveModel() throws YangParserException {
try {
return buildAction.buildEffective();
} catch (ReactorException e) {
return new YangParserException("Failed to assemble sources", reported);
}
- static StatementStreamSource sourceToStatementStream(final SchemaSourceRepresentation source)
+ static StatementStreamSource sourceToStatementStream(final SourceRepresentation source)
throws IOException, YangSyntaxErrorException {
- requireNonNull(source);
- if (source instanceof IRSchemaSource) {
- return YangStatementStreamSource.create((IRSchemaSource) source);
- } else if (source instanceof YangTextSchemaSource) {
- return YangStatementStreamSource.create((YangTextSchemaSource) source);
- } else if (source instanceof YinDomSchemaSource) {
- return YinStatementStreamSource.create((YinDomSchemaSource) source);
- } else if (source instanceof YinTextSchemaSource) {
- try {
- return YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
- (YinTextSchemaSource) source));
- } catch (SAXException e) {
- throw new YangSyntaxErrorException(source.getIdentifier(), 0, 0, "Failed to parse XML text", e);
+ return switch (source) {
+ case YangIRSource irSource -> YangStatementStreamSource.create(irSource);
+ case YangTextSource yangSource -> YangStatementStreamSource.create(yangSource);
+ case YinDomSource yinDom -> YinStatementStreamSource.create(yinDom);
+ case YinTextSource yinText -> {
+ try {
+ yield YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(yinText));
+ } catch (SAXException e) {
+ throw new YangSyntaxErrorException(source.sourceId(), 0, 0, "Failed to parse XML text", e);
+ }
}
- } else if (source instanceof YinXmlSchemaSource) {
- try {
- return YinStatementStreamSource.create((YinXmlSchemaSource) source);
- } catch (TransformerException e) {
- throw new YangSyntaxErrorException(source.getIdentifier(), 0, 0,
- "Failed to assemble in-memory representation", e);
+ case YinXmlSource yinXml -> {
+ try {
+ yield YinStatementStreamSource.create(yinXml);
+ } catch (TransformerException e) {
+ throw new YangSyntaxErrorException(source.sourceId(), 0, 0,
+ "Failed to assemble in-memory representation", e);
+ }
}
- } else {
- throw new IllegalArgumentException("Unsupported source " + source);
- }
+ default -> throw new IllegalArgumentException("Unsupported source " + source);
+ };
}
}