/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * 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.codehaus.groovy.grails.commons;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.codehaus.groovy.grails.exceptions.GrailsConfigurationException;
import org.springframework.core.io.Resource;
/**
 * Utility methods for working with Grails resources and URLs that represent artifacts
 * within a Grails application
 * 
 * @author Graeme Rocher
 *
 * @since 0.2
 * 
 * Created: 20th June 2006
 */
public class GrailsResourceUtils {

    private static final String FS = File.separator;

    /*
    Domain path is always matched against the normalized File representation of an URL and
    can therefore work with slashes as separators.
    */
    public static Pattern DOMAIN_PATH_PATTERN = Pattern.compile(".+/grails-app/domain/(.+)\\.groovy");

    /*
    Resources are resolved against the platform specific path and must therefore obey the
    specific File.separator.
    */
    public static Pattern GRAILS_RESOURCE_PATTERN;
    static {
        String fs = File.separator;
        if (fs.equals("\\")) fs = "\\\\"; // backslashes need escaping in regexes
        GRAILS_RESOURCE_PATTERN = Pattern.compile(".+"+fs +"grails-app"+fs +"\\w+"+fs +"(.+)\\.groovy");
    }


    /**
	 * Checks whether the file referenced by the given url is a domain class
	 * 
	 * @param url The URL instance
	 * @return True if it is a domain class
	 */
	public static boolean isDomainClass(URL url) {
		if (url == null) return false;

        return DOMAIN_PATH_PATTERN.matcher(url.getFile()).find();
    }

	/**
	 * Gets the class name of the specified Grails resource
	 * 
	 * @param resource The Spring Resource
	 * @return The class name or null if the resource is not a Grails class
	 */
	public static String getClassName(Resource resource) {
        try {
        	return getClassName(resource.getFile().getAbsolutePath());
        } catch (IOException e) {
            throw new GrailsConfigurationException("I/O error reading class name from resource ["+resource+"]: " + e.getMessage(),e );
        }        	
	}

	/**
	 * Returns the class name for a Grails resource
	 * 
	 * @param path The path to check
	 * @return The class name or null if it doesn't exist
	 */
	public static String getClassName(String path) {
		Matcher m = GrailsResourceUtils.GRAILS_RESOURCE_PATTERN.matcher(path);
        if(m.find()) {
            return m.group(1);
        }
        return null;
	}	

	/**
	 * Checks whether the specified path is a Grails path
	 * 
	 * @param path The path to check
	 * @return True if it is a Grails path
	 */
	public static boolean isGrailsPath(String path) {
		Matcher m = GrailsResourceUtils.GRAILS_RESOURCE_PATTERN.matcher(path);
        return m.find();
    }
}

@@ -663,16 +663,18 @@
 mmons;%0A%0A
+//
 import j
@@ -686,16 +686,16 @@
 o.File;%0A
-
 import j
@@ -1163,16 +1163,18 @@
  %7B%0A%0A    
+//
 private 
@@ -1599,32 +1599,33 @@
 eparator.%0A    */
+ 
 %0A    public stat
@@ -1681,16 +1681,18 @@
         
+/*
 String f
@@ -1793,16 +1793,49 @@
 regexes%0A
+        */%0A    %09String fs = %22/%22;%0A
         

fix for GRAILS-316