View Javadoc

1   package at.ac.tuwien.infosys.sm4all.copal.api.publisher.xml;
2   
3   import org.apache.log4j.Logger;
4   import org.w3c.dom.Document;
5   import org.w3c.dom.Element;
6   import at.ac.tuwien.infosys.sm4all.copal.api.processor.ContextProcessor;
7   import at.ac.tuwien.infosys.sm4all.copal.api.publisher.ContextPublisher;
8   import at.ac.tuwien.infosys.sm4all.copal.api.xml.Constants;
9   import at.ac.tuwien.infosys.sm4all.copal.api.xml.ElementUtil;
10  import at.ac.tuwien.infosys.sm4all.copal.api.xml.Marshaller;
11  import at.ac.tuwien.infosys.sm4all.copal.api.xml.MissingFieldException;
12  
13  /**
14   * Command that marshalls a {@link ContextPublisher} into an {@link Element}.
15   * 
16   * @author sanjin
17   */
18  public class XMLContextPublisher extends ElementUtil implements
19          Marshaller<ContextPublisher> {
20  
21      /**
22       * Local name of events child {@link Element}.
23       */
24      public static final String EVENTS_ELEMENT_NAME = "Events";
25  
26      private static final Logger LOGGER = Logger.getLogger(XMLContextPublisher.class);
27  
28      private final Element element;
29  
30      /**
31       * Creates instance of {@link ContextPublisher} marshaller which uses
32       * specified {@link Element} to marshal the {@link ContextPublisher}.
33       * 
34       * @param element the {@link Element} used for marshalling.
35       * @throws NullPointerException if specified {@link Element} is
36       *         <code>null</code>.
37       */
38      public XMLContextPublisher(final Element element) {
39          super(element);
40  
41          if (element == null)
42              throw new NullPointerException("XML DOM element cannot be null.");
43  
44          this.element = element;
45      }
46  
47      /**
48       * Marshalls specified {@link ContextPublisher} into specified
49       * {@link Element}.
50       * 
51       * @param publisher the {@link ContextPublisher}.
52       * @throws NullPointerException if specified {@link ContextProcessor} is
53       *         <code>null</code>.
54       */
55      @Override
56      public void marshal(final ContextPublisher publisher) {
57          if (publisher == null)
58              throw new NullPointerException("Publisher cannot be null.");
59  
60          new SourceID(this.element).marshal(publisher);
61  
62          Element events = null;
63          try {
64              events = getChildElement(Constants.COPAL_NAMESPACE_URI,
65                      EVENTS_ELEMENT_NAME);
66          } catch (final MissingFieldException ex) {
67              if (LOGGER.isDebugEnabled())
68                  LOGGER.debug(
69                          "Publisher events element is missing! Appending new one.",
70                          ex);
71              final Document document = this.element.getOwnerDocument();
72              events = document.createElementNS(Constants.COPAL_NAMESPACE_URI,
73                      Constants.COPAL_PREFIX + ":" + EVENTS_ELEMENT_NAME);
74              this.element.appendChild(events);
75          }
76          new Events(events).marshal(publisher.getPublishedTypes());
77      }
78  }