⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printablestringeditor.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
字号:
/*
* Copyright (c) 2000-2005, University of Salford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this 
* list of conditions and the following disclaimer.
* 
* Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation 
* and/or other materials provided with the distribution. 
*
* Neither the name of the University of Salford nor the names of its 
* contributors may be used to endorse or promote products derived from this 
* software without specific prior written permission. 
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
* POSSIBILITY OF SUCH DAMAGE.
*/

package issrg.pa.extensions;

import issrg.ac.*;
import iaik.asn1.ASN1Type;
import java.util.Map;

import issrg.ac.attributes.PermisRole;

import java.io.*;

/**
 * The editor for an attribute with PrintableString values. It is just an input box and a list of
 * strings already input in the set of attribute values.
 *
 * <p>It can be fed a configuration file with OIDs and names of the roles, or
 * it can use the sequence of such values in the pa.cfg
 *
 * <p>The <code>PRINTABLE_STRING_EDITOR_CFG_FILE</code> variable contains the name
 * of the configuration variable, specifying the file with OIDs. The
 * <code>PRINTABLE_STRING_EDITOR_CFG_LINE</code> variable contains the name of the
 * configuration variable to keep the sequence of the role OIDs and their names.
 *
 * <p>The configuration line in the pa.cfg must be of the following format:
 * <code>&lt;OID in dotted form&gt; &lt;attribute name; cannot not contain ';' char&gt;';'</code><p>
 * repeated as many times, as needed.
 *
 * <p>The configuration file for PrintableStringEditor can contain as many lines of
 * the format said above, as needed; the trailing semicolon can be omitted.
 * Empty lines and comments (lines starting with ';' or '#') are ignored.
 *
 * @author A Otenko
 * @version 1.0
 */
public class PrintableStringEditor implements issrg.pa.Utility {	// this one is a bit tricky
  /**
   * This variable specifies the configuration file variable. Its name is
   * <code>"PrintableStringEditor.Config"</code>.
   */
  public final static String PRINTABLE_STRING_EDITOR_CFG_FILE = "PrintableStringEditor.Config";

  /**
   * This variable specifies OID-name pairs directly in the configuration file.
   * Its name is <code>"PrintableStringEditor.OIDs"</code>.
   */
  public final static String PRINTABLE_STRING_EDITOR_CFG_LINE = "PrintableStringEditor.OIDs";
  public final static String COMMENT1 = ";";
  public final static String COMMENT2 = "#";
  public final static String OID_SEPARATOR = "|";
  private boolean use_GUI=false;

  public PrintableStringEditor() {
  }

  /**
   * Reads in PRINTABLE_STRING_EDITOR_CFG_FILE and PRINTABLE_STRING_EDITOR_CFG_LINE
   * and constructs a sequence of PermisRoleEditorHelpers. PRINTABLE_STRING_EDITOR_CFG_LINE
   * definitions take precedence, if OID to attribute type mappings overlap.
   */
  public void registerMe(issrg.pa.Registry where){
    Map env = where.getCollection(issrg.pa.EnvironmentalVariables.VARIABLES_COLLECTION);
    String roleTypesFilename=null;	// these variables are called so just because this class was PermisRoleEditor formerly
    String roleOIDs=null;

    if (env!=null){
      roleTypesFilename=(String)env.get(PRINTABLE_STRING_EDITOR_CFG_FILE);
      roleOIDs=(String)env.get(this.PRINTABLE_STRING_EDITOR_CFG_LINE);
    }

    if (roleTypesFilename==null && roleOIDs==null){
      //?? do what?
      return;
    }

    use_GUI = env.get(issrg.pa.EnvironmentalVariables.GUI_NOT_REQUIRED_FLAG)==null;

    StringBuffer sb = new StringBuffer(roleOIDs==null?"":
                                        roleOIDs.endsWith(this.OID_SEPARATOR)?
                                          roleOIDs:(roleOIDs+this.OID_SEPARATOR)
                                      );

    if (roleTypesFilename!=null){
      try{
        BufferedReader br = new BufferedReader(new FileReader(roleTypesFilename));
        while (br.ready()){
          String s=br.readLine();
	  if (s==null) break;
	  s = s.trim();
          if (s.intern()=="" || s.startsWith(COMMENT1) || s.startsWith(COMMENT2)){
            continue;
          }

          sb.append(s);
          sb.append(OID_SEPARATOR);
        }
        br.close();
      }catch (FileNotFoundException fnfe){
        issrg.utils.Util.bewail("OIDs file "+roleTypesFilename+" not found", fnfe, null);
      }catch (IOException ioe){
        issrg.utils.Util.bewail("Error reading OIDs file "+roleTypesFilename+" : "+ioe.getMessage(), ioe, null);
      }
    }

    roleOIDs = sb.toString();
    int from = 0;
    int l=roleOIDs.length();
    while (l>from){
      int i = roleOIDs.indexOf(this.OID_SEPARATOR, from);
      String o = roleOIDs.substring(from, i).trim();    // remove extra spaces
      from = i+this.OID_SEPARATOR.length();

      i=o.indexOf(" ");
      String name = i>0? o.substring(i+1).trim(): ""; // you could also check i>=0, to be more precise; but i need an OID to be non-empty. that' why
      if (i>0) o = o.substring(0, i); //.trim(); - no, don't trim: we don't need that anymore.

      new PermisRoleEditorHelper(o, name).registerMe(where);  // creating and registering a Helper
          // note that if an Editor for such an OID o is already registered,
          // nothing happens (Register.register returns false)
    }
  }

