Ejemplo n.º 1
1
  public String getTextForGesture(long parId, Point topLeft, Point bottomRight) {

    try {
      Paragraph p = lockManager.getParFromId(parId);

      int parY = documentPanel.textPane.modelToView(p.getOffset()).y;

      topLeft.y = topLeft.y + parY;
      bottomRight.y = bottomRight.y + parY;

      int startOffset = documentPanel.textPane.viewToModel(topLeft);
      int endOffset = documentPanel.textPane.viewToModel(bottomRight);

      while (startOffset > 0
          && Character.isLetterOrDigit((document.getText(startOffset - 1, 1).charAt(0))))
        startOffset--;

      while (endOffset < document.getLength()
          && Character.isLetterOrDigit((document.getText(endOffset, 1).charAt(0)))) endOffset++;

      String text = document.getText(startOffset, endOffset - startOffset);
      return text;
    } catch (Exception e) {
      System.out.println("EditorClient: addGestureAction. error identifying text");
      e.printStackTrace();
      return "";
    }

    // return "PLACEBO";
  }
Ejemplo n.º 2
0
  /**
   * ******************************************************************** Method: compareTwoNames
   * Description: Prompts user for two names and the decade to compare Input: array of Names, user
   * entered values for requestName, searchName, getDecade method calls Output: Displays histogram
   * line comparison between two names user selects
   * -------------------------------------------------------------- Pseudocode for this solution:
   * Begin Set name1 equal to value returned from requestName Set name1Idx equal to value returned
   * from searchName Reset name1 equal to self with first letter capitalized If name1Idx is equal to
   * -1 Begin Display "The name " + name1 + ", was not found!" End Else Begin Set name2 equal to
   * value returned from requestName Set namd2Idx equal to value returned from searchName Reset
   * name2 equal to self with first letter capitalized If name2Idx is equal to -1 Begin Display "The
   * second name, " + name2 + ", was not found!" End Else Begin Set decade equal to value returned
   * from getDecade Display comparison of name1 and name2 histogram lines for the selected decade
   * End if condition End if condition End
   * ********************************************************************
   */
  public static void compareTwoNames(Name[] list) {
    int name1Idx;
    int name2Idx;
    int decade;
    String name1;
    String name2;

    name1 = requestName();
    name1Idx = searchName(name1, list);
    name1 = Character.toUpperCase(name1.charAt(0)) + name1.substring(1);
    if (name1Idx == -1) {
      System.out.println("\nThe name, " + name1 + ", was not found!");
    } else {
      name2 = requestName();
      name2Idx = searchName(name2, list);
      name2 = Character.toUpperCase(name2.charAt(0)) + name2.substring(1);
      if (name2Idx == -1) {
        System.out.println("\nThe second name, " + name2 + ", was not found!");
      } else {
        decade = getDecade(list);
        System.out.println(
            "\nData for "
                + name1
                + "\n"
                + list[name1Idx].getHistoLine(decade)
                + "\nData for "
                + name2
                + "\n"
                + list[name2Idx].getHistoLine(decade));
      }
    }
  }
Ejemplo n.º 3
0
  /**
   * Logically casts input to UTF32 ints then looks up the output or null if the input is not
   * accepted. FST must be INPUT_TYPE.BYTE4.
   */
  public static <T> T get(FST<T> fst, CharSequence input) throws IOException {
    assert fst.inputType == FST.INPUT_TYPE.BYTE4;

    // TODO: would be nice not to alloc this on every lookup
    final FST.Arc<T> arc = fst.getFirstArc(new FST.Arc<T>());

    int charIdx = 0;
    final int charLimit = input.length();

    // Accumulate output as we go
    final T NO_OUTPUT = fst.outputs.getNoOutput();
    T output = NO_OUTPUT;

    while (charIdx < charLimit) {
      final int utf32 = Character.codePointAt(input, charIdx);
      charIdx += Character.charCount(utf32);

      if (fst.findTargetArc(utf32, arc, arc) == null) {
        return null;
      } else if (arc.output != NO_OUTPUT) {
        output = fst.outputs.add(output, arc.output);
      }
    }

    if (fst.findTargetArc(FST.END_LABEL, arc, arc) == null) {
      return null;
    } else if (arc.output != NO_OUTPUT) {
      return fst.outputs.add(output, arc.output);
    } else {
      return output;
    }
  }
