4 * License : The MIT License
5 * Copyright(c) 2013 MikuToga Partners
8 package jp.sfjp.mikutoga.vmd2xml;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.OutputStream;
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 import javax.xml.validation.Schema;
17 import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
18 import jp.sfjp.mikutoga.vmd.IllegalVmdDataException;
19 import jp.sfjp.mikutoga.vmd.model.VmdMotion;
20 import jp.sfjp.mikutoga.vmd.model.binio.VmdExporter;
21 import jp.sfjp.mikutoga.vmd.model.binio.VmdLoader;
22 import jp.sourceforge.mikutoga.vmd.model.xml.Schema110820;
23 import jp.sourceforge.mikutoga.vmd.model.xml.VmdXmlExporter;
24 import jp.sourceforge.mikutoga.vmd.model.xml.Xml2VmdLoader;
25 import jp.sourceforge.mikutoga.xml.BotherHandler;
26 import jp.sourceforge.mikutoga.xml.SchemaUtil;
27 import jp.sourceforge.mikutoga.xml.TogaXmlException;
28 import jp.sourceforge.mikutoga.xml.XmlResourceResolver;
29 import org.xml.sax.InputSource;
30 import org.xml.sax.SAXException;
35 public class Vmd2XmlConv {
37 private MotionFileType inTypes = MotionFileType.NONE;
38 private MotionFileType outTypes = MotionFileType.NONE;
39 private String newLine = "\r\n";
40 private String generator = null;
41 private boolean isQuaternionMode = true;
54 * ドキュメントビルダファクトリを初期化する。
55 * @param builderFactory ドキュメントビルダファクトリ
57 private static void initBuilderFactory(
58 DocumentBuilderFactory builderFactory ){
59 builderFactory.setCoalescing(true);
60 builderFactory.setExpandEntityReferences(true);
61 builderFactory.setIgnoringComments(true);
62 builderFactory.setIgnoringElementContentWhitespace(false);
63 builderFactory.setNamespaceAware(true);
64 builderFactory.setValidating(false);
65 builderFactory.setXIncludeAware(false);
67 // builderFactory.setFeature(name, value);
68 // builderFactory.setAttribute(name, value);
77 private DocumentBuilder buildBuilder(){
78 XmlResourceResolver resolver = new XmlResourceResolver();
81 schema = SchemaUtil.newSchema(resolver, Schema110820.SINGLETON);
83 DocumentBuilderFactory builderFactory =
84 DocumentBuilderFactory.newInstance();
85 initBuilderFactory(builderFactory);
86 builderFactory.setSchema(schema);
88 DocumentBuilder result;
90 result = builderFactory.newDocumentBuilder();
91 }catch(ParserConfigurationException e){
93 throw new AssertionError(e);
95 result.setEntityResolver(resolver);
96 result.setErrorHandler(BotherHandler.HANDLER);
104 * @throws IllegalArgumentException 具体的な種別を渡さなかった
106 public void setInType(MotionFileType type)
107 throws IllegalArgumentException {
108 if(type == null) throw new NullPointerException();
109 if(type == MotionFileType.NONE) throw new IllegalArgumentException();
118 public MotionFileType getInTypes(){
125 * @throws IllegalArgumentException 具体的な種別を渡さなかった
127 public void setOutType(MotionFileType type)
128 throws IllegalArgumentException {
129 if(type == null) throw new NullPointerException();
130 if(type == MotionFileType.NONE) throw new IllegalArgumentException();
131 this.outTypes = type;
139 public MotionFileType getOutTypes(){
140 return this.outTypes;
145 * @param newline 改行文字
147 public void setNewline(String newline){
148 this.newLine = newline;
156 public String getNewline(){
162 * @param generator ジェネレータ名。表示したくない場合はnull
164 public void setGenerator(String generator){
165 this.generator = generator;
171 * @return ジェネレータ名。非表示の場合はnullを返す。
173 public String getGenerator(){
174 return this.generator;
178 * 回転情報をクォータニオン形式でXML出力するか設定する。
179 * @param sw クォータニオン形式ならtrue、
180 * YXZオイラー角で出力したければfalse。
182 public void setQuaterniomMode(boolean sw){
183 this.isQuaternionMode = sw;
189 * @return クォータニオン形式ならtrue、YXZオイラー角形式ならfalse。
191 public boolean isQuaterniomMode(){
192 return this.isQuaternionMode;
199 * @throws IOException 入力エラー
200 * @throws MmdFormatException フォーマットエラー
201 * @throws SAXException XMLエラー
202 * @throws TogaXmlException XMLエラー
203 * @throws IllegalVmdDataException 内部エラー
205 public void convert(InputStream is, OutputStream os)
210 IllegalVmdDataException {
211 VmdMotion motion = readMotion(is);
213 writeMotion(motion, os);
221 * @throws IOException 入力エラー
222 * @throws MmdFormatException フォーマットエラー
223 * @throws SAXException XMLエラー
224 * @throws TogaXmlException XMLエラー
226 public VmdMotion readMotion(InputStream is)
231 VmdMotion motion = null;
233 if(this.inTypes.isVmd()){
234 motion = vmdRead(is);
235 }else if(this.inTypes.isXml()){
236 motion = xmlRead(is);
238 throw new IllegalStateException();
246 * @param motion モーションデータ
248 * @throws IOException 出力エラー
249 * @throws IllegalVmdDataException データの不備
251 public void writeMotion(VmdMotion motion, OutputStream os)
253 IllegalVmdDataException {
254 if(this.outTypes.isVmd()){
256 }else if(this.outTypes.isXml()){
259 throw new IllegalStateException();
266 * VMDファイルからモーションデータを読み込む。
269 * @throws IOException 入力エラー
270 * @throws MmdFormatException 不正なVMDファイルフォーマット
272 private VmdMotion vmdRead(InputStream is)
273 throws IOException, MmdFormatException{
274 VmdLoader loader = new VmdLoader();
276 loader.setIgnoreName(true);
277 loader.setRedundantCheck(false);
279 VmdMotion motion = loader.load(is);
285 * XMLファイルからモーションデータを読み込む。
288 * @throws IOException 入力エラー
289 * @throws SAXException XML構文エラー
290 * @throws TogaXmlException 不正なXMLデータ
292 private VmdMotion xmlRead(InputStream is)
296 InputSource source = new InputSource(is);
297 VmdMotion result = xmlRead(source);
302 * XMLファイルからモーションデータを読み込む。
303 * @param source 入力ソース
305 * @throws IOException 入力エラー
306 * @throws SAXException XML構文エラー
307 * @throws TogaXmlException 不正なXMLデータ
309 private VmdMotion xmlRead(InputSource source)
313 DocumentBuilder builder = buildBuilder();
314 Xml2VmdLoader loader = new Xml2VmdLoader(builder);
315 VmdMotion motion = loader.parse(source);
320 * モーションデータをVMDファイルに出力する。
321 * @param motion モーションデータ
322 * @param ostream 出力ストリーム
323 * @throws IOException 出力エラー
324 * @throws IllegalVmdDataException 不正なモーションデータ
326 private void vmdOut(VmdMotion motion, OutputStream ostream)
327 throws IOException, IllegalVmdDataException{
328 VmdExporter exporter = new VmdExporter();
329 exporter.dumpVmdMotion(motion, ostream);
335 * モーションデータをXMLファイルに出力する。
336 * @param motion モーションデータ
337 * @param ostream 出力ストリーム
338 * @throws IOException 出力エラー
339 * @throws IllegalVmdDataException 不正なモーションデータ
341 private void xmlOut(VmdMotion motion, OutputStream ostream)
342 throws IOException, IllegalVmdDataException{
343 VmdXmlExporter exporter = new VmdXmlExporter(ostream);
345 exporter.setNewLine(this.newLine);
346 exporter.setGenerator(this.generator);
347 exporter.setQuaternionMode(this.isQuaternionMode);
349 exporter.putVmdXml(motion);