1 /*
   2  * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.jemmy.input;
  24 
  25 import java.util.regex.Matcher;
  26 import java.util.regex.Pattern;
  27 import org.jemmy.JemmyException;
  28 import org.jemmy.interfaces.CaretOwner;
  29 import org.jemmy.interfaces.Text;
  30 
  31 /**
  32  *
  33  * @author shura
  34  */
  35 public class RegexCaretDirection extends CaretImpl.DirectionToPosition {
  36 
  37     /**
  38      *
  39      */
  40     public static final String REGEX_FLAGS = "";
  41 
  42     String regex;
  43     boolean front;
  44     int index;
  45     Text text;
  46     int flags;
  47 
  48     /**
  49      *
  50      * @param text
  51      * @param caretOwner
  52      * @param regex
  53      * @param flags
  54      * @param front
  55      * @param index
  56      */
  57     public RegexCaretDirection(Text text, CaretOwner caretOwner, String regex, int flags, boolean front, int index) {
  58         super(caretOwner, 0);
  59         this.text = text;
  60         this.regex = regex;
  61         this.flags = flags;
  62         this.front = front;
  63         this.index = index;
  64         if (index < 0) {
  65             throw new JemmyException("Index must be >=0 but is " + index, regex);
  66         }
  67     }
  68 
  69     /**
  70      *
  71      * @return
  72      */
  73     @Override
  74     protected double position() {
  75         return position(text, regex, flags, front, index);
  76     }
  77 
  78     /**
  79      *
  80      * @param text
  81      * @param regex
  82      * @param flags
  83      * @param front
  84      * @param index
  85      * @return
  86      */
  87     public static int position(Text text, String regex, int flags, boolean front, int index) {
  88         Matcher matcher = Pattern.compile(regex, flags).matcher(text.text());
  89         for (int i = 0; i <= index; i++) {
  90             if (!matcher.find()) {
  91                 throw new JemmyException(index + "'s occurance of \"" + regex + "\" not found.", text);
  92             }
  93         }
  94         return front ? matcher.start() : matcher.end();
  95     }
  96 }