- 追加された行はこの色です。
- 削除された行はこの色です。
RIGHT:[[目次>Hestia Hacks]]|[[前>]]|[[次>]]
#freeze
RIGHT:[[目次>Hestia Hacks#SDKDevDuide]]|[[前>受信したServiceContentをXMLファイルに出力]]|[[次>受信したServiceContentをXMLファイルに変換し、出力]]
目次
#contents
*概要 [#veca412c]
ServiceContent形式でないXMLを入力して、XSLTでServiceContentに変換する方法について解説します。
サンプルプログラム全体を以下に示します。
【PIPSenderStoredMap.java】
1 : import java.io.File;
2 : import java.io.FileInputStream;
3 : import java.util.Date;
4 : import java.util.Properties;
5 :
6 : import com.ados.hestia.mapping.Translator;
7 : import com.ados.hestia.mapping.TranslatorFactory;
8 : import com.ados.hestia.parser.rnbm.access.Validator;
9 : import com.ados.hestia.parser.rnbm.exchange.Container;
10 : import com.ados.hestia.parser.rnbm.exchange.PayloadContainer;
11 : import com.ados.hestia.parser.rnbm.exchange.StatusContainer;
12 : import com.ados.hestia.parser.rnbm.particle.ServiceContent;
13 : import com.ados.hestia.repository.MapRuleStore;
14 : import com.ados.hestia.repository.Mapping;
15 : import com.ados.hestia.sdk.Configuration;
16 : import com.ados.hestia.sdk.Connection;
17 : import com.ados.hestia.sdk.message.DirectMessageExchanger;
18 : import com.ados.hestia.sdk.session.InitiationSession;
19 : import com.ados.hestia.sdk.session.Session;
20 : import com.ados.hestia.utils.UID;
21 :
22 : public class PIPSenderStoredMap {
23 :
24 : public static void main(String[] args) {
25 : try {
26 : String mapid = args[0];
27 : String xml = args[1];
28 :
29 : Properties prop = new Properties();
30 : prop.load(new FileInputStream("sample.properties"));
31 : String HOST = prop.getProperty("HOST");
32 : String USER = prop.getProperty("USER");
33 : String PASS = prop.getProperty("PASS");
34 : String HANDLER_ID = prop.getProperty("HANDLER_ID");
35 :
36 : Connection connection = new Connection(HOST, USER, PASS,
37 : Connection.EXCHANGE_MODE_DIRECT, null, null, null,
38 : HANDLER_ID);
39 : DirectMessageExchanger exchanger = new DirectMessageExchanger(
40 : connection, Configuration.getInstance());
41 :
42 : MapRuleStore store = new MapRuleStore(System
43 : .getProperty("user.dir")
44 : + File.separator, "mapRule");
45 : SampleUtils.println("Mapping :" + store.size());
46 : Mapping mapping = store.getMapping(mapid);
47 : store.fillMappingBaseDir(mapping);
48 : SampleUtils.println("Mapping :" + mapping.toString());
49 : TranslatorFactory factory = new TranslatorFactory();
50 : Translator translator = factory.newTranslator(mapping);
51 : translator.setTargetDocument(new FileInputStream(xml));
52 : translator.translate();
53 : ServiceContent sc = translator.getResultAsServiceContent();
54 :
55 : Session session = new InitiationSession(new PayloadContainer(
56 : new Date(), sc.getFromRole()
57 : .getGlobalBusinessIdentifierNorm(), sc.getToRole()
58 : .getGlobalBusinessIdentifierNorm(), (new UID())
59 : .toString(), sc, null));
60 : SampleUtils.printSessionState("created", session);
61 : PayloadContainer request = session.getMessageToSend();
62 : SampleUtils.printSessionState("got", session);
63 : SampleUtils.printContainerInformation(request);
64 : SampleUtils.printServiceContent(request.getServiceContent());
65 : new Validator(request.getServiceContent().getElement()).validate();
66 : exchanger.sendMessage(request);
67 : SampleUtils.printSessionState("sent", session);
68 :
69 : int time = 0;
70 : boolean loop = true;
71 : while (loop) {
72 : Thread.sleep(5000);
73 : exchanger.hit();
74 : SampleUtils.printSessionState("loop=" + time++, session);
75 :
76 : Container container = exchanger.receiveMessage();
77 : if (container != null) {
78 : SampleUtils.printContainerInformation(container);
79 : session.pushStatus((StatusContainer) container);
80 : SampleUtils.printSessionState("pushed", session);
81 : loop = false;
82 : }
83 : }
84 :
85 : } catch (Exception e) {
86 : e.printStackTrace();
87 : }
88 : }
89 :
90 : }
*MapRuleStoreの生成 [#g22848b2]
ServiceContentへの変換に使用されるXSLTファイルは、MapRuleStoreに登録されます。MapRuleStoreは、XSLTファイルの登録情報が記述されたXMLファイル(以下、MapRuleStoreファイル)を読み込んで生成します。
42 : MapRuleStore store = new MapRuleStore(System
43 : .getProperty("user.dir")
44 : + File.separator, "mapRule");
MapRuleStoreファイルの名前は任意ですが、MapRuleStoreコンストラクタの第2パラメータ(ここでは"mapRule")に「.xml」をつけたものとなります。
MapRuleStoreファイルの記述方法については、Hestiaユーザーズガイドを参照してください。1つのmapRule要素に、XSLTファイル名および適用PIP、バージョン等の情報が記述されます。
【MapRuleStoreファイルの記述例】
<mapRuleStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="mapRule.xsd">
<mapRule>
<id>export0C1_1XML_1Rec</id>
<name>export0C1_R01.02.00</name>
<pipid>0C1</pipid>
<pipVersion>R01.02.00</pipVersion>
<direction>RNBM2Table</direction>
</mapRule>
<mapRule>
・・・
</mapRule>
</mapRuleStore>
|関連クラス|機能|h
|com.ados.hestia.repository.MapRuleStore|XSLTファイルの登録情報の一覧。|
*Mappingの取得 [#aecf825e]
MappingをID値により参照します。Mappingは、1つのmapRule要素に相当します。
46 : Mapping mapping = store.getMapping(mapid);
なお、MapRuleStore#fillMappingBaseDirにより、Mappingに、XSLTファイルのデフォルトディレクトリを設定できます。XSLTファイルのデフォルトディレクトリは、MapRuleStoreファイル下のmappingフォルダです。
47 : store.fillMappingBaseDir(mapping);
|関連クラス|機能|h
|com.ados.hestia.repository.Mapping|XSLTファイルの登録情報。|
*XSLT変換の実行とServiceContentの生成 [#q15cc38e]
TranslatorをTranslatorFactoryから生成します。TranslatorにはMappingが設定されます。
49 : TranslatorFactory factory = new TranslatorFactory();
50 : Translator translator = factory.newTranslator(mapping);
変換されるXMLをInputStream形式で入力します。
51 : translator.setTargetDocument(new FileInputStream(xml));
XSLT変換を実行します。
52 : translator.translate();
変換結果のServiceContentを取得します。
53 : ServiceContent sc = translator.getResultAsServiceContent();
ServiceContentの生成後は、通常の処理によりHestiaへ送信します。ただし、ServiceContentの生成後に、 ServiceContent#setElementValue等のメソッドにより値を上書きすることもできます。
|関連クラス|機能|h
|com.ados.hestia.mapping.TranslatorFactory|Translatorを生成するクラス。|
|com.ados.hestia.mapping.Translator|XSLT変換を実行するクラス。|