/*
 * Copyright 2013, Bruce Mitchener, Jr.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dylanfoundry.deft.registry;

import com.intellij.openapi.diagnostic.Logger;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class DeftRegistryInfo {
  public static final Logger LOG = Logger.getInstance("#org.dylanfoundry.deft.registry");
  private List<DeftRegistryEntryInfo> entries = new ArrayList<DeftRegistryEntryInfo>();

  public DeftRegistryInfo(String location) {
  }

  public List<DeftRegistryEntryInfo> getEntries() {
    return entries;
  }

  @Nullable
  public static DeftRegistryEntryInfo parseRegistryEntry(String location) {
    File file = new File(location);

    // registry files are relative
    File sourceRoot = file.getParentFile().getParentFile().getParentFile();

    try {
      // Registry entry files are a single line with a URL on the first line.
      BufferedReader reader = new BufferedReader(new FileReader(file));
      String line = reader.readLine();

      URI registryUri = new URI(line);
      if (!registryUri.getScheme().equals("abstract")) {
        LOG.warn("Registry entries should begin with abstract://");
        return null;
      }

      String[] pathComponents = registryUri.getPath().split("/");

      StringBuilder sourcePath = new StringBuilder();
      sourcePath.append(sourceRoot.getAbsolutePath());
      sourcePath.append(File.separator);
      for (int i = 1; i < pathComponents.length; i++) {
        if (i > 1) {
          sourcePath.append(File.separator);
        }

        sourcePath.append(pathComponents[i]);
      }

      String libraryName = file.getName();
      String platformName = file.getParentFile().getName();
      return new DeftRegistryEntryInfo(libraryName, platformName,
              sourcePath.toString());
    } catch (URISyntaxException use) {
      LOG.warn("Invalid registry entry", use);
    } catch (IOException ioe) {
      LOG.warn("Invalid registry file", ioe);
    }

    return null;
  }
}

@@ -679,24 +679,118 @@
 tic.Logger;%0A
+import com.intellij.openapi.vfs.LocalFileSystem;%0Aimport com.intellij.openapi.vfs.VirtualFile;%0A
 import org.j
@@ -1159,16 +1159,43 @@
 stry%22);%0A
+  private String location;%0A
   privat
@@ -1320,16 +1320,75 @@
 tion) %7B%0A
+    this.location = location;%0A    parseRegistry(location);%0A
   %7D%0A%0A  p
@@ -1461,16 +1461,573 @@
 s;%0A  %7D%0A%0A
+  public String getLocation() %7B%0A    return location;%0A  %7D%0A%0A  private void parseRegistry(String location) %7B%0A    VirtualFile registry = LocalFileSystem.getInstance().findFileByPath(location);%0A    if (null != registry) %7B%0A      for (VirtualFile platformFolder : registry.getChildren()) %7B%0A        for (VirtualFile registryEntry : platformFolder.getChildren()) %7B%0A          DeftRegistryEntryInfo entryInfo = parseRegistryEntry(registryEntry.getPath());%0A          if (null != entryInfo) %7B%0A            entries.add(entryInfo);%0A          %7D%0A        %7D%0A      %7D%0A    %7D%0A  %7D%0A%0A
   @Nulla

Add support for parsing a full registry.