/* * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/eplv10.html */ package org.opendaylight.yangtools.yang.model.util.repo; import java.io.ByteArrayInputStream; import java.io.InputStream; import org.opendaylight.yangtools.concepts.Delegator; import com.google.common.base.Charsets; import com.google.common.base.Optional; public class SchemaSourceProviders { @SuppressWarnings("rawtypes") private static final SchemaSourceProvider NOOP_PROVIDER = new AdvancedSchemaSourceProvider() { @Override public Optional getSchemaSource(final String moduleName, final Optional revision) { return Optional.absent(); } @Override public Optional getSchemaSource(final SourceIdentifier sourceIdentifier) { return Optional.absent(); } }; @SuppressWarnings("unchecked") public static SchemaSourceProvider noopProvider() { return NOOP_PROVIDER; } public static SchemaSourceProvider inputStreamProviderfromStringProvider( final AdvancedSchemaSourceProvider delegate) { return new StringToInputStreamSchemaSourceProvider(delegate); } public static AdvancedSchemaSourceProvider toAdvancedSchemaSourceProvider(final SchemaSourceProvider schemaSourceProvider) { if (schemaSourceProvider instanceof AdvancedSchemaSourceProvider) { return (AdvancedSchemaSourceProvider) schemaSourceProvider; } return new SchemaSourceCompatibilityWrapper(schemaSourceProvider); } private final static class StringToInputStreamSchemaSourceProvider implements // AdvancedSchemaSourceProvider, Delegator> { private final AdvancedSchemaSourceProvider delegate; public StringToInputStreamSchemaSourceProvider(final AdvancedSchemaSourceProvider delegate) { this.delegate = delegate; } @Override public AdvancedSchemaSourceProvider getDelegate() { return delegate; } @Override public Optional getSchemaSource(final SourceIdentifier sourceIdentifier) { Optional potentialSource = getDelegate().getSchemaSource(sourceIdentifier); if (potentialSource.isPresent()) { final String stringSource = potentialSource.get(); return Optional. of( new ByteArrayInputStream(stringSource.getBytes(Charsets.UTF_8))); } return Optional.absent(); } @Override public Optional getSchemaSource(final String moduleName, final Optional revision) { return getSchemaSource(SourceIdentifier.create(moduleName, revision)); } } private final static class SchemaSourceCompatibilityWrapper implements // AdvancedSchemaSourceProvider, // Delegator> { private final SchemaSourceProvider delegate; public SchemaSourceCompatibilityWrapper(final SchemaSourceProvider delegate) { this.delegate = delegate; } @Override public SchemaSourceProvider getDelegate() { return delegate; } @Override public Optional getSchemaSource(final SourceIdentifier sourceIdentifier) { final String moduleName = sourceIdentifier.getName(); Optional revision = Optional.fromNullable(sourceIdentifier.getRevision()); return delegate.getSchemaSource(moduleName, revision); } @Override public Optional getSchemaSource(final String moduleName, final Optional revision) { return delegate.getSchemaSource(moduleName, revision); } } }