View Javadoc
1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   package org.miloss.fgsms.wsn;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   import javax.xml.parsers.DocumentBuilderFactory;
10  import javax.xml.transform.dom.DOMResult;
11  import javax.xml.ws.wsaddressing.W3CEndpointReference;
12  import org.apache.log4j.Level;
13  import org.apache.log4j.Logger;
14  import org.oasis_open.docs.wsn.b_2.TopicExpressionType;
15  import org.w3c.dom.Document;
16  import org.w3c.dom.Element;
17  import org.w3c.dom.NodeList;
18  
19  /**
20   *
21   */
22  public class WSNUtility {
23  
24      final static Logger log = Logger.getLogger("WS-NotificationBroker");
25  
26      public static List<String> topicExpressionToList(TopicExpressionType te) {
27          List<String> items = new ArrayList<String>();
28          if (te == null) {
29              return items;
30          }
31          if (te.getDialect().equalsIgnoreCase(WSNConstants.WST_TOPICEXPRESSION_SIMPLE)) {
32              for (int k = 0; k < te.getContent().size(); k++) {
33                  items.add(((String) te.getContent().get(k)).trim());
34              }
35          }
36          return items;
37      }
38  
39      public static String getWSAAddress(W3CEndpointReference ref) {
40          try {
41              Document xmlDocument = null;
42              xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
43              Element element = xmlDocument.createElement("elem");
44              ref.writeTo(new DOMResult(element));
45              NodeList nl = element.getElementsByTagNameNS("http://www.w3.org/2005/08/addressing", "Address");
46              if (nl != null && nl.getLength() > 0) {
47                  Element e = (Element) nl.item(0);
48                  log.log(Level.DEBUG, "return " + e.getTextContent().trim() + " for a callback address");
49                  return e.getTextContent().trim();
50              }
51          } catch (Exception ex) {
52              log.log(Level.WARN, "unable to obtain a WS-Addressing endpoint", ex);
53              ex.printStackTrace();
54          }
55          return null;
56      }
57  
58      /**
59       * converts a list of string to a single string whitespace, null and empty
60       * strings are ignored
61       */
62      public static String listStringtoString(List<String> data) {
63          if (data == null || data.isEmpty()) {
64              return "";
65          }
66          String s = "";
67          for (int i = 0; i < data.size(); i++) {
68              if (!stringIsNullOrEmpty(data.get(i))) {
69                  String t = data.get(i);
70                  t = t.trim();
71                  if (!stringIsNullOrEmpty(t)) {
72                      s += t + " ";
73                  }
74  
75              }
76          }
77          return s.substring(0, s.length() - 1);
78      }
79  
80      /**
81       * why in the world doesn't java have this?
82       *
83       * @param s
84       * @return
85       */
86      public static boolean stringIsNullOrEmpty(String s) {
87          if (s == null) {
88              return true;
89          }
90          if (s.length() == 0) {
91              return true;
92          }
93          return false;
94      }
95  }