/** @tests java.io.StreamTokenizer#slashStarComments(boolean) */
  public void test_slashStarComments_withSTClosed() throws IOException {
    Reader reader = new CharArrayReader("t /* t */ t".toCharArray());

    StreamTokenizer st = new StreamTokenizer(reader);
    st.slashStarComments(false);

    assertEquals(StreamTokenizer.TT_WORD, st.nextToken());
    assertEquals(StreamTokenizer.TT_EOF, st.nextToken());
  }
示例#2
0
 /**
  * Constructs a Parser for the given string.
  *
  * @param text The string to be parsed.
  */
 public Parser(String text) {
   Reader reader = new StringReader(text);
   tokenizer = new StreamTokenizer(reader);
   tokenizer.parseNumbers();
   tokenizer.eolIsSignificant(true);
   tokenizer.slashStarComments(true);
   tokenizer.slashSlashComments(true);
   tokenizer.lowerCaseMode(false);
   tokenizer.ordinaryChars(33, 47);
   tokenizer.ordinaryChars(58, 64);
   tokenizer.ordinaryChars(91, 96);
   tokenizer.ordinaryChars(123, 126);
   tokenizer.quoteChar('\"');
   lineNumber = 1;
 }
示例#3
0
 /**
  * This method sets the syntax of the StreamTokenizer. i.e. set the whitespace, comment and
  * delimit chars.
  */
 protected void setSyntax(StreamTokenizer tk) {
   tk.resetSyntax();
   tk.eolIsSignificant(false);
   tk.slashStarComments(true);
   tk.slashSlashComments(true);
   tk.whitespaceChars(0, ' ');
   tk.wordChars(' ' + 1, '\u00ff');
   tk.ordinaryChar('[');
   tk.ordinaryChar(']');
   tk.ordinaryChar('{');
   tk.ordinaryChar('}');
   tk.ordinaryChar('-');
   tk.ordinaryChar('>');
   tk.ordinaryChar('/');
   tk.ordinaryChar('*');
   tk.quoteChar('"');
   tk.whitespaceChars(';', ';');
   tk.ordinaryChar('=');
 }
  /** @tests java.io.StreamTokenizer#nextToken() */
  @SuppressWarnings("deprecation")
  public void test_nextToken() throws IOException {
    // SM.
    setTest(
        "\r\n/* fje fje 43.4 f \r\n f g */  456.459 \r\n"
            + "Hello  / 	\r\n \r\n \n \r \257 Hi \'Hello World\'");
    st.ordinaryChar('/');
    st.slashStarComments(true);
    st.nextToken();
    assertTrue("Wrong Token type1: " + (char) st.ttype, st.ttype == StreamTokenizer.TT_NUMBER);
    st.nextToken();
    assertTrue("Wrong Token type2: " + st.ttype, st.ttype == StreamTokenizer.TT_WORD);
    st.nextToken();
    assertTrue("Wrong Token type3: " + st.ttype, st.ttype == '/');
    st.nextToken();
    assertTrue("Wrong Token type4: " + st.ttype, st.ttype == StreamTokenizer.TT_WORD);
    st.nextToken();
    assertTrue("Wrong Token type5: " + st.ttype, st.ttype == StreamTokenizer.TT_WORD);
    st.nextToken();
    assertTrue("Wrong Token type6: " + st.ttype, st.ttype == '\'');
    assertTrue("Wrong Token type7: " + st.ttype, st.sval.equals("Hello World"));
    st.nextToken();
    assertTrue("Wrong Token type8: " + st.ttype, st.ttype == -1);

    final PipedInputStream pin = new PipedInputStream();
    PipedOutputStream pout = new PipedOutputStream(pin);
    pout.write("hello\n\r\r".getBytes("UTF-8"));
    StreamTokenizer s = new StreamTokenizer(pin);
    s.eolIsSignificant(true);
    assertTrue(
        "Wrong token 1,1", s.nextToken() == StreamTokenizer.TT_WORD && s.sval.equals("hello"));
    assertTrue("Wrong token 1,2", s.nextToken() == '\n');
    assertTrue("Wrong token 1,3", s.nextToken() == '\n');
    assertTrue("Wrong token 1,4", s.nextToken() == '\n');
    pout.close();
    assertTrue("Wrong token 1,5", s.nextToken() == StreamTokenizer.TT_EOF);
    StreamTokenizer tokenizer = new StreamTokenizer(new Support_StringReader("\n \r\n#"));
    tokenizer.ordinaryChar('\n'); // make \n ordinary
    tokenizer.eolIsSignificant(true);
    assertTrue("Wrong token 2,1", tokenizer.nextToken() == '\n');
    assertTrue("Wrong token 2,2", tokenizer.nextToken() == '\n');
    assertEquals("Wrong token 2,3", '#', tokenizer.nextToken());
  }
