gt:         StringBuffer sb = new StringBuffer("");
context: /*
   * WANDORA
   * Knowledge Extraction, Management, and Publishing Application
   * http://wandora.org
   *
   * Copyright (C) 2004-2016 Wandora Team
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   *
   * BigHugeThesaurusSelector.java
   *
   * Created on 26.12.2009, 12:45:42
   */
  
  package org.wandora.application.tools.extractors.bighugethesaurus;
  
  
  import org.wandora.application.contexts.*;
  import org.wandora.application.gui.simple.*;
  import org.wandora.application.gui.*;
  import org.wandora.application.*;
  import org.wandora.topicmap.*;
  
  import javax.swing.*;
  import java.awt.*;
  import java.util.*;
  import java.net.*;
  import java.text.*;
  
  
  /**
   *
   * @author akivela
   */
  public class BigHugeThesaurusSelector extends javax.swing.JDialog {
      public static String defaultLanguage = "en";
  
      public static String apiURL = "http://words.bighugelabs.com/api/2/__2__/__1__/xml";
  
  
      private Wandora wandora = null;
      private Context context = null;
      private boolean accepted = false;
  
  
  
      /** Creates new form BigHugeThesaurusSelector */
      public BigHugeThesaurusSelector(Wandora wandora) {
          super(wandora, true);
          initComponents();
  
          setTitle("Big Huge Thesaurus extractor");
          setSize(500,200);
          wandora.centerWindow(this);
          this.wandora = wandora;
          accepted = false;
      }
  
  
  
      public void setWandora(Wandora wandora) {
          this.wandora = wandora;
      }
  
      public void setContext(Context context) {
          this.context = context;
      }
  
  
  
      public boolean wasAccepted() {
          return accepted;
      }
      public void setAccepted(boolean b) {
          accepted = b;
      }
  
  
  
      public WandoraTool getWandoraTool(WandoraTool parentTool) {
          Component component = tabbedPane.getSelectedComponent();
          WandoraTool wt = null;
  
  
          // ***** WORDS *****
          if(wordPanel.equals(component)) {
              String wordsAll = wordTextField.getText();
              String[] words = urlEncode(commaSplitter(wordsAll));
              String apikey = solveAPIKey();
              if(apikey != null && apikey.length() > 0) {
                  apikey = apikey.trim();
                  String[] wordUrls = completeString(apiURL, words, apikey);
                  XMLBigHugeThesaurusExtractor ex = new XMLBigHugeThesaurusExtractor();
                  ex.setForceUrls( wordUrls );
                  wt = ex;
              }
              else {
                  parentTool.log("Invalid apikey given. Aborting...");
              }
          }
  
          return wt;
      }
  
  
  
  
      public String[] newlineSplitter(String str) {
          if(str.indexOf('\n') != -1) {
              String[] strs = str.split("\n");
              ArrayList<String> strList = new ArrayList<String>();
              String s = null;
              for(int i=0; i<strs.length; i++) {
                  s = strs[i];
                  s = s.trim();
                  if(s.length() > 0) {
                      strList.add(s);
                  }
              }
              return strList.toArray( new String[] {} );
          }
          else {
              return new String[] { str };
          }
      }
  
  
      public String[] commaSplitter(String str) {
          if(str.indexOf(',') != -1) {
              String[] strs = str.split(",");
              ArrayList<String> strList = new ArrayList<String>();
              String s = null;
              for(int i=0; i<strs.length; i++) {
                  s = strs[i];
                  s = s.trim();
                  if(s.length() > 0) {
                      strList.add(s);
                  }
              }
              return strList.toArray( new String[] {} );
          }
          else {
              return new String[] { str };
          }
  
      }
  
  
  
      public String[] completeString(String template, String[] strs) {
          if(strs == null || template == null) return null;
          String[] completed = new String[strs.length];
          for(int i=0; i<strs.length; i++) {
              completed[i] = template.replaceAll("__1__", strs[i]);
          }
          return completed;
      }
  
  
      public String[] completeString(String template, String[] strs1, String[] strs2) {
          if(strs1 == null || strs2 == null || template == null) return null;
          if(strs1.length != strs2.length) return null;
  
          String[] completed = new String[strs1.length];
          for(int i=0; i<strs1.length; i++) {
              completed[i] = template.replaceAll("__1__", strs1[i]);
              completed[i] = completed[i].replaceAll("__2__", strs2[i]);
          }
          return completed;
      }
  
  
      public String[] completeString(String template, String[] strs1, String strs2) {
          if(strs1 == null || strs2 == null || template == null) return null;
  
          String[] completed = new String[strs1.length];
          for(int i=0; i<strs1.length; i++) {
              completed[i] = template.replaceAll("__1__", strs1[i]);
              completed[i] = completed[i].replaceAll("__2__", strs2);
          }
          return completed;
      }
  
      public String[] completeString(String template, String[] strs1, String strs2, String strs3) {
          if(strs1 == null || strs2 == null || template == null) return null;
  
          String[] completed = new String[strs1.length];
          for(int i=0; i<strs1.length; i++) {
              completed[i] = template.replaceAll("__1__", strs1[i]);
              completed[i] = completed[i].replaceAll("__2__", strs2);
              completed[i] = completed[i].replaceAll("__3__", strs3);
          }
          return completed;
      }
  
      public String[] urlEncode(String[] urls) {
          if(urls == null) return null;
          String[] cleanUrls = new String[urls.length];
          for(int i=0; i<urls.length; i++) {
              cleanUrls[i] = urlEncode(urls[i]);
          }
          return cleanUrls;
      }
  
  
  
      public String urlEncode(String url) {
          try {
              return URLEncoder.encode(url, "UTF-8");
          }
          catch(Exception e) {
              return url;
          }
      }
  
  
  
  
      public String getContextAsString() {
          StringBuffer sb = new StringBuffer("");
          if(context != null) {
              try {
                  Iterator contextObjects = context.getContextObjects();
                  String str = null;
                  Object o = null;
                  while(contextObjects.hasNext()) {
                      str = null;
                      o = contextObjects.next();
                      if(o instanceof Topic) {
                          Topic t = (Topic) o;
                          //str = t.getBaseName();
                          str = t.getDisplayName("en");
                          if(str != null) {
                              str = str.trim();
                          }
                      }
  
                      if(str != null && str.length() > 0) {
                          sb.append(str);
                          if(contextObjects.hasNext()) {
                              sb.append(", ");
                          }
                      }
                  }
              }
              catch(Exception e) {
                  e.printStackTrace();
              }
          }
          return sb.toString();
      }
  
  
  
  
  
      public String getContextAsSIs(String delim) {
  