Ejemplo n.º 4
0
 /**
  * This method ensures that the output String has only valid XML unicode characters as specified
  * by the XML 1.0 standard. For reference, please see the standard. This method will return an
  * empty String if the input is null or empty.
  *
  * @author Donoiu Cristian, GPL The String whose non-valid characters we want to remove. in
  *     String, stripped of non-valid characters.
  */
 public static String removeInvalidXMLCharacters(String s) {
   StringBuilder out = new StringBuilder(); // Used to hold the output.
   int codePoint; // Used to reference the current character.
   // String ss = "\ud801\udc00";                           // This is actualy one unicode
   // character, represented by two code units!!!.
   // System.out.println(ss.codePointCount(0, ss.length()));// See: 1
   int i = 0;
   while (i < s.length()) {
     // System.out.println("i=" + i);
     codePoint = s.codePointAt(i); // This is the unicode code of the character.
     if ((codePoint == 0x9)
         || // Consider testing larger ranges first to improve speed.
         (codePoint == 0xA)
         || (codePoint == 0xD)
         || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
         || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
         || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {
       out.append(Character.toChars(codePoint));
     }
     i +=
         Character.charCount(
             codePoint); // Increment with the number of code units(java chars) needed to represent
                         // a Unicode char.
   }
   return out.toString();
 }
Ejemplo n.º 5
0
 public static boolean isFormat(String str, String... format) {
   if (str.length() == 0) return format.length == 1 && format[0].equals("");
   char[] charset = str.toCharArray();
   int c = 0;
   for (int i = 0; i < format.length; i++) {
     String form = format[i];
     if (form.equals(NUMBERS)) {
       if (c >= charset.length) return false;
       if (!Character.isDigit(charset[c])) {
         if (charset[c] == '.') {
           if (c + 1 >= charset.length || !Character.isDigit(charset[c + 1])) return false;
         } else return false;
       }
       while (c < charset.length - 1
           && (Character.isDigit(charset[c + 1]) || charset[c + 1] == '.')) c++;
       c++;
     } else if (form.equals(LETTERS)) {
       if (c >= charset.length || !Character.isLetter(charset[c])) return false;
       while (c < charset.length - 1 && Character.isLetter(charset[c + 1])) c++;
       c++;
     } else {
       char[] formchars = form.toCharArray();
       if (formchars.length > charset.length - c) return false;
       for (int ch = 0; ch < formchars.length; ch++) {
         if (formchars[ch] != charset[c]) return false;
         c++;
       }
     }
   }
   return c == charset.length;
 }
Ejemplo n.º 6
0
 public ParseMessage(String msg) {
   FMValue = msg.substring(3, 5); // first one is starting index,last one is end+1
   ToValue = msg.substring(9, 11); //
   NPVal = Character.getNumericValue(msg.charAt(15));
   HCVal = Character.getNumericValue(msg.charAt(20));
   msgNumber = Integer.parseInt(msg.substring(25, 28));
   int i = msg.indexOf("DATA");
   int j = msg.indexOf("VL");
   if (i != (j + 4)) {
     VLNode = msg.substring(j + 3, i);
   } else VLNode = "";
   DATA_Val = msg.substring(i + 5);
   if (NPVal == 2) {
     int k = msg.indexOf("registered as");
     RegisteredName = "" + msg.charAt(k + 14) + msg.charAt(k + 15);
   }
   if (NPVal == 6) {
     String str = new String(DATA_Val);
     arr = new CopyOnWriteArrayList<>();
     int m = 0;
     do {
       int k = str.indexOf(",");
       arr.add(new Node(str.substring(m, k)));
       str = str.substring(k + 1);
     } while (str.indexOf(",") != -1);
     arr.add(new Node(str));
   }
 }
Ejemplo n.º 7
0
  private boolean checkHandlerWorkaround(
      String elementName, String handlerName, boolean writeHandler)
      throws ClickException, IOException {
    // If talking to an old ControlSocket, try the "handlers" handler
    // instead.
    String s = readString(elementName, "handlers");
    int pos = 0;

    // Find handler with same name.
    while (true) {
      pos = s.indexOf(handlerName, pos);
      if (pos < 0) // no such handler
      return false;
      if ((pos == 0 || s.charAt(pos - 1) == '\n')
          && Character.isWhitespace(s.charAt(pos + handlerName.length()))) break;
      pos++;
    }

    // we have a matching handler: will it be read/write suitable?
    char wantType = (writeHandler ? 'w' : 'r');
    for (pos += handlerName.length();
        pos < s.length() && Character.isWhitespace(s.charAt(pos));
        pos++) /* nada */ ;
    for (; pos < s.length(); pos++) {
      char c = s.charAt(pos);
      if (Character.toLowerCase(c) == wantType) return true;
      else if (Character.isWhitespace(c)) break;
    }
    return false;
  }
Ejemplo n.º 8
0
  public int encounter(Character other) {
    say("You have encountered " + other);
    delay(2000);
    say("His status is:\n" + other.getStatus2());
    delay(2000);

    while (this.health > 0 && other.health > 0) {
      say("Press 1 if you wish to talk.");
      say("Press 2 if you wish to attempt to flee.");
      say("Press 3 if you wish to attack.");

      Scanner sc = new Scanner(System.in);
      int answer = sc.nextInt();

      if (answer == 1) this.talk(other);
      else if (answer == 2) {
        if (this.flee(other)) return 1;
        else return 3;
      } else if (answer == 3) {
        this.attack(other);
        delay(3000);
        other.attack(this);
        delay(3000);
      }
    }
    return 5;
  }
Ejemplo n.º 9
0
  public StringBuffer translate() {
    for (index = 0; index < line.length(); index++) {
      char c = line.charAt(index);

      if (Character.isDigit(c)) {
        dealWithOperand();
      } else if (isOperator(c)) {
        dealWithOperator(c);
      } else if (c == '(') {
        stack.push(new Character(c));
      } else if (c == ')') {
        dealWithCloser();
      } else if (Character.isSpaceChar(c)) {
        // do nothing
      } else {
        System.out.println("Error: unknown character" + c);
      }
    }

    // pop and output all the operators left on the stack
    while (!stack.empty()) {
      out.append(popChar());
    }
    return out;
  }
Ejemplo n.º 10
0
 /**
  * creates a NewAnnotationAction from the input processed by StreamTokenizer <I>tok</I>, which
  * should have the form <br>
  * add [type feature=value feature=value ...] <br>
  * or <br>
  * add [type feature=value feature=value ...] over spanVariable
  */
 public NewAnnotationAction(StreamTokenizer tok) throws IOException, PatternSyntaxError {
   if (tok.nextToken() == StreamTokenizer.TT_WORD && Character.isUpperCase(tok.sval.charAt(0))) {
     bindingVariable = new Variable(tok.sval);
     if (tok.nextToken() != '=') throw new PatternSyntaxError("= expected");
     tok.nextToken();
   }
   if (tok.ttype != '[') throw new PatternSyntaxError("[ expected");
   if (tok.nextToken() != StreamTokenizer.TT_WORD)
     throw new PatternSyntaxError("annotation type expected");
   type = tok.sval;
   features = new FeatureSet(tok, true, ']');
   if (tok.nextToken() == StreamTokenizer.TT_WORD && tok.sval.equalsIgnoreCase("over")) {
     if (tok.nextToken() == StreamTokenizer.TT_WORD && Character.isUpperCase(tok.sval.charAt(0))) {
       spanVariable = new Variable(tok.sval);
       tok.nextToken();
     } else if (tok.ttype == StreamTokenizer.TT_NUMBER && tok.nval == 0) {
       spanVariable = new Variable("0");
       tok.nextToken();
     } else {
       throw new PatternSyntaxError("variable expected after 'over'");
     }
   } else {
     spanVariable = null;
   }
 }
Ejemplo n.º 11
0
  public static void main(String[] args) throws Exception {
    int size = Util.getPropertyInt("size", 100);
    double min = Util.getPropertyDouble("min", 0.01);
    double max = Util.getPropertyDouble("max", 0.9);
    Font font = new Font("serif", Font.PLAIN, size);
    String fpath = Util.getProperty("font", null);
    if (fpath != null) {
      font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(fpath));
    }

    for (char c = Character.MIN_VALUE + 1; c < Character.MAX_VALUE; ++c) {
      int type = Character.getType(c);
      if (type != Character.CONTROL
          && type != Character.FORMAT
          && type != Character.PRIVATE_USE
          && type != Character.SURROGATE
          && type != Character.UNASSIGNED
          && !Character.isMirrored(c)
          && !Character.isSpaceChar(c)) {
        String s = "" + c;
        if (Normalizer.normalize(s, NFKC).contains("\u0308")) continue; // TODO: adhoc
        UnigramMetrics m = new UnigramMetrics(s, size, false, true, font);
        if (min < m.getBlackness() && m.getBlackness() < max) {
          System.out.println("" + c + " " + (int) c);
        }
      }
    }
  }
 /**
  * @param bindex Relative index within base64 character unit; between 0 and 3 (as unit has exactly
  *     4 characters)
  */
 protected IllegalArgumentException reportInvalidBase64Char(
     Base64Variant b64variant, int ch, int bindex, String msg) throws IllegalArgumentException {
   String base;
   if (ch <= INT_SPACE) {
     base =
         "Illegal white space character (code 0x"
             + Integer.toHexString(ch)
             + ") as character #"
             + (bindex + 1)
             + " of 4-char base64 unit: can only used between units";
   } else if (b64variant.usesPaddingChar(ch)) {
     base =
         "Unexpected padding character ('"
             + b64variant.getPaddingChar()
             + "') as character #"
             + (bindex + 1)
             + " of 4-char base64 unit: padding only legal as 3rd or 4th character";
   } else if (!Character.isDefined(ch) || Character.isISOControl(ch)) {
     // Not sure if we can really get here... ? (most illegal xml chars are caught at lower level)
     base = "Illegal character (code 0x" + Integer.toHexString(ch) + ") in base64 content";
   } else {
     base =
         "Illegal character '"
             + ((char) ch)
             + "' (code 0x"
             + Integer.toHexString(ch)
             + ") in base64 content";
   }
   if (msg != null) {
     base = base + ": " + msg;
   }
   return new IllegalArgumentException(base);
 }
