View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api.xml;
19  
20  import java.util.LinkedList;
21  import java.util.List;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.Node;
24  import org.w3c.dom.NodeList;
25  import at.ac.tuwien.infosys.sm4all.copal.api.ContextException;
26  
27  /**
28   * Command that marshals and unmarshalls an array.
29   * 
30   * @param <T> the type of array elements.
31   * @author sanjin
32   */
33  public abstract class ArrayElement<T> extends ArrayMarshaller<T> implements
34          Unmarshaller<T[]> {
35  
36      private final Element parentElement;
37      private final String namespaceURI;
38      private final String localName;
39  
40      /**
41       * Creates an instance of array marshaller and unmarshaller which uses
42       * specified parent {@link Element} to marshal and/or unmarshal array
43       * elements.
44       * 
45       * @param parentElement the {@link Element} that holds all marshalled
46       *        {@link Element}s.
47       * @param namespaceURI the namespace URI of marshalled {@link Element}s.
48       * @param prefix the prefix of marshalled {@link Element}s.
49       * @param localName the local name of marshalled {@link Element}s.
50       * @throws NullPointerException if specified parent {@link Element} is
51       *         <code>null</code>.
52       */
53      public ArrayElement(final Element parentElement, final String namespaceURI,
54              final String prefix, final String localName) {
55          super(parentElement, namespaceURI, prefix, localName);
56  
57          this.parentElement = parentElement;
58          this.namespaceURI = namespaceURI;
59          this.localName = localName;
60      }
61  
62      /**
63       * Unmarshal singled value from specified {@link Element}.
64       * 
65       * @param element the {@link Element}.
66       * @return single value.
67       * @throws ContextException if unmarshalling fails.
68       */
69      protected abstract T unmarshal(Element element) throws ContextException;
70  
71      /**
72       * Returns the unmarshalled child {@link Element}s of specified parent
73       * {@link Element}.
74       * 
75       * @return the unmarshalled child {@link Element}s.
76       * @throws ContextException if unmarshalling of any child {@link Element}
77       *         fails.
78       */
79      public List<T> unmarshalList() throws ContextException {
80          final NodeList children = this.parentElement.getElementsByTagNameNS(
81                  this.namespaceURI, this.localName);
82          final List<T> result = new LinkedList<T>();
83  
84          for (int i = 0; i < children.getLength(); i++) {
85              final Node node = children.item(i);
86              if ((node.getParentNode() == this.parentElement)
87                      && (node instanceof Element))
88                  result.add(unmarshal((Element) node));
89          }
90  
91          return result;
92      }
93  }