View Javadoc

1   /*
2    * $Id: AbstractDirectoryScanner.java 2993 2011-11-24 19:51:48Z andrewinkler $
3    * ============================================================================
4    * Project gluehloch-homepage-core
5    * Copyright (c) 2004-2007 by Andre Winkler. All rights reserved.
6    * ============================================================================
7    *          GNU LESSER GENERAL PUBLIC LICENSE
8    *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
9    *
10   *  This library is free software; you can redistribute it and/or
11   *  modify it under the terms of the GNU Lesser General Public
12   *  License as published by the Free Software Foundation; either
13   *  version 2.1 of the License, or (at your option) any later version.
14   *
15   *  This library is distributed in the hope that it will be useful,
16   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   *  Lesser General Public License for more details.
19   *
20   *  You should have received a copy of the GNU Lesser General Public
21   *  License along with this library; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   */
25  
26  package de.awtools.homegen.directory.utils;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.List;
31  
32  import de.awtools.homegen.TransformerException;
33  import de.awtools.homegen.directory.GHBuilder;
34  import de.awtools.homegen.directory.GHDirectory;
35  import de.awtools.homegen.directory.GHDirectoryScanner;
36  import de.awtools.homegen.directory.GHFile;
37  
38  /**
39   * Utility Klasse für Scanner Implementierungen.
40   *
41   * @version $LastChangedRevision: 2993 $ $LastChangedDate: 2011-11-24 20:51:48 +0100 (Thu, 24 Nov 2011) $
42   * @author by Andre Winkler, $LastChangedBy: andrewinkler $
43   *
44   * @since 1.5
45   */
46  public abstract class AbstractDirectoryScanner implements GHDirectoryScanner {
47  
48      /** Das Root-Verzeichnis	 */
49      private final File rootDirectory;
50  
51      /**
52       * Zuständig für das Erstellen der {@link GHDirectory} oder
53       * {@link GHFile} Objekte.
54       */
55      private final GHBuilder entryBuilder;
56  
57      /**
58       * Konstruktor.
59       *
60       * @param _rootDirectory Das Root-Verzeichnis.
61       */
62      protected AbstractDirectoryScanner(final File _rootDirectory,
63              final GHBuilder _entryBuilder) {
64  
65          rootDirectory = _rootDirectory;
66          entryBuilder = _entryBuilder;
67      }
68  
69      /**
70       * Liefert das Wurzelverzeichnis.
71       *
72       * @return Das Wurzelverzeichnis.
73       */
74      protected final File getRootDirectory() {
75          return rootDirectory;
76      }
77  
78      /**
79       * Liefert den Builder zum Bauen der {@link GHDirectory} und {@link GHFile}
80       * Instanzen.
81       *
82       * @return Der Builder für unseren Scanner.
83       */
84      protected final GHBuilder getBuilder() {
85          return entryBuilder;
86      }
87  
88      /**
89       * Erstellt einen SnippetDirectory-Tree.
90       *
91       * @return Das Wurzelverzeichnis des Snippet-Trees.
92       */
93      @Override
94      public final GHDirectory scan() {
95          GHDirectory directory = getBuilder().buildRootDirectory(
96                  getRootDirectory());
97          scanDirectory(directory);
98          return directory;
99      }
100 
101     /**
102      * Scannt ein Verzeichnis nach Files und Unterverzeichnissen.
103      *
104      * @param directory Das zu untersuchende Verzeichnis.
105      */
106     protected void scanDirectory(final GHDirectory directory) {
107         findDirectories(directory);
108         findFiles(directory);
109     }
110 
111     /**
112      * Sucht nach allen Dateien auf dieser Ebene (parentDirectory) und hängt
113      * diese als {@link GHFile} in das <code>parentDirectory</code>
114      * ein.
115      *
116      * @param parentDirectory Das zu untersuchende Verzeichnis.
117      */
118     protected final void findFiles(final GHDirectory parentDirectory) {
119         List<File> xhtmlSnippets = getBuilder().findFiles(parentDirectory);
120 
121         for (File xhtmlSnippet : xhtmlSnippets) {
122             GHFile file = getBuilder().buildFile(
123                     xhtmlSnippet,
124                     parentDirectory,
125                     GHFileUtils.removeBaseDir(parentDirectory.getFile(),
126                             xhtmlSnippet));
127 
128             parentDirectory.addFile(file);
129             parseFile(file);
130         }
131     }
132 
133     /**
134      * Nachbearbeitung nach {@link GHFile} Instanzierung. Bei Bedarf zum
135      * Überschreiben freigegeben.
136      *
137      * @param file File zur Nachbearbeitung.
138      */
139     protected void parseFile(GHFile file) {
140     }
141 
142     /**
143      * Sucht nach allen Verzeichnissen auf dieser Ebene (parentDirectory) und
144      * hängt diese als {@link GHDirectory} in das
145      * <code>parentDirectory</code> ein.
146      *
147      * @param parentDirectory Das zu untersuchende Verzeichnis.
148      */
149     protected final void findDirectories(final GHDirectory parentDirectory) {
150         List<File> directories = GHFileUtils.findDirectories(parentDirectory
151                 .getFile());
152 
153         for (int index = 0; index < directories.size(); index++) {
154             File directory = directories.get(index);
155 
156             String directoryPath;
157             String parentDirectoryPath;
158             try {
159                 directoryPath = directory.getCanonicalPath();
160                 parentDirectoryPath = parentDirectory.getFile()
161                         .getCanonicalPath();
162             } catch (IOException ex) {
163                 throw new TransformerException(ex);
164             }
165 
166             if (directoryPath.endsWith(".svn") || directoryPath.endsWith("CVS")) {
167                 // Die Dateien für die Subversion Verwaltung stehen außen vor!
168                 // Ebenso die aus CVS.
169                 continue;
170             }
171 
172             String dirName = GHFileUtils.removeBaseDir(parentDirectoryPath,
173                     directoryPath);
174 
175             GHDirectory subDir = getBuilder().buildDirectory(parentDirectory,
176                     directory, dirName);
177 
178             // Unterverzeichniss bestimmen und einhängen.
179             scanDirectory(subDir);
180         }
181     }
182 
183 }