001/*
002 * Anarres C Preprocessor
003 * Copyright (c) 2007-2015, Shevek
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
014 * or implied.  See the License for the specific language governing
015 * permissions and limitations under the License.
016 */
017package org.anarres.cpp;
018
019import java.io.File;
020import java.io.IOException;
021
022/**
023 * A virtual filesystem implementation using java.io.
024 */
025public class JavaFileSystem implements VirtualFileSystem {
026
027    @Override
028    public VirtualFile getFile(String path) {
029        return new JavaFile(path);
030    }
031
032    @Override
033    public VirtualFile getFile(String dir, String name) {
034        return new JavaFile(dir, name);
035    }
036
037    private class JavaFile extends File implements VirtualFile {
038
039        public JavaFile(String path) {
040            super(path);
041        }
042
043        public JavaFile(String dir, String name) {
044            super(dir, name);
045        }
046
047        /* private */
048        public JavaFile(File dir, String name) {
049            super(dir, name);
050        }
051
052        /*
053         @Override
054         public String getPath() {
055         return getCanonicalPath();
056         }
057         */
058        @Override
059        public JavaFile getParentFile() {
060            String parent = getParent();
061            if (parent != null)
062                return new JavaFile(parent);
063            File absolute = getAbsoluteFile();
064            parent = absolute.getParent();
065            /*
066             if (parent == null)
067             return null;
068             */
069            return new JavaFile(parent);
070        }
071
072        @Override
073        public JavaFile getChildFile(String name) {
074            return new JavaFile(this, name);
075        }
076
077        @Override
078        public Source getSource() throws IOException {
079            return new FileLexerSource(this);
080        }
081
082    }
083
084}