Ejemplo n.º 13
0
 /**
  * Read an optional signed integer. If there is no integer in the input stream, return 1. For
  * excessively large exponents, return Integer.MIN_VALUE or Integer.MAX_VALUE.
  */
 public int readOptionalExponent() throws java.io.IOException {
   int sign = read();
   boolean overflow = false;
   int c;
   if (sign == '+' || sign == '-') c = read();
   else {
     c = sign;
     sign = 0;
   }
   int value;
   if (c < 0 || (value = Character.digit((char) c, 10)) < 0) {
     if (sign != 0) error("exponent sign not followed by digit");
     value = 1;
   } else {
     int max = (Integer.MAX_VALUE - 9) / 10;
     for (; ; ) {
       c = read();
       int d = Character.digit((char) c, 10);
       if (d < 0) break;
       if (value > max) overflow = true;
       value = 10 * value + d;
     }
   }
   if (c >= 0) unread(c);
   if (sign == '-') value = -value;
   if (overflow) return sign == '-' ? Integer.MIN_VALUE : Integer.MAX_VALUE;
   return value;
 }
Ejemplo n.º 14
0
 /** Controls the movements and actions of character and tetris pieces */
 public void keyPressed(KeyEvent key) {
   String keyText = KeyEvent.getKeyText(key.getKeyCode());
   if (!pause) {
     if (!shapeAI.getAI()) {
       if (keyText.equalsIgnoreCase("a")) {
         shapes.get(0).moveLeft(map);
         shapeTrack.add(1);
       }
       if (keyText.equalsIgnoreCase("d")) {
         shapes.get(0).moveRight(map);
         shapeTrack.add(2);
       }
       if (keyText.equalsIgnoreCase("w")) {
         shapes.get(0).turn(map, shapeTrack);
       }
     }
     if (keyText.equalsIgnoreCase("left")) {
       character.moveLeft();
     }
     if (keyText.equalsIgnoreCase("right")) {
       character.moveRight();
     }
     if (keyText.equalsIgnoreCase("up")) {
       character.jump();
     }
     if (keyText.equalsIgnoreCase("Slash")) {
       guntrack += 1;
       if (guntrack > weapons.length - 1) {
         guntrack = 0;
       }
     }
   }
   repaint();
 }
