Exemplo n.º 1
1
 private static void mapLines(
     TextBuffer code,
     StructLineNumberTableAttribute table,
     BytecodeMappingTracer tracer,
     int startLine) {
   // build line start offsets map
   HashMap<Integer, Set<Integer>> lineStartOffsets = new HashMap<Integer, Set<Integer>>();
   for (Map.Entry<Integer, Integer> entry : tracer.getMapping().entrySet()) {
     Integer lineNumber = entry.getValue() - startLine;
     Set<Integer> curr = lineStartOffsets.get(lineNumber);
     if (curr == null) {
       curr = new TreeSet<Integer>(); // requires natural sorting!
     }
     curr.add(entry.getKey());
     lineStartOffsets.put(lineNumber, curr);
   }
   String lineSeparator = DecompilerContext.getNewLineSeparator();
   StringBuilder text = code.getOriginalText();
   int pos = text.indexOf(lineSeparator);
   int lineNumber = 0;
   while (pos != -1) {
     Set<Integer> startOffsets = lineStartOffsets.get(lineNumber);
     if (startOffsets != null) {
       for (Integer offset : startOffsets) {
         int number = table.findLineNumber(offset);
         if (number >= 0) {
           code.setLineMapping(number, pos);
           break;
         }
       }
     }
     pos = text.indexOf(lineSeparator, pos + 1);
     lineNumber++;
   }
 }
Exemplo n.º 2
1
  public static StringBuilder replaceAll(StringBuilder in, String p, String r) {
    int pl = p.length(), rl = r.length(), s = in.indexOf(p, 0);

    if (pl == 0) return in;

    while (s >= 0) {
      in.replace(s, s + pl, r);
      s = in.indexOf(p, s + rl);
    }

    return in;
  }
Exemplo n.º 3
1
 public void searchTags() {
   StringBuilder strTags = new StringBuilder();
   for (int i = 0; i < tempStrBuilder.length() - 1; i++) {
     i = tempStrBuilder.indexOf("<");
     if (i == -1) {
       break;
     }
     strTags.append(tempStrBuilder.substring(i, tempStrBuilder.indexOf(">") + 1));
     tempStrBuilder.delete(i, tempStrBuilder.indexOf(">") + 1);
     tags.add(strTags.toString());
     strTags.delete(0, strTags.length());
   }
 }
Exemplo n.º 4
1
 /**
  * @param str The string to find the replacements for.
  * @param fromIndex The index from which replacements are found.
  * @param level The recursion level. The search stops if level is &gt; MAX_RECURSION_LEVEL.
  * @return A list of all possible replacements of a {#link str} given string
  */
 public List<String> getAllReplacements(final String str, final int fromIndex, final int level) {
   List<String> replaced = new ArrayList<>();
   if (level > MAX_RECURSION_LEVEL) { // Stop searching at some point
     replaced.add(str);
     return replaced;
   }
   StringBuilder sb = new StringBuilder();
   sb.append(str);
   int index = MAX_WORD_LENGTH;
   String key = "";
   int keyLength = 0;
   boolean found = false;
   // find first possible replacement after fromIndex position
   for (final String auxKey : replacementsTheRest.keySet()) {
     int auxIndex = sb.indexOf(auxKey, fromIndex);
     if (auxIndex > -1
         && (auxIndex < index
             || (auxIndex == index
                 && !(auxKey.length() < keyLength)))) { // select the longest possible key
       index = auxIndex;
       key = auxKey;
       keyLength = auxKey.length();
     }
   }
   if (index < MAX_WORD_LENGTH) {
     for (final String rep : replacementsTheRest.get(key)) {
       // start a branch without replacement (only once per key)
       if (!found) {
         replaced.addAll(getAllReplacements(str, index + key.length(), level + 1));
         found = true;
       }
       // avoid unnecessary replacements (ex. don't replace L by L·L when L·L already present)
       int ind = sb.indexOf(rep, fromIndex - rep.length() + 1);
       if (rep.length() > key.length()
           && ind > -1
           && (ind == index || ind == index - rep.length() + 1)) {
         continue;
       }
       // start a branch with replacement
       sb.replace(index, index + key.length(), rep);
       replaced.addAll(getAllReplacements(sb.toString(), index + rep.length(), level + 1));
       sb.setLength(0);
       sb.append(str);
     }
   }
   if (!found) {
     replaced.add(sb.toString());
   }
   return replaced;
 }
 private static String munchTo(StringBuilder sb, String delim) {
   int x = sb.indexOf(delim);
   if (x == -1) {
     x = sb.length();
   }
   String ans = sb.substring(0, x);
   sb.delete(0, x + delim.length());
   return ans;
 }
Exemplo n.º 6
1
 /**
  * Given a string, replace all tabs with the appropriate number of spaces.
  *
  * @param tabLength the length of each tab.
  * @param s the String to scan.
  */
 public static void replaceTabs(int tabLength, StringBuilder s) {
   if (whitespace == null || whitespace.length() < tabLength)
     whitespace = String.format("%" + tabLength + "s", " ");
   int index = 0;
   while ((index = s.indexOf("\t", index)) != -1) {
     int spaceCount = tabLength - index % tabLength;
     s.replace(index, index + 1, whitespace.substring(0, spaceCount));
     index += spaceCount;
   }
 }
Exemplo n.º 7
1
  /**
   * Sets a mnemomic for the specified button.
   *
   * @param b button
   * @param mnem mnemonics that have already been assigned
   */
  public static void setMnemonic(final AbstractButton b, final StringBuilder mnem) {
    // do not set mnemonics for Mac! Alt+key used for special characters.
    if (Prop.MAC) return;

    // find and assign unused mnemomic
    final String label = b.getText();
    final int ll = label.length();
    for (int l = 0; l < ll; l++) {
      final char ch = Character.toLowerCase(label.charAt(l));
      if (!letter(ch) || mnem.indexOf(Character.toString(ch)) != -1) continue;
      b.setMnemonic(ch);
      mnem.append(ch);
      break;
    }
  }
Exemplo n.º 8
0
  private String readUntil(StringBuilder src, String stop) throws RequestMessageParsingException {
    int stopIndex = src.indexOf(stop);

    if (stopIndex == -1) {
      String message = String.format("String \"%s\" not found in \"%s\".", stop, src.toString());
      throw new RequestMessageParsingException(message);
    }

    String res = src.substring(0, stopIndex);
    src.delete(0, stopIndex);
    return res;
  }
Exemplo n.º 9
0
 private void addClassPathToScriptEngine(File jarFile) throws ConfigurationException {
   try {
     StringBuilder cmd = new StringBuilder(addClassPathCmd);
     int tagStartPos = cmd.indexOf(parameterTag);
     int tageEndPos = tagStartPos + parameterTag.length();
     cmd.replace(tagStartPos, tageEndPos, jarFile.getAbsolutePath().replace("\\", "/"));
     // System.out.println("cmd " + cmd.toString());
     engine.eval("add-classpath", 1, 1, cmd.toString()); // $NON-NLS-1$
   } catch (Exception ex) {
     String msg = String.format("Failed to load class path %s", jarFile.getName()); // $NON-NLS-1$
     System.err.println(msg + " " + ex);
     throw new ConfigurationException(msg, ex);
   }
 }
Exemplo n.º 10
0
 /**
  * Imports a package into the script engine
  *
  * @param pkgName the name of the package
  * @throws ConfigurationException if the package cannot be imported
  */
 private void importPkgToScriptEngine(String pkgName) throws ConfigurationException {
   try {
     StringBuilder cmd = new StringBuilder(importPackageCmd);
     int tagStartPos = cmd.indexOf(parameterTag);
     int tageEndPos = tagStartPos + parameterTag.length();
     cmd.replace(tagStartPos, tageEndPos, pkgName);
     // System.out.println("cmd " + cmd.toString());
     engine.eval("load-packages", 1, 1, cmd.toString()); // $NON-NLS-1$
   } catch (Exception ex) {
     String msg = String.format("Failed to import package %s", pkgName); // $NON-NLS-1$
     System.err.println(msg + " " + ex);
     throw new ConfigurationException(msg, ex);
   }
 }
