franta-hg@43
|
1 |
/**
|
franta-hg@43
|
2 |
* Alt2XML
|
franta-hg@43
|
3 |
* Copyright © 2014 František Kučera (frantovo.cz)
|
franta-hg@43
|
4 |
*
|
franta-hg@43
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@43
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@111
|
7 |
* the Free Software Foundation, version 3 of the License.
|
franta-hg@43
|
8 |
*
|
franta-hg@43
|
9 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@43
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@43
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@43
|
12 |
* GNU General Public License for more details.
|
franta-hg@43
|
13 |
*
|
franta-hg@43
|
14 |
* You should have received a copy of the GNU General Public License
|
franta-hg@43
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@43
|
16 |
*/
|
franta-hg@43
|
17 |
package cz.frantovo.alt2xml.out;
|
franta-hg@43
|
18 |
|
franta-hg@45
|
19 |
import java.io.IOException;
|
franta-hg@45
|
20 |
import java.util.logging.Level;
|
franta-hg@45
|
21 |
import java.util.logging.Logger;
|
franta-hg@45
|
22 |
import javax.xml.parsers.SAXParser;
|
franta-hg@45
|
23 |
import org.xml.sax.InputSource;
|
franta-hg@45
|
24 |
import org.xml.sax.SAXException;
|
franta-hg@45
|
25 |
import org.xml.sax.SAXNotRecognizedException;
|
franta-hg@45
|
26 |
import org.xml.sax.SAXNotSupportedException;
|
franta-hg@43
|
27 |
import org.xml.sax.ext.LexicalHandler;
|
franta-hg@45
|
28 |
import org.xml.sax.helpers.DefaultHandler;
|
franta-hg@43
|
29 |
|
franta-hg@43
|
30 |
/**
|
franta-hg@45
|
31 |
* Recommended base class for Actions based on handlers.
|
franta-hg@43
|
32 |
*
|
franta-hg@43
|
33 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@43
|
34 |
*/
|
franta-hg@45
|
35 |
public abstract class AbstractHandlerAction extends AbstractAction {
|
franta-hg@43
|
36 |
|
franta-hg@45
|
37 |
private static final Logger log = Logger.getLogger(AbstractHandlerAction.class.getName());
|
franta-hg@45
|
38 |
public static final String LEXICAL_HANDLER_PROPERTY = "http://xml.org/sax/properties/lexical-handler";
|
franta-hg@43
|
39 |
|
franta-hg@45
|
40 |
public AbstractHandlerAction(ActionContext actionContext) {
|
franta-hg@45
|
41 |
super(actionContext);
|
franta-hg@43
|
42 |
}
|
franta-hg@43
|
43 |
|
franta-hg@45
|
44 |
public abstract DefaultHandler getDefaultHandler();
|
franta-hg@43
|
45 |
|
franta-hg@43
|
46 |
/**
|
franta-hg@45
|
47 |
* For e.g. comment nodes.
|
franta-hg@45
|
48 |
*
|
franta-hg@45
|
49 |
* @return may return null if this output module does not support lexical events
|
franta-hg@43
|
50 |
*/
|
franta-hg@43
|
51 |
public LexicalHandler getLexicalHandler() {
|
franta-hg@43
|
52 |
return null;
|
franta-hg@43
|
53 |
}
|
franta-hg@43
|
54 |
|
franta-hg@45
|
55 |
@Override
|
franta-hg@45
|
56 |
public void run(SAXParser parser, InputSource source) throws OutputActionException {
|
franta-hg@45
|
57 |
DefaultHandler defaultHandler = getDefaultHandler();
|
franta-hg@45
|
58 |
LexicalHandler lexicalHandler = getLexicalHandler();
|
franta-hg@45
|
59 |
|
franta-hg@45
|
60 |
if (lexicalHandler == null) {
|
franta-hg@45
|
61 |
log.log(Level.FINE, "No LexicalHandler provided → no comment nodes etc. on output.");
|
franta-hg@45
|
62 |
} else {
|
franta-hg@45
|
63 |
try {
|
franta-hg@45
|
64 |
parser.setProperty(LEXICAL_HANDLER_PROPERTY, lexicalHandler);
|
franta-hg@45
|
65 |
} catch (SAXNotRecognizedException | SAXNotSupportedException e) {
|
franta-hg@45
|
66 |
log.log(Level.WARNING, "LexicalHandler registration exception:", e);
|
franta-hg@45
|
67 |
log.log(Level.WARNING,
|
franta-hg@45
|
68 |
"Tried to register the handler {0} as a LexicalHandler but LexicalHandlers are not supported by the parser {1}",
|
franta-hg@45
|
69 |
new Object[]{lexicalHandler, parser});
|
franta-hg@45
|
70 |
}
|
franta-hg@45
|
71 |
}
|
franta-hg@45
|
72 |
|
franta-hg@45
|
73 |
try {
|
franta-hg@45
|
74 |
parser.parse(source, defaultHandler);
|
franta-hg@45
|
75 |
} catch (IOException | SAXException e) {
|
franta-hg@45
|
76 |
throw new OutputActionException("Error while parsing document: " + source, e);
|
franta-hg@45
|
77 |
}
|
franta-hg@45
|
78 |
}
|
franta-hg@43
|
79 |
}
|