Example #1
1
File: Macro.java Project: bramk/bnd
  int process(CharSequence org, int index, char begin, char end, StringBuilder result, Link link) {
    StringBuilder line = new StringBuilder(org);
    int nesting = 1;

    StringBuilder variable = new StringBuilder();
    outer:
    while (index < line.length()) {
      char c1 = line.charAt(index++);
      if (c1 == end) {
        if (--nesting == 0) {
          result.append(replace(variable.toString(), link));
          return index;
        }
      } else if (c1 == begin) nesting++;
      else if (c1 == '\\' && index < line.length() - 1 && line.charAt(index) == '$') {
        // remove the escape backslash and interpret the dollar
        // as a
        // literal
        index++;
        variable.append('$');
        continue outer;
      } else if (c1 == '$' && index < line.length() - 2) {
        char c2 = line.charAt(index);
        char terminator = getTerminator(c2);
        if (terminator != 0) {
          index = process(line, index + 1, c2, terminator, variable, link);
          continue outer;
        }
      } else if (c1 == '.' && index < line.length() && line.charAt(index) == '/') {
        // Found the sequence ./
        if (index == 1 || Character.isWhitespace(line.charAt(index - 2))) {
          // make sure it is preceded by whitespace or starts at begin
          index++;
          variable.append(domain.getBase().getAbsolutePath());
          variable.append('/');
          continue outer;
        }
      }
      variable.append(c1);
    }
    result.append(variable);
    return index;
  }
  @Override
  public synchronized void run() {
    while (!quit) {
      //			filter_input();
      try {
        Thread.sleep(1); // poll at 1kHz
        // grab data from shared buffer and move it the local one
        ourBuffer.append(sharedBuffer.take());
        // System.out.print(ourBuffer);
        // create a matcher to find an open tag in the buffer
        int endCloseTag;
        int startOpenTag;
        Matcher closeTagMatcher = closeTagPattern.matcher(ourBuffer);

        // look or a closing tag in our buffer
        if (closeTagMatcher.find()) {
          endCloseTag = closeTagMatcher.end();
          openTag = "<" + ourBuffer.charAt(closeTagMatcher.start() + 2) + ">";
          // find corresponding opening tag
          startOpenTag = ourBuffer.indexOf(openTag);
          // send the input string to the GUI for processing
          if (startOpenTag >= 0 && startOpenTag < endCloseTag) {
            gui.incoming(ourBuffer.substring(startOpenTag, endCloseTag));
            // clear our buffer
            ourBuffer.delete(startOpenTag, endCloseTag);
          } else {
            ourBuffer.delete(0, endCloseTag);
          }
        }
      } catch (Exception e) {
        System.out.print(e + "\n");
      }
    }
  }
 private static String getUnmatchedBrackets(final String initialText) {
   String text = initialText.replaceAll("\\\\.", "_"); // Remove escaped characters.
   text = text.replaceAll("'.'", "_"); // Remove character literals.
   text = text.replaceAll("\"([^\\n]*?)\"", "_"); // Remove string literals.
   text = text.replaceAll("/\\*(?s).*?\\*/", "_"); // Remove C comments.
   text = text.replaceAll("//[^\\n]*", "_"); // Remove C++ comments.
   StringBuilder unmatchedBrackets = new StringBuilder();
   for (int i = 0; i < text.length(); ++i) {
     char ch = text.charAt(i);
     if (PBracketUtilities.isOpenBracket(ch) && ch != '<') {
       unmatchedBrackets.append(ch);
     } else if (PBracketUtilities.isCloseBracket(ch) && ch != '>') {
       char openBracket = PBracketUtilities.getPartnerForBracket(ch);
       int lastCharIndex = unmatchedBrackets.length() - 1;
       if (lastCharIndex >= 0 && unmatchedBrackets.charAt(lastCharIndex) == openBracket) {
         unmatchedBrackets.deleteCharAt(lastCharIndex);
       } else {
         unmatchedBrackets.append(ch);
       }
     }
   }
   return unmatchedBrackets.toString();
 }