- 追加された行はこの色です。
- 削除された行はこの色です。
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 : }
*ServiceContentの取得 [#bdd770c0]
*基本情報の参照 [#ae958192]
受信したServiceContentを取得します。
System.out.println("PIP :"+sc.getPIPID());
System.out.println("Ver :"+sc.getFullVersion());
System.out.println("Root :"+sc.getName());
System.out.print("Production :"); if(sc.isProduction())System.out.println("Production");else System.out.println("Test");
System.out.println("DOC ID :"+sc.getDocumentIdentifier());
System.out.println("Date :"+sc.getDocumentGenerationDateTime().getRNDateTime());
System.out.println("Sender DUNS :"+sc.getFromRole().getGlobalBusinessIdentifierNorm());
System.out.println("Receiver DUNS:"+sc.getToRole().getGlobalBusinessIdentifierNorm());
【PIPReceiverRJO.java】
54 : SampleUtils.printServiceContentInformation(request
55 : .getServiceContent());
以降、SampleUtils#printServiceContentInformationの処理について解説します。
*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]
HashMap map = sc.getElement().getPrefixMap();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String uri = (String)it.next();
System.out.println(map.get(uri)+" : "+uri);
}
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#getElementによりルート要素を参照します。
56 : printComplexElementInformation((ComplexElement) sc.getElement(), "");
以降、SampleUtils#printComplexElementInformationの処理について解説します。
*ComplexElementの参照 [#v1d48357]
ComplexElementは枝の要素を表します。
61 : System.out.println(indent + "C:" + el.getName() + ":"
62 : + el.getNamespace() + ":" + el.getClass().getCanonicalName());
*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);