001/*
002 * Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal.doclets.toolkit.util;
027
028import java.nio.file.Path;
029import javax.tools.JavaFileManager;
030import javax.tools.JavaFileManager.Location;
031
032import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseConfiguration;
033import org.jdrupes.mdoclet.internal.doclets.toolkit.DocletException;
034
035import javax.tools.StandardJavaFileManager;
036import javax.tools.StandardLocation;
037
038/**
039 * Factory for DocFile objects.
040 */
041public abstract class DocFileFactory {
042    /**
043     * Get the appropriate factory, based on the file manager given in the
044     * configuration.
045     *
046     * @param configuration the configuration for this doclet
047     * @return the factory associated with this configuration
048     */
049    public static synchronized DocFileFactory
050            getFactory(BaseConfiguration configuration) {
051        DocFileFactory f = configuration.docFileFactory;
052        if (f == null) {
053            JavaFileManager fm = configuration.getFileManager();
054            if (fm instanceof StandardJavaFileManager) {
055                f = new StandardDocFileFactory(configuration);
056            } else {
057                throw new IllegalStateException();
058            }
059            configuration.docFileFactory = f;
060        }
061        return f;
062    }
063
064    protected BaseConfiguration configuration;
065
066    protected DocFileFactory(BaseConfiguration configuration) {
067        this.configuration = configuration;
068    }
069
070    public abstract void setDestDir(String dir) throws DocletException;
071
072    /** Create a DocFile for a directory. */
073    abstract DocFile createFileForDirectory(String file);
074
075    /** Create a DocFile for a file that will be opened for reading. */
076    abstract DocFile createFileForInput(String file);
077
078    /** Create a DocFile for a file that will be opened for reading. */
079    abstract DocFile createFileForInput(Path file);
080
081    /** Create a DocFile for a file that will be opened for writing. */
082    abstract DocFile createFileForOutput(DocPath path);
083
084    /**
085     * List the directories and files found in subdirectories along the
086     * elements of the given location.
087     * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported.
088     * @param path the subdirectory of the directories of the location for which to
089     *  list files
090     */
091    abstract Iterable<DocFile> list(Location location, DocPath path);
092}