policyparser.java

来自「JAVA 所有包」· Java 代码 · 共 947 行 · 第 1/2 页

JAVA
947
字号
	    if (expect.equalsIgnoreCase(","))		found = true;	    break;	case '{':	    if (expect.equalsIgnoreCase("{"))		found = true;	    break;	case '}':	    if (expect.equalsIgnoreCase("}"))		found = true;	    break;	case '"':	    if (expect.equalsIgnoreCase("\""))		found = true;	    break;	case '*':	    if (expect.equalsIgnoreCase("*"))		found = true;	    break;	default:	    	}		return found;     }    private String match(String expect) 	throws ParsingException, IOException    {	String value = null;	switch (lookahead) {	case StreamTokenizer.TT_NUMBER:	    throw new ParsingException(st.lineno(), expect, 					rb.getString("number ") +					String.valueOf(st.nval));	case StreamTokenizer.TT_EOF:	   throw new ParsingException		(rb.getString("expected ") + expect +		rb.getString(", read end of file"));	case StreamTokenizer.TT_WORD:	    if (expect.equalsIgnoreCase(st.sval)) {		lookahead = st.nextToken();	    } else if (expect.equalsIgnoreCase("permission type")) {		value = st.sval;		lookahead = st.nextToken();	    } else if (expect.equalsIgnoreCase("principal type")) {		value = st.sval;		lookahead = st.nextToken();	    } else {		throw new ParsingException(st.lineno(), expect, st.sval);	    }	    break;	case '"':	    if (expect.equalsIgnoreCase("quoted string")) {		value = st.sval;		lookahead = st.nextToken();	    } else if (expect.equalsIgnoreCase("permission type")) {		value = st.sval;		lookahead = st.nextToken();	    } else if (expect.equalsIgnoreCase("principal type")) {		value = st.sval;		lookahead = st.nextToken();	    } else {		throw new ParsingException(st.lineno(), expect, st.sval);	    }	    break;	case ',':	    if (expect.equalsIgnoreCase(","))		lookahead = st.nextToken();	    else		throw new ParsingException(st.lineno(), expect, ",");	    break;	case '{':	    if (expect.equalsIgnoreCase("{"))		lookahead = st.nextToken();	    else		throw new ParsingException(st.lineno(), expect, "{");	    break;	case '}':	    if (expect.equalsIgnoreCase("}"))		lookahead = st.nextToken();	    else		throw new ParsingException(st.lineno(), expect, "}");	    break;	case ';':	    if (expect.equalsIgnoreCase(";"))		lookahead = st.nextToken();	    else		throw new ParsingException(st.lineno(), expect, ";");	    break;	case '*':	    if (expect.equalsIgnoreCase("*"))		lookahead = st.nextToken();	    else		throw new ParsingException(st.lineno(), expect, "*");	    break;	default:	    throw new ParsingException(st.lineno(), expect, 			       new String(new char[] {(char)lookahead}));	}	return value;    }    /**     * skip all tokens for this entry leaving the delimiter ";"     * in the stream.     */    private void skipEntry()	throws ParsingException, IOException    {      while(lookahead != ';') {	switch (lookahead) {	case StreamTokenizer.TT_NUMBER:	    throw new ParsingException(st.lineno(), ";",				       rb.getString("number ") +					String.valueOf(st.nval));	case StreamTokenizer.TT_EOF:	  throw new ParsingException		(rb.getString("expected ';', read end of file"));	default:	  lookahead = st.nextToken();	}      }    }    /**     * Each grant entry in the policy configuration file is      * represented by a     * GrantEntry object.  <p>     *     * <p>     * For example, the entry     * <pre>     *      grant signedBy "Duke" {     * 		permission java.io.FilePermission "/tmp", "read,write";     *      };     *     * </pre>     * is represented internally      * <pre>     *      * pe = new PermissionEntry("java.io.FilePermission",      *                           "/tmp", "read,write");     *     * ge = new GrantEntry("Duke", null);     *     * ge.add(pe);     *     * </pre>     *      * @author Roland Schemers     *     * version 1.19, 05/21/98     */    static class GrantEntry {	public String signedBy;	public String codeBase;	public LinkedList principals;	public Vector permissionEntries;	public GrantEntry() {	    permissionEntries = new Vector();	}	public GrantEntry(String signedBy, String codeBase) {	    this.codeBase = codeBase;	    this.signedBy = signedBy;	    permissionEntries = new Vector();	}	public void add(PermissionEntry pe)	{	    permissionEntries.addElement(pe);	}	public boolean remove(PermissionEntry pe)	{	    return permissionEntries.removeElement(pe);	}	public boolean contains(PermissionEntry pe)	{	    return permissionEntries.contains(pe);	}	/**	 * Enumerate all the permission entries in this GrantEntry.	 */	public Enumeration permissionElements(){	    return permissionEntries.elements();	}	public void write(PrintWriter out) {	    out.print("grant");	    if (signedBy != null) {		out.print(" signedBy \"");		out.print(signedBy);		out.print('"');		if (codeBase != null) 		    out.print(", ");	    }	    if (codeBase != null) {		out.print(" codeBase \"");		out.print(codeBase);		out.print('"');		if (principals != null && principals.size() > 0)		    out.print(",\n");	    }	    if (principals != null && principals.size() > 0) {		ListIterator pli = principals.listIterator();		while (pli.hasNext()) {		    out.print("\tPrincipal ");		    PrincipalEntry pe = (PrincipalEntry)pli.next();		    out.print((String)pe.principalClass +				" \"" + pe.principalName + "\"");		    if (pli.hasNext())			out.print(",\n");		}	    }	    out.println(" {");	    Enumeration enum_ = permissionEntries.elements();	    while (enum_.hasMoreElements()) {		PermissionEntry pe =		    (PermissionEntry) enum_.nextElement();		out.write("  ");		pe.write(out);	    }	    out.println("};");	}    }    /**     * Principal info (class and name) in a grant entry     */    static class PrincipalEntry {	static final String WILDCARD_CLASS = "WILDCARD_PRINCIPAL_CLASS";	static final String WILDCARD_NAME = "WILDCARD_PRINCIPAL_NAME";	String principalClass;	String principalName; 	/**	 * A PrincipalEntry consists of the <code>Principal</code>	 * class and <code>Principal</code> name.	 *	 * <p>	 *	 * @param principalClass the <code>Principal</code> class. <p>	 *	 * @param principalName the <code>Principal</code> name. <p>	 */	public PrincipalEntry(String principalClass, String principalName) {	    if (principalClass == null || principalName == null)		throw new NullPointerException			("null principalClass or principalName");	    this.principalClass = principalClass;	    this.principalName = principalName;	}	/**	 * Test for equality between the specified object and this object.	 * Two PrincipalEntries are equal if their PrincipalClass and	 * PrincipalName values are equal.	 *	 * <p>	 *	 * @param obj the object to test for equality with this object.	 *	 * @return true if the objects are equal, false otherwise.	 */	public boolean equals(Object obj) {	    if (this == obj)		return true;	    if (!(obj instanceof PrincipalEntry))		return false;	    PrincipalEntry that = (PrincipalEntry)obj;	    if (this.principalClass.equals(that.principalClass) &&		this.principalName.equals(that.principalName)) {		return true;	    }	    return false;	}	/**	 * Return a hashcode for this <code>PrincipalEntry</code>.	 *	 * <p>	 *	 * @return a hashcode for this <code>PrincipalEntry</code>.	 */	public int hashCode() {	    return principalClass.hashCode();	}    }    /**     * Each permission entry in the policy configuration file is      * represented by a     * PermissionEntry object.  <p>     *     * <p>     * For example, the entry     * <pre>     * 		permission java.io.FilePermission "/tmp", "read,write";     * </pre>     * is represented internally      * <pre>     *      * pe = new PermissionEntry("java.io.FilePermission",      *                           "/tmp", "read,write");     * </pre>     *      * @author Roland Schemers     *     * version 1.19, 05/21/98     */    static class PermissionEntry {	public String permission;	public String name;	public String action;	public String signedBy;	public PermissionEntry() {	}	public PermissionEntry(String permission, 			String name,			String action) {	    this.permission = permission;	    this.name = name;	    this.action = action;	}	/**	 * Calculates a hash code value for the object.  Objects	 * which are equal will also have the same hashcode.	 */	public int hashCode() {	    int retval = permission.hashCode();	    if (name != null) retval ^= name.hashCode();	    if (action != null) retval ^= action.hashCode();	    return retval;	}	public boolean equals(Object obj) {	    if (obj == this)		return true;	    if (! (obj instanceof PermissionEntry))		return false;	    PermissionEntry that = (PermissionEntry) obj;	    if (this.permission == null) {		if (that.permission != null) return false;	    } else {		if (!this.permission.equals(that.permission)) return false;	    }		    if (this.name == null) {		if (that.name != null) return false;	    } else {		if (!this.name.equals(that.name)) return false;	    }		    if (this.action == null) {		if (that.action != null) return false;	    } else {		if (!this.action.equals(that.action)) return false;	    }		    if (this.signedBy == null) {		if (that.signedBy != null) return false;	    } else {		if (!this.signedBy.equals(that.signedBy)) return false;	    }		    // everything matched -- the 2 objects are equal	    return true;	}	public void write(PrintWriter out) {	    out.print("permission ");	    out.print(permission);	    if (name != null) {		out.print(" \"");		// have to add escape chars for quotes		if (name.indexOf("\"") != -1) {		    int numQuotes = 0;		    char[] chars = name.toCharArray();		    // count the number of quote chars		    for (int i = 0; i < chars.length; i++) {			if (chars[i] == '"')			    numQuotes++;		    }		    // now, add an escape char before each quote		    char[] newChars = new char[chars.length + numQuotes];		    for (int i = 0, j = 0; i < chars.length; i++) {			if (chars[i] != '"') {			    newChars[j++] = chars[i];			} else {			    newChars[j++] = '\\';			    newChars[j++] = chars[i];			}		    }		    name = new String(newChars);		}		out.print(name);		out.print('"');	    } 	    if (action != null) {		out.print(", \"");		out.print(action);		out.print('"');	    }	    if (signedBy != null) {		out.print(", signedBy \"");		out.print(signedBy);		out.print('"');	    }	    out.println(";");	}    }    static class ParsingException extends GeneralSecurityException {	private static final long serialVersionUID = 8240970523155877400L;	/**	 * Constructs a ParsingException with the specified	 * detail message. A detail message is a String that describes	 * this particular exception, which may, for example, specify which	 * algorithm is not available.  	 *	 * @param msg the detail message.  	 */	public ParsingException(String msg) {	    super(msg);	}	public ParsingException(int line, String msg) {	    super(rb.getString("line ") + line + rb.getString(": ") + msg);	}	public ParsingException(int line, String expect, String actual) {	    super(rb.getString("line ") + line + rb.getString(": expected '") +		expect + rb.getString("', found '") + actual +		rb.getString("'"));	}    }    public static void main(String arg[]) throws Exception {	PolicyParser pp = new PolicyParser(true);	pp.read(new FileReader(arg[0]));	FileWriter fr = new FileWriter(arg[1]);	pp.write(fr);        fr.close();    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?