Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YinTextSchemaSource.java
index ce6e3ce4f4d114e75077ecced4d25c023f868456..ed96d9d7185936d825aad1071a8eec6d647354a2 100644 (file)
@@ -15,13 +15,22 @@ import com.google.common.base.Preconditions;
 import com.google.common.io.ByteSource;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Map.Entry;
+import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.common.YangNames;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * YIN text schema source representation. Exposes an RFC6020 XML representation as an {@link InputStream}.
  */
 @Beta
 public abstract class YinTextSchemaSource extends ByteSource implements YinSchemaSourceRepresentation {
+    private static final Logger LOG = LoggerFactory.getLogger(YinTextSchemaSource.class);
+    private static final String XML_EXTENSION = ".xml";
+
     private final SourceIdentifier identifier;
 
     protected YinTextSchemaSource(final SourceIdentifier identifier) {
@@ -30,14 +39,18 @@ public abstract class YinTextSchemaSource extends ByteSource implements YinSchem
 
     public static SourceIdentifier identifierFromFilename(final String name) {
         final String baseName;
-        if (name.endsWith(".xml") || name.endsWith(".yin")) {
-            baseName = name.substring(0, name.length() - 4);
+        if (name.endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION)) {
+            baseName = name.substring(0, name.length() - YangConstants.RFC6020_YIN_FILE_EXTENSION.length());
+        } else if (name.endsWith(XML_EXTENSION)) {
+            // FIXME: BUG-7061: remove this once we do not need it
+            LOG.warn("XML file {} being loaded as YIN", name);
+            baseName = name.substring(0, name.length() - XML_EXTENSION.length());
         } else {
             throw new IllegalArgumentException("Filename " + name + " does not have a .yin or .xml extension");
         }
 
-        // FIXME: add revision-awareness
-        return SourceIdentifier.create(baseName, Optional.absent());
+        final Entry<String, String> parsed = YangNames.parseFilename(baseName);
+        return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
     }
 
     /**
@@ -51,6 +64,7 @@ public abstract class YinTextSchemaSource extends ByteSource implements YinSchem
     /**
      * {@inheritDoc}
      */
+    @Nonnull
     @Override
     public Class<? extends YinTextSchemaSource> getType() {
         return YinTextSchemaSource.class;