BUG-7062: add revision awareness to Yin/YangTextSchemaSource
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YangTextSchemaSource.java
index 50ee12ef4d03358729456081c76216a020f5db71..82651b466a791c5b4da5af0d258b192423f0b5d7 100644 (file)
@@ -3,30 +3,31 @@
  *
  * 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
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.model.repo.api;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
-import com.google.common.base.Objects;
-import com.google.common.base.Objects.ToStringHelper;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Optional;
 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 org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.common.YangNames;
 
 /**
  * YANG text schema source representation. Exposes an RFC6020 text representation
  * as an {@link InputStream}.
  */
 @Beta
-public abstract class YangTextSchemaSource extends ByteSource implements SchemaSourceRepresentation {
+public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation {
     private final SourceIdentifier identifier;
 
     protected YangTextSchemaSource(final SourceIdentifier identifier) {
@@ -34,9 +35,12 @@ public abstract class YangTextSchemaSource extends ByteSource implements SchemaS
     }
 
     public static SourceIdentifier identifierFromFilename(final String name) {
-        checkArgument(name.endsWith(".yang"), "Filename %s does not have a .yang extension", name);
-        // FIXME: add revision-awareness
-        return SourceIdentifier.create(name.substring(0, name.length() - 5), Optional.<String>absent());
+        checkArgument(name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION), "Filename %s does not have a .yang extension",
+            name);
+
+        final String baseName = name.substring(0, name.length() - YangConstants.RFC6020_YANG_FILE_EXTENSION.length());
+        final Entry<String, String> parsed = YangNames.parseFilename(baseName);
+        return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
     }
 
     /**
@@ -57,7 +61,7 @@ public abstract class YangTextSchemaSource extends ByteSource implements SchemaS
 
     @Override
     public final String toString() {
-        return addToStringAttributes(Objects.toStringHelper(this).add("identifier", identifier)).toString();
+        return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
     }
 
     /**
@@ -92,7 +96,7 @@ public abstract class YangTextSchemaSource extends ByteSource implements SchemaS
         }
 
         @Override
-        public final ByteSource getDelegate() {
+        public ByteSource getDelegate() {
             return delegate;
         }