17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
185 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
17 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.openoffice;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.utils.FileUtils;
|
|
|
17 |
import org.openconcerto.utils.StreamUtils;
|
|
|
18 |
import org.openconcerto.utils.ZippedFilesProcessor;
|
|
|
19 |
import org.openconcerto.utils.cc.IClosure;
|
|
|
20 |
|
|
|
21 |
import java.awt.GraphicsEnvironment;
|
|
|
22 |
import java.io.ByteArrayOutputStream;
|
|
|
23 |
import java.io.File;
|
|
|
24 |
import java.io.FileInputStream;
|
|
|
25 |
import java.io.IOException;
|
|
|
26 |
import java.io.InputStream;
|
|
|
27 |
import java.util.regex.Matcher;
|
|
|
28 |
import java.util.regex.Pattern;
|
|
|
29 |
import java.util.zip.ZipEntry;
|
|
|
30 |
|
|
|
31 |
import javax.swing.JFrame;
|
|
|
32 |
import javax.swing.SwingUtilities;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Allow to search for a regexp inside opendocument files.
|
|
|
36 |
*
|
|
|
37 |
* @author Sylvain
|
|
|
38 |
*/
|
|
|
39 |
public class Grep {
|
|
|
40 |
|
|
|
41 |
private static final int CONTEXT_CHARS = 30;
|
|
|
42 |
|
|
|
43 |
public static void main(String[] args) throws IOException {
|
|
|
44 |
if (!GraphicsEnvironment.isHeadless()) {
|
|
|
45 |
SwingUtilities.invokeLater(new Runnable() {
|
|
|
46 |
public void run() {
|
|
|
47 |
final GrepFrame inst = new GrepFrame();
|
|
|
48 |
inst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
49 |
inst.setLocationRelativeTo(null);
|
|
|
50 |
inst.setVisible(true);
|
|
|
51 |
}
|
|
|
52 |
});
|
|
|
53 |
} else if (args.length != 2)
|
|
|
54 |
usage();
|
|
|
55 |
else {
|
|
|
56 |
// the last arg is the file to grep
|
|
|
57 |
final String file = args[args.length - 1];
|
|
|
58 |
final String pattern = args[args.length - 2];
|
|
|
59 |
|
|
|
60 |
new Grep(pattern).grep(new File(file));
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
private static void usage() {
|
|
|
65 |
System.out.println("Usage: " + Grep.class.getName() + " pattern (ODFile|dir)");
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
private final Pattern pattern;
|
|
|
69 |
|
|
|
70 |
public Grep(final String pattern) {
|
|
|
71 |
super();
|
|
|
72 |
this.pattern = Pattern.compile(pattern);
|
|
|
73 |
}
|
|
|
74 |
|
180 |
ilm |
75 |
public final Pattern getPattern() {
|
|
|
76 |
return this.pattern;
|
|
|
77 |
}
|
|
|
78 |
|
17 |
ilm |
79 |
public final void grep(final File dir) {
|
|
|
80 |
FileUtils.walk(dir, new IClosure<File>() {
|
|
|
81 |
@Override
|
|
|
82 |
public void executeChecked(final File f) {
|
|
|
83 |
try {
|
|
|
84 |
grepFile(f);
|
|
|
85 |
} catch (IOException e) {
|
|
|
86 |
// keep going
|
|
|
87 |
e.printStackTrace();
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
});
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
private final void grepFile(final File odfile) throws IOException {
|
|
|
94 |
if (!isODFile(odfile))
|
|
|
95 |
return;
|
|
|
96 |
|
|
|
97 |
final ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
|
|
|
98 |
final InputStream ins = new FileInputStream(odfile);
|
|
|
99 |
try {
|
|
|
100 |
new ZippedFilesProcessor() {
|
|
|
101 |
@Override
|
|
|
102 |
protected void processEntry(ZipEntry entry, InputStream in) throws IOException {
|
|
|
103 |
if (entry.getName().endsWith(".xml")) {
|
|
|
104 |
out.reset();
|
|
|
105 |
StreamUtils.copy(in, out);
|
|
|
106 |
final String s = out.toString("UTF8");
|
|
|
107 |
final Matcher matcher = Grep.this.pattern.matcher(s);
|
|
|
108 |
while (matcher.find()) {
|
|
|
109 |
final int start = Math.max(0, matcher.start() - CONTEXT_CHARS);
|
|
|
110 |
final int end = Math.min(s.length(), matcher.end() + CONTEXT_CHARS);
|
|
|
111 |
System.out.println(odfile + "!" + entry.getName() + "\t" + s.substring(start, end));
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
}.process(ins);
|
|
|
116 |
} finally {
|
|
|
117 |
ins.close();
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
private boolean isODFile(final File odfile) {
|
|
|
122 |
return odfile.isFile() && (odfile.getName().endsWith(".sxw") || odfile.getName().endsWith(".odt"));
|
|
|
123 |
}
|
|
|
124 |
}
|