Exemplo n.º 1
0
  /**
   * Extract JS content from the given HTML content. The HTML elements are replaced with space and
   * JS content is kept. JS content is declared inside the given tags elements.
   *
   * @param html the HTML content which contains JS content.
   * @param tagRegions list of HTML tags which contains JS content.
   * @return the result of the extract of JS content from the given HTML content. The HTML elements
   *     are replaced with space and JS content is kept.
   */
  public static String extractJS(String html, ScriptTagRegion... tagRegions) {
    IState state = createState(tagRegions);
    StringBuilder s = new StringBuilder();
    char[] chars = html.toCharArray();
    for (int i = 0; i < chars.length; i++) {
      char c = chars[i];
      switch (c) {
        case '\n':
        case '\r':
        case '\t':
        case ' ':
          s.append(c);
          break;
        default:
          // try to search region
          Region matchedRegion = state.update(c);
          if (matchedRegion == null) {
            // none matched region
            if (state.isNextRegionToFindType(RegionType.END_SCRIPT)) {
              // the next region to find is end script (ex :
              // </script>)
              // we are inside script element content, add JS
              // character inside the buffer.
              s.append(c);
            } else {
              // here we are not inside script content, add a space.
              s.append(' ');
            }
          } else {
            // a region is found
            if (matchedRegion.getType().equals(RegionType.END_SCRIPT)) {
              // the matched region is end script (ex : </script>)
              // replace last characters of the buffer (</script>)
              // with spaces.
              int length = matchedRegion.getLength();
              s = s.replace(i - length, i, matchedRegion.getSpaces());
              // reset the state.
              state.reset();
            }
            s.append(' ');
          }
      }
    }

    return s.toString();
  }
Exemplo n.º 2
0
 @Override
 public void close() {
   final ReentrantLock lock = m_lock;
   lock.lock();
   try {
     m_state.close(this);
   } finally {
     lock.unlock();
   }
 }
  public static void main(String[] args) throws Exception {
    System.out.println("Welcome to Dungeons and Dragons 21st Century Command Line Style");

    DungeonsAndDragonsGame game = null;
    try {
      game = new DungeonsAndDragonsGame();
    } catch (FileNotFoundException e) {
      System.err.println("Unable to load map file");
    }

    IState state = game.getState();

    do {

      state = state.execute();
      game.setState(state);

    } while (!state.gameOver());

    System.exit(0);
  }
Exemplo n.º 4
0
  @Override
  public boolean reset() {
    final ReentrantLock lock = m_lock;
    if (!lock.tryLock()) // fail-fast
    return false;

    try {
      return m_state.reset(this);
    } finally {
      lock.unlock();
    }
  }
Exemplo n.º 5
0
  @Override
  public boolean schedule(int timeout) {
    if (timeout < 1) throw new IllegalArgumentException();

    final ReentrantLock lock = m_lock;
    if (!lock.tryLock()) // fail-fast
    return false;

    try {
      return m_state.schedule(this, timeout);
    } finally {
      lock.unlock();
    }
  }
  /**
   * Drives the application.
   *
   * @param args first element would be the svn access configuration file
   * @throws Exception if it fails to generate a report
   */
  public void drive(String[] args) throws Exception {

    // We need an access config file in order to generate a report.
    if (args == null || args.length < 1) {
      System.out.println("=== SVN Access Auditor v1.0 ===");
      System.out.println("Outputs mapping information (HTML) between SVN repos and users. ");
      System.out.println("Usage:");
      System.out.println("  java -jar svnaccessauditor.jar <svnaccess.conf>");
      System.out.println();
      System.out.println("Output: repos.html");
      return;
    }

    File accessConfigFile = new File(args[0]);
    if (!accessConfigFile.exists() || accessConfigFile.isDirectory()) {
      System.out.println("File not exists at: " + args[0]);
      return;
    }

    System.out.println("Processing the records...");
    BufferedReader reader = new BufferedReader(new FileReader(accessConfigFile));
    String line;

    // Let the states handle the records.
    currentState = new InitialState(this);
    while ((line = reader.readLine()) != null) {
      currentState.process(line);
    }
    reader.close();

    // Add all users to EVERYONE group.
    for (User u : users.values()) {
      EVERYONE.addUser(u);
    }
    groups.put("EVERYONE", EVERYONE);

    reportGenerator = new HtmlReportGenerator(users, groups, repos);
    reportGenerator.generateReport();
  }
Exemplo n.º 7
0
  /**
   * testStates
   *
   * @param state
   */
  protected void testStates(IState state, StateInitializer initializer, EventResult... results) {
    for (EventResult result : results) {
      // reset the context
      this._context.reset();

      // initialize the state's state
      if (initializer != null) {
        initializer.initialize(state);
      }

      // try current event type test
      try {
        state.transition(this._context, result.event, result.value);

        if (result.valid == false) {
          fail("Was supposed to fail, but passed: " + result);
        }
      } catch (IllegalStateException e) {
        if (result.valid) {
          fail("Was supposed to pass, but failed: " + result + "\n" + e.getMessage());
        }
      }
    }
  }
 public void turnCrank() {
   currentState.turnCrank();
   if (currentState == soldState || currentState == winnerState) currentState.dispense();
 }
 public void backMoney() {
   currentState.backMoney();
 }
 public void insertMoney() {
   currentState.insertMoney();
 }
Exemplo n.º 11
0
 @Override
 public int state() {
   return m_state.state();
 }