~ubuntu-branches/ubuntu/raring/accessodf/raring

« back to all changes in this revision

Viewing changes to addon/be/docarch/accessodf/ooo/IssueManager.java

  • Committer: Package Import Robot
  • Author(s): Sebastian Humenda
  • Date: 2012-04-09 11:21:13 UTC
  • Revision ID: package-import@ubuntu.com-20120409112113-v0kmfdj1ks80xoj8
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  AccessODF - Accessibility checker for OpenOffice.org and LibreOffice Writer.
 
3
 *
 
4
 *  Copyright (c) 2011 by DocArch <http://www.docarch.be>.
 
5
 *
 
6
 *  This program is free software: you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU Lesser General Public License
 
17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
package be.docarch.accessodf.ooo;
 
21
 
 
22
import java.io.File;
 
23
import java.util.Date;
 
24
import java.util.ArrayList;
 
25
import java.util.List;
 
26
import java.util.Map;
 
27
import java.util.HashMap;
 
28
import java.util.Collection;
 
29
import java.util.Collections;
 
30
import java.util.logging.Logger;
 
31
import java.util.logging.Level;
 
32
 
 
33
import java.text.ParseException;
 
34
import java.io.IOException;
 
35
 
 
36
import com.sun.star.lang.EventObject;
 
37
import com.sun.star.container.XEnumeration;
 
38
import com.sun.star.rdf.Statement;
 
39
import com.sun.star.rdf.XURI;
 
40
import com.sun.star.rdf.URI;
 
41
import com.sun.star.rdf.XResource;
 
42
import com.sun.star.rdf.XRepository;
 
43
import com.sun.star.rdf.XNamedGraph;
 
44
 
 
45
import com.sun.star.container.NoSuchElementException;
 
46
import com.sun.star.rdf.RepositoryException;
 
47
import com.sun.star.lang.WrappedTargetException;
 
48
import com.sun.star.lang.IllegalArgumentException;
 
49
import com.sun.star.beans.UnknownPropertyException;
 
50
import com.sun.star.beans.PropertyVetoException;
 
51
 
 
52
import be.docarch.accessodf.Check;
 
53
import be.docarch.accessodf.DummyCheck;
 
54
import be.docarch.accessodf.Checker;
 
55
import be.docarch.accessodf.Provider;
 
56
import be.docarch.accessodf.RunnableChecker;
 
57
import be.docarch.accessodf.RemoteRunnableChecker;
 
58
import be.docarch.accessodf.Constants;
 
59
import be.docarch.accessodf.Report;
 
60
import be.docarch.accessodf.Issue;
 
61
import be.docarch.accessodf.FilterSorter;
 
62
import be.docarch.accessodf.Repairer;
 
63
import be.docarch.accessodf.Repairer.RepairMode;
 
64
import be.docarch.accessodf.ooo.rdf.*;
 
65
 
 
66
/**
 
67
 *
 
68
 * @author Bert Frees
 
69
 */
 
