SearchSettings.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  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.workgroup.settings;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.IqData;
  20. import org.jivesoftware.smack.packet.SimpleIQ;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.provider.IqProvider;
  23. import org.jivesoftware.smack.util.StringUtils;
  24. import org.jivesoftware.smack.xml.XmlPullParser;
  25. import org.jivesoftware.smack.xml.XmlPullParserException;

  26. public class SearchSettings extends SimpleIQ {
  27.     private String forumsLocation;
  28.     private String kbLocation;

  29.     public boolean isSearchEnabled() {
  30.         return StringUtils.isNotEmpty(getForumsLocation()) && StringUtils.isNotEmpty(getKbLocation());
  31.     }

  32.     public String getForumsLocation() {
  33.         return forumsLocation;
  34.     }

  35.     public void setForumsLocation(String forumsLocation) {
  36.         this.forumsLocation = forumsLocation;
  37.     }

  38.     public String getKbLocation() {
  39.         return kbLocation;
  40.     }

  41.     public void setKbLocation(String kbLocation) {
  42.         this.kbLocation = kbLocation;
  43.     }

  44.     public boolean hasKB() {
  45.         return StringUtils.isNotEmpty(getKbLocation());
  46.     }

  47.     public boolean hasForums() {
  48.         return StringUtils.isNotEmpty(getForumsLocation());
  49.     }


  50.     /**
  51.      * Element name of the stanza extension.
  52.      */
  53.     public static final String ELEMENT_NAME = "search-settings";

  54.     /**
  55.      * Namespace of the stanza extension.
  56.      */
  57.     public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";

  58.     public SearchSettings() {
  59.         super(ELEMENT_NAME, NAMESPACE);
  60.     }

  61.     /**
  62.      * Stanza extension provider for AgentStatusRequest packets.
  63.      */
  64.     public static class InternalProvider extends IqProvider<SearchSettings> {

  65.         @Override
  66.         public SearchSettings parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  67.             SearchSettings settings = new SearchSettings();

  68.             boolean done = false;
  69.             String kb = null;
  70.             String forums = null;

  71.             while (!done) {
  72.                 XmlPullParser.Event eventType = parser.next();
  73.                 if (eventType == XmlPullParser.Event.START_ELEMENT && "forums".equals(parser.getName())) {
  74.                     forums = parser.nextText();
  75.                 }
  76.                 else if ((eventType == XmlPullParser.Event.START_ELEMENT) && "kb".equals(parser.getName())) {
  77.                     kb = parser.nextText();
  78.                 }
  79.                 else if (eventType == XmlPullParser.Event.END_ELEMENT && "search-settings".equals(parser.getName())) {
  80.                     done = true;
  81.                 }
  82.             }

  83.             settings.setForumsLocation(forums);
  84.             settings.setKbLocation(kb);
  85.             return settings;
  86.         }
  87.     }
  88. }