FormNode.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub;

  18. import org.jivesoftware.smackx.xdata.Form;

  19. /**
  20.  * Generic packet extension which represents any pubsub form that is
  21.  * parsed from the incoming stream or being sent out to the server.
  22.  *
  23.  * Form types are defined in {@link FormNodeType}.
  24.  *
  25.  * @author Robin Collier
  26.  */
  27. public class FormNode extends NodeExtension
  28. {
  29.     private Form configForm;
  30.    
  31.     /**
  32.      * Create a {@link FormNode} which contains the specified form.
  33.      *
  34.      * @param formType The type of form being sent
  35.      * @param submitForm The form
  36.      */
  37.     public FormNode(FormNodeType formType, Form submitForm)
  38.     {
  39.         super(formType.getNodeElement());

  40.         if (submitForm == null)
  41.             throw new IllegalArgumentException("Submit form cannot be null");
  42.         configForm = submitForm;
  43.     }
  44.    
  45.     /**
  46.      * Create a {@link FormNode} which contains the specified form, which is
  47.      * associated with the specified node.
  48.      *
  49.      * @param formType The type of form being sent
  50.      * @param nodeId The node the form is associated with
  51.      * @param submitForm The form
  52.      */
  53.     public FormNode(FormNodeType formType, String nodeId, Form submitForm)
  54.     {
  55.         super(formType.getNodeElement(), nodeId);

  56.         if (submitForm == null)
  57.             throw new IllegalArgumentException("Submit form cannot be null");
  58.         configForm = submitForm;
  59.     }
  60.    
  61.     /**
  62.      * Get the Form that is to be sent, or was retrieved from the server.
  63.      *
  64.      * @return The form
  65.      */
  66.     public Form getForm()
  67.     {
  68.         return configForm;
  69.     }
  70.    
  71.     @Override
  72.     public CharSequence toXML()
  73.     {
  74.         if (configForm == null)
  75.         {
  76.             return super.toXML();
  77.         }
  78.         else
  79.         {
  80.             StringBuilder builder = new StringBuilder("<");
  81.             builder.append(getElementName());
  82.            
  83.             if (getNode() != null)
  84.             {
  85.                 builder.append(" node='");
  86.                 builder.append(getNode());
  87.                 builder.append("'>");
  88.             }
  89.             else
  90.                 builder.append('>');
  91.             builder.append(configForm.getDataFormToSend().toXML());
  92.             builder.append("</");
  93.             builder.append(getElementName() + '>');
  94.             return builder.toString();
  95.         }
  96.     }

  97. }