/*
 * Created on 2004-12-29
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.core.internal.mmpparser;

import java.io.File;
import java.util.Iterator;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IPath;

/**
 * Parsing a token containing a path to a file or directory 
 * 
 * @author mikolaj
 */
public class PathTokenParser extends AbstractTokenParser {

    /**
     * @param it The iterator to the next token
     * @param rootPath The path for tokens that begins with '\' (may
     * be <code>null</code>)
     * @param localPath The path for tokens not begining with '\'
     * (may be <code>null</code>)
     */
    public PathTokenParser(Iterator it, IPath rootPath, IPath localPath) {
        super(it);
        fRootPath=rootPath;
        fLocalPath=localPath;
    }
    
    void forceDirectory(boolean force)
    {
        fDirectory=force;
    }
    
    void forceFile(boolean force)
    {
        fFile=force;
    }
    
    void setNotFoundSeverity(int severity)
    {
        fNotFoundSeverity=severity;
    }
    

    protected void parse(String token) {
        IPath root;
        if (fContent.startsWith("\\"))
            root=fRootPath;
        	else root=fLocalPath;
        if (root!=null)
        {
            File f=new File(root.append(fContent).toString());
            if (!f.exists())
            {
                setError(fNotFoundSeverity, "File not found");
                return;
            }
            if (f.isDirectory() && fFile)
            {
                setError(IMarker.SEVERITY_ERROR, "Target is a directory");
                return;
            }
            if (f.isFile() && fDirectory)
            {
                setError(IMarker.SEVERITY_ERROR, "Target is a file");
                return;                
            }
            fPath=root.append(fContent);
        }
    }

    /**
     * The obtained path object. <code>null</code> if the path
     * doesn't exist or no root directory for this type of paths
     */
    public IPath fPath;
    
    IPath fRootPath, fLocalPath;
    int fNotFoundSeverity=IMarker.SEVERITY_ERROR;
    boolean fDirectory;
    boolean fFile;
}
