コードをちょっといじった。
package jp.personal.su.to; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import jp.personal.su.to.convert.XMLConvertUtil; /** * Hello world! * */ public final class App { /** * 実行クラスであるためコンストラクタは使用しない. */ private App() { } /** * 実行を開始するクラス. * * @param args 実行時の引数を以下のように指定する。指定しない場合はデフォルト値を利用する。 * <ul> * <li>1番目の引数はXMLファイルのパス.デフォルト値は"in.xml"</li> * <li>2番目の引数はXSLファイルのパス.デフォルト値は"convert.xsl"</li> * <li>3番目の引数は出力ファイルのパス.デフォルト値は"out.xml"</li> * </ul> */ public static void main(final String[] args) { String inXmlFilePath = "in.xml"; String xslFilePath = "convert.xsl"; String outXmlFilePath = "out.xml"; if (args.length >= 1) { inXmlFilePath = args[0]; } if (args.length >= 2) { xslFilePath = args[1]; } if (args.length >= 3) { outXmlFilePath = args[2]; } XMLConvertUtil converter = new XMLConvertUtil(); converter.setOutXmlFilePath(outXmlFilePath); try { converter.convert(inXmlFilePath, xslFilePath); } catch (TransformerConfigurationException tce) { // TODO Auto-generated catch block tce.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package jp.personal.su.to.convert; import java.io.File; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; /** * XMLを変換するためのユーティリティクラス. * */ public class XMLConvertUtil { /** 変換後に出力するファイルのエンコード. */ private String outputEncodingKind_ = "Shift_JIS"; /** 変換後に出力するファイルのパス(デフォルトはout.xml). */ private String outXmlFilePath_ = "out.xml"; /** * デフォルトコンストラクタ. */ public XMLConvertUtil() { } /** * xmlファイルをxslファイルにより変換します. * * @param xmlFilePath 変換データxmlファイルのパス * @param xslFilePath 変換定義xslファイルのパス * @throws TransformerException 変換失敗時に送出する例外 */ public void convert(final String xmlFilePath, final String xslFilePath) throws TransformerException { File xmlFile = new File(xmlFilePath); File xslFile = new File(xslFilePath); File outXmlFile = new File(outXmlFilePath_); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xslFile)); transformer.setOutputProperty(OutputKeys.ENCODING, this.outputEncodingKind_); transformer.transform(new StreamSource(xmlFile), new StreamResult(outXmlFile)); } /** * XMLの出力場所を取得する. * @return XMLの出力場所 */ public String getOutXmlFilePath() { return this.outXmlFilePath_; } /** * XMLの出力場所を設定する. * @param outXmlFilePath XMLの出力場所 */ public void setOutXmlFilePath(final String outXmlFilePath) { this.outXmlFilePath_ = outXmlFilePath; } }