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    
020    package org.apache.isis.core.progmodel.facetdecorators.help.file.internal;
021    
022    import java.io.BufferedReader;
023    import java.io.File;
024    import java.io.FileNotFoundException;
025    import java.io.FileReader;
026    import java.io.IOException;
027    
028    import org.apache.isis.applib.Identifier;
029    import org.apache.isis.core.commons.config.IsisConfiguration;
030    import org.apache.isis.core.progmodel.facetdecorators.help.HelpManagerAbstract;
031    import org.apache.log4j.Logger;
032    
033    public class HelpManagerUsingFiles extends HelpManagerAbstract {
034    
035        private static final Logger LOG = Logger.getLogger(HelpManagerUsingFiles.class);
036    
037        /**
038         * The name of the file used unless overridden with {@link #setFileName(String)}.
039         */
040        public static final String DEFAULT_FILE_NAME = "help.txt";
041        private static final String CLASS_PREFIX = "c:";
042        private static final String NAME_PREFIX = "m:";
043    
044        private String fileName = DEFAULT_FILE_NAME;
045    
046        @SuppressWarnings("unused")
047        private final IsisConfiguration configuration;
048    
049        public HelpManagerUsingFiles(final IsisConfiguration configuration) {
050            this.configuration = configuration;
051        }
052    
053        public void setFileName(final String fileName) {
054            this.fileName = fileName;
055        }
056    
057        @Override
058        public String getHelpText(final Identifier identifier) {
059            BufferedReader reader = null;
060            try {
061                reader = getReader();
062    
063                if (reader == null) {
064                    return "No help available (no file found)";
065                }
066    
067                final String className = CLASS_PREFIX + identifier.getClassName().toLowerCase();
068                final String name = NAME_PREFIX + identifier.getMemberName().toLowerCase();
069    
070                final StringBuffer str = new StringBuffer();
071                String line;
072    
073                boolean lookingForClass = true;
074                boolean lookingForName = identifier.getMemberName().length() > 0;
075                /*
076                 * Read through each line in file.
077                 */
078                while ((line = reader.readLine()) != null) {
079                    // Skip comments - lines begining with hash
080                    if (line.length() > 0 && line.charAt(0) == '#') {
081                        continue;
082                    }
083    
084                    /*
085                     * Look for class.
086                     */
087                    if (line.toLowerCase().equals(className)) {
088                        lookingForClass = false;
089                        continue;
090                    }
091    
092                    if (lookingForClass) {
093                        continue;
094                    } else if (line.toLowerCase().startsWith(CLASS_PREFIX)) {
095                        break;
096                    }
097    
098                    /*
099                     * Look for field/method.
100                     */
101                    if (line.toLowerCase().equals(name)) {
102                        lookingForName = false;
103                        continue;
104                    }
105    
106                    if (lookingForName) {
107                        continue;
108                    } else if (line.toLowerCase().startsWith(NAME_PREFIX)) {
109                        break;
110                    }
111    
112                    str.append(line);
113                    str.append('\n');
114                }
115    
116                return str.toString();
117    
118            } catch (final FileNotFoundException e) {
119                LOG.error("opening help file", e);
120                return "Failure opening help file: " + e.getMessage();
121            } catch (final IOException e) {
122                LOG.error("reading help file", e);
123                return "Failure reading help file: " + e.getMessage();
124            } finally {
125                if (reader != null) {
126                    try {
127                        reader.close();
128                    } catch (final IOException ignore) {
129                    }
130                }
131            }
132        }
133    
134        protected BufferedReader getReader() throws FileNotFoundException {
135            final File file = new File(fileName);
136            if (!file.exists()) {
137                final String message = "No help file found: " + file.getAbsolutePath();
138                LOG.warn(message);
139                return null;
140            }
141    
142            return new BufferedReader(new FileReader(file));
143        }
144    }