示例#5
0
文件: ESParser.java 项目: null3/jpf
  StreamTokenizer createScanner(Reader r) {
    StreamTokenizer s = new StreamTokenizer(r);

    // disable number parsing, since it doesn't work in the context of string expansion
    // and we also would have to preserve the number type (int or double)
    s.ordinaryChars('0', '9');
    s.wordChars('0', '9');
    // s.wordChars('"', '"');

    // those are used to expand events
    s.wordChars('[', '[');
    s.wordChars(']', ']');
    s.wordChars('|', '|');
    s.wordChars('-', '-');
    s.wordChars('<', '<');
    s.wordChars('>', '>');

    // those can be part of component names
    s.wordChars('_', '_');
    s.wordChars('#', '#');
    s.wordChars('*', '*');
    s.wordChars('@', '@');
    s.wordChars('$', '$');
    s.wordChars(':', ':');
    s.wordChars('~', '~');

    s.quoteChar('"');

    s.slashSlashComments(true);
    s.slashStarComments(true);

    s.whitespaceChars(',', ',');
    s.whitespaceChars(';', ';');

    return s;
  }
示例#6
0
  private static void readConfig(File confFile) {

    System.out.println("Reading configuration file: " + confFile);

    try {
      StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new FileReader(confFile)));
      tokenizer.eolIsSignificant(true);
      tokenizer.slashStarComments(true);

      boolean EOF = false;
      int tokType = 0;
      Vector words = new Vector();
      while (!EOF) {
        if ((tokType = tokenizer.nextToken()) == StreamTokenizer.TT_EOF) {
          EOF = true;
        } else if (tokType != StreamTokenizer.TT_EOL) {
          if (tokenizer.sval != null) {
            words.addElement(tokenizer.sval);
          }
        } else {
          if (words.size() == 2) {
            String key = (String) words.elementAt(0);
            String value = (String) words.elementAt(1);
            if (key.equals("SRSServer")) {
              srsServer = new String(value);
            } else if (key.equals("Database")) {
              database = new String(value);
            } else if (key.equals("Layout")) {
              layout = new String(value);
            } else if (key.equals("AutosaveInterval")) {
              if (value.equals("none")) {
                setAutosaveInterval(-1);
              } else {
                try {
                  setAutosaveInterval(Integer.parseInt(value));
                } catch (NumberFormatException e) {
                  System.err.println("Can't parse number: " + value);
                }
              }
            } else if (key.equals("ColourSchemeInstall")) {
              try {
                String installString = value;
                int breakIndex = installString.indexOf(":");

                if (breakIndex < 0) {
                  // adapterRegistry.installDataAdapter(installString);
                } else {
                  String driverName = installString.substring(0, breakIndex);
                  String driverDesc = installString.substring(breakIndex + 1);
                  // adapterRegistry.installDataAdapter(driverName);
                }
              } catch (Throwable e) {
                System.err.println("Could not install driver " + value + " because of " + e);
              }
            } else if (key.equals("FormatAdapterInstall")) {
              try {
                String installString = value;
                int breakIndex = installString.indexOf(":");

                if (breakIndex < 0) {
                  // adapterRegistry.installDataAdapter(installString);
                } else {
                  String driverName = installString.substring(0, breakIndex);
                  String driverDesc = installString.substring(breakIndex + 1);
                  // adapterRegistry.installDataAdapter(driverName);
                }
              } catch (Throwable e) {
                System.err.println("Could not install driver " + value + " because of " + e);
              }
            } else {
              System.out.println("Unknown config key " + key);
            }
          } else {
            if (words.size() != 0) {
              System.out.println(
                  "Too many words on line beginning "
                      + (String) words.elementAt(0)
                      + " in config file");
            }
          }

          words.removeAllElements();
        }
      }
      return;

    } catch (Exception ex) {
      System.out.println(ex);
      return;
    }
  }