70
public class IssueManager {
 
71
 
 
72
    private static final Logger logger = Logger.getLogger(Constants.LOGGER_NAME);
 
73
    private static final String TMP_NAME = Constants.TMP_PREFIX;
 
74
    private static final File TMP_DIR = Constants.getTmpDirectory();
 
75
    
 
76
    public static enum Status { ALERT,
 
77
                                ERROR,
 
78
                                REPAIRED,
 
79
                                IGNORED };
 
80
 
 
81
    private final Document document;
 
82
    private final Settings settings;
 
83
    private final Provider<Checker> checkers;
 
84
    private final Provider<Repairer> repairers;
 
85
 
 
86
    private final List<Issue> allIssues;
 
87
    private FilterSorter filterSorter = null;
 
88
    private Issue selectedIssue = null;
 
89
    private Check selectedCheck = null;
 
90
    private Map<Check,List<Issue>> check2IssuesMap = null;
 
91
  //private Map<Check,Repairer> check2repairerMap = null;
 
92
 
 
93
    public IssueManager(Document document,
 
94
                        Provider<Checker> checkers,
 
95
                        Provider<Repairer> repairers)
 
96
                 throws IllegalArgumentException,
 
97
                        NoSuchElementException,
 
98
                        RepositoryException,
 
99
                        PropertyVetoException,
 
100
                        WrappedTargetException,
 
101
                        ParseException,
 
102
                        com.sun.star.uno.Exception {
 
103
    
 
104
        logger.entering("IssueManager", "<init>");
 
105
 
 
106
        this.document = document;
 
107
        this.checkers = checkers;
 
108
        this.repairers = repairers;
 
109
      //Provider<Check> checks = new CheckProvider(checkers);
 
110
 
 
111
        settings = new Settings(document.xContext);
 
112
      //check2repairerMap = new HashMap<Check,Repairer>();
 
113
        check2IssuesMap = new HashMap<Check,List<Issue>>();
 
114
 
 
115
        filterSorter = new FilterSorter();
 
116
        filterSorter.setOrderPriority(FilterSorter.NAME, true);
 
117
        filterSorter.setOrderPriority(FilterSorter.CHECKID, true);
 
118
        allIssues = new ArrayList<Issue>();
 
119
        
 
120
        // Load accessibility issues from metadata
 
121
        loadMetadata();
 
122
 
 
123
        logger.exiting("IssueManager", "<init>");
 
124
        
 
125
    }
 
126
 
 
127
    public void refresh() throws IllegalArgumentException,
 
128
                                 RepositoryException,
 
129
                                 UnknownPropertyException,
 
130
                                 NoSuchElementException,
 
131
                                 WrappedTargetException,
 
132
                                 PropertyVetoException,
 
133
                                 ParseException,
 
134
                                 IOException,
 
135
                                 com.sun.star.uno.Exception {
 
136
 
 
137
        settings.loadData();
 
138
        File odtFile = null;
 
139
 
 
140
        for (Checker checker : checkers.list()) {
 
141
            if (checker instanceof RemoteRunnableChecker) {
 
142
                if (!settings.brailleChecks() && checker.getIdentifier().equals("http://docarch.be/odt2braille/checker/BrailleChecker")) {
 
143
                    break;
 
144
                }
 
145
                if (odtFile == null) {
 
146
                    odtFile = File.createTempFile(TMP_NAME, ".odt", TMP_DIR);
 
147
                    odtFile.deleteOnExit();
 
148
                    document.ensureMetadataReferences();
 
149
                    document.storeToFile(odtFile);
 
150
                }
 
151
                RemoteRunnableChecker remoteChecker = (RemoteRunnableChecker)checker;
 
152
                remoteChecker.setOdtFile(odtFile);
 
153
                if (remoteChecker.run()) {
 
154
                    document.removeAccessibilityData(remoteChecker.getIdentifier());
 
155
                    Report report = remoteChecker.getAccessibilityReport();
 
156
                    if (report != null) {
 
157
                        document.importAccessibilityData(report.getFile(), remoteChecker.getIdentifier(), report.getName());
 
158
                    }
 
159
                }
 
160
            } else if (checker instanceof RunnableChecker) {
 
161
                ((RunnableChecker)checker).run();
 
162
            }
 
163
        }
 
164
 
 
165
        // Load accessibility issues from metadata
 
166
        loadMetadata();
 
167
    }
 
168
 
 
169
    public void clear() throws IllegalArgumentException,
 
170
                               RepositoryException,
 
171
                               PropertyVetoException,
 
172
                               NoSuchElementException {
 
173
 
 
174
        Collection<XNamedGraph> accessibilityDataGraphs = getAccessibilityDataGraphs();
 
175
 
 
176
        for (XNamedGraph accessibilityDataGraph : accessibilityDataGraphs) {
 
177
            document.xDMA.removeMetadataFile(accessibilityDataGraph.getName());
 
178
        }
 
179
        if (allIssues.size() > 0) {
 
180
            document.setModified();
 
181
            allIssues.clear();
 
182
        }
 
183
 
 
184
        check2IssuesMap.clear();
 
185
 
 
186
        select(null);
 
187
    }
 
188
 
 
189
    private Collection<XNamedGraph> getAccessibilityDataGraphs() throws IllegalArgumentException,
 
190
                                                                        RepositoryException {
 
191
    
 
192
        Collection<XNamedGraph> accessibilityDataGraphs = new ArrayList<XNamedGraph>();
 
193
        
 
194
        XRepository xRepository = document.xDMA.getRDFRepository();
 
195
 
 
196
        for (Checker checker : checkers.list()) {
 
197
            XURI type = URI.create(document.xContext, checker.getIdentifier());
 
198
            XURI[] graphNames = document.xDMA.getMetadataGraphsWithType(type);
 
199
            for (int i=0; i<graphNames.length; i++) {
 
200
                accessibilityDataGraphs.add(xRepository.getGraph(graphNames[i]));
 
201
            }
 
202
        }
 
203
 
 
204
        return accessibilityDataGraphs;
 
205
    }
 
206
 
 
207
    private void loadMetadata() throws NoSuchElementException,
 
208
                                       RepositoryException,
 
209
                                       WrappedTargetException,
 
210
                                       IllegalArgumentException,
 
211
                                       PropertyVetoException {
 
212
 
 
213
        logger.entering("IssueManager", "loadMetadata");
 
214
 
 
215
        Collection<XNamedGraph> accessibilityDataGraphs = getAccessibilityDataGraphs();
 
216
 
 
217
        Map<String,Date> checkerDates = new HashMap<String,Date>();
 
218
        Date longAgo = new Date(0);
 
219
        for (Checker checker : checkers.list()) {
 
220
            checkerDates.put(checker.getIdentifier(), longAgo);
 
221
        }
 
222
 
 
223
        allIssues.clear();
 
224
 
 
225
        Issue issue, sameIssue;
 
226
        for (XNamedGraph graph : accessibilityDataGraphs) {
 
227
            Assertions assertions = new Assertions(graph, document);
 
228
            XEnumeration assertionEnum = graph.getStatements(null, URIs.RDF_TYPE, URIs.EARL_ASSERTION);
 
229
            XResource assertion = null;
 
230
            while (assertionEnum.hasMoreElements()) {
 
231
                assertion = ((Statement)assertionEnum.nextElement()).Subject;
 
232
                try {
 
233
                    issue = assertions.read(assertion, checkers).getIssue();
 
234
                } catch (Exception e) {
 
235
                    logger.log(Level.SEVERE, null, e);
 
236
                    continue;
 
237
                }
 
238
                if (issue.getCheck() instanceof DummyCheck) {
 
239
                    checkerDates.put(issue.getChecker().getIdentifier(), issue.getCheckDate());
 
240
                    break;
 
241
                }
 
242
                if (allIssues.contains(issue)) {
 
243
                    sameIssue = allIssues.remove(allIssues.indexOf(issue));
 
244
                    if (issue.getCheckDate().before(sameIssue.getCheckDate())) {
 
245
                        if (issue.ignored()) {
 
246
                            sameIssue.ignored(true);
 
247
                        }
 
248
                        issue.remove();
 
249
                        issue = sameIssue;
 
250
                    } else {
 
251
                        if (sameIssue.ignored()) {
 
252
                            issue.ignored(true);
 
253
                        }
 
254
                        sameIssue.remove();
 
255
                    }
 
256
                }
 
257
                allIssues.add(issue);
 
258
            }
 
259
        }
 
260
 
 
261
        for (Issue i : allIssues) {
 
262
            String checker = i.getChecker().getIdentifier();
 
263
            if (i.getCheckDate().after(checkerDates.get(checker))) {
 
264
                checkerDates.put(checker, i.getCheckDate());
 
265
            }
 
266
        }
 
267
 
 
268
        for (Issue i : allIssues) {
 
269
            if (i.getCheckDate().before(checkerDates.get(i.getChecker().getIdentifier()))) {
 
270
                i.repaired(true);
 
271
            }
 
272
        }
 
273
 
 
274
        arrangeIssues();
 
275
 
 
276
        logger.exiting("IssueManager", "loadMetadata");
 
277
 
 
278
    }
 
279
 
 
280
    public void select(Object o) {
 
281
 
 
282
        selectedCheck = null;
 
283
        selectedIssue = null;
 
284
 
 
285
        if (o != null) {
 
286
            if (o instanceof Check) {                
 
287
                Check check = (Check)o;
 
288
                for (Check c : getChecks()) {
 
289
                    if (c.equals(check)) {
 
290
                        selectedCheck = c;
 
291
                    }
 
292
                }
 
293
            } else if (o instanceof Issue) {
 
294
                Issue issue = (Issue)o;
 
295
                for (Issue i : allIssues) {
 
296
                    if (issue.equals(i)) {
 
297
                        selectedIssue = i;
 
298
                        selectedCheck = selectedIssue.getCheck();
 
299
                    }
 
300
                }
 
301
            }
 
302
        }
 
303
    }
 
304
 
 
305
    public boolean repair(Issue issue)
 
306
                   throws IllegalArgumentException,
 
307
                          RepositoryException,
 
308
                          PropertyVetoException,
 
309
                          NoSuchElementException {
 
310
 
 
311
        if (issue == null) { return false; }
 
312
        for (Repairer repairer : repairers.list()) {
 
313
            try {
 
314
                if (repairer.repair(issue)) {
 
315
                    issue.repaired(true);
 
316
                    return true;
 
317
                }
 
318
            } catch (Exception e) {
 
319
            }
 
320
        }
 
321
        return false;
 
322
    }
 
323
 
 
324
    public boolean repairable(Issue issue) {
 
325
 
 
326
        for (Repairer repairer : repairers.list()) {
 
327
            if (repairer.supports(issue)) {
 
328
                return true;
 
329
            }
 
330
        }
 
331
        return false;
 
332
    }
 
333
 
 
334
    public RepairMode getRepairMode(Issue issue)
 
335
                             throws java.lang.IllegalArgumentException {
 
336
 
 
337
        for (Repairer repairer : repairers.list()) {
 
338
            if (repairer.supports(issue)) {
 
339
                return repairer.getRepairMode(issue);
 
340
            }
 
341
        }
 
342
        throw new java.lang.IllegalArgumentException();
 
343
    }
 
344
 
 
345
    private void arrangeIssues() {
 
346
 
 
347
        Collections.sort(allIssues, filterSorter);
 
348
 
 
349
        check2IssuesMap.clear();
 
350
 
 
351
        Check prevCheck = null;
 
352
        ArrayList<Issue> issueList = null;
 
353
        for (Issue issue : allIssues) {
 
354
            Check check = issue.getCheck();
 
355
            if (!check.equals(prevCheck)) {
 
356
                issueList = new ArrayList<Issue>();
 
357
                check2IssuesMap.put(check, issueList);
 
358
            }
 
359
            issueList.add(issue);
 
360
            prevCheck = check;
 
361
        }
 
362
    }
 
363
 
 
364
    public Collection<Check> getChecks() {
 
365
        return check2IssuesMap.keySet();
 
366
    }
 
367
 
 
368
    public List<Issue> getIssuesByCheck(Check check) {
 
369
        return check2IssuesMap.get(check);
 
370
    }
 
371
 
 
372
    public Status getStatus(Object o) {
 
373
 
 
374
        if (o != null) {
 
375
            if (o instanceof Check) {
 
376
 
 
377
                Check check = (Check)o;
 
378
                boolean alert = (check.getStatus() == Check.Status.ALERT);
 
379
                boolean allRepaired = true;
 
380
                boolean allIgnored = true;
 
381
                for (Issue issue : check2IssuesMap.get(check)) {
 
382
                    if (!issue.ignored()) {
 
383
                        allIgnored = false;
 
384
                    }
 
385
                    if (!issue.repaired()) {
 
386
                        allRepaired = false;
 
387
                    }
 
388
                }
 
389
                return (allRepaired? Status.REPAIRED:
 
390
                        allIgnored?  Status.IGNORED:
 
391
                        alert?       Status.ALERT:
 
392
                                     Status.ERROR);
 
393
 
 
394
            } else if (o instanceof Issue) {
 
395
 
 
396
                Issue issue = (Issue)o;
 
397
                boolean alert = (issue.getCheck().getStatus() == Check.Status.ALERT);
 
398
 
 
399
                return (issue.repaired()? Status.REPAIRED:
 
400
                        issue.ignored()?  Status.IGNORED:
 
401
                        alert?            Status.ALERT:
 
402
                                          Status.ERROR);
 
403
            }
 
404
        }
 
405
 
 
406
        return null;
 
407
    }
 
408
 
 
409
    public Issue selectedIssue() {
 
410
        return selectedIssue;
 
411
    }
 
412
 
 
413
    public Check selectedCheck() {
 
414
        return selectedCheck;
 
415
    }
 
416
 
 
417
    public void disposing(EventObject event) {}
 
418
 
 
419
}