001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in 015 * Java(TM), hosted at https://github.com/gunterze/dcm4che. 016 * 017 * The Initial Developer of the Original Code is 018 * Agfa Healthcare. 019 * Portions created by the Initial Developer are Copyright (C) 2012 020 * the Initial Developer. All Rights Reserved. 021 * 022 * Contributor(s): 023 * See @authors listed below 024 * 025 * Alternatively, the contents of this file may be used under the terms of 026 * either the GNU General Public License Version 2 or later (the "GPL"), or 027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 028 * in which case the provisions of the GPL or the LGPL are applicable instead 029 * of those above. If you wish to allow use of your version of this file only 030 * under the terms of either the GPL or the LGPL, and not to allow others to 031 * use your version of this file under the terms of the MPL, indicate your 032 * decision by deleting the provisions above and replace them with the notice 033 * and other provisions required by the GPL or the LGPL. If you do not delete 034 * the provisions above, a recipient may use your version of this file under 035 * the terms of any one of the MPL, the GPL or the LGPL. 036 * 037 * ***** END LICENSE BLOCK ***** */ 038 039package org.dcm4che3.tool.hl72xml; 040 041import java.io.ByteArrayInputStream; 042import java.io.File; 043import java.io.FileInputStream; 044import java.io.IOException; 045import java.io.InputStream; 046import java.io.InputStreamReader; 047import java.io.Reader; 048import java.io.SequenceInputStream; 049import java.net.URL; 050import java.util.List; 051import java.util.ResourceBundle; 052 053import javax.xml.transform.OutputKeys; 054import javax.xml.transform.TransformerConfigurationException; 055import javax.xml.transform.TransformerFactory; 056import javax.xml.transform.sax.SAXTransformerFactory; 057import javax.xml.transform.sax.TransformerHandler; 058import javax.xml.transform.stream.StreamResult; 059import javax.xml.transform.stream.StreamSource; 060 061import org.apache.commons.cli.CommandLine; 062import org.apache.commons.cli.OptionBuilder; 063import org.apache.commons.cli.Options; 064import org.apache.commons.cli.ParseException; 065import org.dcm4che3.hl7.HL7Charset; 066import org.dcm4che3.hl7.HL7Parser; 067import org.dcm4che3.hl7.HL7Segment; 068import org.dcm4che3.tool.common.CLIUtils; 069import org.xml.sax.SAXException; 070 071/** 072 * @author Gunter Zeilinger <gunterze@gmail.com> 073 * 074 */ 075public class HL72Xml { 076 077 private static ResourceBundle rb = 078 ResourceBundle.getBundle("org.dcm4che3.tool.hl72xml.messages"); 079 080 private URL xslt; 081 private boolean indent = false; 082 private boolean includeNamespaceDeclaration = false; 083 private String charset; 084 085 public final void setXSLT(URL xslt) { 086 this.xslt = xslt; 087 } 088 089 public final void setIndent(boolean indent) { 090 this.indent = indent; 091 } 092 093 public final void setIncludeNamespaceDeclaration(boolean includeNamespaceDeclaration) { 094 this.includeNamespaceDeclaration = includeNamespaceDeclaration; 095 } 096 097 public String getCharacterSet() { 098 return charset; 099 } 100 101 public void setCharacterSet(String charset) { 102 this.charset = charset; 103 } 104 105 @SuppressWarnings("static-access") 106 private static CommandLine parseComandLine(String[] args) 107 throws ParseException { 108 Options opts = new Options(); 109 CLIUtils.addCommonOptions(opts); 110 opts.addOption(OptionBuilder 111 .withLongOpt("xsl") 112 .hasArg() 113 .withArgName("xsl-file") 114 .withDescription(rb.getString("xsl")) 115 .create("x")); 116 opts.addOption(OptionBuilder 117 .withLongOpt("charset") 118 .hasArg() 119 .withArgName("name") 120 .withDescription(rb.getString("charset")) 121 .create(null)); 122 opts.addOption("I", "indent", false, rb.getString("indent")); 123 opts.addOption(null, "xmlns", false, rb.getString("xmlns")); 124 125 return CLIUtils.parseComandLine(args, opts, rb, HL72Xml.class); 126 } 127 128 @SuppressWarnings("unchecked") 129 public static void main(String[] args) { 130 try { 131 CommandLine cl = parseComandLine(args); 132 HL72Xml main = new HL72Xml(); 133 if (cl.hasOption("x")) { 134 String s = cl.getOptionValue("x"); 135 main.setXSLT(new File(s).toURI().toURL()); 136 } 137 main.setCharacterSet(cl.getOptionValue("charset")); 138 main.setIndent(cl.hasOption("I")); 139 main.setIncludeNamespaceDeclaration(cl.hasOption("xmlns")); 140 String fname = fname(cl.getArgList()); 141 if (fname.equals("-")) { 142 main.parse(System.in); 143 } else { 144 FileInputStream dis = new FileInputStream(fname); 145 try { 146 main.parse(dis); 147 } finally { 148 dis.close(); 149 } 150 } 151 } catch (ParseException e) { 152 System.err.println("hl72xml: " + e.getMessage()); 153 System.err.println(rb.getString("try")); 154 System.exit(2); 155 } catch (Exception e) { 156 System.err.println("hl72xml: " + e.getMessage()); 157 e.printStackTrace(); 158 System.exit(2); 159 } 160 } 161 162 private static String fname(List<String> argList) throws ParseException { 163 int numArgs = argList.size(); 164 if (numArgs == 0) 165 throw new ParseException(rb.getString("missing")); 166 if (numArgs > 1) 167 throw new ParseException(rb.getString("too-many")); 168 return argList.get(0); 169 } 170 171 public void parse(InputStream is) throws IOException, 172 TransformerConfigurationException, SAXException { 173 byte[] buf = new byte[256]; 174 int len = is.read(buf); 175 HL7Segment msh = HL7Segment.parseMSH(buf, buf.length); 176 String charsetName = HL7Charset.toCharsetName(msh.getField(17, charset)); 177 Reader reader = new InputStreamReader( 178 new SequenceInputStream( 179 new ByteArrayInputStream(buf, 0, len), is), 180 charsetName); 181 TransformerHandler th = getTransformerHandler(); 182 th.getTransformer().setOutputProperty(OutputKeys.INDENT, 183 indent ? "yes" : "no"); 184 th.setResult(new StreamResult(System.out)); 185 HL7Parser hl7Parser = new HL7Parser(th); 186 hl7Parser.setIncludeNamespaceDeclaration(includeNamespaceDeclaration); 187 hl7Parser.parse(reader); 188 } 189 190 private TransformerHandler getTransformerHandler() 191 throws TransformerConfigurationException, IOException { 192 SAXTransformerFactory tf = (SAXTransformerFactory) 193 TransformerFactory.newInstance(); 194 if (xslt == null) 195 return tf.newTransformerHandler(); 196 197 TransformerHandler th = tf.newTransformerHandler( 198 new StreamSource(xslt.openStream(), xslt.toExternalForm())); 199 return th; 200 } 201 202}