OSDN Git Service

f276698d12bde970d44ce247daca3964b349ce78
[mikutoga/Pmd2XML.git] / src / main / java / jp / sfjp / mikutoga / pmd / model / xml / ExtraExporter.java
1 /*
2  * extra xml exporter
3  *
4  * License : The MIT License
5  * Copyright(c) 2013 MikuToga Partners
6  */
7
8 package jp.sfjp.mikutoga.pmd.model.xml;
9
10 import java.io.IOException;
11 import jp.sfjp.mikutoga.corelib.I18nText;
12 import jp.sfjp.mikutoga.math.MkPos3D;
13 import jp.sfjp.mikutoga.pmd.Rad3d;
14 import jp.sfjp.mikutoga.pmd.model.SerialNumbered;
15 import jp.sfjp.mikutoga.xml.ProxyXmlExporter;
16 import jp.sfjp.mikutoga.xml.XmlExporter;
17
18 /**
19  * XML出力機構の共通部。
20  */
21 class ExtraExporter extends ProxyXmlExporter {
22
23     static final String PFX_SURFACEGROUP = "sg";
24     static final String PFX_TOONFILE     = "tf";
25     static final String PFX_VERTEX       = "vtx";
26     static final String PFX_BONE         = "bn";
27     static final String PFX_RIGID        = "rd";
28     static final String PFX_RIGIDGROUP   = "rg";
29
30     private static final char CAP_BASIC_LATIN = '\u007f';
31
32
33     /**
34      * コンストラクタ。
35      * @param delegate 委譲先
36      */
37     ExtraExporter(XmlExporter delegate){
38         super(delegate);
39         return;
40     }
41
42
43     /**
44      * 任意の文字列がBasicLatin文字のみから構成されるか判定する。
45      * @param seq 文字列
46      * @return null、長さ0もしくはBasicLatin文字のみから構成されるならtrue
47      */
48     static boolean hasOnlyBasicLatin(CharSequence seq){
49         if(seq == null) return true;
50         int length = seq.length();
51         for(int pos = 0; pos < length; pos++){
52             char ch = seq.charAt(pos);
53             if(ch > CAP_BASIC_LATIN) return false;
54         }
55         return true;
56     }
57
58
59     /**
60      * 文字参照によるエスケープを補佐するためのコメントを出力する。
61      * @param seq 文字列
62      * @throws IOException 出力エラー
63      */
64     void putUnescapedComment(CharSequence seq)
65             throws IOException{
66         if( ! isBasicLatinOnlyOut() ) return;
67         if(hasOnlyBasicLatin(seq)) return;
68         sp().putLineComment(seq);
69         return;
70     }
71
72     /**
73      * 多言語識別名属性のローカルな名前をコメント出力する。
74      * @param name 多言語識別名
75      * @throws IOException 出力エラー
76      */
77     void putLocalNameComment(I18nText name)
78             throws IOException{
79         String localName = name.getText();
80         if(localName.isEmpty()){
81             localName = "[NAMELESS]";
82         }
83         ind().putLineComment(localName);
84         return;
85     }
86
87     /**
88      * 多言語識別名属性のプライマリな名前を出力する。
89      * @param attr 属性名
90      * @param name 多言語識別名
91      * @throws IOException 出力エラー
92      */
93     void putPrimaryNameAttr(PmdAttr attr, I18nText name)
94             throws IOException{
95         String attrName = attr.attr();
96         String primaryName = name.getPrimaryText();
97         putAttr(attrName, primaryName);
98         return;
99     }
100
101     /**
102      * 多言語化された各種識別名を出力する。
103      * プライマリ名は出力対象外。
104      * @param text 多言語文字列
105      * @throws IOException 出力エラー
106      */
107     void putI18nName(I18nText text) throws IOException{
108         for(String lang639 : text.lang639CodeList()){
109             if(lang639.equals(I18nText.CODE639_PRIMARY)) continue;
110             String name = text.getI18nText(lang639);
111             ind().putOpenSTag(PmdTag.I18N_NAME.tag()).sp();
112             putAttr(PmdAttr.LANG.attr(), lang639).sp();
113             putAttr(PmdAttr.NAME.attr(), name).sp();
114             putCloseEmpty();
115             putUnescapedComment(name);
116             ln();
117         }
118         return;
119     }
120
121     /**
122      * 番号付けされたID(IDREF)属性を出力する。
123      * @param attr 属性名
124      * @param prefix IDプレフィクス
125      * @param num 番号
126      * @throws IOException 出力エラー
127      */
128     void putNumberedIdAttr(PmdAttr attr,
129                              CharSequence prefix,
130                              int num )
131             throws IOException{
132         String attrName = attr.attr();
133         putRawText(attrName).putRawCh('=');
134         putRawCh('"');
135         putRawText(prefix).putXsdInt(num);
136         putRawCh('"');
137         return;
138     }
139
140     /**
141      * 番号付けされたID(IDREF)属性を出力する。
142      * @param attr 属性名
143      * @param prefix IDプレフィクス
144      * @param numbered 番号付けされたオブジェクト
145      * @throws IOException 出力エラー
146      */
147     void putNumberedIdAttr(PmdAttr attr,
148                              CharSequence prefix,
149                              SerialNumbered numbered )
150             throws IOException{
151         putNumberedIdAttr(attr, prefix, numbered.getSerialNumber());
152         return;
153     }
154
155     /**
156      * 位置情報を出力する。
157      * @param position 位置情報
158      * @throws IOException 出力エラー
159      */
160     void putPosition(MkPos3D position)
161             throws IOException{
162         putOpenSTag("position").sp();
163
164         putFloatAttr(PmdAttr.X.attr(),
165                 (float) position.getXpos()).sp();
166         putFloatAttr(PmdAttr.Y.attr(),
167                 (float) position.getYpos()).sp();
168         putFloatAttr(PmdAttr.Z.attr(),
169                 (float) position.getZpos()).sp();
170
171         putCloseEmpty();
172
173         return;
174     }
175
176     /**
177      * 姿勢情報(ラジアン)を出力する。
178      * @param rotation 姿勢情報
179      * @throws IOException 出力エラー
180      */
181     void putRadRotation(Rad3d rotation)
182             throws IOException{
183         putOpenSTag("radRotation").sp();
184
185         putFloatAttr(PmdAttr.X_RAD.attr(), rotation.getXRad()).sp();
186         putFloatAttr(PmdAttr.Y_RAD.attr(), rotation.getYRad()).sp();
187         putFloatAttr(PmdAttr.Z_RAD.attr(), rotation.getZRad()).sp();
188
189         putCloseEmpty();
190
191         return;
192     }
193
194 }