Ejemplo n.º 15
0
  /**
   * Move the cursor <i>where</i> characters, withough checking the current buffer.
   *
   * @see #where
   * @param where the number of characters to move to the right or left.
   */
  private final void moveInternal(final int where) throws IOException {
    // debug ("move cursor " + where + " ("
    // + buf.cursor + " => " + (buf.cursor + where) + ")");
    buf.cursor += where;

    char c;

    if (where < 0) {
      int len = 0;
      for (int i = buf.cursor; i < buf.cursor - where; i++) {
        if (buf.getBuffer().charAt(i) == '\t') len += TAB_WIDTH;
        else len++;
      }

      char cbuf[] = new char[len];
      Arrays.fill(cbuf, BACKSPACE);
      out.write(cbuf);

      return;
    } else if (buf.cursor == 0) {
      return;
    } else if (mask != null) {
      c = mask.charValue();
    } else {
      printCharacters(buf.buffer.substring(buf.cursor - where, buf.cursor).toCharArray());
      return;
    }

    // null character mask: don't output anything
    if (NULL_MASK.equals(mask)) {
      return;
    }

    printCharacters(c, Math.abs(where));
  }
 /* pre : empty args
  * post: compare input text file to dictionary
  */
 public void spellCheck() {
   try {
     FileInputStream inf = new FileInputStream(new File("oliver.txt"));
     char let;
     String word = ""; // word to be processed
     int n = 0;
     while ((n = inf.read()) != -1) {
       let = (char) n;
       if (Character.isLetter(let)) {
         word += Character.toLowerCase(let);
       }
       if ((Character.isWhitespace(let) || let == '-') && !word.isEmpty()) {
         // if(dictionary[(int)wordstr.charAt(0)-97].search(wordstr))
         if (dictionary[(int) word.charAt(0) - 97].search(word, stringComparisons)) {
           foundWords++;
           wordsFoundComparison += stringComparisons[0];
         } else {
           notFoundWords++;
           wordsNotFoundComparison += stringComparisons[0];
         }
         word = "";
       }
     }
     inf.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 } // end of spellCheck
Ejemplo n.º 17
0
 /**
  * Search for string in ring.
  *
  * @param str string to search for
  * @param lastIdx last index at which to search for string
  * @param ignoreCase
  * @return index of string if found, else -1
  */
 public int indexOf(String str, int lastIdx, boolean ignoreCase) {
   int strlen = str.length();
   int lastPossible = size - strlen;
   if (lastPossible < 0) return -1;
   lastIdx = (lastIdx < 0 || lastIdx > lastPossible) ? lastPossible : lastIdx;
   int pos = 0;
   // find position of first char of string
   l1:
   while ((pos = indexOf(str.charAt(0), pos, lastIdx, ignoreCase)) >= 0) {
     int ringpos = head + pos;
     for (int ix = 1; ix < strlen; ix++) {
       char ch = str.charAt(ix);
       char rch = chars[(ringpos + ix) % capacity];
       if (ignoreCase) {
         ch = Character.toUpperCase(ch);
         rch = Character.toUpperCase(rch);
       }
       if (ch != rch) {
         pos++;
         continue l1;
       }
     }
     return pos;
   }
   return -1;
 }
 public static void main(String args[]) {
   PorterStemmer s = new PorterStemmer();
   for (int i = 0; i < args.length; i++)
     try {
       InputStream in = new FileInputStream(args[i]);
       byte buffer[] = new byte[1024];
       int bufferLen = in.read(buffer);
       int offset = 0;
       s.reset();
       do {
         int ch;
         if (offset < bufferLen) {
           ch = buffer[offset++];
         } else {
           bufferLen = in.read(buffer);
           offset = 0;
           if (bufferLen < 0) ch = -1;
           else ch = buffer[offset++];
         }
         if (Character.isLetter((char) ch)) {
           s.add(Character.toLowerCase((char) ch));
           continue;
         }
         s.stem();
         System.out.print(s.toString());
         s.reset();
         if (ch < 0) break;
         System.out.print((char) ch);
       } while (true);
       in.close();
     } catch (IOException e) {
       System.out.println((new StringBuilder("error reading ")).append(args[i]).toString());
     }
 }
Ejemplo n.º 19
0
 /**
  * @ensures: if input file is read, a String is filled with words from file and searched in
  *     dictionary list, and if found increase words found counter, otherwise increase words not
  *     found. Otherwise error reading file
  */
 public void readFileOliver() {
   try {
     FileInputStream inf = new FileInputStream(new File("oliver.txt"));
     char let;
     String str = "";
     String key = "";
     int n = 0;
     while ((n = inf.read()) != -1) {
       let = (char) n;
       if (Character.isLetter(let)) {
         str += Character.toLowerCase(let);
       }
       if ((Character.isWhitespace(let) || let == '-') && !str.isEmpty()) {
         key = str;
         str = "";
         boolean a = dictionary[(int) key.charAt(0) - 97].contains(key);
         if (a == true) {
           counter = dictionary[(int) key.charAt(0) - 97].indexOf(key);
           counterWFound++;
           counterWFCompared += counter;
         } else {
           counter =
               dictionary[(int) key.charAt(0) - 97].indexOf(
                   dictionary[(int) key.charAt(0) - 97].getLast());
           counterWNotFound++;
           counterWNFCompared += counter;
         }
       }
     }
     inf.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 20
0
 /** Loads custom hyphenation rules. You probably want to use loadDefault() instead. */
 public void load(BufferedReader input) throws IOException {
   String line;
   while ((line = input.readLine()) != null) {
     if (StringUtils.matches(line, "\\s*(%.*)?")) {
       // comment or blank line
       System.err.println("Skipping: " + line);
       continue;
     }
     char[] linechars = line.toCharArray();
     int[] pattern = new int[linechars.length];
     char[] chars = new char[linechars.length];
     int c = 0;
     for (int i = 0; i < linechars.length; ++i) {
       if (Character.isDigit(linechars[i])) {
         pattern[c] = Character.digit(linechars[i], 10);
       } else {
         chars[c++] = linechars[i];
       }
     }
     char[] shortchars = new char[c];
     int[] shortpattern = new int[c + 1];
     System.arraycopy(chars, 0, shortchars, 0, c);
     System.arraycopy(pattern, 0, shortpattern, 0, c + 1);
     insertHyphPattern(shortchars, shortpattern);
   }
 }
Ejemplo n.º 21
0
  /**
   * Gets the information about an element's handlers in the current router configuration.
   *
   * @param el The element name.
   * @return Vector of HandlerInfo structures.
   * @exception NoSuchElementException If there is no such element in the current configuration.
   * @exception HandlerErrorException If the handler returned an error.
   * @exception PermissionDeniedException If the router would not let us access the handler.
   * @exception IOException If there was some other error accessing the handler (e.g., there was a
   *     stream or socket error, the ControlSocket returned an unknwon unknown error code, or the
   *     response could otherwise not be understood).
   * @see #HandlerInfo
   * @see #getConfigElementNames
   * @see #getRouterConfig
   * @see #getRouterFlatConfig
   */
  public Vector getElementHandlers(String elementName) throws ClickException, IOException {
    Vector v = new Vector();
    Vector vh;

    try {
      char[] buf = read(elementName, "handlers");
      vh = StringUtils.split(buf, 0, '\n');
    } catch (ClickException.NoSuchHandlerException e) {
      return v;
    }

    for (int i = 0; i < vh.size(); i++) {
      String s = (String) vh.elementAt(i);
      int j;
      for (j = 0; j < s.length() && !Character.isWhitespace(s.charAt(j)); j++)
        ; // find record split
      if (j == s.length())
        throw new ClickException.HandlerFormatException(elementName + ".handlers");
      HandlerInfo hi = new HandlerInfo(elementName, s.substring(0, j).trim());
      while (j < s.length() && Character.isWhitespace(s.charAt(j))) j++;
      for (; j < s.length(); j++) {
        char c = s.charAt(j);
        if (Character.toLowerCase(c) == 'r') hi.canRead = true;
        else if (Character.toLowerCase(c) == 'w') hi.canWrite = true;
        else if (Character.isWhitespace(c)) break;
      }
      v.addElement(hi);
    }
    return v;
  }
Ejemplo n.º 22
0
  /**
   * @param clazz the class to be handled (read from and wrote to)
   * @param file the file to be processed
   */
  public void handleConfig(Class<?> clazz, File file) {
    if (!hasRun) {
      hasRun = true;
      for (Field field : clazz.getDeclaredFields()) {
        for (Annotation annotation : field.getAnnotations()) {
          if (annotation.annotationType() == Config.String.class)
            handleString(clazz, field, (Config.String) annotation);
          else if (annotation.annotationType() == Config.Integer.class)
            handleInteger(clazz, field, (Config.Integer) annotation);
          else if (annotation.annotationType() == Config.Boolean.class)
            handleBoolean(clazz, field, (Config.Boolean) annotation);
          else if (annotation.annotationType() == Config.List.class)
            handleList(clazz, field, (Config.List) annotation);
          else if (annotation.annotationType() == Config.Map.class)
            handleMap(clazz, field, (Config.Map) annotation);
          else if (annotation.annotationType() == Config.Long.class)
            handleLong(clazz, field, (Config.Long) annotation);
          else if (annotation.annotationType() == Config.Float.class)
            handleFloat(clazz, field, (Config.Float) annotation);
          else if (annotation.annotationType() == Config.Double.class)
            handleDouble(clazz, field, (Config.Double) annotation);
          else if (annotation.annotationType() == Config.Character.class)
            handleCharacter(clazz, field, (Config.Character) annotation);
        }
      }
    }

    try (BufferedReader reader =
        new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
      String line;
      while ((line = reader.readLine()) != null) {
        if (line.contains("{")) {
          char[] chars = line.toCharArray();
          boolean hasNotEncounteredText = true;
          StringBuilder stringBuilder = new StringBuilder();
          for (Character character : chars) {
            if ((!character.equals(' ') && !character.equals('\t')) || hasNotEncounteredText) {
              hasNotEncounteredText = false;
              stringBuilder.append(character);
            } else if (character.equals(' ')) break;
          }
          categories
              .getOrDefault(stringBuilder.toString(), categories.get("General"))
              .read(clazz, reader);
        }
      }
    } catch (IOException ignored) {
    }

    StringBuilder stringBuilder = new StringBuilder();
    categories.values().forEach(category -> category.write(stringBuilder));
    String fileString = stringBuilder.toString();

    try (BufferedWriter writer =
        new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
      writer.append(fileString);
    } catch (IOException ignored) {
    }
  }
Ejemplo n.º 23
0
  /*
  Extracted words and lowercase the words from the indicated file.
  @param char_data: the file data
  @return char_data: after filter and normalize of the file data
  */
  public static List<Character> filter_chars_normalize(List<Character> char_data) throws Exception {

    for (int i = 0; i < char_data.size(); i++) {
      char ch = char_data.get(i);
      if (!Character.isLetterOrDigit(ch)) char_data.set(i, ' ');
      else char_data.set(i, Character.toLowerCase(ch));
    }
    return char_data;
  }
Ejemplo n.º 24
0
 private static String getSnapshotName(String name, byte[] digest) {
   StringBuilder builder = new StringBuilder(name.length() + 1 + 2 * digest.length);
   builder.append(name).append('.');
   for (byte b : digest) {
     builder.append(Character.forDigit((b & 0xF0) >> 4, 16));
     builder.append(Character.forDigit(b & 0xF, 16));
   }
   return builder.toString();
 }
Ejemplo n.º 25
0
  /**
   * given a sentence, returns a new version of it which is lowercased and strips everything except
   * letters and digit from it
   */
  public static String canonicalizeSentence(String sentence) {
    if (sentence == null) return null;

    StringBuilder sb = new StringBuilder();
    for (char ch : sentence.toCharArray())
      if (Character.isLetterOrDigit(ch)) {
        sb.append(Character.toLowerCase(ch));
      }
    return sb.toString();
  }
Ejemplo n.º 26
0
  private void write(CharSequence text, boolean gsp) {
    if (!gsp) {
      out.print(text);
      return;
    }

    for (int ix = 0, ixz = text.length(); ix < ixz; ix++) {
      char c = text.charAt(ix);
      String rep = null;
      if (Character.isWhitespace(c)) {
        for (ix++; ix < ixz; ix++) {
          if (Character.isWhitespace(text.charAt(ix))) {
            continue;
          }
          ix--;
          rep = " ";
          break;
        }
      } else if (c == '&') {
        if (match("&semi;", text, ix)) {
          rep = ";";
          ix += 5;
        } else if (match("&amp;", text, ix)) {
          rep = "&";
          ix += 4;
        } else if (match("&lt;", text, ix)) {
          rep = "<";
          ix += 3;
        } else if (match("&gt;", text, ix)) {
          rep = ">";
          ix += 3;
        }
      } else if (c == '<') {
        if (match("<br>", text, ix) || match("<hr>", text, ix)) {
          rep = "\n";
          // incrementLineNumber();
          ix += 3;
        } else {
          int end = match(PARA_BREAK, text, ix);
          if (end <= 0) end = match(ROW_BREAK, text, ix);
          if (end > 0) {
            rep = "\n";
            // incrementLineNumber();
            ix = end;
          }
        }
      }
      if (rep != null) {
        out.print(rep);
      } else {
        out.print(c);
      }
    }
  }
Ejemplo n.º 27
0
 /**
  * Helper routine to get the scheme part of a URI. The scheme part is "http:" or "file:" or "ftp:"
  * most commonly. This functions searches for the first ':' that doesn't follow a '/'.
  *
  * @return The length of the scheme component, not counting the colon, (or alternatively the index
  *     of the colon), or -1 if the is no scheme.
  */
 public static int uriSchemeLength(String uri) {
   int len = uri.length();
   for (int i = 0; i < len; i++) {
     char ch = uri.charAt(i);
     if (ch == ':') return i;
     if (i == 0
         ? !Character.isLetter(ch)
         : (!Character.isLetterOrDigit(ch) && ch != '+' && ch != '-' && ch != '.')) return -1;
   }
   return -1;
 }
Ejemplo n.º 28
0
 /**
  * Skips any leading whitespace and return true if any was found. Note that this does not refill
  * the buffer, so callers should do something like this: while(ring.skipLeadingWhiteSpace()) {
  * refill ring; }
  *
  * @return true if any leading whitespace was found, false otherwise
  */
 public boolean skipLeadingWhiteSpace() {
   if (Character.isWhitespace(get(0))) {
     int idx = 0;
     do {
       idx++;
     } while (idx < size() && Character.isWhitespace(get(idx)));
     skip(idx);
     return true;
   }
   return false;
 }
Ejemplo n.º 29
0
  /**
   * Test program for demonstrating the Stemmer. It reads text from a a list of files, stems each
   * word, and writes the result to standard output. Note that the word stemmed is expected to be in
   * lower case: forcing lower case must be done outside the Stemmer class. Usage: Stemmer file-name
   * file-name ...
   */
  public String stemFile(FileInputStream in) {
    char[] w = new char[501];
    String stemmed = new String();
    stemmed = "";
    Stemmer s = new Stemmer();
    // try
    // {
    // FileInputStream in = new FileInputStream(filename);
    try {
      while (true) {
        int ch = in.read();
        if (Character.isLetter((char) ch)) {
          int j = 0;
          while (true) {
            ch = Character.toLowerCase((char) ch);
            w[j] = (char) ch;
            if (j < 500) j++;
            ch = in.read();
            // System.out.println(ch);
            if (!Character.isLetter((char) ch)) {
              /* to test add(char ch) */
              for (int c = 0; c < j; c++) s.add(w[c]);

              /* or, to test add(char[] w, int j) */
              /* s.add(w, j); */

              s.stem();
              {
                String u;

                /* and now, to test toString() : */
                u = s.toString();
                stemmed = stemmed + " " + u;
                /* to test getResultBuffer(), getResultLength() : */
                /* u = new String(s.getResultBuffer(), 0, s.getResultLength()); */

                //      System.out.print(u);
              }
              break;
            }
          }
        }
        if (ch < 0) break;
        // System.out.print((char)ch);
      }
    } catch (IOException e) {
      System.out.println("error reading ");
    }
    // }
    // catch (FileNotFoundException e) {  System.out.println("file not found");  }
    // catch (Exception e) {  System.out.println("file not found");  }
    return stemmed;
  }
Ejemplo n.º 30
0
 private UTF8String toTitleCaseSlow() {
   StringBuffer sb = new StringBuffer();
   String s = toString();
   sb.append(s);
   sb.setCharAt(0, Character.toTitleCase(sb.charAt(0)));
   for (int i = 1; i < s.length(); i++) {
     if (sb.charAt(i - 1) == ' ') {
       sb.setCharAt(i, Character.toTitleCase(sb.charAt(i)));
     }
   }
   return fromString(sb.toString());
 }