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

📄 policyxmlnode.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.pba.rbac.xmlpolicy;

import java.util.Iterator;
import java.util.Map;

/**
 * This is the class that represents the node of the XML Policy. Its subclasses
 * are used when parsing the Policy.
 * <p>
 * As the XMLPolicyParser has been written in 2000, the DOM3 classes were not
 * readily available with Java, so we went for a lightweight parser, SAX parser.
 * XMLPolicyParser is an extension of SAXParser, which builds a tree of 
 * PolicyXMLNodes, similar to the trees of Nodes, built by modern DOM3 parsers.
 * XMLPolicyParser keeps a register of classes corresponding to each XML tag,
 * so when such tag is encountered in XML, a respective class is instantiated.
 * It is assumed that each class extends PolicyXMLNode.
 *
 * @author A Otenko
 * @version 1.0
 */

public class PolicyXMLNode {
    /**
     * The name of the node. It can differ from what is actually stated in the 
     * XML,
     * if the implementation desires.
     */
    protected String name = "";
    
    /**
     * This is the Map of element attributes by their names; each element is a 
     * String.
     */
    protected java.util.Map attributes = new java.util.Hashtable();
    
    /**
     * This is a collection of the element children nodes; each element is of 
     * the
     * PolicyXMLNode class.
     */
    protected java.util.Vector children = new java.util.Vector();
    
    /**
     * This is the string associated with the node. it is normally the string 
     * that appears beteeen the opening
     * tag and the closing tag. For example, if
     * &lt;Obligation&gt;
     * This is the obligation node.
     * &lt;/Obligation&gt;
     * string will be holding the String "This is the obligation node."
     */
    protected String nodeStr=null;
    
    /**
     * This method adds more text that is read in blocks by the XML Parser.
     * This text is the text that appears between the opening and closing tags
     * of a XML element.
     *
     * @param str is the string associated with the node
     */
    public void  addString(String str){
      if (nodeStr==null) nodeStr=str;
      else nodeStr+=str;
    }
    
    /**
     * This method returns the text contained between the opening and closing
     * tags of the element.
     *
     * &lt;Obligation&gt;
     * This is the text returned by this method
     * &lt;/Obligation&gt;
     *
     * @return the String associated with the node
     */
    public String getString(){
        return nodeStr;
    }
    
    /**
     * This constructor builds a PolicyXMLNode with no name and no attributes.
     * This is equivalent to using PolicyXMLNode("", null) constructor.
     */
    protected PolicyXMLNode(){
        _init_("", null);
    }
    
    /**
     * This constructor builds a node with a given name, but no attributes.
     *
     * @param nodeName - the name of the XML element
     */
    protected PolicyXMLNode(String nodeName){
        _init_(nodeName, null);
    }

    /**
     * This method does the actual initialisation of the object and is invoked
     * by all the constructors.
     *
     * @param nodeName - the name of the XML element this object represents
     * @param attr - the collection of attributes of this XML element
     */
    private void _init_(String nodeName, org.xml.sax.Attributes attr){
        name = nodeName;
        
        //System.out.println("XML Node : "+ nodeName);
        
        for (int i=0; i<attr.getLength(); i++){
        //    System.out.println(":"+ attr.getQName(i) + "="+attr.getValue(i));
            attributes.put(attr.getQName(i), attr.getValue(i));
        }
    }
    
    /**
     * This constructor builds a node with the given name and a set of 
     * attributes.
     *
     * @param nodeName - the name of the XML element
     * @param attr - the attributes of the XML element represented by this
     *   object
     */
    public PolicyXMLNode(String nodeName, org.xml.sax.Attributes attr){
        _init_(nodeName, attr);
    }
    
    /**
     * This constructor builds a node with the given name and a set of 
     * attributes,
     * though, the attributes are in a different form.
     *
     * @param nodeName - the name of the XML element
     * @param attrs - the Map of attributes of the XML element represented by
     *   this object
     */
    public PolicyXMLNode(String nodeName, java.util.Map attrs){
        name = nodeName;
        attributes = attrs;
    }
    
    /**
     * This method adds another child to the collection of children. It should 
     * be
     * called before the call to the <code>construct</code> method to have any
     * effect.
     *
     * @see #construct()
     *
     * @param child is the node to append as a child node at the end of the list
     *   of children
     */
    public void addChild(PolicyXMLNode child){
        children.add(child);
    }
    
    /**
     * This method returns the name of the node, so you could walk down the 
     * tree,
     * if you wanted.
     *
     * @return String name of the XML element represented by this object
     */
    public String getName(){
        return name;
    }
    
    /**
     * This method returns the collection of attributes for this node; for 
     * walking the tree.
     *
     * @return Map of the attributes of this XML element
     */
    public java.util.Map getAttributes(){
        return attributes;
    }
    
    /**
     * This method returns the collection of children. Each of the objects of 
     * the
     * Vector is of type PolicyXMLNode.
     *
     * @return Vector of children of this XML element; never null, but may be
     *   empty, if there are no children of this element
     */
    public java.util.Vector getChildren(){
        return children;
    }
    
