- 追加された行はこの色です。
- 削除された行はこの色です。
RIGHT:[[目次>Hestia Hacks]]|[[前>RJOによるServiceContentの作成]]|[[次>RJOによる添付ファイルの送信]]
目次
#contents
*概要 [#ca4cba3c]
ServiceContentのノードツリーの参照方法を解説します。XMLの内容を自動的にDBへ出力する場合などに有効です。
サンプルプログラム全体を以下に示します。
サンプルプログラムを以下に示します。
【PIPReceiverRJO.java】
1 : import java.io.FileInputStream;
2 : import java.util.Properties;
3 :
4 : import com.ados.hestia.parser.rnbm.exchange.Container;
5 : import com.ados.hestia.parser.rnbm.exchange.PayloadContainer;
6 : import com.ados.hestia.parser.rnbm.exchange.StatusContainer;
7 : import com.ados.hestia.sdk.Configuration;
8 : import com.ados.hestia.sdk.Connection;
9 : import com.ados.hestia.sdk.message.DirectMessageExchanger;
10 : import com.ados.hestia.sdk.session.RespondSession;
11 : import com.ados.hestia.sdk.session.Session;
12 :
13 : public class PIPReceiverRJO {
14 :
15 : public static void main(String[] args) {
16 : try {
17 : Properties prop = new Properties();
18 : prop.load(new FileInputStream("sample.properties"));
19 : String HOST = prop.getProperty("HOST");
20 : String USER = prop.getProperty("USER");
21 : String PASS = prop.getProperty("PASS");
22 : String HANDLER_ID = prop.getProperty("HANDLER_ID");
23 :
24 : Connection connection = new Connection(HOST, USER, PASS,
25 : Connection.EXCHANGE_MODE_DIRECT, null, null, null,
26 : HANDLER_ID);
27 : DirectMessageExchanger exchanger = new DirectMessageExchanger(
28 : connection, Configuration.getInstance());
29 :
30 : int time = 0;
31 : boolean loop = true;
32 : while (loop) {
33 : Thread.sleep(5000);
34 : SampleUtils.println("loop : " + time++);
35 : exchanger.hit();
36 :
37 : Container container = exchanger.receiveMessage();
38 : if (container != null) {
39 : SampleUtils.printContainerInformation(container);
40 : PayloadContainer request = (PayloadContainer) container;
41 : SampleUtils.printServiceContent(request.getServiceContent());
42 : Session session = new RespondSession(request
43 : .getRemotePartner(), request.getPIPID());
44 : SampleUtils.printSessionState("created", session);
45 : session.pushMessage(request);
46 : SampleUtils.printSessionState("pushed", session);
47 : session.acceptPartnerMessage();
48 : SampleUtils.printSessionState("accepted", session);
49 : StatusContainer status = session.getStatusToSend();
50 : SampleUtils.printSessionState("sent status", session);
51 : SampleUtils.printContainerInformation(status);
52 : exchanger.sendMessage(status);
53 : exchanger.hit();
54 : SampleUtils.printServiceContentInformation(request
55 : .getServiceContent());
56 : loop = false;
57 : }
58 : }
59 :
60 : } catch (Exception e) {
61 : e.printStackTrace();
62 : }
63 : }
64 :
65 : }
【SampleUtils.java】
32 : public static void printServiceContentInformation(ServiceContent sc) {
33 : System.out.println("PIP :" + sc.getPIPID());
34 : System.out.println("Ver :" + sc.getFullVersion());
35 : System.out.println("Root :" + sc.getName());
36 : System.out.print("Production :");
37 : if (sc.isProduction())
38 : System.out.println("Production");
39 : else
40 : System.out.println("Test");
41 : System.out.println("DOC ID :" + sc.getDocumentIdentifier());
42 : System.out.println("Date :"
43 : + sc.getDocumentGenerationDateTime().getRNDateTime());
44 : System.out.println("Sender DUNS :"
45 : + sc.getFromRole().getGlobalBusinessIdentifierNorm());
46 : System.out.println("Receiver DUNS:"
47 : + sc.getToRole().getGlobalBusinessIdentifierNorm());
48 :
49 : HashMap map = sc.getElement().getPrefixMap();
50 : Iterator it = map.keySet().iterator();
51 : while (it.hasNext()) {
52 : String uri = (String) it.next();
53 : System.out.println(map.get(uri) + " : " + uri);
54 : }
55 :
56 : printComplexElementInformation((ComplexElement) sc.getElement(), "");
57 : }
58 :
59 : public static void printComplexElementInformation(ComplexElement el,
60 : String indent) {
61 : System.out.println(indent + "C:" + el.getName() + ":"
62 : + el.getNamespace() + ":" + el.getClass().getCanonicalName());
63 :
64 : ElementContentModel model = el.getElementContentModel();
65 :
66 : printGroupDeclarationInformation(model.getRoot(), indent + INDENT);
67 :
68 : int att_size = el.getAttributeCount();
69 : for (int i = 0; i < att_size; i++) {
70 : Attribute att = el.getAttribute(i);
71 : System.out.println(indent + INDENT + "A:" + att.getName() + ":"
72 : + att.getValue() + ":" + att.getClass().getCanonicalName());
73 : }
74 : Element[] els = el.getElements();
75 : for (int i = 0; i < els.length; i++) {
76 : if (els[i] instanceof ComplexElement) {
77 : printComplexElementInformation((ComplexElement) els[i], indent
78 : + INDENT);
79 : } else if (els[i] instanceof SimpleElement) {
80 : SimpleElement sel = (SimpleElement) els[i];
81 : System.out.println(indent + INDENT + "S:" + sel.getName() + ":"
82 : + sel.getText() + ":" + sel.getNamespace() + ":"
83 : + sel.getClass().getCanonicalName());
84 : System.out.println(indent + INDENT + INDENT + "D:"
85 : + sel.getTextModel().toString());
86 : } else if (els[i] instanceof TextElement) {
87 : TextElement tel = (TextElement) els[i];
88 : System.out.println(indent + INDENT + "T:" + tel.getName() + ":"
89 : + tel.getText() + ":" + tel.getNamespace() + ":"
90 : + tel.getClass().getCanonicalName());
91 : }
92 : }
93 : }
94 :
95 : public static void printGroupDeclarationInformation(GroupDeclaration group,
96 : String indent) {
97 : switch (group.getType()) {
98 : case GroupDeclaration.GROUP_TYPE_SEQUENCE:
99 : System.out.println(indent + "D:SEQUENCE:" + group.getMinOccur()
100 : + ".." + group.getMaxOccur());
101 : break;
102 : case GroupDeclaration.GROUP_TYPE_CHOICE:
103 : System.out.println(indent + "D:CHOICE:" + group.getMinOccur()
104 : + ".." + group.getMaxOccur());
105 : break;
106 : }
107 : int count = group.getChildCnt();
108 : for (int i = 0; i < count; i++) {
109 : Object o = group.getChild(i);
110 : if (o instanceof ElementDeclaration) {
111 : ElementDeclaration decl = (ElementDeclaration) o;
112 : System.out.println(indent + INDENT + "D:" + decl.getName()
113 : + " : " + decl.getNamespace() + " : "
114 : + decl.getMinOccur() + ".." + decl.getMaxOccur());
115 : } else if (o instanceof GroupDeclaration) {
116 : printGroupDeclarationInformation((GroupDeclaration) o, indent
117 : + INDENT);
118 : }
119 : }
120 : }
なお、サンプルのPIPReceiverRJO.javaにおいて、これらのメソッドを利用しています。
*ServiceContentの取得 [#bdd770c0]
受信したServiceContentを取得します。
【PIPReceiverRJO.java】
54 : SampleUtils.printServiceContentInformation(request
55 : .getServiceContent());
以降、SampleUtils#printServiceContentInformationの処理について解説します。
*ServiceContentの参照 [#ae958192]
*ServiceContentの基本情報の参照 [#ae958192]
ServiceContentからPIP名等の基本情報を参照できます。
33 : System.out.println("PIP :" + sc.getPIPID());
34 : System.out.println("Ver :" + sc.getFullVersion());
35 : System.out.println("Root :" + sc.getName());
36 : System.out.print("Production :");
37 : if (sc.isProduction())
38 : System.out.println("Production");
39 : else
40 : System.out.println("Test");
41 : System.out.println("DOC ID :" + sc.getDocumentIdentifier());
42 : System.out.println("Date :"
43 : + sc.getDocumentGenerationDateTime().getRNDateTime());
44 : System.out.println("Sender DUNS :"
45 : + sc.getFromRole().getGlobalBusinessIdentifierNorm());
46 : System.out.println("Receiver DUNS:"
47 : + sc.getToRole().getGlobalBusinessIdentifierNorm());
*名前空間URI、接頭辞の参照 [#j37e5db7]
|関連クラス|機能|h
|com.ados.hestia.parser.rnbm.particle.ServiceContent|ServiceContentを表すクラス。|
|com.ados.hestia.parser.rnbm.particle.DateTimeStamp|ServiceContent#getDocumentGenerationDateTimeで取得される、ドキュメント生成時間を表すクラス。|
|com.ados.hestia.parser.rnbm.particle.PartnerRoleIdentification|ServiceContent#getFromRole/getToRoleで取得される、パートナー情報を表すクラス。|
|com.ados.hestia.parser.rnbm.particle.PartnerIdentification|PartnerRoleIdentificationの上位クラス。|
ServiceContentに宣言されている名前空間URIおよび接頭辞を参照できます。
49 : HashMap map = sc.getElement().getPrefixMap();
50 : Iterator it = map.keySet().iterator();
51 : while (it.hasNext()) {
52 : String uri = (String) it.next();
53 : System.out.println(map.get(uri) + " : " + uri);
54 : }
*ServiceContentのルート要素の参照 [#i19060c3]
ServiceContentのルート要素を参照できます。
ServiceContent#getElementによりルート要素を参照します。
56 : printComplexElementInformation((ComplexElement) sc.getElement(), "");
以降、SampleUtils#printComplexElementInformationの処理について解説します。
*XMLノードクラスの継承階層 [#ha645875]
RJOでは、具体的なPIPの各要素や属性ごとにクラスが定義されます。たとえば、PIP 3B2に対してはPip3B2AdvanceShipmentNotificationというルート要素のクラスが定義され、BusinessDescription要素に対してはBusinessDescriptionクラスが定義されます。これらのクラスは以下のような継承階層となります。
+ Abstract(抽象クラス)
+ Element(抽象クラス)
+ ComplexElement(抽象クラス)
+ BusinessDescription
+ ContactInformation
+ ・・・
+ RootElement(抽象クラス)
+ Pip3B2AdvanceShipmentNotification
+ Pip4A1StrategicForecastNotification
+ ・・・
+ SimpleElement(抽象クラス)
+ GlobalBusinessIdentifier
+ FreeFormText
+ ・・・
+ TextElement
+ Attribute(抽象クラス)
|関連クラス|機能|h
|com.ados.hestia.parser.rnbm.model.RootElement|ServiceContent#getElementで取得される、ルート要素を表す抽象クラス。|
|com.ados.hestia.parser.rnbm.model.ComplexElement|枝の要素を表す抽象クラス。|
|com.ados.hestia.parser.rnbm.model.SimpleElement|葉(末端)の要素を表す抽象クラス。|
|com.ados.hestia.parser.rnbm.model.Attribute|属性を表す抽象クラス。|
*ComplexElementの参照 [#v1d48357]
ComplexElementは枝の要素を表します。
61 : System.out.println(indent + "C:" + el.getName() + ":"
62 : + el.getNamespace() + ":" + el.getClass().getCanonicalName());
属性を取得することができます。なお、これはComplexElementの上位クラスであるElementから継承された機能です。
70 : Attribute att = el.getAttribute(i);
子要素を取得することができます。なお、これはComplexElementに定義された機能です。
74 : Element[] els = el.getElements();
*Attributeの参照 [#nbbc07d8]
Attributeは属性を表します。
70 : Attribute att = el.getAttribute(i);
71 : System.out.println(indent + INDENT + "A:" + att.getName() + ":"
72 : + att.getValue() + ":" + att.getClass().getCanonicalName());
*子要素の参照 [#o23f1ff9]
子要素はComplexElement(枝の要素)、SimpleElement(葉の要素)、TextElementに分類されます。
74 : Element[] els = el.getElements();
*SimpleElementの参照 [#ac2f8fd8]
80 : SimpleElement sel = (SimpleElement) els[i];
81 : System.out.println(indent + INDENT + "S:" + sel.getName() + ":"
82 : + sel.getText() + ":" + sel.getNamespace() + ":"
83 : + sel.getClass().getCanonicalName());
84 : System.out.println(indent + INDENT + INDENT + "D:"
85 : + sel.getTextModel().toString());
*TextElementの参照 [#h31ed7d6]
87 : TextElement tel = (TextElement) els[i];
88 : System.out.println(indent + INDENT + "T:" + tel.getName() + ":"
89 : + tel.getText() + ":" + tel.getNamespace() + ":"
90 : + tel.getClass().getCanonicalName());
*ElementContentModelの参照 [#m62c165d]
ElementContentModelは要素の型定義を表します。
64 : ElementContentModel model = el.getElementContentModel();
66 : printGroupDeclarationInformation(model.getRoot(), indent + INDENT);