KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > world > WorldModelerXMLUtils


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on Jan 26, 2004
48  * Manta LTD
49  */

50 package org.mr.kernel.world;
51
52
53
54 import java.util.ArrayList JavaDoc;
55
56 import java.util.Iterator JavaDoc;
57 import java.util.LinkedHashMap JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Map JavaDoc;
60 import java.util.Set JavaDoc;
61
62
63 import org.w3c.dom.*;
64
65 import org.mr.MantaAgentConstants;
66 import org.mr.kernel.services.MantaService;
67 import org.mr.kernel.services.topics.VirtualTopicManager;
68 import org.mr.api.jms.MantaConnection;
69 import org.mr.core.net.TransportInfo;
70 import org.mr.core.net.TransportType;
71 /**
72  * WorldModelerXMLUtils - general XML util
73  * @since Jan 26, 2004
74  * @version 1.0
75  * @author Amir Shevat
76  *
77  *
78  */

79 public class WorldModelerXMLUtils {
80
81     /**
82      *
83      */

84     public WorldModelerXMLUtils() {
85         super();
86         
87     }
88     
89     
90     /**
91      * @param agentProp
92      * @param xml_rep_attrib_name2
93      * @return
94      */

95     public static String JavaDoc getAttribute(Node node, String JavaDoc attribName) {
96         String JavaDoc result = null;
97         if(node != null){
98             NamedNodeMap attributes = node.getAttributes();
99             Node agentNameAttribute= attributes.getNamedItem(attribName);
100             if(agentNameAttribute != null)
101                 result = agentNameAttribute.getNodeValue();
102         }
103         
104         return result;
105     }
106     
107     /**
108      * returns direct child tags of this node that have a given childTagName
109      */

110     public static List JavaDoc getChildTags(Node papaTag ,String JavaDoc childTagName ){
111         ArrayList JavaDoc list = new ArrayList JavaDoc();
112         NodeList tags = papaTag.getChildNodes();
113         int size = tags.getLength();
114         for (int i = 0; i < size; i++) {
115             Node tag = tags.item(i);
116             if(tag.getNodeType() != Node.TEXT_NODE){
117                 String JavaDoc tagName = tag.getNodeName();
118                 if(tagName.equalsIgnoreCase(childTagName)){
119                     list.add(tag);
120                 }// if
121
}// if
122
}//for
123

124         return list;
125     }
126     
127     
128     
129     public static String JavaDoc fromWorldMapToXML(WorldModeler src){
130         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
131         LinkedHashMap JavaDoc attrib = new LinkedHashMap JavaDoc();
132         //world
133
attrib.put(WorldModelerLoader.XML_ATTRIB_VER , new Long JavaDoc(src.getVersion()));
134         buff.append(createTag(WorldModelerLoader.XML_TAG_WORLD ,attrib , false ));
135         //domains
136
Iterator JavaDoc domains = src.getDomains().iterator();
137         while(domains.hasNext()){
138             attrib.clear();
139             MantaDomain dom = (MantaDomain)domains.next();
140             String JavaDoc domName = dom.getDomainName();
141             String JavaDoc domDesc = dom.getDesc();
142             attrib.put(WorldModelerLoader.XML_ATTRIB_NAME,domName );
143             attrib.put(WorldModelerLoader.XML_ATTRIB_DESC,domDesc );
144             buff.append(createTag(WorldModelerLoader.XML_TAG_DOMAIN , attrib, false));
145             // services
146
Iterator JavaDoc services = dom.getServices().iterator();
147             while(services.hasNext()){
148                 MantaService service = (MantaService)services.next();
149                 String JavaDoc serviceName = service.getServiceName();
150                 if(!VirtualTopicManager.isWildCardTopic(serviceName)
151                         && !serviceName.startsWith(MantaConnection.TMP_DESTINATION_PREFIX) ){
152                     String JavaDoc persis = String.valueOf(service.getPersistentMode() == MantaAgentConstants.PERSISTENT);
153                     attrib.clear();
154                     attrib.put(WorldModelerLoader.XML_ATTRIB_NAME ,serviceName );
155                     if(service.getServiceType() ==MantaService.SERVICE_TYPE_QUEUE ){
156                         attrib.put(WorldModelerLoader.XML_ATTRIB_SERVICE_TYPE ,WorldModelerLoader.XML_QUEUE_SERVICE_TYPE_ATTRIB_VALUE );
157                     }else{
158                         attrib.put(WorldModelerLoader.XML_ATTRIB_SERVICE_TYPE ,WorldModelerLoader.XML_TOPIC_SERVICE_TYPE_ATTRIB_VALUE );
159                     }
160                     attrib.put(WorldModelerLoader.XML_ATTRIB_PERSISTENT ,persis);
161                     buff.append(createTag(WorldModelerLoader.XML_TAG_SERVICE , attrib, true));
162                 }
163                     
164                 
165                 
166             }
167             // agents
168
Iterator JavaDoc agents = dom.getAgents().iterator();
169             while(agents.hasNext()){
170                 String JavaDoc agentName =(String JavaDoc)agents.next();
171                 attrib.clear();
172                 attrib.put(WorldModelerLoader.XML_ATTRIB_NAME ,agentName );
173                 buff.append(createTag(WorldModelerLoader.XML_TAG_PEER , attrib, false));
174                 // transport info
175
Set JavaDoc transports = dom.getAgentTransportInfoList(agentName);
176                 Iterator JavaDoc i = transports.iterator();
177                 while (i.hasNext()) {
178                     TransportInfo info =(TransportInfo) i.next();
179                     // MWB transports to other agents are transient
180
// and should not be saved.
181
if (info.getTransportInfoType() == TransportType.MWB &&
182                         !agentName.equals("mwb")) {
183                         continue;
184                     }
185                     String JavaDoc ip =info.getIp().getHostAddress();
186                     String JavaDoc port = String.valueOf( info.getPort());
187                     String JavaDoc type = info.getTransportInfoType().toString();
188                     attrib.clear();
189                     attrib.put(WorldModelerLoader.XML_ATTRIB_IP , ip);
190                     attrib.put(WorldModelerLoader.XML_ATTRIB_PORT , port);
191                     attrib.put(WorldModelerLoader.XML_ATTRIB_TRANSPORT_TYPE ,type );
192                     buff.append(createTag(WorldModelerLoader.XML_TAG_TRANSPORT , attrib, true));
193                 }
194                 
195                 // end agent
196
buff.append(getClosingTag(WorldModelerLoader.XML_TAG_PEER));
197             }
198             //end domain
199
buff.append(getClosingTag(WorldModelerLoader.XML_TAG_DOMAIN));
200         }
201         
202         // end world
203
buff.append(getClosingTag(WorldModelerLoader.XML_TAG_WORLD));
204         return buff.toString();
205     }
206     
207     private static String JavaDoc getClosingTag(String JavaDoc tagName){
208         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
209         buff.append("</");
210         buff.append(tagName);
211         buff.append(">");
212         //new line
213
buff.append("\r\n");
214         return buff.toString();
215     }
216     
217     private static String JavaDoc createTag(String JavaDoc tagName , Map JavaDoc attrib , boolean closedTag){
218         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
219         buff.append("<");
220         buff.append(tagName);
221         buff.append(" ");
222         if(attrib != null){
223             Iterator JavaDoc attribIter = attrib.keySet().iterator();
224             while(attribIter.hasNext()){
225                 String JavaDoc attName = (String JavaDoc)attribIter.next();
226                 Object JavaDoc attValue = (Object JavaDoc)attrib.get(attName);
227                 buff.append(attName);
228                 buff.append("=");
229                 buff.append("'");
230                 buff.append(attValue);
231                 buff.append("' ");
232             }
233         }
234         
235         if(closedTag)
236             buff.append("/");
237         buff.append(">");
238         //new line
239
buff.append("\r\n");
240         return buff.toString();
241     }//createTag
242

243 }
244
Popular Tags