Package net.sf.jeters.components

Source Code of net.sf.jeters.components.RegExReplacer_User$NewlineSplitter

/*
* JETERS – Java Extensible Text Replacement System
* Copyright (C) 2006–2008  Tobias Knerr
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/

package net.sf.jeters.components;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import net.sf.jeters.componentInterface.UIComponent;
import net.sf.jeters.componentInterface.dataStructs.NamedDataNotAvailableException;
import net.sf.jeters.componentInterface.dataStructs.NamedDataSet;
import net.sf.jeters.componentInterface.dataStructs.UIRequest;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_Boolean;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_Output;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_String;
import net.sf.jeters.componentInterface.editables.EditableText;
import net.sf.jeters.componentInterface.editables.MediaWikiText;
import net.sf.jeters.configuration.Conf;
import net.sf.jeters.configuration.NotifiedConfigurable;

/**
* an replacer class for JETERS that applies a regex expression the user directly enters.
*
* @author Tobias Knerr
*/
public class RegExReplacer_User extends RegExReplacer implements NotifiedConfigurable {
 
  /**
   * the default summary for user-defined edits.
   */ 
  @Conf protected String defaultSummary = "";

  /**
   * whether change-confirmation-requests will be sent to the user interface by default.
   */     
  @Conf protected boolean confirmChangesByDefault = true;
 
  /**
   * whether the user will be asked again for a regex even if a replacement already exists.
   * If false, it won't be possible to change the regex without restarting JETERS.
   */     
  @Conf protected boolean askForRegexEveryTime = true;

  /**
   * whether the text will be split at newlines
   */     
  @Conf protected boolean splitToLines = true
 
 
  private String summary = ""
 
 
  public RegExReplacer_User(){
    super();
    super.minRatingDescription = 1;
    super.minTotalRatingDescription = 1
    super.splitter = new NewlineSplitter();
  }
   
 
  public MediaWikiText[] edit(EditableText text, UIComponent uiForRequests){
   
    //make sure a replacement exists
   
    if (askForRegexEveryTime
        || replacements == null
        || replacements.size() == 0 ) {
     
      //get input from the user
     
      UIRequest[] requestArray;
     
      if (replacements == null
          || replacements.size() == 0
          || !( replacements.get(0) instanceof RegExReplacement ) ) {
       
        requestArray = new UIRequest[] {
          new UIRequest_String("regex", str("searchFor"), str("helpSearchFor")),
          new UIRequest_String("rwith", str("replaceWith"), str("helpReplaceWith")),
          new UIRequest_String("summary", str("summary"), str("helpSummary"), defaultSummary),
          new UIRequest_Boolean("ask", str("ask"), str("helpAsk"), confirmChangesByDefault),
          new UIRequest_Output(str("help"))
        };
       
      } else {
       
        RegExReplacement rep = (RegExReplacement)replacements.get(0);
        requestArray = new UIRequest[] {
          new UIRequest_String("regex", str("searchFor"), str("helpSearchFor"), rep.regex),
          new UIRequest_String("rwith", str("replaceWith"), str("helpReplaceWith"), rep.rwith),
          new UIRequest_String("summary", str("summary"), str("helpSummary"), summary),
          new UIRequest_Boolean("ask", str("ask"), str("helpAsk"), rep.rating > 0),
          new UIRequest_Output(str("help"))
        };
       
      }     
     
      String regex = null;
      String rwith = null;
      Boolean ask = false;
     
      while (regex == null || rwith == null) {
     
        NamedDataSet reply = uiForRequests.request(requestArray);
               
        if (reply == null) { //user cancelled request
          return null; // #### !! return-point !! ####
        }
       
        try{       
         
          regex = reply.get("regex");
          rwith = reply.get("rwith");
          summary = reply.get("summary");
          ask = reply.get("ask");

          //test whether the regex is correct
          Pattern.compile(regex);
         
        } catch (NamedDataNotAvailableException e) {
          uiForRequests.request(
              new UIRequest_Output(str("errorUserInput")));
        } catch (PatternSyntaxException e) {
          uiForRequests.request(
              new UIRequest_Output(str("errorRegexSyntax")));
          regex = null;
        }       
         
      }
     
      replacements.add(new RegExReplacement(1,
                                    regex,
                                    rwith,
                                    str("descriptionForDiff"),
                                    ask?(1):(0) ) );
                 
   
   
    //use the edit method from RegExReplacer
   
    MediaWikiText[] result = super.edit(text, uiForRequests);
   
    if (result.length > 0) {
      result[0].setEditSummary(summary);
    }
   
    return result;
               
  }
 
 
  /**
   * class for splitting the text at newlines
   */
  private class NewlineSplitter extends Splitter {
   
    public String[] split(String s){ 
       
      Pattern p = Pattern.compile("\n");
       
      return p.split(s);   
       
    }
   
    public String unsplit(String[] sArray){
     
      if( sArray == null || sArray.length == 0 ){ return null; }
     
      else{
       
        String result = sArray[0];
       
        for( int i=1; i<sArray.length; ++i ){
          result += "\n" + sArray[i];
        }
       
        return result;
       
      }
      
    }   
           
  }
 
  @Override
  public void handleConfigurationUpdate() {         
    if(splitToLines) {
      super.splitter = new NewlineSplitter();
    } else {
      super.splitter = new Splitter();
    }
  }

}

TOP

Related Classes of net.sf.jeters.components.RegExReplacer_User$NewlineSplitter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.