Exemplo n.º 11
0
    private static Cubo lectura(BufferedReader br) throws IOException {
        ArrayList<int[][]>aux = new ArrayList<>();
        ArrayList<char[][]>auxS = new ArrayList<>();
        StringTokenizer st;
        int[][] cara = null;
        char[][] caraCol = null;
        char[][][] mCaraCol = null;
        
        String in;
        StringBuilder color = new StringBuilder("");
        
        //Leer primer cara
        caraCol = new char[3][3];
        for (int i = 0; i < 3; i++) {
            in = br.readLine().trim();
            st = new StringTokenizer(in);
            
            for(int k = 0; k < 3; k++){
                String s= st.nextToken();
                caraCol[i][k] = s.charAt(0);

                if( i==1 && k==1 ){
                    color.append(caraCol[i][k]);
                }
            }
            
        }
        
        mCaraCol = new char[4][3][3];
        //Leer parte del centro
        for (int i = 0; i < 3; i++) {
            in = br.readLine().trim();
            st = new StringTokenizer(in);
            for(int j = 0; j < 4; j++) {
                for(int k = 0; k < 3; k++){
                    String s= st.nextToken();
                    mCaraCol[j][i][k] = s.charAt(0);

                    if( i==1 && k==1 ){
                        color.append(mCaraCol[j][i][k]);
                    }
                }
            }
        }
        //Ingresa cara del frente
        auxS.add(mCaraCol[1]);
        //Ingresa cara de arriba
        auxS.add(caraCol);
        //Ingresa cara de Atras
        auxS.add(mCaraCol[3]);
        
        //Leer cara de abajo
        caraCol = new char[3][3];
        for (int i = 0; i < 3; i++) {
            in = br.readLine().trim();
            st = new StringTokenizer(in);
            
            for(int k = 0; k < 3; k++){
                String s= st.nextToken();
                caraCol[i][k] = s.charAt(0);

                if( i==1 && k==1 ){
                    color.append(caraCol[i][k]);
                }
            }
        }
        
        //Ingresa cara de Abajo
        auxS.add(caraCol);
        //Ingresa cara de derecha
        auxS.add(mCaraCol[2]);
        //Ingresa cara de Izquierda
        auxS.add(mCaraCol[0]);
        
        for (int i = 0; i < 6; i++) {
            
            cara = new int[3][3];
            caraCol = auxS.get(i);
            
            for(int j = 0; j < 3; j++){
                for(int k = 0; k < 3; k++){
                    cara[j][k] = color.indexOf(caraCol[j][k]+"");
                }
            }
            
            aux.add(cara);
        }
        
        return new Cubo(aux, color.toString());
    }
  protected String parseStringValue(
      String strVal, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {

    StringBuilder buf = new StringBuilder(strVal);

    int startIndex = strVal.indexOf(this.placeholderPrefix);
    while (startIndex != -1) {
      int endIndex = findPlaceholderEndIndex(buf, startIndex);
      if (endIndex != -1) {
        String placeholder = buf.substring(startIndex + this.placeholderPrefix.length(), endIndex);
        String originalPlaceholder = placeholder;
        if (!visitedPlaceholders.add(originalPlaceholder)) {
          throw new IllegalArgumentException(
              "Circular placeholder reference '"
                  + originalPlaceholder
                  + "' in property definitions");
        }
        // Recursive invocation, parsing placeholders contained in the placeholder key.
        placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
        // Now obtain the value for the fully resolved key...
        String propVal = placeholderResolver.resolvePlaceholder(placeholder);
        if (propVal == null && this.valueSeparator != null) {
          int separatorIndex = placeholder.indexOf(this.valueSeparator);
          if (separatorIndex != -1) {
            String actualPlaceholder = placeholder.substring(0, separatorIndex);
            String defaultValue =
                placeholder.substring(separatorIndex + this.valueSeparator.length());
            propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
            if (propVal == null) {
              propVal = defaultValue;
            }
          }
        }
        if (propVal != null) {
          // Recursive invocation, parsing placeholders contained in the
          // previously resolved placeholder value.
          propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
          buf.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
          if (logger.isTraceEnabled()) {
            logger.trace("Resolved placeholder '" + placeholder + "'");
          }
          startIndex = buf.indexOf(this.placeholderPrefix, startIndex + propVal.length());
        } else if (this.ignoreUnresolvablePlaceholders) {
          // Proceed with unprocessed value.
          startIndex =
              buf.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
        } else {
          throw new IllegalArgumentException(
              "Could not resolve placeholder '"
                  + placeholder
                  + "'"
                  + " in string value \""
                  + strVal
                  + "\"");
        }
        visitedPlaceholders.remove(originalPlaceholder);
      } else {
        startIndex = -1;
      }
    }

    return buf.toString();
  }