Esempio n. 1
0
  private char consumeWhiteSpace(CharacterIterator ci) {
    char c = ci.next();
    // Consume white space;
    // TODO use correct c
    while (c == ' ') c = ci.next();

    return c;
  }
  /**
   * This method returns an instance of <code>CollationElementIterator</code> for the <code>String
   * </code> represented by the specified <code>CharacterIterator</code>.
   *
   * @param source The <code>CharacterIterator</code> with the desired <code>String</code>.
   * @return A <code>CollationElementIterator</code> for the specified <code>String</code>.
   */
  public CollationElementIterator getCollationElementIterator(CharacterIterator source) {
    StringBuffer expand = new StringBuffer("");

    // Right now we assume that we will read from the beginning of the string.
    for (char c = source.first(); c != CharacterIterator.DONE; c = source.next())
      decomposeCharacter(c, expand);

    return getCollationElementIterator(expand.toString());
  }
Esempio n. 3
0
 protected void selected(CharPos start, CharPos end) {
   StringBuilder buf = new StringBuilder();
   synchronized (msgs) {
     boolean sel = false;
     for (Message msg : msgs) {
       if (!(msg.text() instanceof RichText)) continue;
       RichText rt = (RichText) msg.text();
       RichText.Part part = null;
       if (sel) {
         part = rt.parts;
       } else if (msg == start.msg) {
         sel = true;
         for (part = rt.parts; part != null; part = part.next) {
           if (part == start.part) break;
         }
       }
       if (sel) {
         for (; part != null; part = part.next) {
           if (!(part instanceof RichText.TextPart)) continue;
           RichText.TextPart tp = (RichText.TextPart) part;
           CharacterIterator iter = tp.ti();
           int sch;
           if (tp == start.part) sch = tp.start + start.ch.getInsertionIndex();
           else sch = tp.start;
           int ech;
           if (tp == end.part) ech = tp.start + end.ch.getInsertionIndex();
           else ech = tp.end;
           for (int i = sch; i < ech; i++) buf.append(iter.setIndex(i));
           if (part == end.part) {
             sel = false;
             break;
           }
           buf.append(' ');
         }
         if (sel) buf.append('\n');
       }
       if (msg == end.msg) break;
     }
   }
   Clipboard cl;
   if ((cl = java.awt.Toolkit.getDefaultToolkit().getSystemSelection()) == null)
     cl = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
   try {
     final CharPos ownsel = selstart;
     cl.setContents(
         new StringSelection(buf.toString()),
         new ClipboardOwner() {
           public void lostOwnership(Clipboard cl, Transferable tr) {
             if (selstart == ownsel) selstart = selend = null;
           }
         });
   } catch (IllegalStateException e) {
   }
 }
Esempio n. 4
0
 private void parse(CharacterIterator ci) {
   try {
     while (ci.hasNext()) {
       char c = ci.next();
       if (c == '{') {
         processLiteralCharacters();
         parseName(ci);
       } else {
         literalCharactersBuffer.append(c);
       }
     }
     processLiteralCharacters();
   } catch (NoSuchElementException ex) {
     throw new IllegalArgumentException(
         "Invalid syntax for the template, \""
             + template
             + "\". Check if a path parameter is terminated with a '}'.",
         ex);
   }
 }
Esempio n. 5
0
File: World.java Progetto: munro/lzr
 public World() {
   // Parse foobar
   float x = 0, y = 0;
   CharacterIterator it = new StringCharacterIterator(foobar);
   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next(), x++) {
     switch (ch) {
       case '+':
         walls.add(new Wall(new Vector2f(x + 0.5f, y), new Vector2f(x + 0.5f, y + 1.0f)));
       case '-':
         walls.add(new Wall(new Vector2f(x, y + 0.5f), new Vector2f(x + 1.0f, y + 0.5f)));
         break;
       case '|':
         walls.add(new Wall(new Vector2f(x + 0.5f, y), new Vector2f(x + 0.5f, y + 1.0f)));
         break;
       case '\n':
         x = -1;
         y++;
         break;
     }
   }
   singleton = this;
 }
Esempio n. 6
0
  private String parseRegex(CharacterIterator ci) {
    StringBuffer regexBuffer = new StringBuffer();

    int braceCount = 1;
    while (true) {
      char c = ci.next();
      if (c == '{') {
        braceCount++;
      } else if (c == '}') {
        braceCount--;
        if (braceCount == 0) break;
      }
      regexBuffer.append(c);
    }

    return regexBuffer.toString().trim();
  }
Esempio n. 7
0
  private void parseName(CharacterIterator ci) {
    char c = consumeWhiteSpace(ci);

    StringBuffer nameBuffer = new StringBuffer();
    if (Character.isLetterOrDigit(c) || c == '_') {
      // Template name character
      nameBuffer.append(c);
    } else {
      throw new IllegalArgumentException(
          "Illegal character '"
              + c
              + "' at position "
              + ci.pos()
              + " is not as the start of a name");
    }

    String nameRegexString = "";
    while (true) {
      c = ci.next();
      // "\\{(\\w[-\\w\\.]*)
      if (Character.isLetterOrDigit(c) || c == '_' || c == '-' || c == '.') {
        // Template name character
        nameBuffer.append(c);
      } else if (c == ':') {
        nameRegexString = parseRegex(ci);
        break;
      } else if (c == '}') {
        break;
      } else if (c == ' ') {
        c = consumeWhiteSpace(ci);

        if (c == ':') {
          nameRegexString = parseRegex(ci);
          break;
        } else if (c == '}') {
          break;
        } else {
          // Error
          throw new IllegalArgumentException(
              "Illegal character '"
                  + c
                  + "' at position "
                  + ci.pos()
                  + " is not allowed after a name");
        }
      } else {
        throw new IllegalArgumentException(
            "Illegal character '"
                + c
                + "' at position "
                + ci.pos()
                + " is not allowed as part of a name");
      }
    }
    String name = nameBuffer.toString();
    names.add(name);

    try {
      if (nameRegexString.length() > 0) numOfExplicitRegexes++;
      Pattern namePattern =
          (nameRegexString.length() == 0)
              ? TEMPLATE_VALUE_PATTERN
              : Pattern.compile(nameRegexString);
      if (nameToPattern.containsKey(name)) {
        if (!nameToPattern.get(name).equals(namePattern)) {
          throw new IllegalArgumentException(
              "The name '"
                  + name
                  + "' is declared "
                  + "more than once with different regular expressions");
        }
      } else {
        nameToPattern.put(name, namePattern);
      }

      // Determine group count of pattern
      Matcher m = namePattern.matcher("");
      int g = m.groupCount();
      groupCounts.add(g + 1);

      regex.append('(').append(namePattern).append(')');
      normalizedTemplate.append('{').append(name).append('}');
    } catch (PatternSyntaxException ex) {
      throw new IllegalArgumentException(
          "Invalid syntax for the expression '"
              + nameRegexString
              + "' associated with the name '"
              + name
              + "'",
          ex);
    }
  }