📄 policyfilereader.java
字号:
} private boolean isNumber() { return isNumber(tokenType()); } private static boolean isNumber(int ttype) { return ttype == TT_NUMBER; } private boolean isOwnedBy() { return isWord() && string().equals(WORD_OWNEDBY); } private boolean isPermission() { return isWord() && isPermission(string()); } private boolean isPermission(String word) { return WORD_PERMISSION.equals(word); } private boolean isPolicyPermission() { // # return isPermission() || isProtection() || isAllowance(); return isPermission() || isProtection(); } private boolean isProtection() { return isWord() && isProtection(string()); } private boolean isProtection(String word) { return WORD_PROTECTION.equals(word); } private boolean isQuotedString() { return isQuotedString(tokenType()); } private static boolean isQuotedString(int ttype) { return ttype == TT_QUOTED_STRING; } private boolean isSignedBy() { return isWord() && string().equals(WORD_SIGNEDBY); } private boolean isTerminator() { return isTerminator(tokenType()); } private static boolean isTerminator(int ttype) { return ttype == TT_TERMINATOR; } private boolean isWord() { return isWord(tokenType()); } private static boolean isWord(int ttype) { return ttype == TT_WORD; } private int lineno() { return _st.lineno(); } // for test static public void main(String arg[]) { PolicyDB db = null; if (arg.length == 0) { db = getAllPolicyDB(); } else { PolicyFileReader reader = new PolicyFileReader(arg[0]); db = reader.getPolicyDB(); } if (db != null) { System.out.print(db.toString()); } else { System.out.println("Policy file does not exist."); } } private int nextToken() throws PolicyFileParsingException { int ttype; try { ttype = _st.nextToken(); } catch (IOException excpt) { throw getParsingException(excpt.toString()); } return ttype; } private double number() { return _st.nval; } protected synchronized void readPolicyDB() throws PolicyFileParsingException { nextToken(); // at least one grant clause is needed if (!isGrant()) { throw getParsingException("The reserved word '" + WORD_GRANT + "' is expected, not '" + token() + "'."); } if (_db == null) { _db = new PolicyDB(); } while (isGrant()) { readPolicyGrant(); } if (!isEndOfFile()) { throw getParsingException("The reserved word '" + WORD_GRANT + "' is expected, not '" + token() + "'."); } } protected synchronized void readPolicyFile(String filename) throws FileNotFoundException { log.info("Reading security policy file: " + filename); if (filename == null) { throw new FileNotFoundException("Policy filename is null."); } _filename = filename; FileReader reader = new FileReader(filename); _st = new StreamTokenizer(new BufferedReader(reader)); initialize(); try { readPolicyDB(); reader.close(); log.debug("Policy file read complete"); } catch (PolicyFileParsingException excpt) { log.error("Error parsing policy file: ",excpt); } catch (IOException excpt) { log.error("Error parsing policy file: ",excpt); } } protected synchronized void readPolicyGrant() throws PolicyFileParsingException { if (!isGrant()) { throw getParsingException("The reserved word '" + WORD_GRANT + "' is expected, not '" + token() + "'."); } nextToken(); PolicyGrant grant = new PolicyGrant(); boolean signedBy = false; boolean codeBase = false; boolean ownedBy = false; while (!isBeginBlock()) { if (isSignedBy()) { // signed by if (signedBy) { // duplicated throw getParsingException(WORD_SIGNEDBY + " phrase duplicated."); } signedBy = true; nextToken(); // should be signer names if (isQuotedString()) { // signer names grant.setSignerNames(string()); nextToken(); if (!isBeginBlock()) { // optional if (isComma()) { nextToken(); } else { throw getParsingException("A character '" + CHAR_COMMA + "' is expected before '" + token() + "'."); } } } else { throw getParsingException("Signer name(s) should be a quoted string. '" + token() + "'."); } } else if (isCodeBase()) { // code base if (codeBase) { // duplicated throw getParsingException(WORD_CODEBASE + " phrase duplicated."); } codeBase = true; nextToken(); // should be URI pattern as code base if (isQuotedString()) { // URI pattern as code base try { grant.setCodeBase(string()); } catch (MalformedURIPatternException excpt) { throw getParsingException(excpt.toString()); } nextToken(); if (!isBeginBlock()) { // optional if (isComma()) { nextToken(); } else { throw getParsingException("A character '" + CHAR_COMMA + "' is expected before '" + token() + "'."); } } } else { throw getParsingException("An URI pattern as code base should be a quoted string. '" + token() + "'."); } } else if (isOwnedBy()) { // owned by if (ownedBy) { // duplicated throw getParsingException(WORD_OWNEDBY + " phrase duplicated."); } ownedBy = true; nextToken(); // should be owner names if (isQuotedString()) { // owner names grant.setOwnerNames(string()); nextToken(); if (!isBeginBlock()) { // optional if (isComma()) { nextToken(); } else { throw getParsingException("Begin block character '" + CHAR_BEGIN_BLOCK + "' is expected, not '" + token() + "'."); } } } else { throw getParsingException("An owner name should be a quoted string. '" + token() + "'."); } } else { throw getParsingException("Begin block character '" + CHAR_BEGIN_BLOCK + "' is expected, not '" + token() + "'."); } } // should be begin block if (isBeginBlock()) { nextToken(); // at least one permission clause is needed in the block. if (!isPolicyPermission()) { // # throw getParsingException("The reserved word '"+WORD_PERMISSION+"' or '"+WORD_PROTECTION+"' or '"+WORD_ALLOWANCE+"' is expected, not '"+token()+"'."); throw getParsingException("The reserved word '" + WORD_PERMISSION + "' or '" + WORD_PROTECTION + "' is expected, not '" + token() + "'."); } while (isPolicyPermission()) { grant.addPermission(getPolicyPermission()); } // should be end block if (isEndBlock()) { nextToken(); // should be terminator if (isTerminator()) { nextToken(); } else { throw getParsingException("The termination character '" + CHAR_TERMINATOR + "' is expected, not '" + token() + "'."); } } else { throw getParsingException("End block character '" + CHAR_END_BLOCK + "' is expected, not '" + token() + "'."); } } else { throw getParsingException("Begin block character '" + CHAR_BEGIN_BLOCK + "' is expected, not '" + token() + "'."); } _db.addGrant(grant); } private String string() { return _st.sval; } private String token() { String t; final int type = tokenType(); switch (type) { case TT_WORD: case TT_QUOTED_STRING: t = string(); break; case TT_NUMBER: t = Double.toString(number()); break; default: t = String.valueOf((char)type); } return t; } private int tokenType() { return _st.ttype; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -