📄 pmixmlpolicyeditor.java
字号:
/*
* Copyright (c) 2006, University of Kent
* 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.
*
* 1. Neither the name of the University of Kent nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 2. 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.
*
* 3. 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.
*
* 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
* IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
* SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
* SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
* GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
* TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
* IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
* SERIOUS FAULTS, IN THIS SOFTWARE.
*
* 5. This license is governed, except to the extent that local laws
* necessarily apply, by the laws of England and Wales.
*/
/*
* 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.acm.extensions;
import java.util.Vector;
import java.awt.*;
import java.util.Map;
import issrg.ac.AttributeValue;
// here's Java XML Parsing stuff
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import issrg.ac.attributes.PMIXMLPolicy;
import issrg.pba.rbac.xmlpolicy.XMLPolicyParser;
import issrg.pba.PolicyParser;
/**
* This class is intended to provide some GUI for inputting XML
* Policy attribute value. At the moment it simply displays a text area,
* where the user is expected to paste his Policy.
*
* @author A Otenko
* @version 1.0
*/
public class PMIXMLPolicyEditor extends issrg.acm.AttributeEditor {
/**
* This is the OID of the pMIXMLPolicy attribute. Its value is
* "1.2.826.0.1.3344810.1.1.13".
*/
public static final String PMI_XML_POLICY_OID = "1.2.826.0.1.3344810.1.1.13";
/**
* This is what will be added after each line when saving the Policy onto a disk.
* Note, that at the moment it is 0x0a0d, which is DOS format (Unix format
* would be 0x0a only).
*/
public static final String CRLF = "\r\n";
private static final int width = 500;
private static final int height = 400;
private static final int pHeight = 65;
private boolean isCancelled = true; // is set to false by "Ok" button
private boolean dontValidate = true;
/**
* This is a name of configuration file variable to set when the user wants the
* XML to be validated automatically before it can be stored in an AC. Its name
* is "PMIXMLPolicyEditor.Validate". It must just be present in the [variables]
* section.
*/
public static final String VALIDATE_FLAG = "PMIXMLPolicyEditor.Validate";
/**
* This is what the extension of the sought files would be. At the moment it is
* ".xml".
*/
public static final String XML_EXTENSION = ".xml";
private static final javax.swing.filechooser.FileFilter xmlFilter = new javax.swing.filechooser.FileFilter(){
public boolean accept(java.io.File pathname){
return issrg.utils.Util.extensionMatch(pathname.getName(), XML_EXTENSION) || pathname.isDirectory();
}
public String getDescription(){
return "PMI XML Policy files "+issrg.utils.Util.JFC_OPEN_BRACKET+XML_EXTENSION+issrg.utils.Util.JFC_CLOSE_BRACKET;
}
};
private static final javax.swing.JFileChooser jfc = issrg.utils.Util.createFileChooser();
static{
jfc.addChoosableFileFilter(xmlFilter);
}
public PMIXMLPolicyEditor() {
}
public String getName() {
// return "PMI XML Policy : "+getOID();
return "PMI XML Policy";
}
public String getOID() {
return PMI_XML_POLICY_OID;
}
public Vector buildValues(Frame parentComponent, Map environment, Vector values, issrg.acm.Registry registry) throws issrg.acm.ACCreationException {
jfc.setFileFilter(xmlFilter);
PMIXMLPolicy result = null;
dontValidate=registry.getCollection(issrg.acm.EnvironmentalVariables.VARIABLES_COLLECTION).
get(VALIDATE_FLAG)==null;
try{
if (values==null){
result = new PMIXMLPolicy("");
}else{
AttributeValue av = (AttributeValue)(values.firstElement());
if (!av.isDecoded()){
result = new PMIXMLPolicy(av);
}else{
result = (PMIXMLPolicy)av;
}
}
}catch (iaik.asn1.CodingException ce){
throw new issrg.acm.ACCreationException("Error parsing the Attribute value: PMIXMLPolicy attribute was expected", ce);
}
String output = showInputDialog(parentComponent,
"Input XML Policy here:",
result.getPolicy());
if (output!=null){
values= new java.util.Vector();
values.add(new PMIXMLPolicy(output));
}else values=null; // thus spec says
return values;
}
public AttributeValue parseAttribute(AttributeValue av) throws iaik.asn1.CodingException {
return new PMIXMLPolicy(av);
}
/**
* Shows an input dialog with the edit memo and several buttons for loading and
* verification of the policy.
*/
protected String showInputDialog(Frame parentComponent, String title, final String oldvalue){
final javax.swing.JDialog dialog = new javax.swing.JDialog(parentComponent);
final javax.swing.JTextArea text = new javax.swing.JTextArea();
final StringBuffer sb = new StringBuffer(oldvalue);
javax.swing.JPanel contentPane = (javax.swing.JPanel)dialog.getContentPane();
javax.swing.JPanel panel = new javax.swing.JPanel(null);
javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane(text);
text.setBounds(0, 0, width, height);
dialog.setTitle(title);
dialog.setResizable(false);
java.awt.Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
dialog.setBounds((d.width-width)/2, (d.height-height)/2, width, height);
contentPane.setLayout(null/*new java.awt.BorderLayout(0, 2)*/);
scrollPane.setBounds(0,0, width-8, height-pHeight);
panel.setBounds(0, height-pHeight, width, pHeight);
contentPane.add(scrollPane);
contentPane.add(panel);
javax.swing.JButton b;
b = new javax.swing.JButton("Load...");
b.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent ae){
jfc.rescanCurrentDirectory();
jfc.showOpenDialog(dialog);
java.io.File f = jfc.getSelectedFile();
if (f!=null){
try{
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader(f));
StringBuffer sb1 = new StringBuffer();
while (br.ready()){
String s = br.readLine();
if (s==null) break;
sb1.append(s);
sb1.append(CRLF);
}
br.close();
text.setText(sb1.toString());
}catch (java.io.IOException ioe){
issrg.utils.Util.bewail(ioe.getMessage(), ioe, dialog);
}
}
}
});
panel.add(b);
b = new javax.swing.JButton("Verify XML");
b.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent ae){
if (verifyXML(dialog, text.getText())){
javax.swing.JOptionPane.showMessageDialog(dialog,
"XML is correct",
"Verification result",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
}
}
});
panel.add(b);
b = new javax.swing.JButton("Save...");
b.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent ae){
jfc.rescanCurrentDirectory();
jfc.showSaveDialog(dialog);
java.io.File f = jfc.getSelectedFile();
try{
if (f!=null){
java.io.BufferedWriter bw = new java.io.BufferedWriter(new java.io.FileWriter(f));
bw.write(text.getText());
bw.close();
}
}catch (java.io.IOException ioe){
issrg.utils.Util.bewail(ioe.getMessage(), ioe, dialog);
}
}
});
panel.add(b);
b = new javax.swing.JButton("OK");
b.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent ae){
String s=text.getText();
if (dontValidate || verifyXML(dialog, s)){
sb.setLength(0); // remove old string
sb.append(s); // set new value to return from this method
dialog.dispose();
isCancelled=false; // do not return null now: return the policy string.
}
}
});
panel.add(b);
b = new javax.swing.JButton("Cancel");
b.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent ae){
dialog.dispose(); // the old value will be returned
}
});
panel.add(b);
panel.setLayout(new java.awt.FlowLayout());
text.setText(oldvalue);
dialog.setModal(true);
synchronized(this){ // do not allow changing the isCancelled value
isCancelled=true;
dialog.show();
if (isCancelled) return null;
return sb.toString();
}
}
private boolean verifyXML(Component dialog, String s){
/* try{
PolicyParser p = new XMLPolicyParser(s);
return true;
}catch(Throwable th){
issrg.utils.Util.bewail(th.getMessage(), th, dialog);
return false;
} */
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
try{
SAXParser sp = spf.newSAXParser();
sp.parse(new java.io.ByteArrayInputStream(s.getBytes()), new DefaultHandler());
return true;
}catch(Exception ioe){
issrg.utils.Util.bewail(ioe.getMessage(), ioe, dialog);
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -