import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.validation.Schema;
import jp.sfjp.mikutoga.bin.parser.MmdFormatException;
import jp.sfjp.mikutoga.pmd.IllegalPmdDataException;
import jp.sfjp.mikutoga.pmd.model.PmdModel;
import jp.sfjp.mikutoga.pmd.model.binio.PmdExporter;
import jp.sfjp.mikutoga.pmd.model.binio.PmdLoader;
import jp.sfjp.mikutoga.pmd.model.xml.PmdXmlExporter;
-import jp.sfjp.mikutoga.pmd.model.xml.Schema101009;
-import jp.sfjp.mikutoga.pmd.model.xml.Schema130128;
import jp.sfjp.mikutoga.pmd.model.xml.XmlModelFileType;
import jp.sfjp.mikutoga.pmd.model.xml.XmlPmdLoader;
-import jp.sfjp.mikutoga.xml.BotherHandler;
-import jp.sfjp.mikutoga.xml.LocalXmlResource;
-import jp.sfjp.mikutoga.xml.SchemaUtil;
import jp.sfjp.mikutoga.xml.TogaXmlException;
-import jp.sfjp.mikutoga.xml.XmlResourceResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
- * ドキュメントビルダファクトリを初期化する。
- * @param builderFactory ドキュメントビルダファクトリ
- */
- private static void initBuilderFactory(
- DocumentBuilderFactory builderFactory ){
- builderFactory.setCoalescing(true);
- builderFactory.setExpandEntityReferences(true);
- builderFactory.setIgnoringComments(true);
- builderFactory.setIgnoringElementContentWhitespace(false);
- builderFactory.setNamespaceAware(true);
- builderFactory.setValidating(false);
- builderFactory.setXIncludeAware(false);
-
-// builderFactory.setFeature(name, value);
-// builderFactory.setAttribute(name, value);
-
- return;
- }
-
- /**
- * DOMビルダ生成。
- * @return DOMビルダ
- */
- private DocumentBuilder buildBuilder(){
- XmlResourceResolver resolver = new XmlResourceResolver();
-
- LocalXmlResource[] schemaArray;
- switch(this.inTypes){
- case XML_101009:
- schemaArray = new LocalXmlResource[]{
- Schema101009.SINGLETON,
- };
- break;
- case XML_130128:
- schemaArray = new LocalXmlResource[]{
- Schema130128.SINGLETON,
- };
- break;
- case XML_AUTO:
- schemaArray = new LocalXmlResource[]{
- Schema101009.SINGLETON,
- Schema130128.SINGLETON,
- };
- break;
- default:
- throw new IllegalStateException();
- }
-
- Schema schema = SchemaUtil.newSchema(resolver, schemaArray);
-
- DocumentBuilderFactory builderFactory =
- DocumentBuilderFactory.newInstance();
- initBuilderFactory(builderFactory);
- builderFactory.setSchema(schema);
-
- DocumentBuilder result;
- try{
- result = builderFactory.newDocumentBuilder();
- }catch(ParserConfigurationException e){
- assert false;
- throw new AssertionError(e);
- }
- result.setEntityResolver(resolver);
- result.setErrorHandler(BotherHandler.HANDLER);
-
- return result;
- }
-
-
- /**
* 入力ファイル種別を設定する。
* @param type ファイル種別
* @throws NullPointerException 引数がnull
/**
* ファイル変換を行う。
+ * <p>XML入力の場合は{@link #convert(InputSource, OutputStream)}を
+ * 推奨する。
* @param is 入力ストリーム
* @param os 出力ストリーム
* @throws IOException 入力エラー
}
/**
+ * ファイル変換を行う。
+ * <p>PMD入力の場合は{@link InputStream}に
+ * バイトストリームが直接設定されていなければならない。
+ * <p>XML入力の場合は{@link InputStream}に
+ * URL(systemId)のみの設定を推奨する。
+ * @param source 入力ソース
+ * @param os 出力ストリーム
+ * @throws IOException 入力エラー
+ * @throws MmdFormatException フォーマットエラー
+ * @throws SAXException XMLエラー
+ * @throws TogaXmlException XMLエラー
+ * @throws IllegalPmdDataException 内部エラー
+ */
+ public void convert(InputSource source, OutputStream os)
+ throws IOException,
+ MmdFormatException,
+ SAXException,
+ TogaXmlException,
+ IllegalPmdDataException {
+ PmdModel model = readModel(source);
+ writeModel(model, os);
+ return;
+ }
+
+ /**
* モデルファイルを読み込む。
+ * <p>XML読み込みの場合は、
+ * こちらより{@link #readModel(InputSource)}版を推奨する。
* @param is 入力ストリーム
* @return モデルデータ
* @throws IOException 入力エラー
MmdFormatException,
SAXException,
TogaXmlException {
+ InputSource source = new InputSource(is);
+
+ PmdModel model;
+
+ try{
+ model = readModel(source);
+ }finally{
+ is.close();
+ }
+
+ return model;
+ }
+
+ /**
+ * モデルファイルを読み込む。
+ * @param source 入力ソース
+ * @return モデルデータ
+ * @throws IOException 入力エラー
+ * @throws MmdFormatException フォーマットエラー
+ * @throws SAXException XMLエラー
+ * @throws TogaXmlException XMLエラー
+ */
+ public PmdModel readModel(InputSource source)
+ throws IOException,
+ MmdFormatException,
+ SAXException,
+ TogaXmlException {
PmdModel model = null;
if(this.inTypes.isPmd()){
- model = pmdRead(is);
+ InputStream is = XmlInputUtil.openInputSource(source);
+ try{
+ model = pmdRead(is);
+ }finally{
+ is.close();
+ }
}else if(this.inTypes.isXml()){
- model = xmlRead(is);
+ model = xmlRead(source);
}else{
throw new IllegalStateException();
}
/**
* XMLファイルからモデルデータを読み込む。
- * @param is 入力ストリーム
- * @return モデルデータ
- * @throws IOException 入力エラー
- * @throws SAXException XML構文エラー
- * @throws TogaXmlException 不正なXMLデータ
- */
- private PmdModel xmlRead(InputStream is)
- throws IOException,
- SAXException,
- TogaXmlException {
- InputSource source = new InputSource(is);
- PmdModel result = xmlRead(source);
- return result;
- }
-
- /**
- * XMLファイルからモデルデータを読み込む。
* @param source 入力ソース
* @return モデルデータ
* @throws IOException 入力エラー