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

📄 tapfileconfiguration.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 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.
*/

/*
 * TAPFileConfiguration.java - 27/07/06
 */

package issrg.editor2.configurations;

import issrg.editor2.DomainPolicyEditor;
import issrg.utils.xml.XMLChangeEvent;
import issrg.utils.xml.XMLChangeListener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Christian Azzopardi
 */
public class TAPFileConfiguration extends JPanel implements XMLChangeListener
{
    /**
     * Panels needed to place the component on, and get the GUI right. 
     */
    private JPanel eastPanel, westPanel, TAPpanel;
    
    private FileListNodeList TAPFilesNodeList;
    
    private ConfigurationComponent xmlED;
    
    private JTree tree;
    private DefaultTreeModel tapFiles;
    private DefaultMutableTreeNode rootNode;
    
    private TAPFileParser tapParser;
            
    ResourceBundle rbl = ResourceBundle.getBundle("issrg/editor2/PEComponent_i18n");
    String TAPBorderCaption = rbl.getString("Application_Preferences_TAP_Border"); //contains TAP Files
    String TreeBorderCaption = rbl.getString("TAPFile_Configurations_Tree_Panel"); //contains Available TAP Files
    
    /** 
     * Creates a new instance of TAPFileConfiguration 
     */
    public TAPFileConfiguration(ConfigurationComponent xmlED) 
    {
        this.xmlED = xmlED;
        
        this.setLayout(new BorderLayout());        
        this.add(getContentPanel());
        
        buildTAPDOM();
    }
    
    public JPanel getContentPanel()
    {
        JPanel tapConfig = new JPanel(new BorderLayout());
        
        TAPpanel = new JPanel(new BorderLayout());
        TAPFilesNodeList = new FileListNodeList(this.xmlED, ".tap", this, "TAPFiles");
        TAPFilesNodeList.removeReplaceButton();
        TAPFilesNodeList.setCaption("ADD_BUTTON", rbl.getString("AddButton"));
        TAPFilesNodeList.setCaption("DELETE_BUTTON", rbl.getString("DeleteButton"));
        TAPFilesNodeList.setCaption("REPLACE_BUTTON", rbl.getString("ReplaceButton"));
        TAPFilesNodeList.setPreferredSize(new Dimension(275,250));
        TAPpanel.add(TAPFilesNodeList, BorderLayout.CENTER);
        TAPpanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), TAPBorderCaption));
        tapConfig.add(TAPpanel, BorderLayout.WEST);
        
        rootNode = new DefaultMutableTreeNode("TAP Files");
        tapFiles = new DefaultTreeModel(rootNode);
        JPanel TreePanel = new JPanel(new BorderLayout());
        tree = new JTree(tapFiles);       
        tree.setEditable(false);
        tree.setRootVisible(true);
        tree.setShowsRootHandles(true);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(275,250));
        TreePanel.add(scrollPane, BorderLayout.CENTER);
        TreePanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), TreeBorderCaption));
        
        tapConfig.add(TreePanel, BorderLayout.EAST);
        
        return tapConfig;
    }
    
    private DefaultMutableTreeNode createTAPFileTree(Element e)
    {
        URL url = null;
        try{
            url = new URL(e.getAttribute("FilePath"));
        } catch (MalformedURLException malformedURL)
        {
            return null;
        }
        tapParser = new TAPFileParser(url.getPath(), "");
        if (tapParser.document==null) return null;
        Element targetRoot = ((Element)tapParser.document.getElementsByTagName("Target").item(0));
        DefaultMutableTreeNode TAPRoot = new DefaultMutableTreeNode("Target: " + targetRoot.getAttribute("Name"));
        
        NodeList actionsList = DomainPolicyEditor.getChildElements(targetRoot);
        for (int i=0; i<actionsList.getLength(); i++)
        {
            DefaultMutableTreeNode action = new DefaultMutableTreeNode(((Element)actionsList.item(i)).getAttribute("Name"));
            NodeList paramsList = DomainPolicyEditor.getChildElements(actionsList.item(i));
            for (int j=0; j<paramsList.getLength(); j++)
            {
                DefaultMutableTreeNode param = new DefaultMutableTreeNode(((Element)paramsList.item(j)).getAttribute("Name"));    
                action.add(param);
            }
            TAPRoot.add(action);
        }
        return TAPRoot;
    }
    
    public Element getTAPNode()
    {
        if (xmlED.DOM == null) return null;
        Element applicationSettings = ((Element)xmlED.DOM.getElementsByTagName("ApplicationSettings").item(0));
        return ((Element)applicationSettings.getElementsByTagName("TAPFiles").item(0));
    }
    
    public DefaultMutableTreeNode getSelectedNode()
    {
        return (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
    }
    
    public void XMLChanged(XMLChangeEvent e)
    {
        refresh();
    }
    
    public void refresh()
    {
        if (getTAPNode() == null) return;
        
        int children = tapFiles.getChildCount(rootNode);        
        for (int i=0; i<children; i++)
        {
            DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)tapFiles.getChild(rootNode, tapFiles.getChildCount(rootNode)-1);
            tapFiles.removeNodeFromParent(dmtn);            
        }
        
        buildTAPDOM();
        NodeList taps = DomainPolicyEditor.getChildElements(getTAPNode());
        for (int i=0; i < taps.getLength(); i++)
        {
            DefaultMutableTreeNode newTapTree = createTAPFileTree(((Element)taps.item(i)));
            if (tapParser.document==null) continue;
            Element child = copyElement(tapParser.document.getDocumentElement()); 
            this.xmlED.TAPFiles.addItem(child, this.xmlED.TAPFiles.DOM.getDocumentElement());
            tapFiles.insertNodeInto(newTapTree, rootNode, rootNode.getChildCount());
            //expand the top node.
        }
    }
    
    public Element copyElement(Element e)
    { 
        Element toReturn = this.xmlED.TAPFiles.DOM.createElement("Target");
        toReturn.setAttribute("Name", e.getAttribute("Name"));
        
        NodeList actionList = DomainPolicyEditor.getChildElements(e);
        for (int i=0; i<actionList.getLength(); i++)
        {
            Element action = this.xmlED.TAPFiles.DOM.createElement("Action");
            action.setAttribute("Name", ((Element)actionList.item(i)).getAttribute("Name"));
            
            NodeList parameterList = DomainPolicyEditor.getChildElements(actionList.item(i));
            for (int j=0; j<parameterList.getLength(); j++)
            {
                Element parameter = this.xmlED.TAPFiles.DOM.createElement("Parameter");
                parameter.setAttribute("Name", ((Element)parameterList.item(j)).getAttribute("Name"));
                parameter.setAttribute("DataType", ((Element)parameterList.item(j)).getAttribute("DataType"));
                action.appendChild(parameter);
            }
            
            toReturn.appendChild(action);
        }
        
        return toReturn;
    }
    
    public void buildTAPDOM()
    {
        try
        {
            InputStream TAPFilesConfigDOM = ConfigurationComponent.class.getClassLoader().getResourceAsStream("issrg/editor2/TAPFilesConfig.cfg");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            this.xmlED.TAPFiles.DOM = builder.parse(TAPFilesConfigDOM);
        }
        catch(MissingResourceException mre) { }
        catch (SAXException sxe) { }
        catch (ParserConfigurationException pce) { }
        catch (IOException ioe) { }
        catch (SecurityException se) { }
    }
}

⌨️ 快捷键说明

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