/*
** Module   : InMemorySourceCode.java
** Abstract : in-memory Java source code
**
** Copyright (c) 2009, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20090303 Provides in-memory Java source code.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.compile;

import javax.tools.*;
import java.io.*;
import java.net.*;

/**
 * An in-memory representation of the Java source code for a class. This
 * representation is sufficient for passing to the Java compiler bypassing the
 * file system completely.
 */
public class InMemorySourceCode
extends SimpleJavaFileObject
{
   /** The text representation of the source code. */
   private final String source;
   
   /** Optional line-oriented representation of the source code. */
   private String[] lines = null;
   
   /**
    * Construct an instance that represents the source code for the given
    * class name.
    *
    * @param    name
    *           Fully qualified class name (includes package and any inner
    *           class extensions) in proper Java Language Specification format
    *           WITHOUT any ".java" suffix.
    * @param    source
    *           The Java source code for the class.
    */
   public InMemorySourceCode(String name, String source)
   {
      super(URI.create("string:///"                           +
                       name.replaceAll("\\.", File.separator) +
                       Kind.SOURCE.extension),
                       Kind.SOURCE);
      this.source = source;
   }
   
   /**
    * Obtain the source file contents.
    *
    * @param    ignore
    *           <code>true</code> to ignore any encoding errors.
    *
    * @return   The character data or <code>null</code> if no source code is
    *           configured.
    */
   public CharSequence getCharContent(boolean ignore)
   {
      return source;
   }
   
   /**
    * Obtain the specified line of source code.
    *
    * @param    num
    *           Line number (1-based index).
    *
    * @return   The specified line or <code>null</code> if such a line doesn't
    *           exist. 
    */
   public String getLine(int num)
   {
      if (lines == null)
      {
         lines = source.split("\n");
      }
      
      if (lines == null)
         return null;
      
      // convert to zero-based index
      num--;
      
      if (num < 0 || num >= lines.length)
         return null;
      
      return lines[num];
   }
}
