[ADOS] ADOS Developer's Site - XML Stadium
ADOS Company slogan - XML Total Solution

 RIGHT:[[目次>Hestia Hacks]]|[[前>受信したServiceContentをXMLファイルに変換し、出力]]|[[次>受信したServiceContentをCSVファイルに変換し、出力]]
 #freeze
 RIGHT:[[目次>Hestia Hacks#SDKDevDuide]]|[[前>受信したServiceContentをXMLファイルに変換し、出力]]|[[次>受信したServiceContentをCSVファイルに変換し、出力]]
 
 目次
 
 #contents
 
 *概要 [#l7f9a99f]
 
 CSV(Comma Separated Values)は、レガシーシステムのデータ入出力によく使用されるフォーマットです。CSVを入力して、送信するServiceContentに変換する方法を解説します。
 
 【PIPSenderCSV.java】
  1 : import java.io.File;
  2 : import java.io.FileInputStream;
  3 : import java.io.FileReader;
  4 : import java.util.Date;
  5 : import java.util.Properties;
  6 : 
  7 : import com.ados.hestia.mapping.Translator;
  8 : import com.ados.hestia.mapping.TranslatorFactory;
  9 : import com.ados.hestia.parser.rnbm.access.Validator;
  10 : import com.ados.hestia.parser.rnbm.exchange.Container;
  11 : import com.ados.hestia.parser.rnbm.exchange.PayloadContainer;
  12 : import com.ados.hestia.parser.rnbm.exchange.StatusContainer;
  13 : import com.ados.hestia.parser.rnbm.particle.ServiceContent;
  14 : import com.ados.hestia.repository.MapRuleStore;
  15 : import com.ados.hestia.repository.Mapping;
  16 : import com.ados.hestia.sdk.Configuration;
  17 : import com.ados.hestia.sdk.Connection;
  18 : import com.ados.hestia.sdk.message.DirectMessageExchanger;
  19 : import com.ados.hestia.sdk.session.InitiationSession;
  20 : import com.ados.hestia.sdk.session.Session;
  21 : import com.ados.hestia.utils.UID;
  22 : import com.ados.hestia.utils.csv.Parser;
  23 : import com.ados.hestia.utils.csv.ParserImpl;
  24 : import com.ados.hestia.utils.csv.RecordSet;
  25 : 
  26 : public class PIPSenderCSV {
  27 : 
  28 :   public static void main(String[] args) {
  29 :     try {
  30 :       String mapid = args[0];
  31 :       String csv = args[1];
  32 : 
  33 :       Properties prop = new Properties();
  34 :       prop.load(new FileInputStream("sample.properties"));
  35 :       String HOST = prop.getProperty("HOST");
  36 :       String USER = prop.getProperty("USER");
  37 :       String PASS = prop.getProperty("PASS");
  38 :       String HANDLER_ID = prop.getProperty("HANDLER_ID");
  39 : 
  40 :       Connection connection = new Connection(HOST, USER, PASS,
  41 :           Connection.EXCHANGE_MODE_DIRECT, null, null, null,
  42 :           HANDLER_ID);
  43 :       DirectMessageExchanger exchanger = new DirectMessageExchanger(
  44 :           connection, Configuration.getInstance());
  45 : 
  46 :       MapRuleStore store = new MapRuleStore(System.getProperty("user.dir")
  47 :           + File.separator, "mapRule");
  48 :       SampleUtils.println("Mapping :" + store.size());
  49 :       Mapping mapping = store.getMapping(mapid);
  50 :       store.fillMappingBaseDir(mapping);
  51 :       SampleUtils.println("Mapping :" + mapping.toString());
  52 :       Parser parser = new ParserImpl();
  53 :       RecordSet record = parser.parse(new FileReader(csv), ',', '\'', true);
  54 :       TranslatorFactory factory = new TranslatorFactory();
  55 :       Translator translator = factory.newTranslator(mapping);
  56 :       translator.setTargetDocument(record);
  57 :       translator.translate();
  58 :       ServiceContent sc = translator.getResultAsServiceContent();
  59 : 
  60 :       Session session = new InitiationSession(new PayloadContainer(
  61 :           new Date(), sc.getFromRole()
  62 :               .getGlobalBusinessIdentifierNorm(), sc.getToRole()
  63 :               .getGlobalBusinessIdentifierNorm(), (new UID())
  64 :               .toString(), sc, null));
  65 :       SampleUtils.printSessionState("created", session);
  66 :       PayloadContainer request = session.getMessageToSend();
  67 :       SampleUtils.printSessionState("got", session);
  68 :       SampleUtils.printContainerInformation(request);
  69 :       SampleUtils.printServiceContent(request.getServiceContent());
  70 :       new Validator(request.getServiceContent().getElement()).validate();
  71 :       exchanger.sendMessage(request);
  72 :       SampleUtils.printSessionState("sent", session);
  73 : 
  74 :       int time = 0;
  75 :       boolean loop = true;
  76 :       while (loop) {
  77 :         Thread.sleep(5000);
  78 :         exchanger.hit();
  79 :         SampleUtils.printSessionState("loop=" + time++, session);
  80 : 
  81 :         Container container = exchanger.receiveMessage();
  82 :         if (container != null) {
  83 :           SampleUtils.printContainerInformation(container);
  84 :           session.pushStatus((StatusContainer) container);
  85 :           SampleUtils.printSessionState("pushed", session);
  86 :           loop = false;
  87 :         }
  88 :       }
  89 : 
  90 :     } catch (Exception e) {
  91 :       e.printStackTrace();
  92 :     }
  93 :   }
  94 : 
  95 : }
 
 *Mappingの取得 [#o91b52eb]
 
 XMLからServiceContentへの変換と同様、CSVからServiceContentへの変換にもXSLTファイルおよびMappingの仕組みを使用します。Mappingの取得方法は通常と同じです。
 
  46 :       MapRuleStore store = new MapRuleStore(System.getProperty("user.dir")
  47 :           + File.separator, "mapRule");
 
  49 :       Mapping mapping = store.getMapping(mapid);
  50 :       store.fillMappingBaseDir(mapping);
 
 なお、CSVを以下のようなXMLに置き換え、XML to ServiceContentとしてXSLT文書を定義します。
 なお、CSVは以下のようなXML形式に置き換えられ、読み込まれます。XSLTは、XML to ServiceContentとして変換処理を定義します。
 
 【CSVの例】
 |AAA|BBB|CCC|h
 |a1|b1|c1|
 |a2|b2|c2|
 
 【CSVに相当するXMLのイメージ】
  <csv>
      <row>
          <AAA>a1</AAA>
          <BBB>b1</BBB>
          <CCC>c1</CCC>
      </row>
      <row>
          <AAA>a2</AAA>
          <BBB>b2</BBB>
          <CCC>c2</CCC>
      </row>
      ・・・
  </csv>
 
 
 
 *CSVの読み込み [#k9fae848]
 
 CSVを読み込みます。カンマ以外のセパレータを指定することもできます。読み込まれたCSVはRecordSetにより扱えます。
 
  52 :       Parser parser = new ParserImpl();
  53 :       RecordSet record = parser.parse(new FileReader(csv), ',', '\'', true);
 
 |関連クラス|機能|h
 |com.ados.hestia.utils.csv.Parser|CSVを読み込むインターフェイス。|
 |com.ados.hestia.utils.csv.ParserImpl|Parserの実装クラス。|
 |com.ados.hestia.utils.csv.RecordSet|Parserで読み込まれたCSVを表すクラス。|
 
 *変換とServiceContentの取得 [#kcc3626b]
 
 CSVをServiceContentへ変換します。まず、変換を行うTranslatorをTranslatorFactoryから生成します。TranslatorにはMappingが設定されます。
 
  54 :       TranslatorFactory factory = new TranslatorFactory();
  55 :       Translator translator = factory.newTranslator(mapping);
 
 変換されるRecordSetを入力します。
 
  56 :       translator.setTargetDocument(record);
 
 XSLT変換を実行します。
 
  57 :       translator.translate();
 
 変換結果のServiceContentを取得します。
 
  58 :       ServiceContent sc = translator.getResultAsServiceContent();
 
 ServiceContentの生成後は、通常の処理によりHestiaへ送信します。ただし、ServiceContentの生成後に、 ServiceContent#setElementValue等のメソッドにより値を上書きすることもできます。

トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS

Copyright 2005-2008. ADOS Co., Ltd. All Rights Reserved.