001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.progmodel.facetdecorators.help.file.internal;
021
022import java.io.BufferedReader;
023import java.io.File;
024import java.io.FileNotFoundException;
025import java.io.FileReader;
026import java.io.IOException;
027
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import org.apache.isis.applib.Identifier;
032import org.apache.isis.core.commons.config.IsisConfiguration;
033import org.apache.isis.core.progmodel.facetdecorators.help.HelpManagerAbstract;
034
035public class HelpManagerUsingFiles extends HelpManagerAbstract {
036
037    private static final Logger LOG = LoggerFactory.getLogger(HelpManagerUsingFiles.class);
038
039    /**
040     * The name of the file used unless overridden with
041     * {@link #setFileName(String)}.
042     */
043    public static final String DEFAULT_FILE_NAME = "help.txt";
044    private static final String CLASS_PREFIX = "c:";
045    private static final String NAME_PREFIX = "m:";
046
047    private String fileName = DEFAULT_FILE_NAME;
048
049    @SuppressWarnings("unused")
050    private final IsisConfiguration configuration;
051
052    public HelpManagerUsingFiles(final IsisConfiguration configuration) {
053        this.configuration = configuration;
054    }
055
056    public void setFileName(final String fileName) {
057        this.fileName = fileName;
058    }
059
060    @Override
061    public String getHelpText(final Identifier identifier) {
062        BufferedReader reader = null;
063        try {
064            reader = getReader();
065
066            if (reader == null) {
067                return "No help available (no file found)";
068            }
069
070            final String className = CLASS_PREFIX + identifier.getClassName().toLowerCase();
071            final String name = NAME_PREFIX + identifier.getMemberName().toLowerCase();
072
073            final StringBuffer str = new StringBuffer();
074            String line;
075
076            boolean lookingForClass = true;
077            boolean lookingForName = identifier.getMemberName().length() > 0;
078            /*
079             * Read through each line in file.
080             */
081            while ((line = reader.readLine()) != null) {
082                // Skip comments - lines begining with hash
083                if (line.length() > 0 && line.charAt(0) == '#') {
084                    continue;
085                }
086
087                /*
088                 * Look for class.
089                 */
090                if (line.toLowerCase().equals(className)) {
091                    lookingForClass = false;
092                    continue;
093                }
094
095                if (lookingForClass) {
096                    continue;
097                } else if (line.toLowerCase().startsWith(CLASS_PREFIX)) {
098                    break;
099                }
100
101                /*
102                 * Look for field/method.
103                 */
104                if (line.toLowerCase().equals(name)) {
105                    lookingForName = false;
106                    continue;
107                }
108
109                if (lookingForName) {
110                    continue;
111                } else if (line.toLowerCase().startsWith(NAME_PREFIX)) {
112                    break;
113                }
114
115                str.append(line);
116                str.append('\n');
117            }
118
119            return str.toString();
120
121        } catch (final FileNotFoundException e) {
122            LOG.error("opening help file", e);
123            return "Failure opening help file: " + e.getMessage();
124        } catch (final IOException e) {
125            LOG.error("reading help file", e);
126            return "Failure reading help file: " + e.getMessage();
127        } finally {
128            if (reader != null) {
129                try {
130                    reader.close();
131                } catch (final IOException ignore) {
132                }
133            }
134        }
135    }
136
137    protected BufferedReader getReader() throws FileNotFoundException {
138        final File file = new File(fileName);
139        if (!file.exists()) {
140            final String message = "No help file found: " + file.getAbsolutePath();
141            LOG.warn(message);
142            return null;
143        }
144
145        return new BufferedReader(new FileReader(file));
146    }
147}