• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Simon Pepping 2009
3  *
4  * The copyright owner licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* $Id$ */
18 
19 package org.tug.texhyphen;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 
28 import javax.xml.transform.Result;
29 import javax.xml.transform.Source;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.sax.SAXTransformerFactory;
33 import javax.xml.transform.sax.TransformerHandler;
34 import javax.xml.transform.stream.StreamResult;
35 import javax.xml.transform.stream.StreamSource;
36 
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
39 import org.xml.sax.XMLReader;
40 
41 /**
42  * Convert language data in ruby format to XML format
43  */
44 public final class ConvertLanguageData {
45 
46     /**
47      * @param languageDataPath
48      * @throws IOException
49      * @throws TransformerException
50      * @throws SAXException
51      * @throws URISyntaxException
52      */
convert(String languageDataPath, boolean useStylesheet)53     public static void convert(String languageDataPath, boolean useStylesheet)
54     throws IOException, TransformerException, SAXException, URISyntaxException {
55 
56         // input
57         InputStream inis = new FileInputStream(languageDataPath);
58         InputSource input = new InputSource(inis);
59         input.setSystemId(languageDataPath);
60         input.setEncoding("utf-8");
61         XMLReader reader = new LanguageDataParser();
62 
63         // output
64         String outPath = languageDataPath.replaceFirst("\\.rb$", ".xml");
65         Result result = new StreamResult(outPath);
66 
67         // transformation
68         TransformerFactory tf = TransformerFactory.newInstance();
69         if (!tf.getFeature(SAXTransformerFactory.FEATURE)) {
70             throw new TransformerException("TransformerFactory is not a SAXTransformerFactory");
71         }
72         SAXTransformerFactory stf = (SAXTransformerFactory) tf;
73         TransformerHandler th;
74         if (useStylesheet) {
75             URL xsltUrl = ConvertTeXPattern.class.getResource("ConvertLanguageData.xsl");
76             File xsltFile = new File(xsltUrl.toURI());
77             InputStream xsltStream = new FileInputStream(xsltFile);
78             Source xsltSource = new StreamSource(xsltStream);
79             xsltSource.setSystemId(xsltFile.getAbsolutePath());
80             th = stf.newTransformerHandler(xsltSource);
81         } else {
82             th = stf.newTransformerHandler();
83         }
84 
85         // pipeline
86         reader.setContentHandler(th);
87         reader.setProperty("http://xml.org/sax/properties/lexical-handler", th);
88         th.setResult(result);
89         reader.parse(input);
90     }
91 
92     /**
93      * @param args
94      * @throws IOException
95      * @throws TransformerException
96      * @throws SAXException
97      * @throws URISyntaxException
98      */
main(String[] args)99     public static void main(String[] args)
100     throws IOException, TransformerException, SAXException, URISyntaxException {
101         if (args[0].endsWith("--debug")) {
102             convert(args[1], false);
103         } else {
104             convert(args[0], true);
105         }
106     }
107 
108 }
109