    /**
     * This method is called when the node is closed. It performs the actual
     * semantic loading of the node. Without it, the implementations may assume
     * they are not ready to be used.
     * <p>
     * This method does nothing by default, and should be overridden.
     *
     * @throws PolicyParsingException if any syntax or semantics error occurred
     */
    public void construct() throws issrg.pba.rbac.PolicyParsingException {
    }
    
    /**
     * @return a string representation of this XML element; by default returns
     *   the XML representation of it
     */
    public String toString(){
        return toXML();
    }
    
    
    /**
     * This function returnes the XML representation of the XML element. It 
     * outputs the internal structure to 
     * XML text. The text is not a complete XML file as it does not include 
     * the XML headers, etc. It only generates
     * the text as a component of an XML file, thus it's the text of the 
     * subtree begining with the current node.
     *
     * Example Output:
     *
     * <pre>&lt;Obligations&gt;
     *  &lt;Obligation ObligationID="111" Chronicle="BeforeEnforcement"/&gt;
     *  &lt;Obligation ObligationID="123" Chronicle="BeforeEnforcement"&gt;
     *  Any Text here representing the chilldren of the obligation 123.
     *  &lt;/Obligation&gt;
     * &lt;/Obligations&gt;</pre>
     */
    public String toXML() {
        StringBuffer xmlStr=new StringBuffer();
        toXML(xmlStr, "", null);
        return xmlStr.toString();
    }

    /**
     * This method converts the node to XML text with the specified indent
     * for the current node and the indent increment for children nodes.
     *
     * @param xmlStr - the StringBuffer to output the XML text to
     * @param currentIndent - the indent for the current node; if null, no 
     *   indentation is performed for this node, or any children, and the
     *   whole piece of XML will be output as a single line
     * @param indentIncrement - the increment of indentation between the current
     *   node and its childrent; has no effect, if the currentIndent is null;
     *   if this value is null, two white spaces are used as the indentation
     *
     * @return the XML text of the node with all of its children
     */
    public void toXML(StringBuffer xmlStr, String currentIndent, String indentIncrement){
        if (indentIncrement==null) indentIncrement="  ";

        if (currentIndent!=null) xmlStr.append(currentIndent);
        xmlStr.append("<");
        xmlStr.append(this.name);
        // now:
        // ...(indent)...<TAG
        
        //begin to add attributes.
        Iterator entryIter=this.attributes.entrySet().iterator();
        Map.Entry entry=null;
        
        //generate all the text for all attributes
        if(entryIter!=null){
            while(entryIter.hasNext()){
                entry=(Map.Entry) entryIter.next();
                xmlStr.append(" ");
                xmlStr.append((String)entry.getKey());
                xmlStr.append("=\"");
                xmlStr.append((String)entry.getValue()); // assume the values do not require escaping!
                xmlStr.append("\"");
            }
        }
        // now:
        // ...(indent)...<TAG ATTRIBUTE="VALUE" ATTRIBUTE="VALUE"  ...
        
        if(children.size()<1 && (this.nodeStr==null || this.nodeStr.length()==0)){
            // no children, no text - just close the tag
            xmlStr.append("/");
            // now:
            // ...(indent)...<TAG ATTRIBUTE="VALUE" .../
        }else{
          xmlStr.append(">");
          if (currentIndent!=null){
            xmlStr.append("\n");
          }
          // now:
          // ...(indent)...<TAG ATTRIBUTE="VALUE" ...>

          if (this.nodeStr!=null){ // append text, if it exists
            xmlStr.append(this.nodeStr);
            if (currentIndent!=null){
              xmlStr.append("\n");
            }
          }
          // now:
          // ...(indent)...<TAG ATTRIBUTE="VALUE" ...>
          // TEXT (if any)

          String newIndent=currentIndent==null?null:(currentIndent+indentIncrement);

          for(int i=0;i <this.children.size();i++){
            PolicyXMLNode node=(PolicyXMLNode)children.get(i);                    
            node.toXML(xmlStr, newIndent, indentIncrement); //recursively genereated child XML text
            if (currentIndent!=null){
              xmlStr.append("\n");
            }
          }
          // now:
          // ...(indent)...<TAG ATTRIBUTE="VALUE" ...>
          // TEXT (if any)
          // ......(new indent)...<CHILDTAG ........./>(if any)
          // ......(new indent)...<CHILDTAG ........./>
          // ...
          if (currentIndent!=null) xmlStr.append(currentIndent);

          xmlStr.append("</");
          xmlStr.append(name);
          // now:
          // ...(indent)...<TAG ATTRIBUTE="VALUE" ...>
          // TEXT (if any)
          // ......(new indent)...<CHILDTAG ........./>(if any)
          // ...
          // ...(indent)...</TAG
        }

        xmlStr.append(">");
          // now:
          // ...(indent)...<TAG ATTRIBUTE="VALUE" ...>
          // TEXT (if any)
          // ......(new indent)...<CHILDTAG ........./>(if any)
          // ...
          // ...(indent)...</TAG>
          //
          // or, if no text and no children
          //
          // ...(indent)...<TAG ATTRIBUTE="VALUE" .../>
    }
}

⌨️ 快捷键说明

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