58N/A/*
58N/A * CDDL HEADER START
58N/A *
58N/A * The contents of this file are subject to the terms of the
58N/A * Common Development and Distribution License (the "License").
58N/A * You may not use this file except in compliance with the License.
58N/A *
58N/A * See LICENSE.txt included in this distribution for the specific
58N/A * language governing permissions and limitations under the License.
58N/A *
58N/A * When distributing Covered Code, include this CDDL HEADER in each
58N/A * file and include the License file at LICENSE.txt.
58N/A * If applicable, add the following below this CDDL HEADER, with the
58N/A * fields enclosed by brackets "[]" replaced with your own identifying
58N/A * information: Portions Copyright [yyyy] [name of copyright owner]
58N/A *
58N/A * CDDL HEADER END
58N/A */
58N/A
58N/A/*
58N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
58N/A * Use is subject to license terms.
58N/A */
58N/Apackage org.opensolaris.opengrok.configuration;
58N/A
123N/Aimport java.io.File;
1016N/Aimport java.io.FileNotFoundException;
1016N/Aimport java.io.IOException;
123N/A
58N/A/**
77N/A * Placeholder for the information that builds up a project
1190N/A */
58N/Apublic class Project {
58N/A private String path;
1190N/A // this variable is very important, since it's used as the project identifier
1185N/A // all over xrefs and webapp
1185N/A // jel: and yes - awefully misleading. It should be called 'name'!
58N/A private String description;
922N/A
922N/A /**
922N/A * Size of tabs in this project. Used for displaying the xrefs correctly in
922N/A * projects with non-standard tab size.
922N/A */
922N/A private int tabSize;
1190N/A
77N/A /**
77N/A * Get a textual description of this project
77N/A * @return a textual description of the project
77N/A */
58N/A public String getDescription() {
58N/A return description;
58N/A }
1190N/A
77N/A /**
77N/A * Get the path (relative from source root) where this project is located
77N/A * @return the relative path
77N/A */
58N/A public String getPath() {
58N/A return path;
58N/A }
271N/A
271N/A /**
271N/A * Get the project id
271N/A * @return the id of the project
271N/A */
271N/A public String getId() {
271N/A return path;
271N/A }
922N/A
922N/A /**
922N/A * Get the tab size for this project, if tab size has been set.
922N/A *
922N/A * @return tab size if set, 0 otherwise
922N/A * @see #hasTabSizeSetting()
922N/A */
922N/A public int getTabSize() {
922N/A return tabSize;
922N/A }
922N/A
77N/A /**
1327N/A * Set a textual description of this project, prefferably don't use " , "
1327N/A * in the name, since it's used as delimiter for more projects
77N/A * @param description a textual description of the project
77N/A */
58N/A public void setDescription(String description) {
58N/A this.description = description;
58N/A }
1190N/A
77N/A /**
77N/A * Set the path (relative from source root) this project is located
1327N/A * It seems that you should ALWAYS prefix the path with current
1327N/A * file.separator , current environment should always have it set up
77N/A * @param path the relative path from source sroot where this project is
77N/A * located.
77N/A */
1190N/A public void setPath(String path) {
58N/A this.path = path;
58N/A }
922N/A
922N/A /**
922N/A * Set tab size for this project. Used for expanding tabs to spaces
922N/A * in xrefs.
922N/A *
922N/A * @param tabSize the size of tabs in this project
922N/A */
922N/A public void setTabSize(int tabSize) {
922N/A this.tabSize = tabSize;
922N/A }
922N/A
922N/A /**
922N/A * Has this project an explicit tab size setting?
922N/A *
922N/A * @return {@code true} if the tab size has been set for this project, or
922N/A * {@code false} if it hasn't and the default should be used
922N/A */
922N/A public boolean hasTabSizeSetting() {
922N/A return tabSize > 0;
922N/A }
1190N/A
77N/A /**
123N/A * Get the project for a specific file
123N/A * @param path the file to lookup (relative from source root)
1190N/A * @return the project that this file belongs to (or null if the file
123N/A * doesn't belong to a project)
123N/A */
123N/A public static Project getProject(String path) {
123N/A Project ret = null;
949N/A String lpath=path;
1185N/A if (File.separatorChar != '/') {
1185N/A lpath = path.replace(File.separatorChar, '/');
1185N/A }
1474N/A //FIXME bogus if sub projects are used
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A if (cfg.hasProjects()) {
1470N/A for (Project proj : cfg.getProjects()) {
949N/A if (lpath.indexOf(proj.getPath()) == 0) {
123N/A ret = proj;
123N/A }
123N/A }
123N/A }
123N/A return ret;
123N/A }
123N/A
123N/A /**
123N/A * Get the project for a specific file
123N/A * @param file the file to lookup
1190N/A * @return the project that this file belongs to (or null if the file
123N/A * doesn't belong to a project)
123N/A */
123N/A public static Project getProject(File file) {
123N/A Project ret = null;
1016N/A try {
1470N/A ret = getProject(RuntimeEnvironment.getConfig()
1470N/A .getPathRelativeToSourceRoot(file, 0));
1016N/A } catch (FileNotFoundException e) { // NOPMD
1016N/A // ignore if not under source root
1016N/A } catch (IOException e) { // NOPMD
1016N/A // problem has already been logged, just return null
123N/A }
123N/A return ret;
123N/A }
850N/A
853N/A /**
1470N/A * Returns project object by its description, used in webapp to figure out
1470N/A * which project is to be searched.
853N/A * @param desc description of the project
853N/A * @return project that fits the description
853N/A */
850N/A public static Project getByDescription(String desc) {
850N/A Project ret = null;
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A if (cfg.hasProjects()) {
1470N/A for (Project proj : cfg.getProjects()) {
850N/A if (desc.indexOf(proj.getDescription()) == 0) {
850N/A ret = proj;
850N/A }
850N/A }
850N/A }
850N/A return ret;
1190N/A }
58N/A}