OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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