示例#7
0
  /**
   * Parse a policy file, incorporating the permission definitions described therein.
   *
   * @param url The URL of the policy file to read.
   * @throws IOException if an I/O error occurs, or if the policy file cannot be parsed.
   */
  private void parse(final URL url) throws IOException {
    logger.log(Component.POLICY, "reading policy file from {0}", url);
    final StreamTokenizer in = new StreamTokenizer(new InputStreamReader(url.openStream()));
    in.resetSyntax();
    in.slashSlashComments(true);
    in.slashStarComments(true);
    in.wordChars('A', 'Z');
    in.wordChars('a', 'z');
    in.wordChars('0', '9');
    in.wordChars('.', '.');
    in.wordChars('_', '_');
    in.wordChars('$', '$');
    in.whitespaceChars(' ', ' ');
    in.whitespaceChars('\t', '\t');
    in.whitespaceChars('\f', '\f');
    in.whitespaceChars('\n', '\n');
    in.whitespaceChars('\r', '\r');
    in.quoteChar('\'');
    in.quoteChar('"');

    int tok;
    int state = STATE_BEGIN;
    List keystores = new LinkedList();
    URL currentBase = null;
    List currentCerts = new LinkedList();
    Permissions currentPerms = new Permissions();
    while ((tok = in.nextToken()) != StreamTokenizer.TT_EOF) {
      switch (tok) {
        case '{':
          if (state != STATE_GRANT) error(url, in, "spurious '{'");
          state = STATE_PERMS;
          tok = in.nextToken();
          break;
        case '}':
          if (state != STATE_PERMS) error(url, in, "spurious '}'");
          state = STATE_BEGIN;
          currentPerms.setReadOnly();
          Certificate[] c = null;
          if (!currentCerts.isEmpty())
            c = (Certificate[]) currentCerts.toArray(new Certificate[currentCerts.size()]);
          cs2pc.put(new CodeSource(currentBase, c), currentPerms);
          currentCerts.clear();
          currentPerms = new Permissions();
          currentBase = null;
          tok = in.nextToken();
          if (tok != ';') in.pushBack();
          continue;
      }
      if (tok != StreamTokenizer.TT_WORD) {
        error(url, in, "expecting word token");
      }

      // keystore "<keystore-path>" [',' "<keystore-type>"] ';'
      if (in.sval.equalsIgnoreCase("keystore")) {
        String alg = KeyStore.getDefaultType();
        tok = in.nextToken();
        if (tok != '"' && tok != '\'') error(url, in, "expecting key store URL");
        String store = in.sval;
        tok = in.nextToken();
        if (tok == ',') {
          tok = in.nextToken();
          if (tok != '"' && tok != '\'') error(url, in, "expecting key store type");
          alg = in.sval;
          tok = in.nextToken();
        }
        if (tok != ';') error(url, in, "expecting semicolon");
        try {
          KeyStore keystore = KeyStore.getInstance(alg);
          keystore.load(new URL(url, store).openStream(), null);
          keystores.add(keystore);
        } catch (Exception x) {
          error(url, in, x.toString());
        }
      } else if (in.sval.equalsIgnoreCase("grant")) {
        if (state != STATE_BEGIN) error(url, in, "extraneous grant keyword");
        state = STATE_GRANT;
      } else if (in.sval.equalsIgnoreCase("signedBy")) {
        if (state != STATE_GRANT && state != STATE_PERMS) error(url, in, "spurious 'signedBy'");
        if (keystores.isEmpty()) error(url, in, "'signedBy' with no keystores");
        tok = in.nextToken();
        if (tok != '"' && tok != '\'') error(url, in, "expecting signedBy name");
        StringTokenizer st = new StringTokenizer(in.sval, ",");
        while (st.hasMoreTokens()) {
          String alias = st.nextToken();
          for (Iterator it = keystores.iterator(); it.hasNext(); ) {
            KeyStore keystore = (KeyStore) it.next();
            try {
              if (keystore.isCertificateEntry(alias))
                currentCerts.add(keystore.getCertificate(alias));
            } catch (KeyStoreException kse) {
              error(url, in, kse.toString());
            }
          }
        }
        tok = in.nextToken();
        if (tok != ',') {
          if (state != STATE_GRANT) error(url, in, "spurious ','");
          in.pushBack();
        }
      } else if (in.sval.equalsIgnoreCase("codeBase")) {
        if (state != STATE_GRANT) error(url, in, "spurious 'codeBase'");
        tok = in.nextToken();
        if (tok != '"' && tok != '\'') error(url, in, "expecting code base URL");
        String base = expand(in.sval);
        if (File.separatorChar != '/') base = base.replace(File.separatorChar, '/');
        try {
          currentBase = new URL(base);
        } catch (MalformedURLException mue) {
          error(url, in, mue.toString());
        }
        tok = in.nextToken();
        if (tok != ',') in.pushBack();
      } else if (in.sval.equalsIgnoreCase("principal")) {
        if (state != STATE_GRANT) error(url, in, "spurious 'principal'");
        tok = in.nextToken();
        if (tok == StreamTokenizer.TT_WORD) {
          tok = in.nextToken();
          if (tok != '"' && tok != '\'') error(url, in, "expecting principal name");
          String name = in.sval;
          Principal p = null;
          try {
            Class pclass = Class.forName(in.sval);
            Constructor c = pclass.getConstructor(new Class[] {String.class});
            p = (Principal) c.newInstance(new Object[] {name});
          } catch (Exception x) {
            error(url, in, x.toString());
          }
          for (Iterator it = keystores.iterator(); it.hasNext(); ) {
            KeyStore ks = (KeyStore) it.next();
            try {
              for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
                String alias = (String) e.nextElement();
                if (ks.isCertificateEntry(alias)) {
                  Certificate cert = ks.getCertificate(alias);
                  if (!(cert instanceof X509Certificate)) continue;
                  if (p.equals(((X509Certificate) cert).getSubjectDN())
                      || p.equals(((X509Certificate) cert).getSubjectX500Principal()))
                    currentCerts.add(cert);
                }
              }
            } catch (KeyStoreException kse) {
              error(url, in, kse.toString());
            }
          }
        } else if (tok == '"' || tok == '\'') {
          String alias = in.sval;
          for (Iterator it = keystores.iterator(); it.hasNext(); ) {
            KeyStore ks = (KeyStore) it.next();
            try {
              if (ks.isCertificateEntry(alias)) currentCerts.add(ks.getCertificate(alias));
            } catch (KeyStoreException kse) {
              error(url, in, kse.toString());
            }
          }
        } else error(url, in, "expecting principal");
        tok = in.nextToken();
        if (tok != ',') in.pushBack();
      } else if (in.sval.equalsIgnoreCase("permission")) {
        if (state != STATE_PERMS) error(url, in, "spurious 'permission'");
        tok = in.nextToken();
        if (tok != StreamTokenizer.TT_WORD) error(url, in, "expecting permission class name");
        String className = in.sval;
        Class clazz = null;
        try {
          clazz = Class.forName(className);
        } catch (ClassNotFoundException cnfe) {
        }
        tok = in.nextToken();
        if (tok == ';') {
          if (clazz == null) {
            currentPerms.add(
                new UnresolvedPermission(
                    className,
                    null,
                    null,
                    (Certificate[]) currentCerts.toArray(new Certificate[currentCerts.size()])));
            continue;
          }
          try {
            currentPerms.add((Permission) clazz.newInstance());
          } catch (Exception x) {
            error(url, in, x.toString());
          }
          continue;
        }
        if (tok != '"' && tok != '\'') error(url, in, "expecting permission target");
        String target = expand(in.sval);
        tok = in.nextToken();
        if (tok == ';') {
          if (clazz == null) {
            currentPerms.add(
                new UnresolvedPermission(
                    className,
                    target,
                    null,
                    (Certificate[]) currentCerts.toArray(new Certificate[currentCerts.size()])));
            continue;
          }
          try {
            Constructor c = clazz.getConstructor(new Class[] {String.class});
            currentPerms.add((Permission) c.newInstance(new Object[] {target}));
          } catch (Exception x) {
            error(url, in, x.toString());
          }
          continue;
        }
        if (tok != ',') error(url, in, "expecting ','");
        tok = in.nextToken();
        if (tok == StreamTokenizer.TT_WORD) {
          if (!in.sval.equalsIgnoreCase("signedBy")) error(url, in, "expecting 'signedBy'");
          try {
            Constructor c = clazz.getConstructor(new Class[] {String.class});
            currentPerms.add((Permission) c.newInstance(new Object[] {target}));
          } catch (Exception x) {
            error(url, in, x.toString());
          }
          in.pushBack();
          continue;
        }
        if (tok != '"' && tok != '\'') error(url, in, "expecting permission action");
        String action = in.sval;
        if (clazz == null) {
          currentPerms.add(
              new UnresolvedPermission(
                  className,
                  target,
                  action,
                  (Certificate[]) currentCerts.toArray(new Certificate[currentCerts.size()])));
          continue;
        } else {
          try {
            Constructor c = clazz.getConstructor(new Class[] {String.class, String.class});
            currentPerms.add((Permission) c.newInstance(new Object[] {target, action}));
          } catch (Exception x) {
            error(url, in, x.toString());
          }
        }
        tok = in.nextToken();
        if (tok != ';' && tok != ',') error(url, in, "expecting ';' or ','");
      }
    }
  }
 /** @tests java.io.StreamTokenizer#slashStarComments(boolean) */
 public void test_slashStarCommentsZ() throws IOException {
   setTest("/* foo \r\n /fiji \r\n*/ -456");
   st.ordinaryChar('/');
   st.slashStarComments(true);
   assertTrue("Test failed.", st.nextToken() == StreamTokenizer.TT_NUMBER);
 }