  public void bewail(String message){
    if (use_GUI){
      javax.swing.JOptionPane.showMessageDialog(null, message, "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
    }else{
      System.err.println(message);
    }
  }

}

/**
 * This class is what actually gets registered with the Registry; one instance
 * per OID read from the configuration line and file.
 */
class PermisRoleEditorHelper extends issrg.pa.AttributeEditor {
  public String PERMIS_ROLE_OID;
  public String PERMIS_ROLE_TYPE;

  protected PermisRoleEditorHelper(String OID, String Type){
        if (Type.intern()=="") Type=OID;  // let's output something, if Type is not specified.

	PERMIS_ROLE_OID = OID;
	PERMIS_ROLE_TYPE = Type;
  }

  public String getName() {
    return PERMIS_ROLE_TYPE+" "+getOID();
  }

  public String getOID() {
    return PERMIS_ROLE_OID;
  }

  public AttributeValue parseAttribute(AttributeValue attributeValue) throws iaik.asn1.CodingException{
    return new PermisRole(attributeValue);
  }

  public java.util.Vector buildValues(java.awt.Component parentComponent, Map environment, java.util.Vector values, issrg.pa.Registry registry) throws issrg.pa.ACCreationException {
    java.util.Vector result=new java.util.Vector();
    try{
      if (values==null){
        //result.add(""); // ohmygod :-) this tiny thingy was causing troubles with null value being inserted :-)
      }else{
        for (int i=values.size(); i-->0;){
          AttributeValue attributeValue = (AttributeValue)values.get(i);

          String rv;
          if (!attributeValue.isDecoded()){
            rv = new PermisRole(attributeValue).getRoleValue();
          }else rv=((PermisRole)attributeValue).getRoleValue();

          result.add(0, rv);
        }
      }
    }catch (iaik.asn1.CodingException ce){
      throw new issrg.pa.ACCreationException("Error parsing the Attribute value: PermisRole attribute was expected", ce);
    }

    result = showInputDialog(parentComponent, result);
    if (result!=null){
      values= new java.util.Vector();

      for (int i=result.size(); i-->0;){
        values.add(0, new PermisRole((String)(result.get(i))));
      }
    }else values=null;  // it is specified to return null.

    return values;
  }


  private java.util.Vector showInputDialog(java.awt.Component parentFrame, final java.util.Vector values){
    //"Input Role Value:", javax.swing.JOptionPane.QUESTION_MESSAGE);
    final int width = 400;
    final int height = 200;
    final int bw = 25;

    final java.util.Vector [] result = new java.util.Vector[]{null};

    final javax.swing.JDialog dialog = new javax.swing.JDialog();
    final javax.swing.JList list = new javax.swing.JList(values);
    final javax.swing.JTextField text = new javax.swing.JTextField("");
    dialog.setTitle("Editing "+this.PERMIS_ROLE_TYPE+" Attribute value");
    java.awt.Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    dialog.setBounds((d.width-width)/2, (d.height-height)/2, width, height);
    dialog.setModal(true);

    list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

    javax.swing.JPanel contentPane = (javax.swing.JPanel)dialog.getContentPane();
    contentPane.setLayout(null);

    javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(list);
    //list.setBounds(0,0,width-10, height-10);
    scrollPane.setBounds(0, 0, width/2-2, height-25);
    scrollPane.setHorizontalScrollBarPolicy(scrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setVerticalScrollBarPolicy(scrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    contentPane.add(scrollPane);

    javax.swing.JButton b = new javax.swing.JButton(">");
    b.setBounds(width/2, 2, bw+20, bw);
    contentPane.add(b);

    b.addActionListener(new java.awt.event.ActionListener(){
              public void actionPerformed(java.awt.event.ActionEvent ae){
                int i;
                if ((i=list.getSelectedIndex())>=0){
                  text.setText((String)values.remove(i));
                  list.setListData(values);
                }
              }
          });

    int y = b.getY();
    b = new javax.swing.JButton("<");
    b.setBounds(width/2, y+bw, bw+20, bw);
    contentPane.add(b);

    b.addActionListener(new java.awt.event.ActionListener(){
              public void actionPerformed(java.awt.event.ActionEvent ae){
                String s;
                if ((s=text.getText()).intern()=="") return;  // at the moment it does not allow to insert empty lines

                values.add(s);
                text.setText("");

                int i=list.getSelectedIndex();
                list.setListData(values);
                list.setSelectedIndex(i);
                list.ensureIndexIsVisible(values.size()-1);
              }
          });

    int x=width/2+b.getWidth()+4;
    text.setBounds(x, bw/2, width-x-8, bw);
    contentPane.add(text);

    b = new javax.swing.JButton("OK");
    b.setBounds(width/2+2, height-bw-35, 60, bw);
    contentPane.add(b);

    b.addActionListener(new java.awt.event.ActionListener(){
              public void actionPerformed(java.awt.event.ActionEvent ae){
                result[0]=values;
                dialog.dispose();
              }
          });

    x = b.getX()+b.getWidth();
    y = b.getY();
    b = new javax.swing.JButton("Cancel");
    b.setBounds(x+2, y, 80, bw);
    contentPane.add(b);

    b.addActionListener(new java.awt.event.ActionListener(){
              public void actionPerformed(java.awt.event.ActionEvent ae){
                dialog.dispose();
              }
          });

    javax.swing.JSeparator js = new javax.swing.JSeparator(javax.swing.JSeparator.HORIZONTAL);
    js.setBounds(width/2, y-10, width/2, 2);
    contentPane.add(js);

    dialog.show();

    return result[0];
  }

}


⌨️ 快捷键说明

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