View Javadoc

1   package at.ac.tuwien.infosys.sm4all.copal.api.processor.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.xml.Constants;
8   import at.ac.tuwien.infosys.sm4all.copal.api.xml.ElementUtil;
9   import at.ac.tuwien.infosys.sm4all.copal.api.xml.Marshaller;
10  import at.ac.tuwien.infosys.sm4all.copal.api.xml.MissingFieldException;
11  
12  /**
13   * Command that marshalls a {@link ContextProcessor} into an {@link Element}.
14   * 
15   * @author sanjin
16   */
17  public class XMLContextProcessor extends ElementUtil implements
18          Marshaller<ContextProcessor> {
19  
20      private static final String ACTIONS_ELEMENT_NAME = "Actions";
21  
22      private static final Logger LOGGER = Logger.getLogger(XMLContextProcessor.class);
23  
24      private final Element element;
25  
26      /**
27       * Creates instance of {@link ContextProcessor} marshaller which uses
28       * specified {@link Element} to marshal the {@link ContextProcessor}.
29       * 
30       * @param element the {@link Element} used for marshalling.
31       * @throws NullPointerException if specified {@link Element} is
32       *         <code>null</code>.
33       */
34      public XMLContextProcessor(final Element element) {
35          super(element);
36  
37          this.element = element;
38      }
39  
40      /**
41       * Marshalls specified {@link ContextProcessor} into specified
42       * {@link Element}.
43       * 
44       * @param processor the {@link ContextProcessor}.
45       * @throws NullPointerException if specified {@link ContextProcessor} is
46       *         <code>null</code>.
47       */
48      @Override
49      public void marshal(final ContextProcessor processor) {
50          if (processor == null)
51              throw new NullPointerException("Processor cannot be null.");
52  
53          new Name(this.element).marshal(processor);
54  
55          Element events = null;
56          try {
57              events = getChildElement(Constants.COPAL_NAMESPACE_URI,
58                      ACTIONS_ELEMENT_NAME);
59          } catch (final MissingFieldException ex) {
60              if (LOGGER.isDebugEnabled())
61                  LOGGER.debug(
62                          "Processor actions element is missing! Appending new one.",
63                          ex);
64              final Document document = this.element.getOwnerDocument();
65              events = document.createElementNS(Constants.COPAL_NAMESPACE_URI,
66                      Constants.COPAL_PREFIX + ":" + ACTIONS_ELEMENT_NAME);
67              this.element.appendChild(events);
68          }
69          new XMLProcessorActions(events).marshal(processor.getActions());
70      }
71  }