public GElement generateGraph(String dotFile) throws IOException {

    BufferedReader br = new BufferedReader(new FileReader(dotFile));
    graph = null;

    /**
     * The problem here is that DOT sometime inserts a '\' at the end of a long line so we have to
     * skip it and continue to parse until a "real" EOL is reached. Example: statement ->
     * compoundStatement [pos="e,3264,507 3271,2417 3293,2392 ... 3237,565 3234,560 32\ 39,545
     * 3243,534 3249,523 3257,514"];
     */
    StringBuffer line = new StringBuffer();
    int c; // current character
    int pc = -1; // previous character
    while ((c = br.read()) != -1) {
      if (c == '\n') {
        if (pc == '\\') {
          // Remove the last \ if it was part of the DOT wrapping character
          line.deleteCharAt(line.length() - 1);
        } else {
          GElement element = parseLine(line.toString());
          if (element != null) {
            if (graph == null) graph = element;
            else graph.addElement(element);
          }
          line.delete(0, line.length());
        }
      } else if (c != '\r') {
        line.append((char) c);
      }
      pc = c;
    }
    return graph;
  }
Пример #2
0
    /**
     * Create a Transferable to use as the source for a data transfer.
     *
     * @param c The component holding the data to be transfered. This argument is provided to enable
     *     sharing of TransferHandlers by multiple components.
     * @return The representation of the data to be transfered.
     */
    protected Transferable createTransferable(JComponent c) {
      Object[] values = null;
      if (c instanceof JList) {
        values = ((JList) c).getSelectedValues();
      } else if (c instanceof JTable) {
        JTable table = (JTable) c;
        int[] rows = table.getSelectedRows();
        if (rows != null) {
          values = new Object[rows.length];
          for (int i = 0; i < rows.length; i++) {
            values[i] = table.getValueAt(rows[i], 0);
          }
        }
      }
      if (values == null || values.length == 0) {
        return null;
      }

      StringBuffer plainBuf = new StringBuffer();
      StringBuffer htmlBuf = new StringBuffer();

      htmlBuf.append("<html>\n<body>\n<ul>\n");

      for (Object obj : values) {
        String val = ((obj == null) ? "" : obj.toString());
        plainBuf.append(val + "\n");
        htmlBuf.append("  <li>" + val + "\n");
      }

      // remove the last newline
      plainBuf.deleteCharAt(plainBuf.length() - 1);
      htmlBuf.append("</ul>\n</body>\n</html>");

      return new FileTransferable(plainBuf.toString(), htmlBuf.toString(), values);
    }
Пример #3
0
 /**
  * Reads a line of text edited by the user, terminated by <code>ENTER</code>. Editing is performed
  * at the current cursor position.
  *
  * @param field Maximum length of the text
  * @return The text read. <code>null</code> if it was pressed the <code>Esc</code> key.
  */
 public static String nextLine(int field) {
   boolean oldCursorOn = frame.isCursorOn();
   boolean oldEcho = frame.isEcho();
   cursor(false);
   int lin = frame.getLin(), col = frame.getCol();
   for (int i = 0; i < field; ++i) print(' ');
   cursor(lin, col);
   cursor(true);
   echo(false);
   StringBuffer res = new StringBuffer(field);
   char c;
   while ((c = waitChar(0)) != '\n') {
     if (c == KeyEvent.VK_ESCAPE) {
       res = null;
       break;
     } // Esc (27)
     else if (c == KeyEvent.VK_BACK_SPACE) { // Backspace (8)
       if (res.length() == 0) continue;
       res.deleteCharAt(res.length() - 1);
       cursor(false);
       cursor(frame.getLin(), frame.getCol() - 1);
       print(' ');
       cursor(frame.getLin(), frame.getCol() - 1);
       cursor(true);
     } else if (res.length() < field) {
       print(c);
       res.append(c);
     }
   }
   cursor(oldCursorOn);
   echo(oldEcho);
   return res == null ? null : res.toString();
 }
Пример #4
0
  boolean removeFromLine(int x, int y) {
    try {
      // getLine
      StringBuffer currentLine = (StringBuffer) vectorLines.elementAt(y);

      currentLine.deleteCharAt(x - 1);
      // setLine
      vectorLines.setElementAt(currentLine, y);
      return true;
    } catch (Exception ex) {
      System.out.println(ex.toString() + " in removeFromLine()");
      return false;
    }
  }
Пример #5
0
  // 读取指定的名字对应的文件的内容
  public static String getContentByName(String title, Context context) {
    FileInputStream fin = null;
    InputStreamReader ir = null;
    BufferedReader br = null;
    StringBuffer sb = null;
    try {
      fin = context.openFileInput(title);
      ir = new InputStreamReader(fin);
      br = new BufferedReader(ir);
      String line = null;
      sb = new StringBuffer();
      while ((line = br.readLine()) != null) {
        sb.append(line).append("\r\n");
      }

      // 防止取到空的数据,然后报数组越界
      if (sb.length() != 0) {
        sb.deleteCharAt(sb.length() - 1);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      if (ir != null) {
        try {
          ir.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }

      if (fin != null) {
        try {
          fin.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return sb.toString();
  }
Пример #6
0
  public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

    if (args.length != 2) {
      System.out.println("Usage: FeatureMatching ID <inputName.jpeg/inputName.jpg>");
      System.exit(1);
    }

    SimpleDateFormat sdf = new SimpleDateFormat("", Locale.US);
    sdf.applyPattern("yyyy-MM-dd_HH-mm-ss");
    String time = sdf.format(new Date());

    Job job = Job.getInstance();

    ID = "/" + args[0];
    String filename = args[1];
    filename = filename.toLowerCase();
    System.out.println("current filename:" + filename);

    // Detect illegal username (if the username dir doesn't exist)
    File userPath = new File(LOCAL_USER_DIR + ID);
    if (!userPath.exists()) {
      System.out.println("Unauthorized username!!!\nExiting......");
      System.exit(1);
    }
    // Preprocess the input image.jpg from local dir: /local.../user/ID/input/image.jpg
    // Save the features to local dir: hdfs://.../user/ID/input/image.jpg
    extractQueryFeatures2HDFS(filename, job);

    // Add the feature file to the hdfs cache
    String featureFileName = filename.substring(0, filename.lastIndexOf(".")) + ".json";
    //        job.addCacheFile(new Path(HDFS_HOME + USER + ID + INPUT + "/" +
    // featureFileName).toUri());
    job.getConfiguration()
        .set("featureFilePath", HDFS_HOME + USER + ID + INPUT + "/" + featureFileName);

    // Check the file type. Only support jpeg/jpg type images
    String type = filename.substring(args[1].lastIndexOf("."));
    if (!(type.equals(".jpg") || type.equals(".jpeg"))) {
      System.out.println("Image type not supported!!!\nExiting");
      System.exit(1);
    }

    // Input: hdfs://.../features/
    // The feature dir is a location of all features extracted from the database
    String inputPathStr = HDFS_HOME + FEATURES;
    // Output: hdfs://.../user/ID/output/
    String outputPathStr = HDFS_HOME + USER + ID + OUTPUT + "/" + time;

    job.setInputFormatClass(KeyValueTextInputFormat.class);
    //        job.setOutputFormatClass(TextOutputFormat.class);

    // Get the lists of all feature files: /.../features/data/part-*
    FileSystem fs = FileSystem.get(job.getConfiguration());
    FileStatus[] statuses = fs.listStatus(new Path(inputPathStr));
    StringBuffer sb = new StringBuffer();
    for (FileStatus fileStatus : statuses) {
      sb.append(fileStatus.getPath() + ",");
    }
    sb.deleteCharAt(sb.lastIndexOf(","));

    job.setJarByClass(FeatureMatching.class);
    job.setMapperClass(FeatureMatchMapper.class);
    job.setReducerClass(FeatureMatchReducer.class);

    // only need one reducer to collect the result
    job.setNumReduceTasks(1);

    job.setMapOutputKeyClass(IntWritable.class);
    job.setMapOutputValueClass(Text.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(DoubleWritable.class);

    // Input a directory, so need the recursive input
    FileInputFormat.setInputDirRecursive(job, true);
    // Set the PathFilter, to omit _SUCCESS files
    // (This is not working correctly, as the PathFilter class is an interface rather than a class.
    // But the 2nd arg asks me to extend the PathFilter)
    //        FileInputFormat.setInputPathFilter(job, MyPathFilter.class);
    //
    //        FileInputFormat.setInputPaths(job, new Path(inputPathStr));
    FileInputFormat.setInputPaths(job, sb.toString());
    FileOutputFormat.setOutputPath(job, new Path(outputPathStr));

    boolean success = job.waitForCompletion(true);
    return success ? 0 : 1;
  }
Пример #7
0
  public CommentsCollection parse(final InputStream in, final String charsetName)
      throws IOException, UnsupportedEncodingException {
    boolean lastWasASlashR = false;
    BufferedReader br = new BufferedReader(new InputStreamReader(in, charsetName));
    CommentsCollection comments = new CommentsCollection();
    int r;

    Deque prevTwoChars = new LinkedList<Character>(Arrays.asList('z', 'z'));

    State state = State.CODE;
    LineComment currentLineComment = null;
    BlockComment currentBlockComment = null;
    StringBuffer currentContent = null;

    int currLine = 1;
    int currCol = 1;

    while ((r = br.read()) != -1) {
      char c = (char) r;
      if (c == '\r') {
        lastWasASlashR = true;
      } else if (c == '\n' && lastWasASlashR) {
        lastWasASlashR = false;
        continue;
      } else {
        lastWasASlashR = false;
      }
      switch (state) {
        case CODE:
          if (prevTwoChars.peekLast().equals('/') && c == '/') {
            currentLineComment = new LineComment();
            currentLineComment.setBeginLine(currLine);
            currentLineComment.setBeginColumn(currCol - 1);
            state = State.IN_LINE_COMMENT;
            currentContent = new StringBuffer();
          } else if (prevTwoChars.peekLast().equals('/') && c == '*') {
            currentBlockComment = new BlockComment();
            currentBlockComment.setBeginLine(currLine);
            currentBlockComment.setBeginColumn(currCol - 1);
            state = State.IN_BLOCK_COMMENT;
            currentContent = new StringBuffer();
          } else if (c == '"') {
            state = State.IN_STRING;
          } else {
            // nothing to do
          }
          break;
        case IN_LINE_COMMENT:
          if (c == '\n' || c == '\r') {
            currentLineComment.setContent(currentContent.toString());
            currentLineComment.setEndLine(currLine);
            currentLineComment.setEndColumn(currCol);
            comments.addComment(currentLineComment);
            state = State.CODE;
          } else {
            currentContent.append(c);
          }
          break;
        case IN_BLOCK_COMMENT:
          if (prevTwoChars.peekLast().equals('*')
              && c == '/'
              && !prevTwoChars.peekFirst().equals('/')) {

            // delete last character
            String content =
                currentContent.deleteCharAt(currentContent.toString().length() - 1).toString();

            if (content.startsWith("*")) {
              JavadocComment javadocComment = new JavadocComment();
              javadocComment.setContent(content.substring(1));
              javadocComment.setBeginLine(currentBlockComment.getBeginLine());
              javadocComment.setBeginColumn(currentBlockComment.getBeginColumn());
              javadocComment.setEndLine(currLine);
              javadocComment.setEndColumn(currCol + 1);
              comments.addComment(javadocComment);
            } else {
              currentBlockComment.setContent(content);
              currentBlockComment.setEndLine(currLine);
              currentBlockComment.setEndColumn(currCol + 1);
              comments.addComment(currentBlockComment);
            }
            state = State.CODE;
          } else {
            currentContent.append(c == '\r' ? '\n' : c);
          }
          break;
        case IN_STRING:
          if (!prevTwoChars.peekLast().equals('\\') && c == '"') {
            state = State.CODE;
          }
          break;
        default:
          throw new RuntimeException("Unexpected");
      }
      switch (c) {
        case '\n':
        case '\r':
          currLine += 1;
          currCol = 1;
          break;
        case '\t':
          currCol += COLUMNS_PER_TAB;
          break;
        default:
          currCol += 1;
      }
      prevTwoChars.remove();
      prevTwoChars.add(c);
    }

    if (state == State.IN_LINE_COMMENT) {
      currentLineComment.setContent(currentContent.toString());
      currentLineComment.setEndLine(currLine);
      currentLineComment.setEndColumn(currCol);
      comments.addComment(currentLineComment);
    }

    return comments;
  }
Пример #8
0
  public void getInput() {
    if (gameStatus != -1) {
      float b = (float) (15f / Main.fps);
      switch (controlMode) {
        case 3:
          if (Keyboard.isKeyDown(Keyboard.KEY_DOWN) && !player.isBot) {
            if (player.y <= 100 * Main.ratio - player.amplitude * 2 - player.sy)
              player.playerYShift += b;
          }
        case 1:
          if (Keyboard.isKeyDown(Keyboard.KEY_LEFT) && !player.isBot) {
            if (player.freak >= player.minFreak) player.freak -= freakChanger;
          }
          if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT) && !player.isBot) {
            if (player.freak <= player.maxFreak) player.freak += freakChanger;
          }
          if (player2 != null) {
            if (Keyboard.isKeyDown(Keyboard.KEY_A) && !player2.isBot) {
              if (player2.freak >= player.minFreak) player2.freak -= freakChanger;
            }
            if (Keyboard.isKeyDown(Keyboard.KEY_D) && !player2.isBot) {
              if (player2.freak <= player.maxFreak) player2.freak += freakChanger;
            }
          }
          break;
        case 2:
          if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && !player.isBot) {
            if (player.freak <= player.maxFreak) player.freak += freakChanger;
          }

          if (Keyboard.isKeyDown(Keyboard.KEY_1)) {
            ListWorker.getList();
          }
          if (Keyboard.isKeyDown(Keyboard.KEY_M)) {
            gameConfiguration.mute = true;
            if (Draw.musicIsPlaying(Music.FON1)) Draw.musicStop(Music.FON1);
          }
          if (Keyboard.isKeyDown(Keyboard.KEY_N)) {
            gameConfiguration.mute = false;
            if (!Draw.musicIsPlaying(Music.FON1)) Draw.musicPlay(Music.FON1);
          }
          if (Keyboard.isKeyDown(Keyboard.KEY_0)) {
            mouseGrabbed = true;
          }
          if (Keyboard.isKeyDown(Keyboard.KEY_9)) {
            mouseGrabbed = false;
          }
          if (Keyboard.isKeyDown(Keyboard.KEY_P)) {
            pause = true;
          }
          if (Keyboard.isKeyDown(Keyboard.KEY_O)) {
            pause = false;
          }
          if (player2 != null) {
            if (Keyboard.isKeyDown(Keyboard.KEY_Q) && !player2.isBot) {
              if (player2.freak <= player2.maxFreak) player2.freak += freakChanger;
            }
          }
          break;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_I)) {
        player.immortalityDie = 99999999;
        player.die = false;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_O)) {
        player.immortalityDie = 0;
        player.die = false;
      }
      /*if (Keyboard.isKeyDown(Keyboard.KEY_3)) {
          if (gameStatus == 9)
              restartGame = true;
          gameStatus = 3;
          changedGameStatus = true;
          return;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_1)) {
          if (gameStatus == 9)
              restartGame = true;
          gameStatus = 1;
          changedGameStatus = true;
          return;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_2)) {
          if (gameStatus == 9)
              restartGame = true;
          gameStatus = 2;
          changedGameStatus = true;

          return;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_0)) {
          if (gameStatus == 9)
              restartGame = true;
          gameStatus = 0;
          changedGameStatus = true;
          return;
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_4)) {
          if (gameStatus == 9)
              restartGame = true;
          gameStatus = 4;
          changedGameStatus = true;
          return;
      } */
    }
    lastKeyDownTime++;

    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      if (lastKeyDownTime > 10) {
        lastKeyDownTime = 0;
        changedGameStatus = true;
        if (gameStatus == 9) {
          restartGame = true;
          gameStatus = 0;
          changedGameStatus = true;
        } else {
          if (gameStatus == 2) {
            gameStatus = 1;
          } else {
            if (gameStatus == 1) {
              gameStatus = 2;
            } else {
              if (gameStatus == 0) {
                changedGameStatus = false;
              } else {
                gameStatus = 0;
              }
            }
          }
        }
      }
    }

    if (Mouse.isButtonDown(0)) {
      if (lastKeyDownTime > 10) {
        lastKeyDownTime = 0;
        switch (checkButtonIsOver()) {
          case 1:
            switch (gameStatus) {
              case (-1):
                gameStatus = 0;

                break;

              case 0:
                gameStatus = 2;
                changedGameStatus = true;
                Main.restartGame();

                break;
              case 1:
                gameStatus = 2;
                changedGameStatus = true;
                break;
              case 3:
                if (pg > 1) {
                  pg--;
                }
                //  getPreviousTopList();
                break;
              case 4:
                setMultiplayer();
                break;
              case 9:
                saveScore();
                gameStatus = 10;
                changedGameStatus = true;
                break;

              case 10:
                gameStatus = 2;

                Main.restartGame();
                break;
            }

            break;
          case 2:
            switch (gameStatus) {
              case 0:
                gameStatus = 3; // TOP SCORES
                changedGameStatus = true;
                break;
              case 1:
                gameStatus = 2;

                Main.restartGame();
                break;
              case 3:
                if (pg < maxPg) {
                  pg++;
                }
                //  getNextTopList();
                break;
              case 4:
                setBot();
                break;
              case 9:
                gameStatus = 10;

                break;

              case 10:
                gameStatus = 0;

                changedGameStatus = true;
                break;
            }
            changedGameStatus = true;
            break;
          case 3:
            switch (gameStatus) {
              case 0:
                gameStatus = 4; // Settings
                createNewConfiguration();
                changedGameStatus = true;
                break;
              case 1:
                gameStatus = 0;
                changedGameStatus = true;
                break;
              case 3:
                gameStatus = 0;
                changedGameStatus = true;
                break;
              case 4:
                setAudio();
                break;
            }

            break;
          case 4:
            switch (gameStatus) {
              case 0:
                exit();
                break;
              case 4:

                //   setPreviousMusicGenre();
                break;
            }
            break;
          case 5:
            if (gameStatus == 4)

              //  setNextMusicGenre();
              break;
          case 6:
            if (gameStatus == 4) gameStatus = 0;
            changedGameStatus = true;
            if (changedSettings == true) {
              changedSettings = false;
              saveConfiguration();
              gameConfigurationNew = null;
            }
            break;
          case 7:
            if (gameStatus == 4) gameStatus = 0;
            changedGameStatus = true;
            if (changedSettings == true) {
              changedSettings = false;
              gameConfigurationNew = null;
            }

            break;

          default:
            /*
            if (gameStatus == 9) {
                restartGame = true;
                gameStatus = 2;
                //changedGameStatus = true;

            }*/
            break;
        }
      }
    }

    if ((gameStatus == -1) || (gameStatus == 9)) {

      while (Keyboard.next()) {

        // System.out.println(playerName);

        char x = Keyboard.getEventCharacter();

        // if ((int)(x) == 42) {
        //     continue;
        //  }

        if ((int) (x) == 0) continue;

        if ((int) (x) == 32) continue;

        if ((int) (x) == 13) {
          gameStatus = 0;
          break;
        }

        if ((int) (x) == 8) {
          if (playerNamelenght > 0) {
            playerName.deleteCharAt(playerNamelenght - 1);
            playerNamelenght--;
          }
          continue;
        }

        // playerName = Keyboard.getKeyName(x);
        // playerNameChars[playerNamelenght] = Keyboard.get
        if (playerNamelenght < 15) {
          playerName.append(x);
          playerNamelenght++;
        }

        // System.out.println(playerName);

      }
    }
  }