Original 2.4
[marc4j.git] / src / org / marc4j / util / XmlMarcDriver.java
1 //$Id: XmlMarcDriver.java,v 1.4 2006/12/28 08:09:10 bpeters Exp $\r
2 /**\r
3  * Copyright (C) 2004 Bas Peters\r
4  *\r
5  * This file is part of MARC4J\r
6  *\r
7  * MARC4J is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU Lesser General Public \r
9  * License as published by the Free Software Foundation; either \r
10  * version 2.1 of the License, or (at your option) any later version.\r
11  *\r
12  * MARC4J is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
15  * Lesser General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU Lesser General Public \r
18  * License along with MARC4J; if not, write to the Free Software\r
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20  */\r
21 package org.marc4j.util;\r
22 \r
23 import java.io.File;\r
24 import java.io.FileInputStream;\r
25 import java.io.FileNotFoundException;\r
26 import java.io.FileOutputStream;\r
27 import java.io.InputStream;\r
28 import java.io.OutputStream;\r
29 \r
30 import javax.xml.transform.Source;\r
31 import javax.xml.transform.stream.StreamSource;\r
32 \r
33 import org.marc4j.Constants;\r
34 import org.marc4j.MarcStreamWriter;\r
35 import org.marc4j.MarcXmlReader;\r
36 import org.marc4j.converter.CharConverter;\r
37 import org.marc4j.converter.impl.UnicodeToAnsel;\r
38 import org.marc4j.converter.impl.UnicodeToIso5426;\r
39 import org.marc4j.converter.impl.UnicodeToIso6937;\r
40 import org.marc4j.marc.Record;\r
41 \r
42 /**\r
43  * Provides a driver to convert MARCXML records to MARC format.\r
44  * \r
45  * <p>\r
46  * The following example reads input.xml and writes output to the console:\r
47  * </p>\r
48  * \r
49  * <pre>\r
50  *       java org.marc4j.util.XmlMarcDriver input.xml\r
51  * </pre>\r
52  * \r
53  * <p>\r
54  * The following example reads input.xml, converts UTF-8 and writes output in\r
55  * MARC-8 to output.mrc:\r
56  * </p>\r
57  * \r
58  * <pre>\r
59  *       java org.marc4j.util.XmlMarcDriver -convert MARC8 -out output.mrc input.xml\r
60  * </pre>\r
61  * \r
62  * <p>\r
63  * It is possible to pre-process the input file using an XSLT stylesheet. The\r
64  * transformation should produce valid MARCXML. The following example transforms\r
65  * a MODS file to MARCXML and outputs MARC records.\r
66  * </p>\r
67  * \r
68  * <pre>\r
69  *       java org.marc4j.util.XmlMarcDriver -convert MARC8 -out output.mrc -xsl http://www.loc.gov/standards/marcxml/xslt/MODS2MARC21slim.xsl modsfile.xml\r
70  * </pre>\r
71  * \r
72  * <p>\r
73  * For usage, run from the command-line with the following command:\r
74  * </p>\r
75  * \r
76  * <pre>\r
77  *       java org.marc4j.util.XmlMarcDriver -usage\r
78  * </pre>\r
79  * \r
80  * <p>\r
81  * Check the home page for <a href="http://www.loc.gov/standards/marcxml/">\r
82  * MARCXML </a> for more information about the MARCXML format.\r
83  * </p>\r
84  * \r
85  * @author Bas Peters\r
86  * @version $Revision: 1.4 $\r
87  * \r
88  */\r
89 public class XmlMarcDriver {\r
90 \r
91     /**\r
92      * Provides a static entry point.\r
93      * \r
94      * <p>\r
95      * Arguments:\r
96      * </p>\r
97      * <ul>\r
98      * <li>-xsl &lt;stylesheet URL&gt; - pre-process using XSLT-stylesheet</li>\r
99      * <li>-out &lt;output file&gt; - write to output file</li>\r
100      * <li>-convert &lt;encoding&gt; - convert UTF-8 to &lt;encoding&gt;\r
101      * (Supported encodings: MARC8, ISO5426, ISO6937)</li>\r
102      * <li>-encoding &lt;encoding&gt; - Output using specified Java character\r
103      * encoding</li>\r
104      * <li>-usage - show usage</li>\r
105      * <li>&lt;input file&gt; - input file with MARCXML records or a\r
106      * transformation source\r
107      * </ul>\r
108      */\r
109     public static void main(String args[]) {\r
110         long start = System.currentTimeMillis();\r
111 \r
112         String input = null;\r
113         String output = null;\r
114         String stylesheet = null;\r
115         String convert = null;\r
116         String encoding = null;\r
117 \r
118         for (int i = 0; i < args.length; i++) {\r
119             if (args[i].equals("-xsl")) {\r
120                 if (i == args.length - 1) {\r
121                     usage();\r
122                 }\r
123                 stylesheet = args[++i].trim();\r
124             } else if (args[i].equals("-out")) {\r
125                 if (i == args.length - 1) {\r
126                     usage();\r
127                 }\r
128                 output = args[++i].trim();\r
129             } else if (args[i].equals("-convert")) {\r
130                 if (i == args.length - 1) {\r
131                     usage();\r
132                 }\r
133                 convert = args[++i].trim();\r
134             } else if (args[i].equals("-encoding")) {\r
135                 if (i == args.length - 1) {\r
136                     usage();\r
137                 }\r
138                 encoding = args[++i].trim();\r
139             } else if (args[i].equals("-usage")) {\r
140                 usage();\r
141             } else if (args[i].equals("-help")) {\r
142                 usage();\r
143             } else {\r
144                 input = args[i].trim();\r
145 \r
146                 // Must be last arg\r
147                 if (i != args.length - 1) {\r
148                     usage();\r
149                 }\r
150             }\r
151         }\r
152         if (input == null) {\r
153             usage();\r
154         }\r
155 \r
156         InputStream in = null;\r
157         try {\r
158             in = new FileInputStream(new File(input));\r
159         } catch (FileNotFoundException e) {\r
160             e.printStackTrace();\r
161         }\r
162         MarcXmlReader reader = null;\r
163         if (stylesheet == null)\r
164             reader = new MarcXmlReader(in);\r
165         else {\r
166             Source source = new StreamSource(stylesheet);\r
167             reader = new MarcXmlReader(in, source);\r
168         }\r
169 \r
170         OutputStream out = null;\r
171         if (output != null)\r
172             try {\r
173                 out = new FileOutputStream(output);\r
174             } catch (FileNotFoundException e) {\r
175                 e.printStackTrace();\r
176             }\r
177         else\r
178             out = System.out;\r
179 \r
180         MarcStreamWriter writer = null;\r
181         if (encoding != null)\r
182             writer = new MarcStreamWriter(out, encoding);\r
183         else\r
184             writer = new MarcStreamWriter(out);\r
185 \r
186         if (convert != null) {\r
187             CharConverter charconv = null;\r
188             if (Constants.MARC_8_ENCODING.equals(convert))\r
189                 charconv = new UnicodeToAnsel();\r
190             else if (Constants.ISO5426_ENCODING.equals(convert))\r
191                 charconv = new UnicodeToIso5426();\r
192             else if (Constants.ISO6937_ENCODING.equals(convert))\r
193                 charconv = new UnicodeToIso6937();\r
194             else {\r
195                 System.err.println("Unknown character set");\r
196                 System.exit(1);\r
197             }\r
198             writer.setConverter(charconv);\r
199         }\r
200 \r
201         while (reader.hasNext()) {\r
202             Record record = reader.next();\r
203             if (Constants.MARC_8_ENCODING.equals(convert))\r
204                 record.getLeader().setCharCodingScheme(' ');\r
205             writer.write(record);\r
206         }\r
207         writer.close();\r
208 \r
209         System.err.println("Total time: "\r
210                 + (System.currentTimeMillis() - start) + " miliseconds");\r
211     }\r
212 \r
213     private static void usage() {\r
214         System.err.println("MARC4J, Copyright (C) 2002-2006 Bas Peters");\r
215         System.err\r
216                 .println("Usage: org.marc4j.util.XmlMarcDriver [-options] <file.mrc>");\r
217         System.err\r
218                 .println("       -convert <encoding> = Converts UTF-8 to <encoding>");\r
219         System.err\r
220                 .println("       Valid encodings are: MARC8, ISO5426, ISO6937");\r
221         System.err\r
222                 .println("       -encoding <encoding> = Output using specified Java character encoding");\r
223         System.err\r
224                 .println("       -xsl <file> = Pre-process MARCXML using XSLT stylesheet <file>");\r
225         System.err.println("       -out <file> = Output using <file>");\r
226         System.err.println("       -usage or -help = this message");\r
227         System.err\r
228                 .println("The program outputs MARC records in ISO 2709 format");\r
229         System.err\r
230                 .println("See http://marc4j.tigris.org for more information.");\r
231         System.exit(1);\r
232     }\r
233 \r
234 }