View Javadoc

1   package at.ac.tuwien.infosys.sm4all.copal.api.event.xml.processor;
2   
3   import org.w3c.dom.Element;
4   import at.ac.tuwien.infosys.sm4all.copal.api.ContextProcessor;
5   import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Constants;
6   import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.Marshaller;
7   
8   /**
9    * Command that marshalls a {@link ContextProcessor} into an {@link Element}.
10   * 
11   * @author sanjin
12   */
13  public class XMLContextProcessor implements Marshaller<ContextProcessor> {
14  
15      private final Element element;
16  
17      /**
18       * Creates instance of {@link ContextProcessor} marshaller which uses
19       * specified {@link Element} to marshal the {@link ContextProcessor}.
20       * 
21       * @param element the {@link Element} used for marshalling.
22       * @throws NullPointerException if specified {@link Element} is
23       *         <code>null</code>.
24       */
25      public XMLContextProcessor(final Element element) {
26          super();
27  
28          if (element == null)
29              throw new NullPointerException("XML DOM element cannot be null.");
30  
31          this.element = element;
32      }
33  
34      /**
35       * Marshalls specified {@link ContextProcessor} into specified
36       * {@link Element}.
37       * 
38       * @param processor the {@link ContextProcessor}.
39       * @throws NullPointerException if specified {@link ContextProcessor} is
40       *         <code>null</code>.
41       */
42      @Override
43      public void marshal(final ContextProcessor processor) {
44          if (processor == null)
45              throw new NullPointerException("Processor cannot be null.");
46  
47          // remove all children
48          while (this.element.hasChildNodes())
49              this.element.removeChild(this.element.getFirstChild());
50  
51          new Name(this.element).marshal(processor);
52  
53          final Element action = this.element.getOwnerDocument().createElementNS(
54                  Constants.COPAL_NAMESPACE_URI,
55                  Constants.COPAL_PREFIX + ":Action");
56          new XMLProcessorAction(action).marshal(processor.getAction());
57          this.element.appendChild(action);
58      }
59  }