commit | author | age
|
fad719
|
1 |
/*
|
U |
2 |
|
|
3 |
Dateiverwaltung - File management in your browser
|
|
4 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
|
|
5 |
|
|
6 |
This program is free software: you can redistribute it and/or modify
|
|
7 |
it under the terms of the GNU Affero General Public License as
|
|
8 |
published by the Free Software Foundation, either version 3 of the
|
|
9 |
License, or (at your option) any later version.
|
|
10 |
|
|
11 |
This program is distributed in the hope that it will be useful,
|
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 |
GNU Affero General Public License for more details.
|
|
15 |
|
|
16 |
You should have received a copy of the GNU Affero General Public License
|
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18 |
|
|
19 |
*/
|
|
20 |
|
|
21 |
package de.uhilger.filecms.api;
|
|
22 |
|
|
23 |
import java.io.BufferedReader;
|
|
24 |
import java.io.File;
|
|
25 |
import java.io.FileFilter;
|
|
26 |
import java.io.FileNotFoundException;
|
|
27 |
import java.io.FileReader;
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.PrintWriter;
|
|
30 |
import java.util.logging.Level;
|
|
31 |
import java.util.logging.Logger;
|
|
32 |
import org.apache.commons.io.FileUtils;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Die Klasse HtmlExportService stellt Methoden bereit, mit denen
|
|
36 |
* .htmi-Dateien der WebBox zusammen mit Bildern und Stylesheets als
|
|
37 |
* Ordner exportierten werden koennen, aus denen heraus die Inhalte
|
|
38 |
* als HTML-Seiten im Browser zu oeffnen sind.
|
|
39 |
*/
|
|
40 |
public class HtmlExportService extends Api {
|
|
41 |
|
|
42 |
private static final Logger logger = Logger.getLogger(HtmlExportService.class.getName());
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Den Inhalt eines Ordners als HTML exportieren
|
|
46 |
*
|
|
47 |
* Annahme hierbei ist, dass der Ordner, auf den im Parameter relPath
|
|
48 |
* verwiesen wird eine oder mehrere .htmi-Dateien enthaelt. Optional
|
|
49 |
* koennen Bilder im Ordner oder seinen Unterordnern enthalten sein, die
|
|
50 |
* dann in derselben Struktur im Ausgabeordner landen.
|
|
51 |
*
|
|
52 |
* Es werden nur .htmi-Dateien exortiert, die im mit relPath bezeichneten
|
|
53 |
* Ordner liegen. Eventuell in darin befindlichen Unterordnern befindliche
|
|
54 |
* .htmk-Dateien werden nicht exportiert.
|
|
55 |
*
|
|
56 |
* Es sollten also keine komplexen Webseiten-Strukturen mit dieser Methode
|
|
57 |
* erzeugt werden sondern einfache Dokumente mit einer oder wenigen
|
|
58 |
* einzelnen Dateien.
|
|
59 |
*
|
|
60 |
* Die Ausgabe erfolgt in den Ordner, in dem der mit relPath bezeichnete
|
|
61 |
* Ordner liegt, also in dessen Eltern-Ordner. Der ausgegebene Ordner heisst
|
|
62 |
* genauso wir der mit relPath bezeichnete Ordner und wird mit der
|
|
63 |
* Ergaenzung _html versehen.
|
|
64 |
*
|
|
65 |
* @param relPath
|
|
66 |
* @return 'ok' oder Fehlermeldung
|
|
67 |
*/
|
|
68 |
public String exportHtml(String relPath) {
|
|
69 |
String result = null;
|
|
70 |
File dir = getTargetDir(relPath);
|
|
71 |
File parentDir = dir.getParentFile();
|
|
72 |
File outDir = new File(parentDir, dir.getName() + "_html/");
|
|
73 |
File[] files = dir.listFiles(new HtmiFileFilter());
|
|
74 |
for(int i = 0; i < files.length; i++) {
|
|
75 |
if (files[i].isDirectory()) {
|
|
76 |
try {
|
|
77 |
FileUtils.copyDirectoryToDirectory(files[i], outDir);
|
|
78 |
} catch (IOException ex) {
|
|
79 |
result = ex.getLocalizedMessage();
|
|
80 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
81 |
}
|
|
82 |
} else {
|
|
83 |
BufferedReader br = null;
|
|
84 |
try {
|
|
85 |
File out = new File(outDir, stripExt(files[i].getName()) + ".html");
|
|
86 |
PrintWriter w = new PrintWriter(out);
|
|
87 |
printHeader(w);
|
|
88 |
br = new BufferedReader(new FileReader(files[i]));
|
|
89 |
String line = br.readLine();
|
|
90 |
while(line != null) {
|
|
91 |
w.print(line);
|
|
92 |
line = br.readLine();
|
|
93 |
}
|
|
94 |
printFooter(w);
|
|
95 |
w.flush();
|
|
96 |
w.close();
|
|
97 |
br.close();
|
|
98 |
} catch (FileNotFoundException ex) {
|
|
99 |
result = ex.getLocalizedMessage();
|
|
100 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
101 |
} catch (IOException ex) {
|
|
102 |
result = ex.getLocalizedMessage();
|
|
103 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
104 |
} finally {
|
|
105 |
try {
|
|
106 |
if(br != null) {
|
|
107 |
br.close();
|
|
108 |
}
|
|
109 |
} catch (IOException ex) {
|
|
110 |
result = ex.getLocalizedMessage();
|
|
111 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|
|
115 |
}
|
|
116 |
File catalinaBase = new File(getCatalinaBase());
|
|
117 |
File bsDir = new File(catalinaBase, "webapps/jslib/bootstrap/css/");
|
|
118 |
try {
|
|
119 |
FileUtils.copyFile(new File(bsDir, "bootstrap.min.css"), new File(outDir, "bootstrap.min.css"), true);
|
|
120 |
File stile = new File(dir, "stile.css");
|
|
121 |
if(stile.exists()) {
|
|
122 |
FileUtils.copyFile(stile, new File(outDir, "stile.css"), true);
|
|
123 |
}
|
|
124 |
} catch (IOException ex) {
|
|
125 |
result = ex.getLocalizedMessage();
|
|
126 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
|
|
127 |
}
|
|
128 |
|
|
129 |
return result;
|
|
130 |
}
|
|
131 |
|
|
132 |
private String stripExt(String name) {
|
|
133 |
int dotpos = name.indexOf(".");
|
|
134 |
if(dotpos > -1) {
|
|
135 |
name = name.substring(0, dotpos);
|
|
136 |
}
|
|
137 |
return name;
|
|
138 |
}
|
|
139 |
|
|
140 |
public class HtmiFileFilter implements FileFilter {
|
|
141 |
|
|
142 |
@Override
|
|
143 |
public boolean accept(File pathname) {
|
|
144 |
boolean doAccept = false;
|
|
145 |
if(pathname.getName().endsWith(".htmi") || pathname.isDirectory()) {
|
|
146 |
doAccept = true;
|
|
147 |
}
|
|
148 |
return doAccept;
|
|
149 |
}
|
|
150 |
|
|
151 |
}
|
|
152 |
|
|
153 |
private void printHeader(PrintWriter out) throws IOException {
|
|
154 |
out.print("<!DOCTYPE html><html><head>\r\n");
|
|
155 |
out.print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>");
|
|
156 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"bootstrap.min.css\">\r\n");
|
|
157 |
out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"stile.css\">\r\n");
|
|
158 |
out.print("</head><body class=\"p-3\">\r\n");
|
|
159 |
}
|
|
160 |
|
|
161 |
private void printFooter(PrintWriter out) throws IOException {
|
|
162 |
out.print("</body></html>");
|
|
163 |
}
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
}
|